ORA-02291 parent key not found, first - sql

My first programming program is an Oracle Database graduate certificate program, and the textbook is not Oracle friendly in some places (it is a generic database textbook). I had to rewrite the CREATE table commands and what I came up with is written below.
The tables are created in my database, and I can insert values into the vendor table; however, every time I insert values into the product table I receive the ORA-02291 integrity error.
I realize that the parent key is not being found in the vendor table, but I am at a loss as to why. I have tried a combination of column and table constraints on both tables, and nothing works. If someone could help me set up this relationship so I can practice that would be great!
CREATE TABLE VENDOR(
V_CODE INTEGER NOT NULL CONSTRAINT VENDOR_P_K PRIMARY KEY,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(25) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
V_PHONE CHAR(8) NOT NULL,
V_STATE CHAR(2) NOT NULL,
V_ORDER CHAR(1) NOT NULL
);
----------------------------------------------------------------------------
CREATE TABLE PRODUCT(
P_CODE VARCHAR2(10) CONSTRAINT PRODUCT_P_CODE_PK PRIMARY KEY,
P_DESCRIPT VARCHAR2(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH NUMBER NOT NULL,
P_MIN NUMBER NOT NULL,
P_PRICE NUMBER(8,2) NOT NULL,
P_DISCOUNT NUMBER(5,2) NOT NULL,
V_CODE INTEGER NOT NULL,
CONSTRAINT V_CODE_FK FOREIGN KEY (V_CODE) REFERENCES VENDOR (V_CODE)
);
EDIT
INSERT INTO VENDOR 2
VALUES (21225, 'Bryson, Inc.', 'Smithson', '615','223-3234','TN','Y');
INSERT INTO VENDOR 2
VALUES (21226,'Superloo, Inc.','Flushing','904','215-8995','FL','N');
INSERT INTO PRODUCT 2
VALUES ('11QER/31','Power painter, 15 psi., 3-nozzle','03-Nov-13',8,5,109.99,0.00,25595);

The relationship is set up correctly.
You can only insert foreign key values matching a primary key value in the master table. In your example, you are inserting a product with V_CODE = 25595 but you are never inserting a vendor with this V_CODE.
Maybe you intended to insert the vendor later. This does not work, as the database is enforcing the constraints for every command. Therefore, insert the vendor first and append his products later.
If you want to delete vendors, first delete its products, then delete the vendor unless you are using Foreign Keys with Cascade Delete.

Related

Creating triggers and selecting the sum of shipped order with two variables

I currently have this assignment where we are supposed to create triggers to block the insert of a shipment depending on whether they exceed the total or not.
I have to sum all the shipped quantities and group them by order and product number.
Here are my tables to give an outlook:
My variables are in French, I'm sorry
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison,
FOREIGN KEY (noProduit) REFERENCES Produit,
FOREIGN KEY (noCommande) REFERENCES Commande
)
/
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL,
CHECK (quantite > 0),
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande,
FOREIGN KEY (noProduit) REFERENCES Produit
)
/
When I try to execute it, Oracle mentions that the SQL command does not end correctly
Here is what I have so far for the first trigger.
CREATE OR REPLACE TRIGGER bloquerInsertionCommande
BEFORE INSERT
ON LigneLivraison
REFERENCING
NEW AS NouvelleLivraison
FOR EACH ROW
BEGIN
SELECT LigneLivraison.noProduit, LigneLivraison.noCommande, LigneCommande.quantite, SUM(LigneLivraison.quantiteLivree) shipped, (LigneCommande.quantite - shipped) Total
FROM LigneLivraison, LigneCommande
WHERE LigneLivraison.noCommande = LigneCommande.noCommande
GROUP BY noCommande AND noProduit;
IF :NouvelleLivraison.quantiteLivree > Total THEN raise_application_error(-20100, 'La quantite a livrer est trop elevee');
END IF;
END;
/
Since shipping can be made seperately and an x number of times, I need to add all the same product of a certain order number to compare it with what the client originally ordered.
Your create table commands are not working as there are multiple issues as mentioned in the comments inline in following code:
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison(pk_column_name_of_livraison_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table), -- you need to add column name of referencing table here
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table) -- you need to add column name of referencing table here
); -- ending statement with ;
-- / -- this is not needed in sql statements
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL CHECK (quantite > 0), -- combined it in single column level constraint
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table) -- you need to add column name of referencing table here
);
--/
Also, pk of referencing table is written in comment for clarity. You can use pk or unique key in references clause according to your requirement.
After your tables are created properly then only you will be able to identify proper error in the trigger.

Simple SQL issues, cannot figure out what I'm doing wrong to get these errors - ORA-00942 / 907 / 922

This is a supposed to be a simple SQL project, but I'm stuck on the first step. I've never worked with SQL before, so I'm pretty lost. Can someone please tell me what I did wrong that's causing these errors?
Here is the file:
SPOOL output.log;
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id));
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE));
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL));
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL));
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL));
SPOOL OFF;
The errors for the DROP statements are to be expected because the tables do not yet exist. You can safely ignore them.
Most of your problems come from the second closing parenthesis, e.g.
CREATE TABLE movie (
...
cat_mov_id CHAR(10) NOT NULL UNIQUE));
^ ---- here
You need to remove them. It is only needed in the first statement because of the column list of the foreign key.
The first (real) error you get is because you misspelled CONSTRAINT (you wrote CONTRAINT. The full clause needs to be
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
^ -- the "s" was missing here
However you can't create a foreign key constraint to a table that does not exist. So you need to change the order of the create statements to first create the customer table, then you can create the rental table.
Putting all that together, your script should look like this:
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL
);
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE
);
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL
);
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL
);
Unrelated, but: you do not want to use the CHAR data type. It's more efficient and will give you less headaches if you use VARCHAR for every character column.
Of course the first time you run the script your DROP TABLEs will fail because you haven't created them yet. That's to be expected and is acceptable in such a script.
Beyond that, if you are going to create FK relationships, you need to create the parent table before you create the child, which you will see after you correct your misspelled key word.
I will leave it as an exercise for the student to take that clue and locate the mis-spelled key word in the first CREATE TABLE statement.

The INSERT statement conflicted with the FOREIGN KEY constraint"

I'm trying to insert data into my SQL Server 2014 build and I keep getting this error. I tried searching all over the internet and at my whits end, I sort of get what people were saying but still having difficulty understanding what's wrong with my particular code.
All tables are empty without data. I cannot add data once the foreign keys are set. I read that both tables must be populated for this to work but how does that happen when I can't add anything? If I add data before the foreign key, then I'm unable to add foreign keys. Please help!
When trying to insert data into Gym row using the INSERT INTO I get this error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Gym__staffNo". The conflict occurred in database "FitnessApp", table "dbo.Staff", column 'staffNo'.
This also happens when trying to add data into the Equipment or Staff tables as well.
See code here:
Schema:
CREATE TABLE Gym
(
gymNo int NOT NULL IDENTITY(1,1),
staffNo int NOT NULL,
streetAddress varchar(100) NOT NULL,
streetAddress2 varchar(100) NULL,
city varchar(50) NOT NULL,
state char(2) NOT NULL,
zip char(5) NOT NULL,
phone char(10) NOT NULL
) ON [fitnessPlusGroup1];
CREATE TABLE Staff
(
staffNo int NOT NULL IDENTITY(1,1),
gymNo int NOT NULL,
position varchar(50) NOT NULL,
firstName varchar(50) NOT NULL,
lastName varchar(50) NOT NULL,
streetAddress varchar(100) NOT NULL,
streetAddress2 varchar(100) NULL,
city varchar(50) NOT NULL,
state char(2) NOT NULL,
zip char(5) NOT NULL,
phone char(10) NOT NULL,
hireDate date NOT NULL
) ON [fitnessPlusGroup1];
CREATE TABLE Member
(
memberNo int NOT NULL IDENTITY(1,1),
gymNo int NOT NULL,
firstName varchar(30) NOT NULL,
lastName varchar(30) NOT NULL,
streetAddress varchar(100) NOT NULL,
streetAddress2 varchar(100) NULL,
city varchar(50) NOT NULL,
state char(2) NOT NULL,
zip char(5) NOT NULL,
phone char(10) NOT NULL,
memberSince date NOT NULL,
scheduleID int NULL
) ON [fitnessPlusGroup1];
CREATE TABLE Schedule
(
scheduleID int NOT NULL IDENTITY(1,1),
staffNo int NOT NULL,
trainDate date NOT NULL,
trainTime time(0) NOT NULL
) ON [fitnessPlusGroup1];
CREATE TABLE Equipment
(
equipNo int NOT NULL IDENTITY(1,1),
gymNo int NOT NULL,
staffNo int NOT NULL,
name varchar(50) NOT NULL,
quantity int NOT NULL
) ON [fitnessPlusGroup1];
Foreign key relationships setup:
ALTER TABLE Gym
ADD FOREIGN KEY (staffNo) REFERENCES Staff(staffNo);
ALTER TABLE Staff
ADD FOREIGN KEY (gymNo) REFERENCES Gym(gymNo);
ALTER TABLE Member
ADD FOREIGN KEY (gymNo) REFERENCES Gym(gymNo);
ALTER TABLE Member
ADD FOREIGN KEY (scheduleID) REFERENCES Schedule(scheduleID);
ALTER TABLE Schedule
ADD FOREIGN KEY (staffNo) REFERENCES Staff(staffNo);
ALTER TABLE Equipment
ADD FOREIGN KEY (gymNo) REFERENCES Gym(gymNo);
ALTER TABLE Equipment
ADD FOREIGN KEY (staffNo) REFERENCES Staff(staffNo);
The insert commands that give errors:
INSERT INTO Gym(staffNo, streetAddress, streetAddress2, city, state, zip, phone)
VALUES (1, '7300 W Greens Rd', NULL, 'Houston', 'TX', '77064', '2818946151');
Any help would be much appreciated sorry if this seems like a bit much and I hope I provided all the info I could..
I think the problem is in your table design. Take these two FKEY, as an exmpale:
ALTER TABLE Gym
ADD FOREIGN KEY (staffNo) REFERENCES Staff(staffNo);
ALTER TABLE Staff
ADD FOREIGN KEY (gymNo) REFERENCES Gym(gymNo);
You cannot add a record to Gym until you've added the supporting staffNo to Staff. But you cannot add a record to Staff until you've added the supporting gymNo to Gym. These keys prevent you populating either table, as both require records to be present in the other.
Why is this? Because an FKey is like a promise. It guarantees that the value in column x can always be found in table y. In order to fulfill this promise table y must be populated first. But when you have a circular reference, back to the original table, this can never be achieved.
Here is one possible solution. You could remove the staffNo from Gym and GymNo from Staff. Then add a new table StaffGym. This table would have two fields staffNo and gymNo. It would be populated after Staff and Gym, providing a bridge between the two. This is called a cross reference table, or sometimes xref for short.
You say:
All tables are empty without data.
So there is no staffNo with id = 1 in your Staff. You've got a foreign key constraint so you can only reference items that exist. So you first need to populate the lookup data in Staff before you can link to it with an ID.
Also, remove one of the constraint between Staff and Gym, you have this:
ALTER TABLE Gym
ADD FOREIGN KEY (staffNo)
REFERENCES Staff(staffNo);
ALTER TABLE Staff
ADD FOREIGN KEY (gymNo)
REFERENCES Gym(gymNo);
When you only need one of these. I'd say remove it from Gym, as a Gym could have many staff I'd assume, and also remove the StaffNo column from Gym. As this is preventing you from creating one without the other. Once you've removed that link, you should insert data in the correct order.
The order of the inserts is key. If you remove the column I suggested above, you should insert in the following order based on your constraints:
Gym
Staff (requires gym record)
Schedule (requires staff record)
Member (requires schedule record)
Equipment (requires gym and staff record)

(SQL) integrity constraint violated - parent key not found

