简体   繁体   English

如何修改SQL Server 2008 R2 FK的父表

[英]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数据类型,则取决于更改:

  • If the old type can be implicitly casted to the new type without any loss: 如果可以将旧类型隐式转换为新类型而不会造成任何损失:
    1. Change the PK type first. 首先更改PK类型。
    2. Change the FK type to the same. 将FK类型更改为相同。
  • If the old type cannot be implicitly casted to the new type without any loss: 如果不能将旧类型隐式转换为新类型而不会造成任何损失:
    1. Break the relationship first (ie remove the FK restriction/index). 首先中断关系(即删除FK限制/索引)。
    2. Convert the PK. 转换PK。 If the data needs to be modified, save both the old values and the new ones in a temporary table. 如果需要修改数据,请将旧值和新值都保存在临时表中。
    3. Convert the FK. 转换FK。 If the PK data was changed in previous step, update the FK using the mapped values from the temporary table. 如果在上一步中更改了PK数据,请使用临时表中的映射值更新FK。
    4. Create the relationship again (ie create the FK restriction/index). 再次创建关系(即创建FK限制/索引)。

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上,您必须:

  1. Drop the existing foreign key constraint. 删除现有的外键约束。
  2. Update as necessary so that all values in the applicable column have a matching value in TableC. 根据需要进行更新,以使适用列中的所有值在TableC中都具有匹配的值。
  3. Add a new foreign key constraint. 添加一个新的外键约束。

in that order. 以该顺序。

Edit Starts Here 编辑从这里开始

Here is a link to the syntax, 是语法的链接,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM