我假设您在谈论家庭评估。
[appraisal] 表是父表,[owners]/[location] 是子表。
这是 tempdb 中的播放模式。
-- just playing
use tempdb;
go
-- drop table
if object_id('appraisal') > 0
drop table appraisal
go
-- create table - home appraisal
create table appraisal
(
app_id int identity (1,1),
app_request datetime,
app_employee_id int
);
-- drop table
if object_id('owners') > 0
drop table owners
go
-- create table - the owners
create table owners
(
own_id int identity (1,1) primary key,
own_first_nm varchar(64),
own_last_nm varchar(64),
app_id int
);
-- drop table
if object_id('location') > 0
drop table location
go
-- the location
create table location
(
loc_id int identity (1,1) primary key,
loc_street varchar(64),
loc_city varchar(64),
loc_state varchar(2),
loc_zip varchar(9),
app_id int
);
go
通过触发器创建空子记录时,您必须定义默认值或在触发器中提供它们。
另外,我把设置外键留给你处理。
触发器的代码如下所示。
-- The trigger
CREATE TRIGGER DBO.make_empty_children on dbo.appraisal
for insert, update, delete
as
BEGIN
-- nothing to do?
IF (@@rowcount = 0) RETURN;
-- do not count rows
SET NOCOUNT ON;
-- delete
IF NOT EXISTS (SELECT * FROM inserted)
BEGIN
RETURN;
END
-- insert
ELSE IF NOT EXISTS (SELECT * FROM deleted)
BEGIN
-- dummy record for owners
insert into owners
(
own_first_nm,
own_last_nm,
app_id
)
select
'enter first name',
'enter last name',
app_id
from
inserted;
-- dummy record for location
insert into location
(
loc_street,
loc_city,
loc_state,
loc_zip,
app_id
)
select
'enter street',
'enter city',
'ri',
'00000',
app_id
from
inserted;
RETURN;
END
-- update
ELSE
BEGIN
RETURN;
END
END
GO
我为 DELETE 和 UPDATE 操作留下了占位符。
1 - 您是否要拒绝对评估表中的 id(键)的更新。可能不是。这也可以通过外键 (FK) 来处理(防止)。
2 - 你想级联删除吗?这也可以由 FK 或在触发器中的代码中处理。
让我们看看表中没有记录。
select * from appraisal;
select * from owners;
select * from location;
就像 Marc / Aaron 所说,插入/删除的表是记录集。没有一行。
不保证订单。如果您希望按应用 ID 顺序插入记录,请使用 order by。
-- Insert 2 records
insert into appraisal
(
app_request,
app_employee_id
)
values
(getdate(), 7),
(dateadd(d, 1, getdate()), 7);
-- Lets see the data
select * from appraisal;
select * from owners;
select * from location;