如何在SQL Server脚本中改变具有pk约束和fk约束的表的列长度? [英] How to alter length of the column for a table with pk constraints and fk constraints in SQL server script wise?
问题描述
如何在Sql server脚本中改变具有pk约束和fk约束的表的列长度?
< pre lang =SQL> ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar ( 50 )
获取以下错误
< pre lang =C#> 对象 ' PK_Non- MOL_Corporate' 取决于列' CorporateId 跨度>。
消息 4922 ,等级 16 ,状态 9 ,行 1
ALTER TABLE ALTER COLUMN CorporateId失败,因为一个或多个对象访问 this column。
我的尝试:
我尝试如下:
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar ( 50 )
获取以下错误
object ' PK_Non-MOL_Corporate' 取决于列' CorporateId'。
消息 4922 ,等级 16 ,状态 9 ,行 1
ALTER TABLE ALTER COLUMN CorporateId失败,因为一个或多个对象访问 this column。您好,
如果存在某些外键约束,则会出现此错误。要避免此问题,首先删除外键约束然后更改列,然后再次创建外键约束。
试试这个
ALTER TABLE [dbo.childtable] DROP CONSTRAINT [FK_Non-MOL_Corporate];
---做其他事情
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)
ALTER TABLE [dbo.childtable] ADD FOREIGN KEY(FK_Non-MOL_Corporate)
参考NON_MOL_Corporate(CorporateId);
谢谢
How to alter length of the column for a table with pk constraints and fk constraints in Sql server script wise?
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)
Getting below error
The object 'PK_Non-MOL_Corporate' is dependent on column 'CorporateId'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN CorporateId failed because one or more objects access this column.
What I have tried:
I tried as below:
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)
Getting below error
The object 'PK_Non-MOL_Corporate' is dependent on column 'CorporateId'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN CorporateId failed because one or more objects access this column.
Hello ,
This error comes if there exists some foreign key constraints . To avoid this issue , you first drop the foreign key constraints then alter the column and then create the foreign key constraint again .
Try this
ALTER TABLE [dbo.childtable] DROP CONSTRAINT [FK_Non-MOL_Corporate]; ---do the other stuff ALTER TABLE NON_MOL_Corporate ALTER COLUMN [CorporateId] varchar(50) ALTER TABLE [dbo.childtable] ADD FOREIGN KEY (FK_Non-MOL_Corporate) REFERENCES NON_MOL_Corporate(CorporateId);
Thanks
这篇关于如何在SQL Server脚本中改变具有pk约束和fk约束的表的列长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!