I did look up for solutions for this problem but i still get the same error..
I'm trying to insert values into PART and MANUFACTURER tables. Initially, i inserted values into MANUFACTURER without knowing the fact i need to deal with the parent table i.e. PART. So, i did the PART then the MANUFACTURER but still not working :(.
These are the tables:
PART(PNum, PName, PUnitPrice, ComponentOf)
primary key (PNum)
foreign key (ComponentOf) references PART(PNum)
MANUFACTURER(MName, MAddress, MPhone)
primary key (MName)
candidate key (MPhone)
candidate key (MAddress)
PART-MANUFACTURED(MDate, PNum, MName, Quantity)
primary key (MName, PNum, MDate)
foreign key (PNum) references PART(PNum)
foreign key (MName) references MANUFACTURER(MName)
CUSTOMER(CNum, CName, CType)
primary key (CNum)
domain constraint ctype in ('INDIVIDUAL', 'INSTITUTION')
ORDERS(CNum, PNum, OrderDate, OrderQuantity)
primary key (CNum, PNum, OrderDate)
foreign key (CNum) references CUSTOMER(CNum)
foreign key (PNum) references PART(PNum)
Create statements:
CREATE TABLE PART(PNum VARCHAR(25) NOT NULL, PName VARCHAR(75) NOT NULL, PUnitPrice NUMBER(7,2) NOT NULL, ComponentOf VARCHAR(25), PRIMARY KEY(PNum), FOREIGN KEY(ComponentOf) REFERENCES PART(PNum));
Table created.
SQL> CREATE TABLE MANUFACTURER(MName VARCHAR(50) NOT NULL, MAddress VARCHAR(100) NOT NULL, MPhone VARCHAR(25) NOT NULL, PRIMARY KEY(MName), CONSTRAINT UK_MADDRESS Unique(MAddress), CONSTRAINT UK_MPHONE UNIQUE(MPhone));
Table created.
SQL> CREATE TABLE PARTMANUFACTURED(MDate DATE NOT NULL, PNum VARCHAR(25) NOT NULL, MName VARCHAR(50) NOT NULL, QUANTITY NUMBER(10) NOT NULL, PRIMARY KEY(MName, PNum, MDate), FOREIGN KEY(PNum) REFERENCES PART(PNum), FOREIGN KEY(MName) REFERENCES MANUFACTURER(MName));
Table created.
SQL> CREATE TABLE CUSTOMER(CNum VARCHAR(25) NOT NULL, CName VARCHAR(75) NOT NULL, CType VARCHAR(20) NOT NULL, PRIMARY KEY(CNum), CHECK(Ctype in('INDIVIDUAL','INSTITUTION')));
Table created.
SQL> CREATE TABLE ORDERS(CNum VARCHAR(25) NOT NULL, PNum VARCHAR(25) NOT NULL, OrderDate DATE NOT NULL, OrderQuantity NUMBER(7,2) NOT NULL, PRIMARY KEY(CNum, PNum, OrderDate), FOREIGN KEY(CNum) REFERENCES CUSTOMER(CNum), FOREIGN KEY(PNum) REFERENCES PART(PNum));
Isn't the PNum already the primary or parent key? and PART table is the parent table? since, other tables have the PNum as foreign key.. i really don't get it..
anyone knows and can help me with it, is greatly appreciated. thanks :)
The error with your insert statement INSERT INTO PART VALUES('S001', 'System-economy', 1100, 'Null') is that you are trying to insert a string 'NULL' rather than an actual NULL for the column ComponentOf in the PART table.
The problem with the string 'NULL' is that you have a FOREIGN KEY constraint on ComponentOf that references the column PNum, which means that all the values in the column ComponentOf must also be in PNum. However, there is no value 'NULL' in PNum so that's why it threw the error. An actual NULL works since it means that it is not referencing anything.
The value inserted for ComponentOf has to match an existing PNum in the PARTS table. Your key is their to ensure you don't have any "orphaned" components.
If you try to insert 'Null' (a string value as mentioned in the comments) then it can't find the "parent". However, null is allowed since it means that particular part is not a component of any other part, i.e. it doesn't have a "parent".

Removing all references from a tuple using Oracle/Access

