SQL Error Missing Right Paretheses - sql

I am just learning SQL and when I try to create the "Order" table I get the error "missing right parentheses". When I remove the FK constraint I get the error "invalid identifier"
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(
CUST_ID INT PRIMARY KEY,
COMP_NAME VARCHAR(40),
CONT_LNAME VARCHAR(30),
CONT_FNAME VARCHAR(30),
PHONE VARCHAR(30),
EMAIL VARCHAR(40),
ADDRESS VARCHAR(40),
CITY VARCHAR(30),
ZIP NUMBER
);
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(1,'Atomic Liqours','Signor','Rose','518-990-8765','rose#atomicliqours.com','76 Hackett Blvd','Albany','12204');
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(2,'HBD Bar','Capozolli','Rose','889-908-6666','rose#hbd.com','888 Project Rd','Troy','12180');
INSERT INTO CUSTOMER (CUST_ID, COMP_NAME, CONT_LNAME, CONT_FNAME, PHONE, EMAIL, ADDRESS, CITY,ZIP) VALUES
(3,'Lee Harvey','Smith','Seth','675-888-9999','seth#lh.com','78 Healy Ave','Troy','12222');
-- CREATING ORDER TABLE
CREATE TABLE ORDERS
(
ORD_ID INT PRIMARY KEY,
CUST_ID INT FOREIGN KEY REFERENCES Customers (CUST_ID),
ORD_DATE DATE,
DEL_DATE DATE,
);

Looks like your have a typo in your script. Your table name is CUSTOMER, but in CREATE TABLE you references to CUSTOMERS. So, I think that your create table statement must looks like:
CREATE TABLE ORDERS
(
ORD_ID INT PRIMARY KEY,
CUST_ID INT FOREIGN KEY REFERENCES Customer (CUST_ID),
ORD_DATE DATE,
DEL_DATE DATE,
);

Related

error 1452 when trying to paste in values

