Click here to Skip to main content
16,010,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

Would anyone please tell me how to drop a foreign key constraint defined on column level in sql(or we can say having no name).

Code Example:
SQL
Create Table Employee
(
Employee_ID int Primary Key,
Job_ID int Foreign Key references JOB(Job_ID)  
)

/*consider there is another table Named "JOB" with column Job-ID*/
Posted
Updated 6-Jun-12 0:23am
v2

You can't drop the column because it depends on another column, but you can remove the key from the column. object explorer->expand your table->keys->right click->delete
 
Share this answer
 
hi,

CREATE TABLE [dbo].[t3](
[id] [int] NOT NULL,
[n_id] [int] NULL,
CONSTRAINT [PK_t3] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[t3](
[id] [int] NOT NULL,
[n_id] [int] NULL,
CONSTRAINT [PK_t3] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[t3] WITH CHECK ADD CONSTRAINT [FK_t3_t2] FOREIGN KEY([id])
REFERENCES [dbo].[t2] ([n_id])
GO

alter table drop constraint FK_t3_t2
 
Share this answer
 
alter table Employee drop Employee_Job_ID
 
Share this answer
 
Comments
Sandeep Mewara 6-Jun-12 6:36am    
OP wanted to remove foreign key not delete the entire column and loose data!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900