Hi to all readers,
In this article you will be able to apply the foreign key coding in MySQL with cascade update and Cascade delete option.
The sample code for implementing the foreign key is as follows:-
drop database foreigenkeydemo;
create database foreigenkeydemo;
use foreigenkeydemo;
create table teacher
(
tno int primary key,
tname varchar(20)
);
create table student
(
admno int primary key,
stname varchar(20),
tno int,
foreign key(tno)references teacher(tno)on update cascade on delete restrict
);
insert into teacher values(1,'Manish'),(2,'Mohit'),(3,'Rashmi'),(4,'Laxman');
insert into student values(101,'Ram',1),(102,'Rakhi',1),(103,'Raju',2),(104,'Anita',1);
In this article you will be able to apply the foreign key coding in MySQL with cascade update and Cascade delete option.
The sample code for implementing the foreign key is as follows:-
drop database foreigenkeydemo;
create database foreigenkeydemo;
use foreigenkeydemo;
create table teacher
(
tno int primary key,
tname varchar(20)
);
create table student
(
admno int primary key,
stname varchar(20),
tno int,
foreign key(tno)references teacher(tno)on update cascade on delete restrict
);
insert into teacher values(1,'Manish'),(2,'Mohit'),(3,'Rashmi'),(4,'Laxman');
insert into student values(101,'Ram',1),(102,'Rakhi',1),(103,'Raju',2),(104,'Anita',1);
No comments:
Post a Comment