Cascading Referential Integrity Constraints in SQL Server through Query.
|No Action,Set Null,Set default,Cascade|.
--Cascading Referential Integrity Constraints in SQL Server through Query
--The Cascading Referential Integrity Constraints in SQL Server that tell
--SQL Server to perform certain actions whenever a user attempts to delete or update a primary key
--NO ACTION
--This is the default action that SQL Server performs. This specifies that if an update or deletes statement affects rows in foreign key tables,
--then the action will be denied and rolled back. An error message will be raised
create Table Country
(
CountryId int primary key,
Name varchar(100)
)
Insert into Country values(1,'India')
Insert into Country values(2,'UK')
create Table Customer
(
CustomerId int primary key,
Name varchar(100),
CountryId int foreign key references Country(CountryId)
)
insert into Customer values(1,'Arun',1)
insert into Customer values(2,'Biju',null)
insert into Customer values(3,'Peter',2)
insert into Customer values(4,'Meera',1)
select * from Country
select * from Customer
--delete from Country where CountryId =1
--SET NULL
--when the primary key record is deleted or updated in the Primary key table that time foreign key column will be set to null
--we marked that column as allow null
--drop table Country
--drop table Customer
create Table Country
(
CountryId int primary key,
Name varchar(100)
)
Insert into Country values(1,'India')
Insert into Country values(2,'UK')
create Table Customer
(
CustomerId int primary key,
Name varchar(100),
CountryId int foreign key references Country(CountryId)
on delete set null
)
insert into Customer values(1,'Arun',1)
insert into Customer values(2,'Biju',null)
insert into Customer values(3,'Peter',2)
insert into Customer values(4,'Meera',1)
select * from Country
select * from Customer
--delete from Country where CountryId =1
--CASCADE
--when the primary key record is deleted or updated in the Primary key table that time foreign key column will also be deleted or updated
--drop table Country
--drop table Customer
create Table Country
(
CountryId int primary key,
Name varchar(100)
)
Insert into Country values(1,'India')
Insert into Country values(2,'UK')
create Table Customer
(
CustomerId int primary key,
Name varchar(100),
CountryId int foreign key references Country(CountryId)
on delete cascade
)
insert into Customer values(1,'Arun',1)
insert into Customer values(2,'Biju',null)
insert into Customer values(3,'Peter',2)
insert into Customer values(4,'Meera',1)
select * from Country
select * from Customer
--delete from Country where CountryId =1
--SET DEFAULT
--If a delete or update statement affects rows in a foreign key table, then -all rows containing those foreign keys are set to the default value.
create Table Customer
(
CustomerId int primary key,
Name varchar(100),
CountryId int default 2 foreign key references Country(CountryId)
on delete set default
)
Sql Query for jobs
# / @sqlqueryforjobs
Playlists:
• SQL Query Interview Questions
• Stored Procedure in sql
• Sql Basics Through Query
• User Defined Functions in sql
• Build in functions in sql
• Views in sql
Cte Links:
• CTE in sql.| Common Table Expression|
• CTE using multiple tables/multiple CT...
• How to insert,update,delete data usin...
• Recursive CTE in sql.| Recursive Comm...
Stored procedure Links:
• How to create,alter,drop a stored pro...
• How to create a stored procedure with...
• Optional Parameter Stored Procedure i...
• How to Encrypt stored procedure in S...
• Return value of stored procedure in S...
• stored procedure summary in sql.
• How to use User defined Table type in...
• How to use Temp Tables in Stored Proc...
User Defined Functions Link:
• User Defined Scalar Functions in sq...
• Inline table valued function in sql |...
• Multi-Statement Table Valued Function...
• How to insert,update,delete datas to ...
• How to use User defined Table type in...
• How to set SchemaBinding in User defi...
Sql Query Interview Questions Link:
• Write a query to find the stock of ea...
• Write a query to fetch the employees ...
• Write a Query to Get Largest Sold Pr...
• Write a Query to find Diffrence of ea...
• How to delete duplicate records in sq...
• Write a query to merge source table d...
• Write a query to display two column v...
• Write a query to display converts the...
• Write a query to generate cricket tou...
• Write a Procedure to Get display Tabl...
#sql #sqlserver #sqlab #sqlinjection #mssql #sqldeveloper #sqlsaturday
#sql #programming #database #sqlserver #coding #developer #programmer #software #datascience #code #computerscience #data #dataanalytics
#technology #computer #development #bigdata #backend #coder #codinglife #microsoft #sqldatabase #softwaredeveloper #informationtechnology #programminglife #softwaredevelopment #programmerslife #sqlforbeginners #sqltraining #sqlinterviewquestions #mssqlserver
Смотрите видео Cascading Referential Integrity Constraints in SQL Server through Query онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Sql Query for jobs 26 Октябрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 38 раз и оно понравилось 1 людям.