Apologies in advance that this is definitely a beginner issue; but I'm genuinely having trouble finding the solution.
In the chart linked here, I am told which columns must be linked to other tables. chart
However, I'm stumped with the Books
table because it needs to have 2 keyed columns. One is the primary key (Book ID
), which I've already set up. But the Title
is also supposed to be keyed with the Book ID
in the Book Copies
table. I tried to specify Book ID
as a foreign key that references the Title
in Books
and got an error message saying
No primary or candidate key are referenced in 'Books'
This is because I didn't know how to set Title as a key, since one already exists in that table. My table creation code for Books
and Book Copies
is shown below. The foreign key link doesn't work right now because I'm unsure how to reference a column that is not a primary key. If someone could help me out with how to set "Title" in "Books" as a non-primary key that can be referenced by "Book ID" in "Book Copies", I'd truly appreciate it.
Thanks!!
CREATE TABLE Books
(
Book_ID INT PRIMARY KEY NOT NULL IDENTITY (1,1),
Title VARCHAR(50) NOT NULL
Publisher_NAME VARCHAR(50) NOT NULL
CONSTRAINT fk_Publisher_Name
FOREIGN KEY REFERENCES Publisher(Publisher_Name)
ON UPDATE CASCADE ON DELETE CASCADE,
);
CREATE TABLE Book_Copies
(
Book_ID INT NOT NULL
CONSTRAINT fk_Title
FOREIGN KEY REFERENCES Books(Title) ON UPDATE CASCADE ON DELETE CASCADE,
Branch_ID VARCHAR(50) NOT NULL,
Number_Of_Copies INT NOT NULL
);