how do i cascade updates and deletions in oracle? - sql

I'm trying to follow the examples in my book to cascade updates and deletions but the problem is their syntax doesnt work in oracle DB..
I understand to cascade an update would mean an update to a parent would also update its child and same with deletions.. but i cant figure out the syntax for oracle.. the specific questions from my book are:
7.5 -- Write a CREATE TABLE statement for the EMPLOYEE table. Email is
required and is an alternate key, and the default value of Department is
Human Resources. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
7.6 -- Write a CREATE TABLE statement for PROJECT table. The default value for
MaxHours is 100. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
7.7 -- Write a CREATE TABLE statement for the ASSIGNMENT table. Cascade only
deletions from PROJECT to ASSIGNMENT; do not cascade either deletions or
updates from EMPLOYEE to ASSIGNMENT.
I finally managed to successfully created these tables in iSQL *Plus with this Query:
CREATE TABLE DEPARTMENT (
DepartmentName char(35) NOT NULL,
BudgetCode char(30) NOT NULL,
OfficeNumber char(15) NOT NULL,
Phone char(12) NOT NULL,
Constraint DepartmentPK PRIMARY KEY(DepartmentName)
);
INSERT INTO DEPARTMENT VALUES (
'Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');
CREATE TABLE EMPLOYEE (
EmployeeNumber int NOT NULL,
FirstName char(25) NOT NULL,
LastName char(25) NOT NULL,
Department char(35) DEFAULT 'Human Resources' NOT NULL,
Phone char(12) NULL,
Email char(30) NOT NULL,
DepartmentName_FK char(35) NOT NULL,
Constraint EmployeePK PRIMARY KEY(EmployeeNumber),
Constraint EmployeeAK1 UNIQUE(Email),
Constraint DepartmentFK FOREIGN KEY (DepartmentName_FK)
references DEPARTMENT(DepartmentName)
--ON UPDATE CASCADE
--ON DELETE no ACTION
);
CREATE TABLE PROJECT (
ProjectID int NOT NULL,
Name char(30) NOT NULL,
Department1 char(15) NOT NULL,
MaxHours int DEFAULT 100 NOT NULL,
StartDate DATE NULL,
EndDate DATE NULL,
DepartmentName_FK1 char(30) NULL,
Constraint datecheck check (StartDate < EndDate),
Constraint ProjectIDPK PRIMARY KEY(ProjectID),
Constraint DepartmentFK1 FOREIGN KEY (DepartmentName_FK1)
references DEPARTMENT(DepartmentName)
-- ON UPDATE CASCADE
-- ON DELETE no ACTION
);
CREATE TABLE Assignment(
ProjectID Number NOT NULL,
EmployeeNumber Number NOT NULL,
HoursWorked Number NULL,
Constraint ProjectIDEmpNumPK PRIMARY KEY(ProjectID, EmployeeNumber),
constraint ProjectIDFK FOREIGN KEY(ProjectID)
references Project(ProjectID),
constraint EmpNumFK FOREIGN KEY(EmployeeNumber)
references Employee(EmployeeNumber)
--CONSTRAINT UniqueEmployee UNIQUE (EmployeNumber)
--ON DELETE CASCADE
);
but how do I specify cascading delete and update and specify not to?

Did you try this?
CREATE TABLE Assignment(
ProjectID Number NOT NULL,
EmployeeNumber Number NOT NULL,
HoursWorked Number NULL,
Constraint ProjectIDEmpNumPK PRIMARY KEY(ProjectID, EmployeeNumber),
constraint ProjectIDFK FOREIGN KEY(ProjectID)
references Project(ProjectID)
ON DELETE CASCADE
ON UPDATE no ACTION ,
constraint EmpNumFK FOREIGN KEY(EmployeeNumber)
references Employee(EmployeeNumber)
ON DELETE no ACTION
ON UPDATE no ACTION
);

Related

How can I correct the following SQL script?

I am assigned to create a database in SQL based on an ERD that I studied and recreated a week prior. I am using this app called "Oracle SQL Developer" and trying to learn about creating tables, primary keys, foreign keys, sequences, views, etc. I tested my drafts out on the developer and they keep on coming up with the following errors:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vk0cu.png this is some syntax error due to partially recognized rules.
Other errors involve missing right parentheses, tables having more than one primary key, etc. So far, this is my best effort at starting a database:
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER
(CUST_EMAIL VARCHAR(10) PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR(10) NOT NULL,
CUST_CITY VARCHAR(10) NOT NULL,
CUST_STATE VARCHAR(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS
(ORDER_NO NUMERIC(10) PRIMARY KEY,
CUST_EMAIL VARCHAR(10) FOREIGN KEY,
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE(10) NOT NULL,
SHIP_DATE DATE(10) NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO)
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT
(PAY_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
CARD_TYPE VARCHAR(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE(10) NOT NULL,
PAY_CONF INTEGER(10) NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY
(DEL_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
SHIP_DATE DATE(10) FOREIGN KEY,
DEL_DATE DATE(10) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
DEL_MODE VARCHAR(10) NOT NULL,
INVOICE_NO INTEGER(10) NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (SHIP_DATE) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT
(STREET_ADDRESS VARCHAR(10) PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
STATE VARCHAR(10) NOT NULL,
ZIP INTEGER(10) NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
Where should I place my parentheses if the app is correct in saying that I am missing some of them? Where do I even have more than one primary key and how can I rephrase my lines to reduce them? How can I take my rules from partially recognized to fully recognized?
This is for a college project on relational database systems. I just need to create some tables, primary keys, and foreign keys so I can be allowed to create sequences.
DATE and INTEGER do not have a precision.
Either declare the PRIMARY KEY inline or out-of-line but you cannot do both.
Same for foreign keys (and inline foreign keys need the REFERENCES keyword and not the FOREIGN KEY keywords).
VARCHAR would be better as VARCHAR2
You cannot have a FOREIGN KEY that refers to a non-primary key, non-unique column (i.e. SHIP_DATE). While you could create a UNIQUE composite key on ORDER_NO and SHIP_DATE and reference that (example below); it is probably better to entirely remove SHIP_DATE from the DELIVERY table (and then you don't need a foreign key) and just keep it in a single table so the tables are in 3rd normal form. If you want the information to display it then JOIN the tables using the ORDER_NO foreign key.
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER(
CUST_EMAIL VARCHAR2(10),
CUST_NAME VARCHAR2(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR2(10) NOT NULL,
CUST_CITY VARCHAR2(10) NOT NULL,
CUST_STATE VARCHAR2(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS(
ORDER_NO NUMERIC(10),
CUST_EMAIL VARCHAR2(10),
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE NOT NULL,
SHIP_DATE DATE NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO),
CONSTRAINT U_PRODUCTS UNIQUE (ORDER_NO, SHIP_DATE),
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER
);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT(
PAY_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
CARD_TYPE VARCHAR2(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE NOT NULL,
PAY_CONF INTEGER NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY(
DEL_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
SHIP_DATE DATE, -- Delete this line
DEL_DATE DATE NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
DEL_MODE VARCHAR2(10) NOT NULL,
INVOICE_NO INTEGER NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS,
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (ORDER_NO, SHIP_DATE) REFERENCES PRODUCTS (ORDER_NO, SHIP_DATE) -- Delete this line.
);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT(
STREET_ADDRESS VARCHAR2(10),
NAME VARCHAR2(10) NOT NULL,
CITY VARCHAR2(10) NOT NULL,
STATE VARCHAR2(10) NOT NULL,
ZIP INTEGER NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
db<>fiddle here

REFERENCING ISSUE

CREATE TABLE Loan
(
Customerid Char(9) NOT NULL,
EquipmentCode Char(5) NOT NULL,
StartDate DateTime NOT NULL,
EndDate DateTime NULL,
CONSTRAINT CHK_ID
CHECK (Customerid LIKE '[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]'),
CONSTRAINT CHK_Date CHECK (EndDate >= StartDate),
CONSTRAINT Loan_PK PRIMARY KEY(Customerid),
CONSTRAINT CUST_FK
FOREIGN KEY(Customerid) REFERENCES CUST(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE EQUIPMENT
(
EquipmentCode CHAR(5) NOT NULL,
EquipmentName VARCHAR(50) NOT NULL,
Description VARCHAR(255) NULL,
RentalRatePerDay DECIMAL(4,2) NOT NULL,
CONSTRAINT EQP_PK PRIMARY KEY(EquipmentCode),
CONSTRAINT CHK_Rate CHECK (RentalRatePerDay BETWEEN 4 AND 50),
CONSTRAINT LOAN_FK
FOREIGN KEY(EquipmentCode) REFERENCES Loan(EquipmentCode)
ON UPDATE CASCADE
ON DELETE CASCADE
);
I was able to reference earlier on in the above loan.table but for the equipment table it states
Msg 1776, Level 16, State 0, Line 49
There are no primary or candidate keys in the referenced table 'Loan' that match the referencing column list in the foreign key 'LOAN_FK'.
Msg 1750, Level 16, State 1, Line 49
Could not create constraint or index. See previous errors.
Please advise.
When we create a ForeignKey on a dependent table, it MUST refer back to the PrimaryKey (or a Unique Key see: https://stackoverflow.com/a/18435114/1690217) on the principal table.
In your case, EQUIPMENT is the principal end of the relationship, and Loan is the dependent. What this means is that the FK needs to be on the Loan table instead, so you should have this:
CREATE TABLE EQUIPMENT
(
EquipmentCode CHAR(5) NOT NULL,
EquipmentName VARCHAR(50) NOT NULL,
Description VARCHAR(255) NULL,
RentalRatePerDay DECIMAL(4,2) NOT NULL,
CONSTRAINT EQP_PK PRIMARY KEY(EquipmentCode),
CONSTRAINT CHK_Rate CHECK (RentalRatePerDay BETWEEN 4 AND 50),
);
CREATE TABLE Loan
(
Customerid Char(9) NOT NULL,
EquipmentCode Char(5) NOT NULL,
StartDate DateTime NOT NULL,
EndDate DateTime NULL,
CONSTRAINT CHK_ID
CHECK (Customerid LIKE '[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]'),
CONSTRAINT CHK_Date CHECK (EndDate >= StartDate),
CONSTRAINT Loan_PK PRIMARY KEY(Customerid),
CONSTRAINT CUST_FK
FOREIGN KEY(Customerid) REFERENCES CUST(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT EQUIPMENT_FK
FOREIGN KEY(EquipmentCode) REFERENCES EQUIPMENT(EquipmentCode)
ON UPDATE CASCADE
ON DELETE CASCADE
);
A foreign key references a primary key on another table. You have the FK on the wrong table

Oracle PLSQL Cascade Delete doesn't work?

I have some tables with foreign keys which should be deleted. I Put the "on delete cascade" everywhere I needed it, but when I try to Drop the Table i get following error:
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
This is the Table I want to drop:
DROP TABLE Author;
CREATE TABLE Author (
id NUMBER(4) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
date_of_birth DATE NOT NULL,
date_of_death DATE NULL,
CONSTRAINT Author_PK PRIMARY KEY (id)
);
And this is the Table that is in relation to the Author table:
CREATE TABLE Book (
id NUMBER(4) NOT NULL,
author NUMBER(4) NULL,
title VARCHAR2(30) NOT NULL,
ISBN VARCHAR2(13) NOT NULL,
book_language VARCHAR2(2) NOT NULL,
book_genre VARCHAR2(20) NOT NULL,
CONSTRAINT Book_PK PRIMARY KEY (id),
CONSTRAINT Book_Author FOREIGN KEY (author) REFERENCES Author(id) ON DELETE cascade
);
DROP is a DDL. It has nothing to do with DELETE (DML) (affected by the way you created the foreign key constraint).
Drop child table first; then drop its parent.
I resolved it now with help from #a_horse_with_no_name
If you have the same issue, just write
drop table table_name cascade constraints;

I want to create a table with "WITH CHECK OPTION"

create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
For the Gender column I want to insert ONLY 'M','F','O' characters only.
How can I do with this the "WITH CHECK OPTION"
I don't think you want with check option. You want a check constraint:
alter table staff
add constraint chk_staff_gender check (gender in ('M', 'F', 'O'));
(You can put this in the create table statement as well.)
with check option is an option on views that ensures that data remains consistent even when the data in underlying tables changes (see here) .
add , constraint gender_chk check (Gender in ('M','F','O'))
the with check option is on by default.
create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID)
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
, constraint gender_chk check (Gender in ('M','F','O'))
)
From the MSDN Documentation for ALTER TABLE (emphasis added):
WITH CHECK | WITH NOCHECK
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.
If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.
The query optimizer does not consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL.
You are refering to check constraint
create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01) NOT NULL,
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID),
constraint Gender_ck check (Gender in ('M','F','O'))
)
"WITH CHECK OPTION" is an optional clause for of a view definition
Any attempt to update/insert a record, through the view, that cannot be selected by the view, will raise an error.

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