如何正确使用删除查询

时间:2017-09-25 02:28:22

标签: sql oracle

我对DBMS很陌生,需要一些关于如何为这个问题编写删除的建议。我一直在谷歌上搜索,这是我到目前为止所得到的。查询将不会运行并抛出错误:

  

违规 - 找到儿童记录

这就是问题:

删除会员David Jones及所有相关记录。确保从所有相关关系中删除相关元组,并按正确顺序删除它们。

代码:

CREATE TABLE Book
    (bookID INT,
    ISBN INT,
    title varchar (25),
    author  varchar (20),
    publish_year INT,
    category varchar(20),
    PRIMARY KEY (bookID));  

CREATE TABLE Member
    (memberID INT,
    lastname varchar (20),
    firstname varchar (20),
    address varchar(20),
    phone_number INT,   
    limit_ INT,
    PRIMARY KEY (memberID));

CREATE TABLE CurrentLoan
    (memberID INT ,
    bookID INT,
    loan_date DATE,
    due_date DATE,
    PRIMARY KEY (memberID, bookID),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));

CREATE TABLE History
    (memberID INT,
    bookID INT,
    loan_date DATE,
    return_date DATE,
    PRIMARY KEY (memberID, bookID, loan_date),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));

INSERT INTO Book VALUES (10, 1113312336, 'The Dog', 'Jack Crow', 1990, 'fiction');
INSERT INTO Book VALUES (12, 2221254896, 'Worms', 'Jim Kan', 2013, 'childrens');
INSERT INTO Book VALUES (13, 3332546987, 'Crow', 'Jan Flo', 2000, 'fiction'); 
INSERT INTO Book VALUES (14, 4443456215, 'Big Dog', 'Lan Big', 1993, 'children');
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'children');
INSERT INTO Book VALUES (16, 6664581631, 'Red Bean', 'Khang Nk', 2017, 'fiction');
INSERT INTO Book VALUES (17, 7771452369, 'XML and XQuery Knowledge', 'Author Le', 2017, 'non-fiction');
INSERT INTO Book VALUES (18, 8881245525, 'The Dark Room', 'Jack Se', 2017, 'fiction');
INSERT INTO Book VALUES (19, 9991123546, 'Lonely Mens', 'Geen Brown', 2014, 'refrence');
INSERT INTO Book VALUES (20, 1122112356, 'XML or XQuery', 'Heart Le', 2002, 'fiction');
INSERT INTO BOOK VALUES (21, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (22, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (24, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (25, 1414141414, 'The Very Blue Boy', 'Ben Jen', 2006, 'fiction');

INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 2);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 2);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 2);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 2);
INSERT INTO Member VALUES (006, 'Khang', 'Nkaujyi', '358 Spencer', 2145345625, 2);
INSERT INTO Member VALUES (007, 'Jones', 'David', '4th Street', 14812365789, 2);

INSERT INTO CurrentLoan VALUES (001, 10, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (003, 16, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (005, 15, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '13-APR-17', '12-MAY-17');
INSERT INTO CurrentLoan VALUES (007, 25, '02-OCT-17', '25-DEC-17');

INSERT INTO History VALUES (001, 10, '14-Jan-17', '04-OCT-17');
INSERT INTO History VALUES (003, 19, '12-Jan-17', '04-NOV-17');
INSERT INTO History VALUES (002, 13, '14-APR-17', '08-OCT-17');
INSERT INTO History VALUES (005, 20, '14-MAY-17', '04-DEC-17');
INSERT INTO History VALUES (007, 25, '02-OCT-16', '25-DEC-16');
COMMIT;

我的查询:

DELETE FROM Book
WHERE bookID = 25;

DELETE FROM MEMBER 
WHERE lastname = 'Jones' AND firstname = 'David';

DELETE FROM CurrentLoan
WHERE memberID = 007;

DELETE FROM History
WHERE memberID = 007;

3 个答案:

答案 0 :(得分:2)

我不确定DBMS,但遵循任何关系数据库逻辑,除非删除依赖于FK的记录,否则无法删除FK(foriegn键)。在删除所述密钥之前,您需要删除任何引用外键的记录。如果我正确地读取您的数据,您有两个表(Currentloan和History)依赖于Book和Member中的外键。在删除Book和member中的外键之前,您需要删除Currentloan和History中的记录。

新查询

DELETE FROM CurrentLoan
WHERE memberID = 007;

DELETE FROM History
WHERE memberID = 007;

DELETE FROM Book
WHERE bookID = 25;

DELETE FROM MEMBER 
WHERE lastname = 'Jones' AND firstname = 'David';

答案 1 :(得分:0)

/* 
    delete records from the tables with relations first using the memberID 
    */
delete from currentloan where memberID = 7 ;
delete from history where memberID = 7 ;
/*
    delete the member using the memberID
    */
delete from member where memberID = 7 ;

答案 2 :(得分:0)

删除名为David Jones amd相关记录的所有成员记录的方法。使用以下命令创建架构和虚拟数据。请注意,David Jones有两条记录。

CREATE TABLE Book
    (bookID INT,
    ISBN INT,
    title varchar (25),
    author  varchar (20),
    publish_year INT,
    category varchar(20),
    PRIMARY KEY (bookID));  

CREATE TABLE Member
    (memberID INT,
    lastname varchar (20),
    firstname varchar (20),
    address varchar(20),
    phone_number INT,   
    limit_ INT,
    PRIMARY KEY (memberID));

CREATE TABLE CurrentLoan
    (memberID INT ,
    bookID INT,
    loan_date DATE,
    due_date DATE,
    PRIMARY KEY (memberID, bookID),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));

CREATE TABLE History
    (memberID INT,
    bookID INT,
    loan_date DATE,
    return_date DATE,
    PRIMARY KEY (memberID, bookID, loan_date),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));

