Тёмный

CASCADE IN SQL SERVER 

Syed Ali
Подписаться 800
Просмотров 1,4 тыс.
50% 1

CASCADE in SQL Server
ON DELETE CASCADE A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
ON UPDATE CASCADE A foreign key with cascade Update means that if a record in the parent table is updated, then the corresponding records in the child table will automatically be updated. This is called a cascade update in SQL Server.
ON UPDATE CASCADE ON DELETE CASCADE means that if you UPDATE OR DELETE the parent, the change is cascaded to the child automatically.
Example:
Parent table:
CREATE TABLE [Products](
[ProductID] [int] NOT NULL,
[ProductDesc] [varchar](50) NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
)) ON [PRIMARY]
Child Table:
CREATE TABLE [dbo].[ProductDetails](
[ProductDetailID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[Total] [int] NOT NULL,
CONSTRAINT [PK_ProductDetails] PRIMARY KEY CLUSTERED
(
[ProductDetailID] ASC
),
CONSTRAINT FK_ProductDetails_Products FOREIGN KEY([ProductID])
REFERENCES [Products] ([ProductID])
ON UPDATE CASCADE
ON DELETE CASCADE
)
 Insert data into parent table
INSERT INTO Products (ProductID, ProductDesc)
SELECT 1, 'VMW' UNION ALL SELECT 2, 'Maruti' UNION ALL SELECT 3, 'Jungle Book'
Insert data into child table
INSERT INTO ProductDetails
([ProductDetailID],[ProductID],[Total])
SELECT 1, 1, 200 UNION ALL SELECT 2, 1, 100 UNION ALL SELECT 3, 1, 111 UNION ALL SELECT 4, 2, 200 UNION ALL
SELECT 5, 3, 100 UNION ALL SELECT 6, 3, 100 UNION ALL SELECT 7, 3, 200
select * from Products
select * from ProductDetails
select * from Products
select * from ProductDetails
DELETE FROM Products WHERE ProductID = 1
update ProductDetails set productid=200 WHERE ProductID = 2

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 1   
@akshatjain3800
@akshatjain3800 2 года назад
👍
Далее
НИКИТА ПОДСТАВИЛ ДЖОНИ 😡
01:00
Просмотров 130 тыс.
7 Database Design Mistakes to Avoid (With Solutions)
11:29
If Your Tech Job is Comfortable, You're in Danger
20:57