将外键添加到多个现有表

时间:2013-04-21 13:32:11

标签: sql sql-server-2008-r2 foreign-keys

我有一个表'user',用户名为PK.I需要将此作为FK添加到多个现有表的2列。现在,我正在更新每个表格。方式。

alter table table1
add foreign key(createdby) references users(id)
go
alter table table1
add foreign key(createdby) references users(id)
go

是否有更简单的方法可以一次更新所有表格。谢谢。

3 个答案:

答案 0 :(得分:1)

这适用于具有不同语法和不同DDL更改的另一个数据库,但结构相同。声明一个查询数据字典表并返回要运行的SQL语句的游标,然后在执行每个语句时打开并循环游标。

DECLARE SQLStr SHORTSTRING(1000);
DECLARE NoDefaultsCursor CURSOR FOR
   SELECT 'ALTER TABLE "' || TABLE_NAME ||
          '" ALTER COLUMN "' || FIELD_NAME || '" SET DEFAULT ' ||
     CASE FIELD_TYPE_SQL
       WHEN 'INTEGER' THEN '0'
       WHEN 'BOOLEAN' THEN 'FALSE'
       WHEN 'SHORTSTRING' THEN ''''''
       WHEN 'CHARACTER VARYING' THEN ''''''
     END || ';' AS AddDefaultSQL
   FROM #FIELDS WHERE NOT FIELD_HASDEFAULT
     AND FIELD_TYPE_SQL IN
       ('INTEGER','BOOLEAN','SHORTSTRING','CHARACTER VARYING');
OPEN NoDefaultsCursor;
FETCH NEXT FROM NoDefaultsCursor INTO SQLStr;
WHILE @@FETCH_STATUS = 0 DO
    Execute Immediate SQLStr;
    FETCH NEXT FROM NoDefaultsCursor INTO SQLStr;
END WHILE;
CLOSE NoDefaultsCursor; 

答案 1 :(得分:1)

像这样的maby可以做到这一点!

DECLARE @TableName AS NVARCHAR(128)
DECLARE @Schema AS NVARCHAR(128)
DECLARE @Modify AS INTEGER
DECLARE @Created AS INTEGER
DECLARE @sql1 as nvarchar(max)
DECLARE @sql2 as nvarchar(max)
DECLARE curs CURSOR FOR
SELECT
A.[name],
S.[name] as schemaname,
(SELECT 1 FROM sys.all_objects innero 
where innero.[name] = 'FK_ModifiedBy' 
AND innero.parent_object_id = a.[object_id]) as ModifiedBy,
(SELECT 1 FROM sys.all_objects innero 
where innero.[name] = 'FK_CreatedBy' 
AND innero.parent_object_id = a.[object_id]) as CreatedBy
FROM
    sys.all_objects A
INNER JOIN
sys.all_columns O ON
A.[object_id] = O.[object_id]
INNER JOIN
SYS.schemas S ON
A.[schema_id] = S.[schema_id]
WHERE
O.name = 'ModifiedBy'
OPEN curs
FETCH NEXT FROM curs
INTO @TableName, @Schema, @Modify, @Created
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Modify IS NULL
BEGIN
set @sql1 = N'ALTER TABLE ' + @Schema + '.' + @TableName 
 + ' ADD CONSTRAINT FK_ModifiedBy FOREIGN KEY (ModifiedBy) 
REFERENCES ' + @Schema + '.Users(userid)'
exec SP_EXECUTESQL @sql1
END
IF @Created IS NULL
BEGIN
set @sql2 = N'ALTER TABLE ' + @Schema + '.' + @TableName 
+ ' ADD CONSTRAINT FK_CreatedBy FOREIGN KEY (createdby) 
REFERENCES ' + @Schema + '.Users(userid)'
exec SP_EXECUTESQL @sql2
END
FETCH NEXT FROM curs
INTO @TableName, @Schema, @Modify, @Created
END
CLOSE curs
DEALLOCATE curs


很抱歉在sql构建中没有很好的缩进,在这里做起来似乎有点麻烦。如果某些表只有另外两列中的一列,则可能需要稍微调整一下。

答案 2 :(得分:0)

我也尝试了一个简洁版的christer。这也很有用。思想可能对其他人有帮助

declare @tblname nvarchar(50),@sql1 nvarchar(max),@sql2 nvarchar(max)
declare curs cursor for
select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME ='createdby'
open curs
 fetch next from curs into @tblname
 while @@FETCH_STATUS=0
  begin
   set @sql1='alter table dbo.'+ @tblname+
   ' add foreign key(createdby) references users(id)'
   exec sp_executesql @sql1
   set @sql2='alter table dbo.'+@tblname+
   ' add foreign key(modifiedby) references users(id)'
   exec sp_executesql @sql2
   fetch next from curs into @tblname
  end
close curs
deallocate curs