Saturday, 19 November 2011


The FOREIGN KEY is a column (or a group of columns) whose values are derived from the primary key or unique key of some other table.
Foreign keys represent relationships between tables.
The FOREIGN KEY in one table points to a PRIMARY KEY in another table.

FOREIGN KEY Constraint Example

We have following "Student_Detail" name table:

S_NoS_NameTotalPercentage
1Jay50777.84
2Ajay48774.92
3Bhavesh46972.15
4Jaimin46371.23
5Dolar42565.38

We have following "Stu_Detail" name table:

Enrollment_NoS_NoS_Name
19210900941Jay
19210900642Ajay
19210900743Bhavesh
19210900924Jaimin
19210900875Dolar

The "S_No" column in the "Student_Detail" table is the FOREIGN KEY Reference point to the "Stu_Detail" table and "Enrollment_No" culumn.

FOREIGN KEY constraint defined at the Column level

Syntax:
Column_Name Datatype (size) REFERENCES Table_Name(Column_Name)
Example Statement:
CREATE TABLE Student_Detail
(
S_No number(2) Reference Stu_Detail(Enrollment_No),
Name varchar(25),
Total number(3),
);

FOREIGN KEY constraint defined at the Table level

Syntax:
FOREIGN KEY (Column_Name,Column_Name,..) 
REFERENCES Table_Name(Column_Name,Column_Name,..)
Example Statement:
CREATE TABLE Student_Detail
(
S_No number(2),
Name varchar(25),
Total number(3),
FOREIGN KEY (S_No) Reference Stu_Detail(Enrollment_No)
);

0 comments:

Post a Comment

TrainingHUB. Powered by Blogger.

Total Pageviews

THE BEST QTP TRAINING INSTITUTE IN HYDERABAD

QTP Training in hyderabad

Sql Tutorial

Popular Posts

Our Facebook Page

TrainingHUB

Followers