alonli 发表于 2016-10-31 04:05:23

sql server 定义主键

  drop table father;
create table father(
id int identity(1,1) primary key,
name varchar(20) not null,
age int not null

)
  drop table mother;
  create table mother(
id int identity(1,1),
name varchar(20) not null,
age int not null,
husband int not null
)
  alter table mother
add constraint FK_ID foreign key(husband) references father(id)
  
  
  
  如果 在father中没能定义主键,
  drop table father;
create table father(
id int identity(1,1), (未定义 primary key)
name varchar(20) not null,
age int not null
)
  则会报如下异常:
  
  在被引用表 'father' 中没有与外键 'FK_ID' 的引用列的列表匹配的主键或候选键
  
页: [1]
查看完整版本: sql server 定义主键