I created a simple databse using Oracle that has several tables and a few constraints, i am using Access 2007 to interact with the database.
My problem is that I have a table that is called "Customer" which holds several fields, most notably a Primary Key called CUSTID.
I have another table called "Order" which uses the Primary Key in "Customer" as a Foreign Key.
Obviously if i try to delete a customer that is being used in "Order" i get an error as it's being used. This means i have to delete the particular order that references the Customer and then i can delete the Custome record.
I know that "cascade" should delete everything associated with it, my question is how can i do that from Access? I have limited knowledge of databases but enough to create one etc..
This is more of a validation thing, it's just a hassle to delete certain tuples before i can remove another.
This is the database created for Oracle:
--Used to create a "Clean" slate
DROP TABLE ITEM CASCADE CONSTRAINTS;
DROP TABLE CUSTOMER CASCADE CONSTRAINTS;
DROP TABLE CORDER CASCADE CONSTRAINTS;
DROP TABLE FORUM CASCADE CONSTRAINTS;
CREATE TABLE ITEM
(
ITEMID NUMBER(4) NOT NULL,
NAME CHAR(15) NOT NULL,
CATEGORY CHAR(15) NOT NULL,
PRICE NUMBER(8) NOT NULL,
CONSTRAINT ITEM_PK PRIMARY KEY (ITEMID)
);
INSERT INTO ITEM VALUES (1000,'CARROT SEEDS','PACKET SEEDS',2.99);
INSERT INTO ITEM VALUES (2250,'ROSES','FLOWERS',5.99);
INSERT INTO ITEM VALUES (3300,'TOMATOES','PACKET SEEDS',2.99);
INSERT INTO ITEM VALUES (4050,'POTATOES','PACKET SEEDS',1.99);
CREATE TABLE CUSTOMER
(
CUSTID NUMBER(4) NOT NULL,
FNAME CHAR(10) NOT NULL,
LNAME CHAR(10) NOT NULL,
ADDRESS CHAR(40) NOT NULL,
CITY CHAR(15) NOT NULL,
PCODE CHAR(7) NOT NULL,
CNUMBER NUMBER(11) NOT NULL,
CONSTRAINT CUSTOMER_PK PRIMARY KEY (CUSTID)
);
INSERT INTO CUSTOMER VALUES (1010,'JAMIE','KEELING','149 OLD MANSFIELD ROAD','DERBY','DE214SA',07500966490);
INSERT INTO CUSTOMER VALUES (2020,'HELEN','DARLINGTON','27 MOORPARK AVENUE','ROCHDALE','OL113JQ',07890189802);
INSERT INTO CUSTOMER VALUES (3030,'STEVEN','SEGAL','123 FAKE STREET','OHIO','SE095BG',01559345467);
INSERT INTO CUSTOMER VALUES (4040,'BRUCE','WAYNE','17 LAKEVIEW CRESCENT','CHICAGO','MN432BD',07500966490);
CREATE TABLE CORDER
(
ORDERID NUMBER(4) NOT NULL,
CUSTID NUMBER(4) NOT NULL,
SHIPADD CHAR(40) NOT NULL,
SHIPPCODE CHAR(7) NOT NULL,
SHIPDATE DATE,
ITEMID NUMBER(4) NOT NULL,
QUANTITY NUMBER(3) NOT NULL,
TOTAL NUMBER(8) NOT NULL,
CONSTRAINT ORDER_PK PRIMARY KEY (ORDERID),
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID),
CONSTRAINT FK_ITEM FOREIGN KEY (ITEMID) REFERENCES ITEM(ITEMID)
);
INSERT INTO CORDER VALUES (1000,1010,'149 OLD MANSFIELD ROAD','DE214SA','12-JAN-07',1000,100,100.00);
INSERT INTO CORDER VALUES (2000,2020,'27 MOORPARK AVENUE','OL113JQ','04-NOV-10',2250,200,100.00);
INSERT INTO CORDER VALUES (3000,3030,'123 FAKE STREET','SE095BG','30-OCT-08',3300,150,100.00);
INSERT INTO CORDER VALUES (4000,4040,'17 LAKEVIEW CRESCENT','MN432BD','25-JUL-07',4050,125,100.00);
CREATE TABLE FORUM
(
FORUMID NUMBER(4) NOT NULL,
TITLE CHAR(30) NOT NULL,
THREADNAME CHAR(30) NOT NULL,
POSTER CHAR(20) NOT NULL,
POSTDATE DATE,
CONSTRAINT FORUM_PK PRIMARY KEY (FORUMID)
);
INSERT INTO FORUM VALUES (1001,'GENERAL CHAT','BEGINNER QUESTIONS','JAMIE KEELING', '25-NOV-09');
INSERT INTO FORUM VALUES (2002,'OFF TOPIC','FAVOURITE BAND','HELEN DARLINGTON', '12-JAN-09');
INSERT INTO FORUM VALUES (3003,'GENERAL CHAT','WHEN TO HARVEST?','BRUCE WAYNE', '02-NOV-08');
INSERT INTO FORUM VALUES (4004,'OFF TOPIC','WHERE DO YOU LIVE?','STEVEN SEGAL', '13-JAN-08');
Standard SQL way to create foreign key with ON DELETE CASCADE is following. I don't know if Access supports this, but Oracle does have support for ON DELETE CASCADE.
alter table CORDER
add foreign key FK_CUSTOMER
references CUSTOMER(custid)
on delete cascade
If you are re-creating tables, you can also define the foreign keys with on delete cascade in table creation statement. i.e. replace this
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID)
with this
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID) ON DELETE CASCADE