create table A
(
Aid int not null primary key,
ABid int not null
)
create table B
(
Bid int not null primary key,
Bname varchar(8) not null
)
insert into A values(1,1)
insert into A values(2,2)
insert into B values(1,网求)
insert into B values(2,晚秋)
alter table A add Constraint A_B_FK
foreign key (ABid) references B(Bid)
insert into A values(3,(select Bid from B where Bname='晚秋'))

解决方案 »

  1.   

    create table A
    (
    Aid int not null primary key,
    ABid int not null
    )
    go
    create table B
    (
    Bid int not null primary key,
    Bname varchar(8) not null
    )
    go
    insert into A values(1,1)
    go
    insert into A values(2,2)
    go
    insert into B values(1,'网求')
    go
    insert into B values(2,'晚秋')
    go
    alter table A add Constraint A_B_FK
    foreign key (ABid) references B(Bid)
    go
    insert into A 
    select 3, Bid from B where Bname='晚秋'
    go
      

  2.   

    --A的結果select * from A
    --result
    Aid         ABid        
    ----------- ----------- 
    1           1
    2           2
    3           2(3 row(s) affected)