I want to create a table, that stores available books based on a QUANTITY value. If quantity > 0 this book is available. But I have an error:
Msg 1776, Level 16, State 0, Line 48
There are no primary or candidate keys in the referenced table 'BOOK_INFO' that match the referencing column list in the foreign key 'FK_QUANTITY'.
Msg 1750, Level 16, State 1, Line 48
Could not create constraint or index. See previous errors.
Code (the problem is table 'AVAILABLE_BOOKS'):
CREATE TABLE BOOKS
(
ID INT IDENTITY PRIMARY KEY,
BOOK_NAME VARCHAR(30) NOT NULL,
BOOK_AUTHOR VARCHAR(30) NOT NULL
)
CREATE TABLE BOOK_INFO
(
BOOK_ID INT PRIMARY KEY FOREIGN KEY REFERENCES BOOKS(ID),
QUANTITY INT DEFAULT 1 NOT NULL,
PRICE DECIMAL(5,2) NOT NULL,
CONSTRAINT CHECK_PRICE CHECK (PRICE > 0),
CONSTRAINT CHECK_QUANTITY CHECK (QUANTITY > 0)
)
CREATE TABLE ABAILABLE_BOOKS
(
BOOK_ID INT PRIMARY KEY,
QUANTITY INT DEFAULT 1 NOT NULL,
CONSTRAINT FK_BOOK_ID FOREIGN KEY (BOOK_ID) REFERENCES BOOKS(ID),
CONSTRAINT FK_QUANTITY FOREIGN KEY (QUANTITY) REFERENCES BOOK_INFO(QUANTITY),
CONSTRAINT CHECK_QUANTITY CHECK (QUANTITY > 0)
)
I was trying to change field declaration (QUANTITY INT
-> QUANTITY INT DEFAULT 1 NOT NULL)
,
and many other things that didn't help.