I have a new table that has foreign key references to a number of existing tables. When I create the new table for the first time, I created the foreign keys in the following way:
ALTER TABLE [dbo].[NewTable] WITH CHECK ADD FOREIGN KEY([SectionDatabaseID])
REFERENCES [dbo].[Section] ([SectionDatabaseID])
ALTER TABLE [dbo].[NewTable] WITH CHECK ADD FOREIGN KEY([TermDatabaseID])
REFERENCES [dbo].[Term] ([TermDatabaseID])
The above way will generate names for the foreign key constraints using it's own automated naming convention.
However, I want to drop the new table because I need to do some major modifications by creating it from scratch.
Obviously, if I just execute a drop statement, the SQL Server database will complain stating that the new table has foreign key references to other tables.
I've heard of running the following script to figure out what foreign keys exist so that we can drop them:
use perls;
SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME +
'] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']'
FROM information_schema.table_constraints
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' and TABLE_NAME = 'NewTable'
The above will give results that basically show alter statements which I need to run.
I need something more automated. Why do I have to drop each foreign key constraint separately, and then only be able to drop the table?
I want to drop the table in a simpler way. It's got to be easier than what I need to do above.