SQL Foreign Key
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.
Look at the following two tables:
Notice that the “PersonID” column in the “Orders” table points to the “PersonID” column in the “Persons” table.
– The “PersonID” column in the “Persons” table is the PRIMARY KEY in the “Persons” table.
– The “PersonID” column in the “Orders” table is a FOREIGN KEY in the “Orders” table.
– The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table
Example:
CREATE TABLE tablename(ColumnName1 Datatype(SIZE) PRIMARY KEY, ColumnNameN Datatype(SIZE) FOREIGN KEY REFERENCES PARENT_TABLE_NAME(Primary_Key_Of_Parent_tbl));
The following SQL creates a FOREIGN KEY on the “PersonID” column when the “Orders” table is created:
CREATE TABLE Orders (
OrderID int PRIMARY KEY,
OrderNumber int ,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
CREATE TABLE Orders (
OrderID int ,
OrderNumber int ,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
To create a FOREIGN KEY constraint on the “PersonID” column when the “Orders” table is already created, use the following SQL:
ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
To allow the naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
To drop a FOREIGN KEY constraint, use the following SQL:
ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
Course Video
1.Create two table in 1st table set primary key and insert 10 records and in 2nd table set FOREIGN key and insert 5 records in it.
2.Create two tables the second table should have a foreign key referencing the first table. Also, insert sample data into both tables
3.Create table without foreign key and add foreign key using alter table
Hint: A foreign key constraint is added to the Budget table referencing the customer id column in the Customer table.
4.Create table with constrain primary key foreign key and unique key
Hint: A unique constraint is placed on the order id. A foreign key constraint is added to ensure referential integrity between the Orders table and the Customers table.1
5.write sql query to drop foreign key constrain
6. Design and implement a SQL script to establish two relational tables named “Courses” and “Students.” The “Courses” table should include columns for CourseID (integer, primary key), CourseName (string) and credits(int). The “Students” table should comprise columns for StudentID (integer, primary key), StudentName (string), and CourseID (integer). Furthermore, implement a foreign key constraint in the “Students” table to ensure referential integrity by linking the CourseID column to the corresponding column in the “Courses” table.
7. Design and execute a SQL script to establish two relational tables named “Customers” and “Orders.” The “Customers” table should include columns for CustomerID (integer, primary key) and CustomerName (string). The “Orders” table should comprise columns for OrderID (integer, primary key), OrderDate (date), and CustomerID (integer). Furthermore, implement a foreign key constraint in the “Orders” table to ensure referential integrity by linking the CustomerID column to the corresponding column in the “Customers” table.
8. Write an SQL query to retrieve all CountryRegionCode entries where the Name starts with the letter ‘A’ or ‘G’ and the ModifiedDate is earlier than January 1, 2009. From Person_CountryRegion. [OUTPUT: 30 ROWS]
[HINT: use ADVENTURE2019 Database]
9. Craft a comprehensive SQL script to initialize two distinct tables, namely “Library” and “Books,” devoid of any foreign key constraints. The “Library” table should include columns for LibraryID (integer, primary key) and LibraryName (string). Correspondingly, the “Books” table should include columns for BookID (integer, primary key), Title (string), and LibraryID (integer). Subsequently, integrate a foreign key constraint into the “Books” table to establish a reference to the LibraryID column within the Library table. Finally, execute insertion operations to populate both tables with relevant data. And try to insert the record that violet the foreign key constraint.
10. write a SQL query to drop the foreign key constraint from the Books table.
YouTube Reference :
1) SQL Foreign Key in Hindi/Urdu
2) SQL Foreign Key in English