Hello all hope all is well. Im still new to mysql and im beginning to use JOIN functions and solving a problem which involves the combination of 3 Tables. Ive followed the Syntax i found
SELECT t1.col, t3.col FROM table1 join table2 ON table1.primarykey = table2.foreignkey
join table3 ON table2.primarykey = table3.foreignkey
The question is:
"Find a name of product ordered and order date of all products ordered by a customer with customer code SAVEA and such that quantity of product is 10."
Here is my Solution
SELECT CUSTOMER.CUSTOMER_CODE, ORDERS.ORDER_DATE, ORDER_DETAIL.PRODUCT_NAME, ORDER_DETAIL.QUANTITY
FROM ORDERS
JOIN CUSTOMER ON CUSTOMER.CUSTOMER_CODE = ORDERS.CUSTOMER_CODE
JOIN ORDER_DETAIL ON ORDERS.ORDER_ID = ORDER_DETAIL.ORDER_ID
WHERE ORDER_DETAIL.QUANTITY = '10';
and here are the 3 tables i am using, labeled.
TABLE CUSTOMER
CREATE TABLE CUSTOMER
(
CUSTOMER_CODE VARCHAR(5) NOT NULL,
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30),
CONTACT_TITLE VARCHAR(30),
ADDRESS VARCHAR(60),
CITY VARCHAR(15),
REGION VARCHAR(15),
POSTAL_CODE VARCHAR(10),
COUNTRY VARCHAR(15),
PHONE VARCHAR(24),
FAX VARCHAR(24),
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMER_CODE)
);
TABLE ORDERS
CREATE TABLE ORDERS
(
ORDER_ID DECIMAL(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID DECIMAL(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE,
SHIPPED_DATE DATE,
SHIP_VIA VARCHAR(40),
FREIGHT DECIMAL(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40),
SHIP_ADDRESS VARCHAR(60),
SHIP_CITY VARCHAR(15),
SHIP_REGION VARCHAR(15),
SHIP_POSTAL_CODE VARCHAR(10),
SHIP_COUNTRY VARCHAR(15),
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
TABLE ORDER_DETAIL
CREATE TABLE ORDER_DETAIL
(
ORDER_ID DECIMAL(9) NOT NULL,
PRODUCT_NAME VARCHAR(40) NOT NULL,
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
QUANTITY DECIMAL(9) NOT NULL DEFAULT 1 ,
DISCOUNT DECIMAL(4,2) NOT NULL DEFAULT 0,
CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);
The exact error i get is ERROR 1054(42522) Unknown column 'CUSTOMER_CODE' in 'field list'. I believe i am follwing the right Syntax so maybe its an issue with a comma or my order. if anyone could help it would be much appreciated
Related
Hi all i have a pretty basic question but i cant pickup where im wrong in my Syntax. Im supposed to create a concat message that shows what items are discounted against a certain constraint. The first step is adding data that meets the requirement. In this case it is an ORDER that is created after the 30th of April 2019, as shown below
INSERT INTO ORDERS
(ORDER_ID, CUSTOMER_CODE, EMPLOYEE_ID, ORDER_DATE, REQUIRED_DATE, SHIPPED_DATE, SHIP_VIA, FREIGHT, SHIP_NAME, SHIP_ADDRESS, SHIP_CITY, SHIP_REGION, SHIP_POSTAL_CODE, SHIP_COUNTRY)
VALUES
(4000,'SAVEA', 4, STR_TO_DATE('01/05/2019', '%m/%d/%Y'), STR_TO_DATE('01/05/2019', '%m/%d/%Y'),
STR_TO_DATE('02/28/2019', '%m/%d/%Y'), 'Ausralia Post', 15.63, 'Titanic', '185 League Street.',
'Sydney', 'AB', '56728', 'Australia');
INSERT INTO ORDER_DETAIL
( ORDER_ID, PRODUCT_NAME ,UNIT_PRICE, QUANTITY, DISCOUNT )
VALUES
(4000, 'Konbu',6,10,0);
INSERT INTO ORDER_DETAIL
( ORDER_ID, PRODUCT_NAME ,UNIT_PRICE, QUANTITY, DISCOUNT )
VALUES
(4000, 'Alice Mutton',39,10,0);
The next step is too create a table to store the message
CREATE TABLE MESSAGES (
SIZE VARCHAR(500) NOT NULL
);
the next step, which is where i believe my syntax error lies, is to create the concat message itself
INSERT INTO MESSAGE
SELECT CONCAT('Order', ORDERS.ORDER_ID, 'submitted on',ORDERS.ORDER_DATE,'includes a discontinued product', ORDER_DETAIL.PRODUCT_NAME)
FROM PRODUCT
JOIN ORDER_DETAIL ON PRODUCT.PRODUCT_NAME = ORDER_DETAIL.PRODUCT_NAME JOIN ORDERS ON ORDERS.ORDER_ID = ORDER_DETAIL.ORDER_ID
WHERE PRODUCT.DISCONTINUED = 'N' AND ORDERS.ORDER_DATE > STR_TO_DATE('04/30/2019','%m/%d/%Y');
im following a passed assingment example to the Tee and i dont understand why it brings No results. Below ill post the tables used
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20),
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
UNITS_IN_STOCK DECIMAL(9) NOT NULL DEFAULT 0,
UNITS_ON_ORDER DECIMAL(9) NOT NULL DEFAULT 0,
REORDER_LEVEL DECIMAL(9) NOT NULL DEFAULT 0,
DISCONTINUED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
CREATE TABLE ORDERS
(
ORDER_ID DECIMAL(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID DECIMAL(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE,
SHIPPED_DATE DATE,
SHIP_VIA VARCHAR(40),
FREIGHT DECIMAL(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40),
SHIP_ADDRESS VARCHAR(60),
SHIP_CITY VARCHAR(15),
SHIP_REGION VARCHAR(15),
SHIP_POSTAL_CODE VARCHAR(10),
SHIP_COUNTRY VARCHAR(15),
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
CREATE TABLE ORDER_DETAIL
(
ORDER_ID DECIMAL(9) NOT NULL,
PRODUCT_NAME VARCHAR(40) NOT NULL,
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
QUANTITY DECIMAL(9) NOT NULL DEFAULT 1 ,
DISCOUNT DECIMAL(4,2) NOT NULL DEFAULT 0,
CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);
To debug this, first do just the select part, don’t do the insert part.
I bet the problem is around how you specify the date. Try dropping that out of the query and see what you get.
SOLVED: I was putting the date in wrong, i was doing day month then year not month day and year
Hello all Im trying to Find the total value of an order Through Arthmetic(Quanity * Unit_price) which i can do BUT the question wants me to do so using a nest query and a WITH clause. How would I implement the WITH clause?
below is what ive done but not meeting the procedure requirements
SELECT ORDER_ID
FROM ORDERS
WHERE ORDER_ID IN
(SELECT(QUANTITY * UNIT_PRICE) AS TOTAL_VALUE
FROM ORDER_DETAIL)
ODER BY ORDER_ID ASC;
CREATE TABLE ORDERS
(
ORDER_ID DECIMAL(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID DECIMAL(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE,
SHIPPED_DATE DATE,
SHIP_VIA VARCHAR(40),
FREIGHT DECIMAL(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40),
SHIP_ADDRESS VARCHAR(60),
SHIP_CITY VARCHAR(15),
SHIP_REGION VARCHAR(15),
SHIP_POSTAL_CODE VARCHAR(10),
SHIP_COUNTRY VARCHAR(15),
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
CREATE TABLE ORDER_DETAIL
(
ORDER_ID DECIMAL(9) NOT NULL,
PRODUCT_NAME VARCHAR(40) NOT NULL,
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
QUANTITY DECIMAL(9) NOT NULL DEFAULT 1 ,
DISCOUNT DECIMAL(4,2) NOT NULL DEFAULT 0,
CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);
i think i might need to use this table but i am unsure(TABLE PRODUCT)
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20),
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
UNITS_IN_STOCK DECIMAL(9) NOT NULL DEFAULT 0,
UNITS_ON_ORDER DECIMAL(9) NOT NULL DEFAULT 0,
REORDER_LEVEL DECIMAL(9) NOT NULL DEFAULT 0,
DISCONTINUED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
This is the exact question
Insert into a file solution1.sql implementation of the following query as
SELECT statement with WITH clause.
Find all orders such that a value of each order is greater than an average value of all orders submitted so far. List in each line of output an order identifier, a total value of an order, and an average value of all orders. The results must be sorted in the descending order of a total value of each order.
"A total value of an order must be computed as the summation of unit price * quantityover all items included in the order. The query must be implemented as a sequence of subquery definitions following WITH keyword and ended with the final SELECT.
(i) The first subquery definition must find a total value of each order together with an order identifier (attribute order_id). "
im currently on (i)
Ignoring for a moment the subsequent requirements, the first block of code might look like this:
SELECT o.some
, o.columns
, o.from_orders
, x.total_value
FROM orders o
JOIN
( SELECT order_id
, SUM(quantity * unit_price) total_value
FROM order_detail
GROUP
BY order_id
) x
ON x.order_id = o.order_id
The task given is:
Create a new relational table to store information about the company names of all suppliers and the total number of products supplied by each supplier. Enforce, the appropriate consistencyconstraints on the new table. Next, copy into the new table information about the company names of all suppliers and the total number of products supplied by each supplier.
I'm recieving an error Duplicate Entry for key "PRIMARY" When i try and run this script
CREATE TABLE COMPANY_AND_SUPPLIERS (
COMPANY_NAME VARCHAR (40) NOT NULL DEFAULT 'EMPTY',
PRODUCT_NAME VARCHAR(40) NOT NULL DEFAULT 'EMPTY' ,
TOTAL_PRODUCTS VARCHAR(40) NOT NULL DEFAULT 'EMPTY',
CONSTRAINT SUPPLIER_PKEY PRIMARY KEY(COMPANY_NAME) ,
CONSTRAINT SUPPLIER_FKEY FOREIGN KEY (COMPANY_NAME) REFERENCES SUPPLIER(COMPANY_NAME)
);
INSERT INTO COMPANY_AND_SUPPLIERS(COMPANY_NAME, PRODUCT_NAME)
SELECT DISTINCT SUPPLIER.COMPANY_NAME, PRODUCT.PRODUCT_NAME
FROM SUPPLIER, PRODUCT;
UPDATE COMPANY_AND_SUPPLIERS
SET TOTAL_PRODUCTS = (SELECT COUNT(*) AS TOTALPRODUCTS
FROM PRODUCT);
The Whole purpose of the exercise is to copy company names of all suppliers and the total number of products supplied by each supplier.
TABLES GIVEN
CREATE TABLE SUPPLIER
(
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30),
CONTACT_TITLE VARCHAR(30),
ADDRESS VARCHAR(60),
CITY VARCHAR(15),
REGION VARCHAR(15),
POSTAL_CODE VARCHAR(10),
COUNTRY VARCHAR(15),
PHONE VARCHAR(24),
FAX VARCHAR(24),
HOME_PAGE VARCHAR(500),
CONSTRAINT PK_SUPPLIER PRIMARY KEY (COMPANY_NAME)
);
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20),
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
UNITS_IN_STOCK DECIMAL(9) NOT NULL DEFAULT 0,
UNITS_ON_ORDER DECIMAL(9) NOT NULL DEFAULT 0,
REORDER_LEVEL DECIMAL(9) NOT NULL DEFAULT 0,
DISCONTINUED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
The column company_name should not be a primary key because a primary key is a unique value.
Take this as an example
If a Database administrator creates a table with first_name as the primary key, it would be a disaster since there are a lot of people who has John as their first name
That is why most of the time, the primary key is a integer, then we make it unique using this method.
Your table isn't supposed to contain the product names, just the company and total products. The PRODUCTS table already has the supplier names in it for each product. So you just need to count the number of products for each supplier from that table.
CREATE TABLE Company_Totals (
Company_name VARCHAR(40) NOT NULL,
Total_Products INT(11) NOT NULL,
PRIMARY KEY (Company_name),
FOREIGN KEY (Company_name) REFERENCES Supplier(Company_name)
);
INSERT INTO Company_Totals (Company_name, Total_Products)
SELECT SUPPLIER_NAME, COUNT(*)
FROM PRODUCT
GROUP BY SUPPLIER_NAME;
//this is the question
Find customer name, order number and order amount if order amount is greater than average
sales amount of the customers and sort the record by customer name [Hint: use WHERE
clause].
SELECT
name, orderno, amount
FROM
customerinfo,
orderdetails
WHERE
amount > (SELECT
AVG(amount)
FROM
orderdetails)
ORDER BY name;
//this the result the result are not what i look for
this the table coding
CREATE TABLE customerinfo (
customercode VARCHAR(10) NOT NULL UNIQUE,
name VARCHAR(60) NOT NULL,
address VARCHAR(60),
poscode INT(5),
city VARCHAR(30),
state VARCHAR(30),
hpno VARCHAR(20),
email VARCHAR(50),
salesmanid VARCHAR(10),
PRIMARY KEY (customercode),
FOREIGN KEY (salesmanid)
REFERENCES salesman (salesmanid)
);
CREATE TABLE salesman (
salesmanid VARCHAR(10) NOT NULL UNIQUE,
salesmanname VARCHAR(60) NOT NULL,
PRIMARY KEY (salesmaniD)
);
CREATE TABLE orderinfo (
orderno INT(10) NOT NULL,
ordertype VARCHAR(50),
orderdate DATE,
deliverydate DATE,
PRIMARY KEY (orderno)
);
CREATE TABLE orderdetails (
orderno INT(10) NOT NULL,
itemcode VARCHAR(10) NOT NULL,
quantity INT(5),
amount DECIMAL(9 , 2 ),
PRIMARY KEY (orderno , itemcode),
FOREIGN KEY (orderno)
REFERENCES orderinfo (orderno),
FOREIGN KEY (itemcode)
REFERENCES item (itemcode)
);
CREATE TABLE item (
itemcode VARCHAR(10) NOT NULL UNIQUE,
itemdesc VARCHAR(60),
unit VARCHAR(10),
sellingprice DECIMAL(9 , 2 ) NOT NULL,
PRIMARY KEY (itemcode)
);
CREATE TABLE ordersalesmaninfo (
salesmanid VARCHAR(10) NOT NULL,
orderno INT(10) NOT NULL,
PRIMARY KEY (salesmanid),
FOREIGN KEY (salesmanid)
REFERENCES salesman(salesmanid),
FOREIGN KEY (orderno)
REFERENCES orderinfo (orderno)
);
i may have problem with the normalisation process but i think there will not cause any impact to the question .
I am trying to insert data into my DB and I keep getting the "ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails" on 1 insert command. I believe it has to do with the FKs I used when I created the table. But I am not sure how to fix it. Any suggestions would be awesome.
Original Create table commands:
REATE TABLE Donut
(DonutID int,
DonutName VARCHAR(25) not null,
Description VARCHAR(25) not null,
UnitPrice decimal(4,2) not null,
PRIMARY KEY (DonutID) ) ;
CREATE TABLE Customer
( CustomerID INT not null,
FirstName VARCHAR(20) not null,
LastName VARCHAR(20) not null,
StreetAddress VARCHAR(20) not null,
Apt VARCHAR(20),
City VARCHAR(20) not null,
State VARCHAR(2) not null,
ZipCode VARCHAR(5) not null,
HomePhone VARCHAR(10) not null,
MobilePhone VARCHAR(10) not null,
OtherPhone VARCHAR(10),
PRIMARY KEY(CustomerID) ) ;
CREATE TABLE Orders
( OrderID int not null,
CustomerID int,
OrderDate Date not null,
Notes VARCHAR(100),
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID)
REFERENCES Customer (CustomerID) ) ;
CREATE TABLE OrderItem
( OrderID INT,
DonutID INT,
Qty SMALLINT not null,
PRIMARY KEY(OrderID, DonutID),
FOREIGN KEY (OrderID)
REFERENCES Orders (CustomerID),
FOREIGN KEY (DonutID)
REFERENCES Donut (DonutID) ) ;
Insert command that is failing:
INSERT INTO OrderItem
(OrderID,DonutID,Qty) VALUES
(991,1,12),
(992,2,500),
(993,3,6);
This error is probably happening because one or both of your inserted records refer to records in the Orders and/or Donut tables which do not exist.
Try running the following queries:
SELECT *
FROM Orders
WHERE CustomerID IN (991, 992);
and this one:
SELECT *
FROM Donut
WHERE DonutID IN (1, 2);
If you don't see four parent records from the two above queries, then you have the answer to your problem. The solution of course is to not refer to records which do not exist.
I had to change The Foreign key reference for OrderID..it was referencing Orders (customerID) and it should be Reference Orders (OrderID)
CREATE TABLE OrderItem
( OrderID INT,
DonutID INT,
Qty SMALLINT not null,
PRIMARY KEY(OrderID, DonutID),
FOREIGN KEY (OrderID)
REFERENCES Orders (OrderID),
FOREIGN KEY (DonutID)
REFERENCES Donut (DonutID) ) ;
Answered my own question!