Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 551 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove foreign key constraint in sql server?

#11
Depending on the **DB** you are using there's a syntax or another.

If you're using **Oracle** you have to put what the other users told you:

ALTER TABLE table_name DROP CONSTRAINT fk_name;

But if you use **MySQL** then this will give you a syntax error, instead you can type:

ALTER TABLE table_name DROP INDEX fk_name;
Reply

#12
Drop all the foreign keys of a table:

USE [Database_Name]
DECLARE @FOREIGN_KEY_NAME VARCHAR(100)

DECLARE FOREIGN_KEY_CURSOR CURSOR FOR
SELECT name FOREIGN_KEY_NAME FROM sys.foreign_keys WHERE parent_object_id = (SELECT object_id FROM sys.objects WHERE name = 'Table_Name' AND TYPE = 'U')

OPEN FOREIGN_KEY_CURSOR
----------------------------------------------------------
FETCH NEXT FROM FOREIGN_KEY_CURSOR INTO @FOREIGN_KEY_NAME
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @DROP_COMMAND NVARCHAR(150) = 'ALTER TABLE Table_Name DROP CONSTRAINT' + ' ' + @FOREIGN_KEY_NAME

EXECUTE Sp_executesql @DROP_COMMAND

FETCH NEXT FROM FOREIGN_KEY_CURSOR INTO @FOREIGN_KEY_NAME

END
-----------------------------------------------------------------------------------------------------------------
CLOSE FOREIGN_KEY_CURSOR
DEALLOCATE FOREIGN_KEY_CURSOR
Reply

#13
If you don't know foreign key constraint name then try this to find it.
```
sp_help 'TableName'
```
additionally for different schema
```
sp_help 'schemaName.TableName'
```
then
```
ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>
```
Reply

#14
firstly use
```sql
show create table table_name;
```
to see the descriptive structure of your table.

There you may see constraints respective to foreign keys you used in that table.
First delete the respective constraint with
```sql
alter table table_name drop constraint constraint_name;
```

and then delete the respective foreign keys or column you wanted...GoodLuck!!
Reply

#15
To be on the safer side, just name all your constraints and take note of them in the comment section.

ALTER TABLE[table_name]
DROP CONSTRAINT Constraint_name

Reply

#16
If you find yourself in a situation where the FK name of a table has been auto-generated and you aren't able to view what it exactly is (in the case of not having rights to a database for instance) you could try something like this:

```
DECLARE @table NVARCHAR(512), @sql NVARCHAR(MAX);
SELECT @table = N'dbo.Table';
SELECT @sql = 'ALTER TABLE ' + @table
+ ' DROP CONSTRAINT ' + NAME + ';'
FROM sys.foreign_keys
WHERE [type] = 'F'
AND [parent_object_id] = OBJECT_ID(@table);
EXEC sp_executeSQL @sql;
```

Build up a stored proc which drops the constraint of the specified table without specifying the actual FK name. It drops the constraint where the object ```[type]``` is equal to F (Foreign Key constraint).

**Note**: if there are multiple FK's in the table it will drop them all. So this solution works best if the table you are targeting has just one FK.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through