--先试删除外键,避免后面如删除主表异常IF OBJECT_ID('FK_MainID') IS NOT NulLBEGIN alter table Sub_table drop constraint FK_MainIDEND--删除主表再建IF OBJECT_ID('Main_table') IS NOT NulLBEGIN DROP table Main_tableENDGOCREATE table Main_table( MainID INT,Mainname NVARCHAR(10),CONSTRAINT PK_Main_table PRIMARY KEY( MainID ASC ) )GO--删除子表再建IF OBJECT_ID('Sub_table') IS NOT NulLBEGIN DROP table Sub_tableENDGOCREATE table Sub_table( SubID INT,MainID INT,CONSTRAINT PK_Sub_table PRIMARY KEY( SubID ASC ),CONSTRAINT FK_MainID FOREIGN KEY (MainID) REFERENCES Main_table( MainID ))GO--插入主表数据INSERT INTO Main_table (MainID,Mainname) VALUES(1,'小明')INSERT INTO Main_table (MainID,Mainname) VALUES(2,'小华')--插入子表数据INSERT INTO Sub_table (SubID,MainID,Mainname) VALUES(10,1,'吃饭')INSERT INTO Sub_table (SubID,Mainname) VALUES(11,'睡觉')INSERT INTO Sub_table (SubID,Mainname) VALUES(12,'洗衣服')INSERT INTO Sub_table (SubID,Mainname) VALUES(20,2,'吃好饭')INSERT INTO Sub_table (SubID,Mainname) VALUES(21,'睡好觉')INSERT INTO Sub_table (SubID,Mainname) VALUES(22,'洗好衣服')--输入数据SELECT * FROM Main_table mtSELECT * FROM Sub_table st--修改主表主键索引EXEC sp_rename 'PK_Main_table','PK_Main_table2'--修改列名类型测试ALTER table Main_table ALTER ColUMN MainID BIGINT
以上是内存溢出为你收集整理的SQLServer 有物理主外键关系的表在主表改变主键索引时的测试全部内容,希望文章能够帮你解决SQLServer 有物理主外键关系的表在主表改变主键索引时的测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)