The SQL statement removes and adds methods for foreign keys and primary keys

  • 2020-05-07 20:21:12
  • OfStack

-- delete foreign keys
Syntax: alter table table name drop constraint foreign key constraint name
Such as:
alter table Stu_PkFk_Sc drop constraint FK_s
alter table Stu_PkFk_SC drop constraint FK_c
-- add a foreign key
Syntax: alter table table name add constraint foreign key constraint name foreign key(column name) references reference foreign key table (column name)
Such as:
alter table Stu_PkFk_Sc
add constraint Fk_s
foreign key (sno)
references Stu_PkFk_S(sno)
go
-- delete the primary key
Syntax: alter table table name drop constraint primary key constraint name
Such as:
alter table Stu_PkFk_S drop constraint PK_S
go
-- add the primary key
Syntax: alter table name add constraint primary key constraint name primary key
alter table Stu_PkFk_S add constraint PK_S primary key (sno)
go

Related articles: