[英]How i can modify the Parent table for my Sql server 2008 r2 FK
I have a FK inside my table but i want to modify the parent table for the FK . 我的表内有FK,但我想修改FK的父表。 so is there an alter command that can achieve this ?
那么是否有可以实现此目的的alter命令? or i need to remove the FK and create a new one ?
或者我需要删除FK并创建一个新的? Thanks
谢谢
Add this to your PK and it will automatically update all FKs for you: 将此添加到您的PK中,它将自动为您更新所有FK:
ON UPDATE CASCADE
For full details, you can read this article . 有关完整的详细信息,您可以阅读本文 。
EDIT Based on your comment, if you want to change the PK data type, it depends on the change: 编辑根据您的评论,如果要更改PK数据类型,则取决于更改:
To modify the data type, use the ALTER
command, the syntax is: 要修改数据类型,请使用
ALTER
命令,语法为:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Examples: 例子:
ALTER TABLE table_name
ALTER COLUMN id NUMBER(10,2);
ALTER TABLE table_name
ALTER COLUMN id VARCHAR(20);
For full details, you can read this article . 有关完整的详细信息,您可以阅读本文 。
Looks like you are looking for alter
statement but since you didn't mention exactly what you are looking to modify; 看起来您在寻找
alter
语句,但是由于您没有确切提及要修改的内容; I assume that you want to change column data type size. 我假设您要更改列数据类型的大小。 You can do something like this (an example; say you want to change size from 10 to 15)
您可以执行以下操作(示例;例如,您要将大小从10更改为15)
alter table sample3
alter column name varchar(15)
EDIT: 编辑:
In that case this is what you should be doing. 在这种情况下,这就是您应该做的。 You need to drop the existing constraint and recreate the constraint to point to
TableC
您需要删除现有约束并重新创建约束以指向
TableC
alter table TableA
drop constraint your_FK_constraint_name
alter table TableA
add constraint constraint_name
FOREIGN KEY (column_name) references TableC(some other column name)
An Example: 一个例子:
alter table sample2
drop constraint FK__sample2__realnam__09DE7BCC
alter table sample2
add constraint FK__sample2__realnam
FOREIGN KEY (realname) references sample1(name)
Based on this comment, "now my current FK inside TableA is referring to another table primary key TableB. but i need my modify my current FK to refer to tableC instead of tableB ... this what i need (to modify the parent table for my FK)– " 基于此评论,“现在我在TableA中的当前FK引用了另一个表主键TableB。但是我需要修改当前FK以引用tableC而不是tableB ...这是我需要的(修改父表我的FK)–“
The parent table is TableB. 父表是TableB。 No action is required on that table.
对该表无需执行任何操作。
On TableA, you have to: 在TableA上,您必须:
in that order. 以该顺序。
Edit Starts Here 编辑从这里开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.