各位大侠,本人初学编成,遇到了一些比较弱智的问题,请各位高人指教。
这是关于数据库上的。现有两张表table 1和table 2。table 1表的主键为varchar类型,同时该主键也为外键,与table 2为外键关系。
现在我想执行删除 table 1 表某行数据时,同时要删除 table 2 表中相应的数据。
该怎样写Sql语句呢?
请各位高人指教。

解决方案 »

  1.   

    create table T1(ID int primary key)
    create table T2(ID int primary key)
    alter table T2 add constraint Del_T2 foreign key ID references t1(ID) on delete cascade
      

  2.   

    alter table T2 add constraint Del_T2 foreign key ID references t1(ID) on delete cascade
      

  3.   

    begin tran
      delete from table_2
      where id = @id
      delete from table_1
      where id = @id
    commit