Skip to content

REFERENCES

CREATE TABLE Authors(
AuthorID INT,
AuthorName VARCHAR(100),
PRIMARY KEY (AuthorID)
);
CREATE TABLE Books(
BookID INT,
BookName VARCHAR(100),
AuthorID INT,
PRIMARY KEY (BookID),
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);
Query OK, 0 rows affected (0.20 sec)
Query OK, 0 rows affected (0.12 sec)

The SQL above creates two tables: Authors and Books. The Authors table has two columns (AuthorID and AuthorName) with AuthorID as the primary key. The Books table has three columns (BookID, BookName, and AuthorID) with BookID as the primary key. The AuthorID in the Books table is declared as a foreign key that references the AuthorID in the Authors table. This establishes a relationship between the two tables, ensuring that every book has a corresponding author in the Authors table.