Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
188 views5 pages

Database Foreign Key Basics

Foreign key is a constraint that links data between tables based on relationships. It represents a link between columns of tables and refers to the primary key column in another, parent table. The table with the foreign key is called the child table while the table with the primary key is the parent table. The foreign key constraint is created using the REFERENCES keyword and can have rules like ON DELETE SET NULL or ON DELETE CASCADE that specify what happens to child records when the parent record is deleted.

Uploaded by

Aparsha Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views5 pages

Database Foreign Key Basics

Foreign key is a constraint that links data between tables based on relationships. It represents a link between columns of tables and refers to the primary key column in another, parent table. The table with the foreign key is called the child table while the table with the primary key is the parent table. The foreign key constraint is created using the REFERENCES keyword and can have rules like ON DELETE SET NULL or ON DELETE CASCADE that specify what happens to child records when the parent record is deleted.

Uploaded by

Aparsha Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FOREIGN KEY Constraint/ referential integrity constraint

Definition

Foreign key is an Input/output data constraint which is also known as referential integrity
constraint. Foreign key represents a link or say a relationship between columns of 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.

Reference keyword is used to create foreign key constraint

Seema Kumari ,SCA , Asst. Prof


---------create author table----------------------------------------------------------------

create table author


(
author_id int primary key,
author_name varchar(23)

Seema Kumari ,SCA , Asst. Prof


);

---------------------create books table---------------------

create table books


(
book_id int Primary key,
author_id int ,
book_name varchar(21)
constraint fkk foreign key(author_id) references
author(author_id)
);

-----------drop constaint--------------------
alter table books drop constraint fkk;

------------------- 2ND WAY TO CREATE OR add foreign key----------------------------------------

alter table books add constraint fkk foreign


key(author_id) references author(author_id);

-------------------------insertion-------------------------------

----------------------deletion---------------------------------------

--------------------------------updation--------------------------

ON DELETE SET NULL


create table author
(

Seema Kumari ,SCA , Asst. Prof


author_id int primary key,
author_name varchar(23)
);

create table books


(
book_id int Primary key,
author_id int ,
book_name varchar(21)
constraint fkk foreign key(author_id) references
author(author_id)ON
DELETE SET NULL
);

ON DELETE CASCADE

create table author


(
author_id int primary key,
author_name varchar(23)
);

create table books


(
book_id int Primary key,
author_id int ,
book_name varchar(21)
constraint fkk foreign key(author_id) references
author(author_id)ON
DELETE CASCADE
);

Seema Kumari ,SCA , Asst. Prof


Seema Kumari ,SCA , Asst. Prof

You might also like