INSERT INTO Book VALUES (10, 1113312336, 'The Dog', 'Jack Crow', 1990, 'fiction');
INSERT INTO Book VALUES (12, 2221254896, 'Worms', 'Jim Kan', 2013, 'childrens');
INSERT INTO Book VALUES (13, 3332546987, 'Crow', 'Jan Flo', 2000, 'fiction'); 
INSERT INTO Book VALUES (14, 4443456215, 'Big Dog', 'Lan Big', 1993, 'children');
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'children');
INSERT INTO Book VALUES (16, 6664581631, 'Red Bean', 'Khang Nk', 2017, 'fiction');
INSERT INTO Book VALUES (17, 7771452369, 'XML and XQuery Knowledge', 'Author Le', 2017, 'non-fiction');
INSERT INTO Book VALUES (18, 8881245525, 'The Dark Room', 'Jack Se', 2017, 'fiction');
INSERT INTO Book VALUES (19, 9991123546, 'Lonely Mens', 'Geen Brown', 2014, 'refrence');
INSERT INTO Book VALUES (20, 1122112356, 'XML or XQuery', 'Heart Le', 2002, 'fiction');
INSERT INTO BOOK VALUES (21, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (22, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (24, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (25, 1414141414, 'The Very Blue Boy', 'Ben Jen', 2006, 'fiction');

INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 2);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 2);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 2);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 2);
INSERT INTO Member VALUES (006, 'Khang', 'Nkaujyi', '358 Spencer', 2145345625, 2);
INSERT INTO Member VALUES (007, 'Jones', 'David', '4th Street', 14812365789, 2);
INSERT INTO Member VALUES (008, 'Jones', 'David', '5th Street', 1234561256, 2);

INSERT INTO CurrentLoan VALUES (001, 10, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (003, 16, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (005, 15, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '13-APR-17', '12-MAY-17');
INSERT INTO CurrentLoan VALUES (007, 25, '02-OCT-17', '25-DEC-17');

INSERT INTO History VALUES (001, 10, '14-Jan-17', '04-OCT-17');
INSERT INTO History VALUES (003, 19, '12-Jan-17', '04-NOV-17');
INSERT INTO History VALUES (002, 13, '14-APR-17', '08-OCT-17');
INSERT INTO History VALUES (005, 20, '14-MAY-17', '04-DEC-17');
INSERT INTO History VALUES (007, 25, '02-OCT-16', '25-DEC-16');
COMMIT;

两条记录名为David Jones

select * from member where firstName = 'David' and lastName = 'Jones' ;

memberID    lastname    firstname   address phone_number    limit_
7   Jones   David   4th Street  2147483647  2
8   Jones   David   5th Street  1234561256  2

使用in子句删除成员记录和任何相关记录的SQL。

/* 
    delete records from the tables with relations first using the memberIDs
    related to any David Jones
    */
delete from currentloan where memberID in (
    select memberId from member where firstName = 'David' and lastName = 'Jones' 
)  ;
commit ;

delete from history where memberID in (
    select memberId from member where firstName = 'David' and lastName = 'Jones' 
)  ;
commit ;

/*
    delete any members called David Jones using the firstName and lastName
    */
delete from member where firstName = 'David' and lastName = 'Jones' ; 
commit ;

/* 
    no records for David Jones 
    */

select count(*) count from member where firstName = 'David' and lastName = 'Jones' ;

/*
    results
    */
count
0