关于表具有来自2个不同表的2个外键时的删除操作

时间:2013-11-10 09:35:10

标签: sql database foreign-keys constraints

我有一个表,比方说table3,它包含两个外键,每个外键引用一个不同的表。我想知道,我可以为它们定义两个不同的ON DELETE动作。

让我通过一个例子解释一下。

create table table3 (
    ID varchar(255),
    Name varchar(255),
    primary key(ID,Name),
    foreign key(ID) References user(id),
    foreign key(Name) References shops(StoreName)
       on update cascade
       on delete cascade // I want to cascade table if id is deleted
       on delete no actions); // and do not allowed  if StoreName is deleted.

有人帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

不确定我是否完全理解您要做的事情 - 但是如果您希望{f}引用ON DELETE CASCADE表上的User和fk上的ON DELETE NO ACTIONSShops表的引用,您需要使用此T-SQL:

create table table3 (
    ID varchar(255),
    Name varchar(255),
    primary key(ID,Name),

    foreign key(ID) References user(id)
       on delete cascade,    // I want to cascade table if id is deleted        

    foreign key(Name) References shops(StoreName)
       on update cascade
       on delete no actions); // and do not allowed  if StoreName is deleted.

您需要使用ON DELETE ....定义

指定foreign key操作权限