TechnoLearnAcademy

TECHNOLEARN

FOREIGN KEY CONSTRAINT

A FOREIGN KEY constraint in SQL is a rule that establishes a relationship between two tables by ensuring the referential integrity of the data. It defines a column in one table as a foreign key that references the primary key in another table. This relationship allows you to maintain consistency and integrity when working with data across multiple tables in a relational database.
    • The table with the foreign key is called the child table.
    • The table with the primary key is called the referenced or parent table.

Example:

   Objective: We will create a foreign key constraint on the Ecode column of the Project table, referencing the Ecode                                  column of the Emp table.
                      CREATE TABLE TBL_Project(
                                                                   Ecode int Foreign Key references TBL_EMP(Ecode),
                                                                   ProjectName varchar(20),
                                                                   SiteCode tinyint
                                                                                             );
Scroll to Top