40

I have created one table using the below command:

create table Table1(
    Id int Not Null 
        Foreign key 
        references Table2(Id)  
        on delete cascade 
        on update cascade,
    UserName nvarchar(150),
    TimeInSeconds int Not Null 

    primary key(Id,TimeInSeconds)
);

But now I want to drop the foreign key. As I haven't given a constraint name, I can't use:

Alter table <tablename>
drop foreign key <foreign key name>

Is there any way?

Patrick Yoder
  • 1,065
  • 4
  • 14
  • 19
Wondering
  • 4,950
  • 22
  • 71
  • 90

8 Answers8

37

You can find the name of the constraint in INFORMATION_SCHEMA.TABLE_CONSTRAINTS

select CONSTRAINT_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where TABLE_NAME = 'Table1'
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
  • +1. Quick draw. I was going for: select * from syscontraints where xtype = 'F' and name like '%table1%' – Dead account May 05 '09 at 12:22
  • 1
    Except what happens if you are doing this in a script to release to multiple databases that should have the same schema hence the name of the constraint will be potentially different on each instance of the database? In that case this will not work. – Peter Sep 28 '11 at 09:31
  • @Peter, ideally you would have named constraints, but if you don't you can still get the names at script runtime. Don't see a problem. – Ed Guiness Feb 14 '12 at 15:49
  • I guess I'm thinking of more reusable script. If you haven't been using names for your constraints then a table could have multple constraints on and that information_schema.table_constraints doesn't tell you which fields are involved. – Peter Mar 20 '12 at 13:04
  • 3
    Do we have an answer to Peter's question? I am trying to do something like this: ALTER TABLE ef_jobpremium DROP CONSTRAINT . Hope I am clear, I am trying to do this in one execution. – rajugaadu Mar 04 '14 at 04:58
  • 4
    This gives all the constraint name. How to get constraint name for specific field ? – Jeeva J Jun 18 '14 at 04:34
  • @JeevaJsb you can use the name provided in that list to drop a key, e.g., `ALTER TABLE table_name DROP FOREIGN_KEY key_constraint_name` – SSM Dec 07 '17 at 15:14
  • @ShubhamSinghManhas Dear question is already mentioned that user don't know the constraint name. How can you dynamically remove the specific field as asked by JeevaJsb – Mahavirsinh Padhiyar Feb 23 '18 at 06:45
  • @Mahavirsinh we can find out the name of constraint in the information schema itself.The would be of the form of fk_ – SSM Feb 25 '18 at 07:45
  • @ShubhamSinghManhas No you getting me wrong I mean is that user also don't know the name what name schema has so dynamically we have to find it and remove it as well and that can not be done using schema you have provided that's it – Mahavirsinh Padhiyar Feb 25 '18 at 10:01
  • @Mahavirsinh maybe I am wrong, but doesn't INFORMATION_SCHEMA is the standard schema available. And we can access it with INFORMATION_SCHEMA.CONSTRAINT as it is. That is, the name of the schema itself will be INFORMATION_SCHEMA in this case. Thus, we are already knowing the name of schema. – SSM Feb 27 '18 at 03:48
32

Similar to Ed's Answer but you can use this to select the key name based on the table and column name.

That way you can run it in a script or maybe as a subquery to drop the constraint.

SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME =  'post'
AND COLUMN_NAME =  'userID'
Lightbulb1
  • 12,882
  • 6
  • 22
  • 23
  • There were 2 of them in there. I had to get this one `position_in_unique_constraint IS NOT NULL` – mehmet May 26 '20 at 18:14
19

Expanding on the answers since I ran into some gotchas. Also, I had 2 foreign keys declared, so I added an optional key to keep, if it's null it'll just be ignored:

declare @name varchar(255),
     @table varchar(255) = 'mytable',
     @column varchar(255) = 'mykeycolumn',
     @validkey varchar(255) =  'mykeyIwanttokeep'

SELECT @name = CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @table
    AND COLUMN_NAME =  @column
    AND (CONSTRAINT_NAME != @validkey or @validkey is null)

declare @sql varchar(1023) = 'alter table ' + @table + ' drop ' + @name 

exec (@sql)
SumGuy
  • 602
  • 7
  • 18
1

A SQL Server option:

DECLARE @foreignkey varchar(100)
DECLARE @tablename varchar(100)
DECLARE @command nvarchar(1000)

DECLARE db_cursor CURSOR FOR
SELECT fk.name, t.name
FROM sys.foreign_keys fk
JOIN sys.tables t ON t.object_id = fk.parent_object_id
WHERE t.name IN (
    'table_1_name_here',
    'table_2_name_here'
)

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @foreignkey, @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @command = 'ALTER TABLE ' + @tablename + ' DROP CONSTRAINT ' + @foreignkey
    EXECUTE(@command)
    FETCH NEXT FROM db_cursor INTO @foreignkey, @tablename
END
CLOSE db_cursor
DEALLOCATE db_cursor

The SQL selects all the constraints for the tables you care about into a cursor and drops them one by one. All you need to know are the names of the tables you want them dropped from.

user1786981
  • 116
  • 6
1

To drop a foreign key use the following commands :

  1. SHOW CREATE TABLE table_name;
  2. ALTER TABLE table_name DROP FOREIGN KEY table_name_ibfk_3; ("table_name_ibfk_3" is constraint foreign key name assigned for unnamed constraints). It varies.
  3. ALTER TABLE table_name DROP column_name.
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Erick
  • 11
  • 2
0

Never mind, below is Postgres syntax. It would be better if the question had sql server in the title as I didn't notice the tag

You could also drop and re-add the primary key on the parent table using cascade. This will remove any foreign keys that reference that table without you needing to know the foreign key names.

ALTER TABLE parent_table
DROP CONSTRAINT 'pk_id' CASCADE

-- add back pk
ALTER TABLE parent_table
ADD CONSTRAINT 'pk_id' PRIMARY KEY (id)

WARNING: you'd want to check all the dependencies first and if there are other tables, you'd need to add back their foreign keys. That does allow you to name the foreign keys properly when you add them back. This approach also may not be viable in a high transaction system due to the blocking transaction.

What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42
0

If you just look at the table in enterprise manager / management studio you will be able to see the list of keys and delete it from there.

Robin Day
  • 100,552
  • 23
  • 116
  • 167
-2

you can put:

> show create table tablename;

you will see how was created the table...columns, types...etc. and you could see your constraint name.

suely
  • 334
  • 1
  • 8
  • 19
  • 2
    `SHOW CREATE TABLE tablename;` is a MySQL specific command. This question is about Microsoft SQL Sever, completely different product. – Raystorm Sep 12 '14 at 23:20