代码之家  ›  专栏  ›  技术社区  ›  Jamie Keeling

使用Oracle/Access从元组中删除所有引用

  •  0
  • Jamie Keeling  · 技术社区  · 15 年前

    我使用Oracle创建了一个简单的数据库,它有几个表和一些约束,我使用Access2007与数据库进行交互。

    我的问题是我有一个名为“customer”的表,它包含几个字段,最显著的是一个名为custid的主键。

    我有另一个名为“order”的表,它使用“customer”中的主键作为外键。

    显然,如果我试图删除一个正在“订单”中使用的客户,在使用过程中会得到一个错误。这意味着我必须删除引用客户的特定订单,然后才能删除客户记录。

    我知道“级联”应该删除与之相关的所有内容,我的问题是如何从Access中做到这一点?我对数据库的了解有限,但足以创建一个数据库等。

    这更像是一个验证,在删除另一个元组之前删除某些元组只是一个麻烦。

    这是为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');
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Juha Syrjälä    15 年前

    标准的SQL创建方法 foreign key 在删除时,层叠如下。我不知道Access是否支持这一点,但Oracle确实支持删除级联。

    alter table CORDER
    add foreign key FK_CUSTOMER
    references CUSTOMER(custid)
    on delete cascade
    

    如果要重新创建表,还可以在表创建语句中使用ON DELETE CASCADE定义外键。也就是说,更换这个

    CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID)
    

    用这个

    CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID) ON DELETE CASCADE