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 KEELIN开发者_JS百科G', '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
精彩评论