首先建立两个表,用来测试,代码如下:
create table test(
tt varchar(50) not null
primary key (tt)
)
create table myTest(
aa varchar(40) not null,
bb varchar(50) not null,
cc varchar(50) not null,
constraint PK_myTest primary key (aa),
constraint fk_myTest foreign key (bb) references test
)
注意:要修改主键和外键的话需要先指定主键和外键的名称,以后修改时要用到。
这是的主键和外键为:
修改的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
修改后的主键和外键: