使用sql语句修改表的主键和外键

首先建立两个表,用来测试,代码如下:

View Code
 1 create table test(
 2 
 3 tt varchar(50) not null
 4 
 5 primary key (tt)
 6 
 7 )
 8 
 9 create table myTest(
10 
11 aa varchar(40) not null,
12 
13 bb varchar(50) not null,
14 
15 cc varchar(50) not null,
16 
17 constraint PK_myTest primary key (aa),
18 
19 constraint fk_myTest foreign key (bb) references test
20 
21 )

注意:要修改主键和外键的话需要先指定主键和外键的名称,以后修改时要用到。

这是的主键和外键为:

clip_image002

修改的sql语句:

--修改主键的名称PK_myTest为PK_myTest22

alter table myTest drop constraint PK_myTest

alter table myTest add constraint PK_myTest22 primary key(aa)

--修改外键的名称fk_myTest为fk_myTest22

alter table myTest drop constraint fk_myTest

alter table myTest add constraint fk_myTest22 foreign key(bb) references test

修改后的主键和外键:

 clip_image004

拷贝于http://blog.csdn.net/the_fire/article/details/5585816

原文地址:https://www.cnblogs.com/wsl2011/p/2584113.html