A foreign key is a field in one table that is also the primary key of another table

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Primary Key: A primary key is used to ensure that data in the specific column is unique. A column cannot have NULL values. It is either an existing table column or a column that is specifically generated by the database according to a defined sequence. 

    Example: Refer the figure – 
    STUD_NO, as well as STUD_PHONE both, are candidate keys for relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many candidate keys). 

    Foreign Key: 
    A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It is a column (or columns) that references a column (most often the primary key) of another table. 

    Example: Refer the figure – 
    STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation. 

    Figure:

    A foreign key is a field in one table that is also the primary key of another table

    Let’s see the difference between Primary Key and Foreign Key:

    S.NO.PRIMARY KEYFOREIGN KEY
    1 A primary key is used to ensure data in the specific column is unique. A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables.
    2 It uniquely identifies a record in the relational database table. It refers to the field in a table which is the primary key of another table.
    3 Only one primary key is allowed in a table. Whereas more than one foreign key are allowed in a table.
    4 It is a combination of UNIQUE and Not Null constraints. It can contain duplicate values and a table in a relational database.
    5 It does not allow NULL values. It can also contain NULL values.
    6 Its value cannot be deleted from the parent table. Its value can be deleted from the child table.
    7 It constraint can be implicitly defined on the temporary tables. It constraint cannot be defined on the local or global temporary tables.

    SQL FOREIGN KEY Keyword


    The FOREIGN KEY constraint is a key used to link two tables together.

    A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.


    SQL FOREIGN KEY on CREATE TABLE

    The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:

    MySQL:

    CREATE TABLE Orders (
        OrderID int NOT NULL,
        OrderNumber int NOT NULL,
        PersonID int,
        PRIMARY KEY (OrderID),
        FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
    );

    SQL Server / Oracle / MS Access:

    CREATE TABLE Orders (
        OrderID int NOT NULL PRIMARY KEY,
        OrderNumber int NOT NULL,
        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:

    MySQL / SQL Server / Oracle / MS Access:

    CREATE TABLE Orders (
        OrderID int NOT NULL,
        OrderNumber int NOT NULL,
        PersonID int,
        PRIMARY KEY (OrderID),
        CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
        REFERENCES Persons(PersonID)
    );


    SQL FOREIGN KEY on ALTER TABLE

    To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL:

    MySQL / SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    ADD FOREIGN KEY (PersonID) 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:

    MySQL / SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    ADD CONSTRAINT FK_PersonOrder
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);


    DROP a FOREIGN KEY Constraint

    To drop a FOREIGN KEY constraint, use the following SQL:

    MySQL:

    ALTER TABLE Orders
    DROP FOREIGN KEY FK_PersonOrder;

    SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    DROP CONSTRAINT FK_PersonOrder;



    Can a foreign key be a primary key in another table?

    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.

    What is foreign key How do you define a foreign key in your table?

    A foreign key is a column (or combination of columns) in a table whose values must match values of a column in some other table. FOREIGN KEY constraints enforce referential integrity, which essentially says that if column value A refers to column value B, then column value B must exist.

    What is the difference between a primary key and a foreign key quizlet?

    Question: What is the difference between a primary key and a foreign key? Answer: The primary key is the field or combination of fields that uniquely identifies each record in a table. A foreign key is a field in a related table that is also a primary key in a primary table. You just studied 9 terms!

    What is the use of primary key and foreign key in SQL with example?

    The primary key is limited to a single table and is put to uniquely identify the corresponding rows of a table. When we talk about Foreign key, we can have as many Foreign keys as we want. A foreign key comes to use when we need to link tables to one another and have data spread over multiple tables.