I understand the code remove the constraint
ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>
But I do not know my FK name I did not give it one and have read that is given one automatically.
How can I find this?
I understand the code remove the constraint
ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>
But I do not know my FK name I did not give it one and have read that is given one automatically.
How can I find this?
Assuming you are asking about SQL Server, you can try this:
DECLARE @table_name varchar(50) = 'MyTable'
DECLARE @fk_name varchar(100)
SELECT @fk_name = CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = @table_name AND CONSTRAINT_TYPE='FOREIGN KEY'
DECLARE @sql nvarchar(200) = 'ALTER TABLE [' + @table_name + '] DROP CONSTRAINT [' + @fk_name + ']'
EXEC sp_executesql @sql