各位大虾 if   exists(select   *   from   sysobjects   where   name='a1') 
  drop   table   a1 
create   table   a1 

学号   
姓名   
总分   
等级 

if   exists(select   *   from   sysobjects   where   name='a2') 
  drop   table   a2 
create   table   a2 

id   
学号   
总分 

建立上面两张TABLE后,要想a2中的总分与a1中的总分关联起来,请问如何设置主键和外键~~ 
还有就是在T-SQL中如何用SQL语句设置各表关系的“创建中检查现存数据”“级联更新相关的字段”“级联删除相关的记录”,小弟跪谢了~~

解决方案 »

  1.   

    alter table a2
     add constraint fk_count foreign key(总分) references a1(总分) on delete cascade
      

  2.   

    create       table       a1   
    (   
    学号   int identity primary key 
    姓名       
    总分       
    等级   
    )  create       table       a1   
    (   
    学号  references a1(学号)     
    姓名       
    总分       
    等级   
    )  
      

  3.   

    alter table 从表  add constraint 外键名 foreign key(从表字段) references 主表(主表字段) on delete cascade