Chi*_*hin 2 sql sql-server sql-server-2005 visual-studio-2008
我正在尝试向表中添加外键,但数据库,Sql Server 2005,不喜欢它.
它表示列与现有主键或唯一约束不匹配.
如何找到有问题的列,以便删除/更改它们并添加外键?
左键连接到相关键上的父表,然后检查子表中左连接父表中的值为空的值.
例如,如果这是您的架构......
table1:
myKey int primary key,
...other columns...
table2:
otherKey int primary key,
myKeyFromTable1 int
...other columns...
Run Code Online (Sandbox Code Playgroud)
你这样做:
select distinct
t2.myKeyFromTable1
from table2 t2
left join table1 t1 on t1.myKey = t2.myKeyFromTable1
where t1.myKey is null
Run Code Online (Sandbox Code Playgroud)
这将为您提供table2不具有相应父级的不同值table1.
| 归档时间: |
|
| 查看次数: |
3665 次 |
| 最近记录: |