创建一个包含包含来自另一个表的值的属性的表

时间:2019-11-12 11:56:20

标签: sql sql-server

我有一个关系,其中Medic是一个人,Medic具有三个属性:-IDpaycheckspeciality,而一个人拥有{{ 1}},nameagegender,由于医生是address,所以我想成为

因此,当使用指定的person创建医务人员时,它将从ID获得info( name,age etc )并带有person

有没有一种方法可以不使用id来实现,而是在表创建时实现,就像说update具有属性table Medic如果Name where Medic.name = Person.Name一样。

1 个答案:

答案 0 :(得分:0)

您将使用外键引用定义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;
相关问题