alter table 单位表
add constraint pri_id primary key(id)
go
alter table 其它表
add constriant for_id foreign key (id) references 单位表(id) on delete cascade
on update cascade
go

解决方案 »

  1.   

    to chinaandys(努力混颗星,回家泡MM) 
       主,外键没用过,请问删除,修改主键单位表的一条记录时,要先判断它是否已经被使用.要怎样判断.
      

  2.   

    根本就不需要,当你用:on delete cascade on update cascade级联操作符后:如果你先删除主表(包含主键的列)就会出现错误信息,也就是必须先删除从表中的数据。
      

  3.   

    主、外键一例:
    create table t1(A int primary key, B int)
    insert into t1 select 1,100
    insert into t1 select 2,200
    go
    create table t2(A int not null,C int)
    go
    alter table t2 
    add constraint for_t foreign key(A) references t1(A)
    go
      

  4.   

    修改不是修改t1主键,A,而是修改内容B
      

  5.   

    alter table 单位表
    add constraint pri_id primary key(id)
    go
    alter table 其它表
    add constriant for_id foreign key (id) references 单位表(id) on delete cascade
    on update cascade
    go