when i try enter the last part of code i get the error message "error code : 1452 cannot add or update a child row a foreign key constraint fails"
CREATE TABLE products (
id int auto_increment primary key,
name varchar (30),
price decimal(3,2),
coffee_origin varchar (30)
);
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR (30),
gender ENUM ('M','F'),
phone_number VARCHAR(11)
);
CREATE TABLE orders(
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
customer_id INT,
order_time DATETIME,
FOREIGN KEY (product_id) REFERENCES products(id),
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
INSERT INTO orders (product_id,customer_id,order_time) VALUES (1,1,'2017-01-01 08-02-11');
In order to be able to insert your values into the orders table, I think you might need to add rows to your products and customers tables.
Here are possible examples of how you could add rows to your products and customers tables:
insert into products (name, price, coffee_origin) values ('Dark Blend', 4.99, 'Brazil');
insert into customers (first_name, last_name, gender, phone_number) values ('First Name', 'Last Name', 'M', '123-456-7890');
Then, you can find the id values that you might need to use for your final insert action by running the following queries:
select id from products where name = 'Dark Blend';
select id from customers where phone_number = '123-456-7890';
Finally, you can try running your insert action again, but this time using the id values from the queries above this; maybe like this, if you get back 1 for the ids of each of those queries above:
insert into orders (product_id, customer_id, order_time) values (1, 1, '2021-01-01 00:00:00');

Insert row into a table with foreign key

I have created two tables (SupplierName and SupplierAddress) in the address_book database.
Table SupplierName has one row inserted as shown in the insert statement below. I am struggling how to insert data into the table called SupplierAddress.
Please note that the SupplierID (primary key) from the SupplierName table is the foreign key in the SupplierAddress table. If my table design is incorrect please opine on that as well. I am just trying to create a simple database to learn SQL.
CREATE TABLE SupplierName
(
SupplierID int Primary Key identity(1,1) NOT NULL,
CompanyName varchar(50) NOT NULL,
EIN char(9) NOT NULL UNIQUE
)
CREATE TABLE SupplierAddress
(
ID int PRIMARY KEY IDENTITY(1,1) NOT NULL,
Address varchar(50),
City varchar(50),
State char(2),
ZipCode char(5),
Phone varchar(10),
SupplierAddressID int FOREIGN KEY REFERENCES SupplierName(SupplierID)
)
INSERT INTO SupplierName (CompanyName, EIN)
VALUES ('Clarks Electronics, Inc.', '123456789');
INSERT INTO SupplierAddress (Address, City, State, ZipCode, Phone)
VALUES ('2020 Garnet Road', 'York', 'PA', '17403', '717-123-4567')
SELECT
SupplierName.SupplierID, SupplierAddress.SupplierAddressID
FROM
SupplierName
INNER JOIN
SupplierAddress ON SupplierAddressID=SupplierID
Remove the hyphens from the phone number, or create table with more characters in phone
Look at http://www.dpriver.com/pp/sqlformat.htm to pretty format the sql
CREATE TABLE supplieraddress
(
id INT PRIMARY KEY IDENTITY(1, 1) NOT NULL,
address VARCHAR(50),
city VARCHAR(50),
state CHAR(2),
zipcode CHAR(5),
phone VARCHAR(10), -- make this longer
supplieraddressid INT FOREIGN KEY REFERENCES suppliername(supplierid)
)
INSERT INTO supplieraddress
(address,
city,
state,
zipcode,
phone)
-- add supplieraddressid and its value so that the FK is created
VALUES ('2020 Garnet Road',
'York',
'PA',
'17403',
'717-123-4567') -- or remove hyphens
SELECT suppliername.supplierid,
supplieraddress.supplieraddressid
FROM suppliername
INNER JOIN supplieraddress
ON supplieraddressid = supplierid

MIN/MAX VALUES GROUP BY ID

Find the names and addresses of the customers with the highest balance and the people with the lowest loan in each branch.
Tables:
CREATE TABLE Branch(
BranchID INT NOT NULL,
Address varchar(100),
ManagerSsn varchar(100),
Branch_name varchar(255),
PRIMARY KEY (BranchID)
);
CREATE TABLE Has_Account(
Assn Int,
ANo CHAR(16),
Opened_date date,
PRIMARY KEY(Assn),
FOREIGN KEY(Assn) REFERENCES Customer(Ssn),
FOREIGN KEY(ANo) REFERENCES Account(AccountNo)
);
CREATE TABLE Account(
AccountNo CHAR(16) NOT NULL,
BranchID Int,
Balance Int,
PRIMARY KEY(AccountNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Customer(
Ssn Int CHECK(0101000000000 < Ssn < 3112999995999) NOT NULL,
Address VARCHAR(100),
Name VARCHAR(100),
EmployerID Int,
PRIMARY KEY(Ssn)
);
CREATE TABLE Loan(
LoanNo CHAR(16) NOT NULL,
BranchID Int,
Amount Int,
PRIMARY KEY(LoanNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Has_Loan(
LNo CHAR(16),
Lssn Int,
Repay_date date,
PRIMARY KEY (LNo),
FOREIGN KEY(Lno) REFERENCES Loan(LoanNo),
FOREIGN KEY(Lssn) REFERENCES Customer(Ssn)
);
Populating the tables
INSERT INTO CUSTOMER
VALUES('2004980890999', ‘Kanaribakken 2B’, ‘Paul Stolten’, ‘’);
INSERT INTO CUSTOMER
VALUES(‘1010968250234’, ‘Parkveien 5’, ‘Anders Nilsson’, ‘’, );
INSERT INTO CUSTOMER
VALUES(‘2104996495987’, ‘Kongeveien 24B’, ‘Karoline Torskerud’, ‘123’, );
INSERT INTO CUSTOMER
VALUES(‘2910943135444’, ‘Gåsesmauet 3’, ‘Bente Arildsen’, ‘555’, );
INSERT INTO CUSTOMER
VALUES(‘1304980890678’, ‘Knallebakken 30’, ‘Knut Solberg’, ‘555’,);
INSERT INTO CUSTOMER
VALUES('1019730000234', ‘Laksebakken 40’, ‘Harald Olsen’, ‘’);
INSERT INTO BRANCH
VALUES (‘123’, ‘Børsveien 49’, ‘1412963670786’, ‘Nesttun’,);
INSERT INTO BRANCH
VALUES(‘555’, ‘Vimpelveien 24’, ‘2412983905999’, ‘DNB’,);
INSERT INTO ACCOUNT
VALUES (‘1234567890087620’, ‘123’, ‘459’,);
INSERT INTO ACCOUNT
VALUES (‘8402849284910340’, ‘123’, ‘35120’,);
INSERT INTO ACCOUNT
VALUES (‘3452948293817280’, ‘555’, ‘69’,);
INSERT INTO HAS_ACCOUNT
VALUES(‘1010968250234’, ‘1234567890087620’, ‘17.04.2012’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2104996495987’, ‘8402849284910340’, ‘20.09.2000’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2910943135444’, ‘3452948293817280’, ‘29.01.2006’,);
INSERT INTO LOAN
VALUES (‘2291928394039280’, ‘555’, ‘50000’,);
INSERT INTO LOAN
VALUES (‘1295829384932100’, ‘123’, ‘400000’,);
INSERT INTO LOAN
VALUES ('1295829384932222', ‘123’, ‘40000’);
INSERT INTO HAS_LOAN
VALUES (‘2910943135444’, ‘2291928394039280’, ‘09.09.2019’,);
INSERT INTO HAS_LOAN
VALUES (‘2104996495987’, ‘1295829384932100’, ‘14.03.2022’,);
INSERT INTO HAS_LOAN
VALUES ('2004980890999', '1295829384932222', '01.02.2021');
Draft so far:
SELECT DISTINCT Customer.Name, Customer.Address
FROM Customer, Account, Loan, Has_Account, Has_Loan
WHERE Customer.Ssn = Has_Account.Assn AND Has_Account.Assn = Has_Loan.Lssn
(SELECT Account.Balance MAX(Balance) GROUP BY(BranchID)) AND
Loan.AMOUNT(SELECT Loan.Amount MIN(Amount) GROUP BY(BranchID));
I think it is correct that we need to group our output by BranchID, and max/min is ok. Looks like the statement formalities are wrong somehow.
Any help or hint is much appreciated!

Cross Referencing Table Value for Another Value to allow Insert

How do I look at one table and use it to check another table for data integrity?
I have two SQL Tables.
One that is a person table
CREATE TABLE PERSON
(
ID INT IDENTITY(10000,1) NOT NULL,
Firstname VARCHAR(15),
Lastname VARCHAR(25) NOT NULL,
Birthdate DATE,
Gender VARCHAR(1),
CHECK ( GENDER IN ('M', 'F')),
Street VARCHAR(50),
City VARCHAR(15),
State VARCHAR(2),
CHECK (State IN ('FL','GA','PA')),
Zip INT,
Phone VARCHAR(10),
Employee VARCHAR(1),
CHECK ( Employee IN('Y','N')),
Member VARCHAR(1),
CHECK ( Member IN('Y','N')),
CHECK (Member IN ('Y') or Employee IN ('Y')),
CONSTRAINT PERSON_PK PRIMARY KEY (ID));
And Employee Table
CREATE TABLE EMPLOYEE
(
ID INT NOT NULL,
Datehired DATE DEFAULT GETDATE(),
Status VARCHAR(1),
CHECK ( Status IN ('F','C')),
Position VARCHAR(25),
EmpType VARCHAR(25),
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (ID),
CONSTRAINT EMPLOYEE_PERSON_FK FOREIGN KEY (ID) REFERENCES PERSON);
Let's say someone isn't an employee. I can still insert them into the Employee Table.
INSERT INTO EMPLOYEE
(ID, Status, Position, EmpType)
VALUES
('10000','C','Teaching Classes','Instructor');
How Do I prevent this from happening.
One method is to have a redundant key:
alter table person
contraint unq_person_id_employee
unique (id, employee);
Then add a computed column to employee:
alter table employee add employee as ('Y') persisted;
Finally, add the constraint:
alter table employee
add constraint fk_employee_person
foreign key (id, employee) references person(id, employee);
Now, you are guaranteed that only employees are in the Employee table.

Shop database, SQL for order table

I have a University project and I have to create a DB for a plant shop.
I have a problem with the order table. At the moment it only allows a customer to buy one product at a time but in real life a customer buys many products at a time.
For example,
We have a customer John Doe, and he bus two products that are in the product table. How do I pull those two (or more) products and add them to one order table?
Below is the SQL code I wrote:
CREATE TABLE customer(
customer_id INT(3),
customer_fname VARCHAR(20),
customer_lname VARCHAR(20),
customer_gender CHAR(1),
customer_tel VARCHAR(20),
customer_email VARCHAR(30),
customer_dateJoined DATE,
address_id INT(3),
PRIMARY KEY(customer_id),
INDEX(customer_id),
FOREIGN KEY(customer_id) REFERENCES address);
CREATE TABLE address(
adress_id INT(3),
customer_street VARCHAR(30),
customer_town VARCHAR(30),
customer_postcode CHAR(7),
PRIMARY KEY(address_id),
INDEX(address_id),
FOREIGN KEY(address_id) REFERENCES customer(address_id),
FOREIGN KEY(address_id) REFERENCES employee(address_id));
CREATE TABLE product(
product_id INT(5),
product_name VARCHAR(20),
product_season VARCHAR(15),
product_price NUMERIC(4,2),
product_origin VARCHAR(15),
product_type VARCHAR(15),
product_inStock BOOLEAN,
PRIMARY KEY(product_id),
INDEX(product_id));
CREATE TABLE orders(
order_id INT(3),
customer_id INT(3),
employee_id INT(3),
product_name VARCHAR(20),
quantity INT(4),
order_date TIMESTAMP,
PRIMARY KEY(order_id),
INDEX(order_id));
CREATE TABLE employee(
employee_id INT(3),
employee_fname VARCHAR(20),
employee_lname VARCHAR(20),
address_id INT (3),
employee_pay NUMERIC(2,2),
employee_daysOff INT(2),
employee_hoursWorked INT(3),
PRIMARY KEY(staff_id),
INDEX(staff_id));
You have to create Kettle Table customer_orders, in this table you store customer_id and order_id and connect them with foreign keys to the customer and orders tables.
Like in the following query:
CREATE TABLE customer_orders(
customer_id INT(3),
order_id INT(3),
PRIMARY KEY(customer_id, order_id),
FOREIGN KEY(customer_id) REFERENCES customer(customer_id),
FOREIGN KEY(order_id) REFERENCES orders(order_id)
);
CREATE TABLE sales.stores (
store_id INT IDENTITY (1, 1) PRIMARY KEY,
store_name VARCHAR (255) NOT NULL,
phone VARCHAR (25),
email VARCHAR (255),
street VARCHAR (255),
city VARCHAR (255),
state VARCHAR (10),
zip_code VARCHAR (5)
);
One way to design this is to have two tables for Orders.
OrderHeader :- This contains order id with customer details.
OrderItem :- Contains Line items per order.
OrderHeader fields could be
order_id //primary key
customer_id
employee_id
order_date
OrderItem :- Fields could be
order_id //composite key
line_Item_id //composite key
product_id,
product_quant