我有一个关系,其中医生是一个人,医生有3个属性:- ID,paycheck和speciality,一个人有name,age,gender,address,既然医生是person,我想做它
因此,当创建一个具有指定ID的医疗器时,它将使用该info( name,age etc )从person中获取该id。
有没有办法在不使用update的情况下做到这一点,而是在创建表时实现这一点,就像说table Medic有Name where Medic.name = Person.Name属性if person.id = medic.id一样。
发布于 2019-11-12 19:59:30
您可以使用外键引用来定义medics:
create table medics (
medic_id int identity(1, 1) primary key,
payment ?, -- whatever the type is
specialty nvarchar(255),
person_id int,
foreign key (person_id) references persons(person_id)
);
create tabel persons (
person_id int identity(1, 1) primary key,
name nvarchar(255), -- or whatever
. . . -- and so on
);然后,对于insert
insert into medics (payment, specialty, person_id)
select @payment, @specialty, p.person_id
from persons p
where p.name = @name;https://stackoverflow.com/questions/58818207
复制相似问题