Creating and Using FOREIGN KEY Constraint in SQL
📖 Scenario: You are building a simple database for a small bookstore. You want to keep track of books and their authors. Each book must be linked to an author to ensure data consistency.
🎯 Goal: Create two tables: Authors and Books. Use a FOREIGN KEY constraint in the Books table to link each book to an author in the Authors table.
📋 What You'll Learn
Create an
Authors table with columns AuthorID (primary key) and Name.Create a
Books table with columns BookID (primary key), Title, and AuthorID.Add a
FOREIGN KEY constraint on AuthorID in the Books table referencing AuthorID in the Authors table.💡 Why This Matters
🌍 Real World
Bookstores and many other businesses use foreign keys to keep related data connected and consistent.
💼 Career
Understanding foreign keys is essential for database design and ensuring data integrity in real-world applications.
Progress0 / 4 steps