How to use Foreign Key in SQL?

In this article, we will figure out how A FOREIGN KEY in one table focuses to a PRIMARY KEY in another table?

Fundamentally, Foreign Key speaks to connection between tables.

Syntax:

    column-name data_type (size) CONSTRAINT constraint-name  
    References  Table-name (Column-name)

Example:

Table 1:

How to use Foreign Key in SQL?

Table 2:

How to use Foreign Key in SQL?

First create a primary key in first table which is also called unique key.

Like this:

create table student 
(enroll number(6) primary key, s_name varchar(25), address varchar2(20), 
contact number(10) );

Second create foreign key by using second table.

Like this:

create table student_fees
(enroll number(6) constraint en_fk References Student (enroll) , course_fee number(8), 
status varchar2(6), amount number(10) );
How to use Foreign Key in SQL?

End:

In this article, we have figured out how to utilize outside key and how it functions and valuable for us to recover information from the social database? I trust you comprehend the idea; in the event that you have any question, don’t hesitate to ask in the remark area.

Leave a Comment