首先,我很抱歉,这些表的名称等都是用另一种语言写的。
问题是,出于某种原因,我似乎无法将kopia\u knihy表中名为id\u kopie的外键添加到sklad表中。当它将外键添加到表sklad时,抛出一个错误1215。代码如下:
CREATE TABLE BOOK (
BOOK_NAME VARCHAR(30) NOT NULL,
YEAR CHAR(4) NOT NULL,
NAME_OF_EDITOR VARCHAR(30) NOT NULL,
WRITER_ID INTEGER NOT NULL,
ISBN VARCHAR(17) NOT NULL,
BOOK_ID INTEGER NOT NULL,
PRIMARY KEY (BOOK_ID),
);
CREATE TABLE BOOK_COPY(
BOOK_ID INTEGER NOT NULL,
LANGUAGE_CODE CHAR(3) NOT NULL,
COPY_ID INTEGER NOT NULL,
BOOK_PICTURES CHAR(1) NOT NULL
CHECK (BOOK_PICTURES IN ("Y", "N")),
PRIMARY KEY (BOOK_ID, LANGUAGE_CODE, COPY_ID)
FOREIGN KEY(BOOK_ID)
REFERENCES BOOK(BOOK_ID),
);
CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
我在网上查了一下错误代码1215,没有发现我的数据库有什么问题。我检查了一下是否有错别字或者我是否忘了添加参考资料。
这是错误:
0 769 18:19:37 CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
Error Code: 1215. Cannot add foreign key constraint 0.016 sec
我的问题是,如何才能解决这个问题,它将工作。
我们将不胜感激。
1条答案
按热度按时间ql3eal8s1#
这样试试。请一定要把这张表改一下
ON UPDATE ... ON DELETE
本例中的语法与您需要的语法相同。在SQLFiddle上试一试
.