if object_id('tb')is not null drop table tb
go
if object_id('ta')is not null drop table ta
go
create table ta(Id int ,[Name] varchar(10) unique)
insert Ta select 1,'A'
insert Ta select 2,'B'
insert Ta select 3,null
create table tb(ID int ,[test] varchar(10) foreign key references ta([name]) on update  cascade)
insert tb select 1,null

解决方案 »

  1.   

    if object_id('tb')is not null drop table tb
    go
    if object_id('ta')is not null drop table ta
    go
    create table ta(Id int ,[Name] varchar(10) unique)
    insert Ta select 1,'A'
    insert Ta select 2,'B'
    insert Ta select 3,null
    insert ta  select 4,''
    create table tb(ID int ,[test] varchar(10) foreign key references ta([name]) on update  cascade)
    insert tb select 1,null
    insert tb select 2,''
    select * from tb
    /*
    ID          test       
    ----------- ---------- 
    1           NULL
    2           (影響 2 個資料列)*/
      

  2.   

    借6楼的数据 if object_id('tb')is not null drop table tb
    go
    if object_id('ta')is not null drop table ta
    go
    create table ta(Id int ,[Name] varchar(10) unique)
    insert Ta select 1,'A'
    insert Ta select 2,'B'create table tb(ID int ,[test] varchar(10) foreign key references ta([name]) )
    insert tb select 1,null
    select * from tb
    /*
    ID          test
    ----------- ----------
    1           NULL(1 行受影响)
    */
      

  3.   

    外键的值要么为空(NULL),要么是引用表的某个主键或唯一性键值.