SQL invalid identifier error.....? - sql

Currently working on SSH secure shell code that keeps giving this error code. I've tried changing the table names but still, I get this error. HELP!
Here's what the questions ask:
List the product ID, product name, and product price for the product(s) sold in more orders than any any other products (based on number of order occurrences, not the quantity). Format the price as currency, and use the following column headings: ProductID, Name, Price.
List the category ID, product ID, product name, and product price for the lowest priced product in each category. Format the price as currency, and use the following column headings: Cat_ID, Prod_ID, Prod_Name, Price.
Here's what the code with the errors:
SQL> --question 15
SQL>
SQL> SELECT Pro.ProductID as ProductID, Pro.ProductName AS Name, to_char(Pro.ProductPrice,'$99.99') AS Price
2 FROM Product_mys Pro
3 WHERE Pro.ProductPrice = (SELECT MAX(Pro.ProductPrice)
4 FROM OrderDetail_mys Pro);
WHERE Pro.ProductPrice = (SELECT MAX(Pro.ProductPrice)
*
ERROR at line 3:
ORA-00904: "PRO"."PRODUCTPRICE": invalid identifier
SQL>
SQL> --question 16
SQL>
SQL> SELECT Cat.CatID as Cat_ID,Pro.ProductID as Prod_ID, Pro.ProductName AS Prod_Name, to_char(Pro.ProductPrice,'$99.99') AS Price
2 FROM Category_mys Cat,Product_mys Pro
3 WHERE Pro.ProductPrice = (SELECT MIN(Pro.ProductPrice)
4 FROM OrderDetail_mys Pro);
WHERE Pro.ProductPrice = (SELECT MIN(Pro.ProductPrice)
*
These are the schema tables I'm using for the values
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID CHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES SalesRep_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProductPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;

You're attempting to use ProductPrice from Product_mys which doesn't have that column.

Related

Can have in my case foreign key duplicated value?

can have in this case foreign key duplicated value? Or better just have an index on column?
Here is my table structure:
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
CREATE TABLE orders(
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price INT(10) NOT NULL
);
But in orderitems table (Where I have stored the ordered products, the customer can have multiple products ordered so the foreign key value (cus_id) can be duplicated)
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
cus_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordered_items_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
EDIT:
Sorry the ordered_items table have a unique ID column as well.
Yes! you can have multiple value of cus_id inside ordered_items. however, your intention is better served if you replace cus_id by order_id from orders, Thus your relationship would sound like,
a **customer** can have multiple *orders*,
an **order** can have multiple *order items*,
and an **order item** can only have single *product*
your sql would look like this
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
CREATE TABLE orders(
id INT (5) NOT NULL,
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
ALTER TABLE orders ADD CONSTRAINT order_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price DOUBLE NOT NULL
);
ALTER TABLE products ADD CONSTRAINT product_id PRIMARY KEY ( id ) ;
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
order_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_id PRIMARY KEY ( id ) ;
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_order_frK FOREIGN KEY ( order_id ) REFERENCES orders ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_prd_frK FOREIGN KEY ( product_id) REFERENCES products ( id );

SQL Server : code created and not sure why it is not processing - foreign key problem?

CREATE TABLE THREE_GIRLS_COFFEE_HUT
(
ShopName Char NOT NULL,
PhoneNumber Char(12) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
CONSTRAINT ShopPK PRIMARY KEY (ShopName)
);
CREATE TABLE EMPLOYEE
(
EmployeeID Int NOT NULL IDENTITY(1,1),
EmployeeName Char(30) NOT NULL,
PhoneNumber Char(10) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
EmployeeType Char(10) NOT NULL,
ShopName Char (25) FOREIGN KEY REFERENCES THREE_GIRLS_COFFEE_HUT (ShopName)
CONSTRAINT EmployeePK PRIMARY KEY(EmployeeID),
CONSTRAINT EmployeeAK1 UNIQUE(EmployeeName)
);
CREATE TABLE CUSTOMER
(
CustomerID Int NOT NULL IDENTITY(1000,1),
CustomerName Char(30) NULL,
PhoneNumber Char(10) NULL,
EmailAddress Char(30) NOT NULL,
CONSTRAINT CustomerPK PRIMARY KEY(CustomerID),
CONSTRAINT CustomerAK1 UNIQUE(EmailAddress)
);
CREATE TABLE [ORDER]
(
SalesOrderNumber INT NOT NULL IDENTITY (1500,1),
Date Numeric(6) NOT NULL,
Subtotal Numeric(6, 2) NOT NULL,
Tax Numeric(6, 2) NOT NULL,
Total Numeric(6, 2) NOT NULL,
CONSTRAINT OrderPK PRIMARY KEY (SalesOrderNumber),
CONSTRAINT EmpOrdFK FOREIGN KEY(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID)
ON UPDATE CASCADE
ON DELETE NO ACTION,
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID)
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE PRODUCT
(
ProductNumber Int NOT NULL IDENTITY(2000,1),
ProductDescription Char(20) NOT NULL,
QuantityOnOrder Numeric(4) NOT NULL,
QuantityOnHand Numeric(4) NOT NULL,
OrderDate Date NOT NULL,
ExpirationDate Date NOT NULL,
CONSTRAINT ProductPK PRIMARY KEY(ProductNumber),
CONSTRAINT ValidExpDate CHECK (ExpirationDate > OrderDate)
);
CREATE TABLE MENU_ITEM (
ItemNumber Int NOT NULL IDENTITY(3000,1),
ItemDescription Char(30) NOT NULL,
ItemCost Numeric(6,2) NOT NULL,
ProductNumber Int FOREIGN KEY REFERENCES PRODUCT(ProductNumber)
CONSTRAINT MenuPK PRIMARY KEY(ItemNumber),
CONSTRAINT MenuAK1 UNIQUE(ItemDescription),
);
CREATE TABLE ORDER_LINE_ITEM (
SalesOrderNumber INT FOREIGN KEY REFERENCES [ORDER](SalesOrderNumber),
ItemNumber INT FOREIGN KEY REFERENCES MENU_ITEM(ItemNumber),
Quantity Numeric NOT NULL,
UnitPrice Numeric(6,2) NOT NULL,
ExtendedPrice Numeric (6,2) NOT NULL,
);
I got these errors and it will not process- I think I have problem with my foreign key but I am not sure.
Msg 1769, Level 16, State 1, Line 40
Foreign key 'EmpOrdFK' references invalid column 'EmployeeID' in referencing table 'ORDER'.
Msg 1750, Level 16, State 0, Line 40
Could not create constraint or index. See previous errors.
There's no column EmployeeID in the ORDER table, so:
EmpOrdFK FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEE(EmployeeID)
which says, create a foreign key in which Order.EmployeeId references Employee.EmployeeId, can't work.
Did you want to add an EmployeeId column to Order?
(There's also no CustomerId in Order, so the next line
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
would have failed too, except it never got to run because of the first error.)
You probably want to add EmployeeId and CustomerId to the Order table.
When you are mapping foreign key from Order table to Employee table, you have to map on common column in both tables. If you look at order table, which is missing employeeId in
that table.
And also don't call it as ORDER table. ORDER is reserved keyword in SQL server.
References: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver15

SQL invalid identifier?

I finished typing out SQL statements but now I'm getting errors codes when I run it on omega. I keep getting the invalid identifier code...
Here's what I get once I ran it in omega.
SQL>
SQL> --question 14
SQL>
SQL> SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
2 FROM Product_mys;
SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
*
ERROR at line 1:
ORA-00904: "PRODUCTPRICE": invalid identifier
These are the sql tables where the data is being pulled from
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID CHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES SalesRep_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProductPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
Your query references Sales_Rep_mys but you create SalesRep_mys. So that's at least some of the problem.
Same with Order_Detail_mys and OrderDetail_mys
Looks like those 2 fixes should do it.

Need help in the query (oracle 11 g)

First of all, these are my create_table statements.
/*--- Base tables ---*/
CREATE TABLE CUSTOMER (
CUST_ID VARCHAR(10) NOT NULL,
CUST_FNAME VARCHAR(15),
CUST_LNAME VARCHAR(15),
CUST_HP NUMBER(10),
CUST_EMAIL VARCHAR(40),
primary key (CUST_ID)
);
CREATE TABLE STAFF(
STAFF_ID NUMBER(12) NOT NULL,
STAFF_FNAME VARCHAR(15),
STAFF_LNAME VARCHAR(15),
STAFF_DOB Date,
STAFF_AGE NUMBER(2),
STAFF_ADDRESS VARCHAR(70),
STAFF_HP NUMBER(10),
STAFF_SALARY NUMBER(6,2),
primary key (STAFF_ID)
);
CREATE TABLE SUPPLIER(
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_NAME VARCHAR(30),
SUPP_TEL NUMBER(10),
SUPP_EMAIL VARCHAR(25),
SUPP_ADDRESS VARCHAR(80),
SUPP_CITY VARCHAR(20),
SUPP_STATE VARCHAR(10),
SUPP_ZIP NUMBER(5),
primary key (SUPP_ID)
);
CREATE TABLE PRODUCT(
PROD_ID VARCHAR(10) NOT NULL,
PROD_NAME VARCHAR(30),
PROD_DESC VARCHAR(70),
R_UNIT_PRICE NUMBER(3,2) DEFAULT 4.50,
L_UNIT_PRICE NUMBER(3,2) DEFAULT 5.00,
primary key (PROD_ID)
);
CREATE TABLE INGREDIENT(
ING_ID VARCHAR(10) NOT NULL,
ING_NAME VARCHAR(20),
ING_DESC VARCHAR(60),
primary key (ING_ID)
);
CREATE TABLE TOPPING(
TOP_ID VARCHAR(10) NOT NULL,
TOP_NAME VARCHAR(20),
TOP_DESC VARCHAR(40),
TOP_PRICE NUMBER(2,2),
primary key (TOP_ID)
);
/*--- Child tables ---*/
CREATE TABLE ORDERS(
ORDERS_ID VARCHAR(10) NOT NULL,
CUST_ID VARCHAR(10) NOT NULL,
STAFF_ID NUMBER(12) NOT NULL,
TOTAL_PRICE NUMBER(5,2),
ORDERS_DATE DATE,
primary key (ORDERS_ID),
foreign key (CUST_ID) references CUSTOMER(CUST_ID),
foreign key (STAFF_ID) references STAFF(STAFF_ID)
);
CREATE TABLE ORD_DETAIL(
SK1_ID NUMBER(4) NOT NULL,
ORDERS_ID VARCHAR(10) NOT NULL,
PROD_ID VARCHAR(10) NOT NULL,
TOP_ID VARCHAR(10) NOT NULL,
TEATYPE VARCHAR(8),
CUPSIZE CHAR(1),
QTY NUMBER(2),
CONSTRAINT CHK_CUPSIZE CHECK (CUPSIZE = 'R' OR CUPSIZE = 'L'),
CONSTRAINT CHK_TEATYPE CHECK (TEATYPE = 'Oolong' OR TEATYPE = 'Jasmine'),
primary key (SK1_ID),
foreign key (ORDERS_ID) references ORDERS(ORDERS_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (TOP_ID) references TOPPING(TOP_ID)
);
CREATE TABLE PROD_ING(
PROD_ID VARCHAR(10) NOT NULL,
ING_ID VARCHAR(10) NOT NULL,
primary key (PROD_ID , ING_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID)
);
CREATE TABLE ING_SUPP(
ING_ID VARCHAR(10) NOT NULL,
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_PRICE NUMBER(6,2),
primary key (ING_ID, SUPP_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID),
foreign key (SUPP_ID) references SUPPLIER(SUPP_ID)
);
And my query is this:Which employee served the most number of customers?
But when I executed these statements:
SELECT MAX (COUNT (O.CUST_ID)) AS "Served customer"
FROM STAFF S, ORDERS O
WHERE (S.STAFF_ID = O.STAFF_ID)
GROUP BY O.STAFF_ID
HAVING COUNT (O.CUST_ID) > 0;
What I got was there was a column named "Served customer" with the maximum number only.
So, now my question is how to display the employee's ID, first name and last name along with
"Served customer" column.
The question is: "Which employee served the most number of customers? " Your query does not even return information about the employee, only about the number of customers.
The following aggregation returns for each staff member, the number of customers who were served:
SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID;
To get the maximum number we just need to order this by the count and take the first row. In Oracle, this uses a subquery:
SELECT so.*
FROM (SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID
ORDER BY COUNT(O.CUST_ID) DESC
) so
WHERE rownum = 1;

INSERT statements gives me different errors

I'm trying to run a file which contains INSERT statements. The statements will either give me an not enough values error or unique constraint error.
Below is my create table statement:
CREATE TABLE PART
(PNum VARCHAR(25) NOT NULL,
PName VARCHAR(75) NOT NULL,
PUnitPrice NUMBER(7,2) NOT NULL,
ComponentOf VARCHAR(25),
CONSTRAINT PART_PKEY PRIMARY KEY(PNum),
CONSTRAINT PART_FKEY FOREIGN KEY(ComponentOf)
REFERENCES PART(PNum)
};
CREATE TABLE MANUFACTURER
(MName VARCHAR(50) NOT NULL,
MAddress VARCHAR(100) NOT NULL,
MPhone VARCHAR(25) NOT NULL,
CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY(MName),
);
CREATE TABLE PART-MANUFACTURED
(MDate DATE,not null
PNum VARCHAR(25) NOT NULL,
MName VARCHAR(50) NOT NULL,
Quantity NUMBER(10) NOT NULL,
CONSTRAINT PART-MANUFACTURED_PKEY PRIMARY KEY(MName,PNum,MDate),
CONSTRAINT PART-MANUFACTURED_FKEY1 FOREIGN KEY(PNum)
REFERENCES PART(PNum),
CONSTRAINT PART-MANUFACTURED_FKEY2 FOREIGN KEY(MName)
REFERENCES MANUFACTURER(MName)
);
CREATE TABLE CUSTOMER
(CNum VARCHAR(25) NOT NULL,
CName VARCHAR(75) NOT NULL,
CType VARCHAR(20) NOT NULL,
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY(CNum),
CONSTRAINT CHECK_CType
CHECK(CType IN (‘INDIVIDUAL,’INSTITUITION’))
);
CREATE TABLE ORDERS
(CNum VARCHAR(25) NOT NULL,
PNum VARCHAR(25) NOT NULL,
OrderDate DATE NOT NULL,
OrderQuantity NUMBER(7,2) NOT NULL,
CONSTRAINT ORDERS_PKEY PRIMARY KEY(CNum,PNum,OrderDate),
CONSTRAINT ORDERS_FKEY1 FOREIGN KEY(CNum)
REFERENCES CUSTOMER(CNum),
CONSTRAINT ORDERS_FKEY2 FOREIGN KEY(PNum)
REFERENCES PART(PNum)
);
Below is the statement that gives me the not enough values error:
INSERT INTO PART
VALUES('S001','System-Economy',1100,null);
INSERT INTO PART
VALUES('M001','Monitor-17 inch',250,'S001');
For the unique constraints error, I believe i have to insert the data in order of their primary and referencing keys right?
So what do I need to change in order for the the insert statements to work?
I dont see any error on Insert. The problem is with your create table. Plenty to correct. Try this
CREATE TABLE PART ( PNUM VARCHAR ( 25 ) NOT NULL,
PNAME VARCHAR ( 75 ) NOT NULL,
PUNITPRICE NUMBER ( 7, 2 ) NOT NULL,
COMPONENTOF VARCHAR ( 25 ),
CONSTRAINT PART_PKEY PRIMARY KEY ( PNUM ),
CONSTRAINT PART_FKEY FOREIGN KEY
( COMPONENTOF )
REFERENCES PART ( PNUM ) );
CREATE TABLE MANUFACTURER ( MNAME VARCHAR ( 50 ) NOT NULL,
MADDRESS VARCHAR ( 100 ) NOT NULL,
MPHONE VARCHAR ( 25 ) NOT NULL,
CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY ( MNAME ) );
CREATE TABLE PART_MANUFACTURED ( MDATE DATE NOT NULL,
PNUM VARCHAR ( 25 ) NOT NULL,
MNAME VARCHAR ( 50 ) NOT NULL,
QUANTITY NUMBER ( 10 ) NOT NULL,
CONSTRAINT PART_MANUFACTURED_PKEY PRIMARY KEY
( MNAME, PNUM, MDATE ),
CONSTRAINT PART_MANUFACTURED_FKEY1 FOREIGN KEY
( PNUM )
REFERENCES PART ( PNUM ),
CONSTRAINT PART_MANUFACTURED_FKEY2 FOREIGN KEY
( MNAME )
REFERENCES MANUFACTURER ( MNAME ) );
CREATE TABLE CUSTOMER ( CNUM VARCHAR ( 25 ) NOT NULL,
CNAME VARCHAR ( 75 ) NOT NULL,
CTYPE VARCHAR ( 20 ) NOT NULL,
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY ( CNUM ),
CONSTRAINT CHECK_CTYPE CHECK
( CTYPE IN ('INDIVIDUAL', 'INSTITUITION') ) );
CREATE TABLE ORDERS ( CNUM VARCHAR ( 25 ) NOT NULL,
PNUM VARCHAR ( 25 ) NOT NULL,
ORDERDATE DATE NOT NULL,
ORDERQUANTITY NUMBER ( 7, 2 ) NOT NULL,
CONSTRAINT ORDERS_PKEY PRIMARY KEY
( CNUM, PNUM, ORDERDATE ),
CONSTRAINT ORDERS_FKEY1 FOREIGN KEY
( CNUM )
REFERENCES CUSTOMER ( CNUM ),
CONSTRAINT ORDERS_FKEY2 FOREIGN KEY
( PNUM )
REFERENCES PART ( PNUM ) );
INSERT INTO
PART
VALUES
( 'S001',
'System-Economy',
1100,
NULL );
INSERT INTO
PART
VALUES
( 'M001',
'Monitor-17 inch',
250,
'S001' );