(1)在两个表间建立为维护参照完整性使用的级联删除触发器、级联修改触发器和受限插入触发器。 使用sql server的语法
时间: 2023-12-04 22:07:26 浏览: 78
建立级联删除触发器:
```
CREATE TRIGGER trigger_name
ON table1
AFTER DELETE
AS
BEGIN
DELETE table2
FROM table2
INNER JOIN deleted ON table2.foreign_key = deleted.primary_key
END;
```
建立级联修改触发器:
```
CREATE TRIGGER trigger_name
ON table1
AFTER UPDATE
AS
BEGIN
UPDATE table2
SET column_name = inserted.column_name
FROM table2
INNER JOIN inserted ON table2.foreign_key = inserted.primary_key
END;
```
建立受限插入触发器:
```
CREATE TRIGGER trigger_name
ON table1
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS (SELECT * FROM table2 INNER JOIN inserted ON table2.foreign_key = inserted.primary_key)
BEGIN
RAISERROR ('Cannot insert record because of foreign key constraint violation', 16, 1);
ROLLBACK TRANSACTION;
END
ELSE
BEGIN
INSERT INTO table1 (column1, column2)
SELECT column1, column2 FROM inserted;
END
END;
```
阅读全文