如何在子类型 table 中插入数据并从 T-SQL 中的超类型获取密钥?

how to insert data in subtype table and get key from super type in T-SQL?

例如在超级类型中得到一个用户:

create table Userr
(
    id int primary key identity,
    firstName nvarchar(20),
    lastName nvarchar(20),
    email nvarchar(15) unique not null,
    userName nvarchar(15) unique not null,
    passKey nvarchar(15) not null,
    sex nvarchar(1),
    userType nvarchar(1),
    depID nvarchar(5) ,
)

然后在子类型中我得到了学生和讲师:

create table Student
(
    stuID int primary key,
    gradYear date ,
    constraint c2 foreign key(stuID) references Userr(id)
)

create table Instructor
(
    insID int primary key,
    salary money ,
    constraint c3 foreign key(insID) references Userr(id)
)

我只想添加新学生和新讲师,我怎样才能有效地做到这一点?

all I want is to add new student and new instructor ,how can I make it in efficient way?

"subtype table"的意思不清楚,但是为了INSERT new student和new instructor你应该使用简单的INSERT查询。

INSERT Userr(firstName,lastName,email, userName, passKey)
    VALUES ('Ronen','Ariely', 'NotForYou','pituach','NoWay')
GO
INSERT Student(stuID, gradYear) VALUES (1, '2001-02-27')
GO

您必须首先在 userr table 中包含引用的用户。这意味着您必须先插入引用的用户(如果它不存在)。