Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:54:37+00:00 2026-06-12T07:54:37+00:00

I have a new table that has foreign key references to a number of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:54:38+00:00Added an answer on June 12, 2026 at 7:54 am

    Yes you must drop the FKs first. I’ve ran into this before and have written just the script you need to automate the task. Replace DBName and TABLENAME below.

    Warning to all: This script will drop a specific table (or group of tables) as well as all FK constraints! Use with caution, and always take a db backup before doing a major operation like this.

        use DBName
        /* 
        SCRIPT TO DROP TABLES WITH FK'S, INDEXES 
        https://mellodev.snipt.net/drop-tables-with-fk-contraints/
        */
    
        set nocount on
    
    declare @tables table (tablename varchar(255));
    insert into @tables
    select name from sys.tables where name in 
    ('TABLENAME')
    
    -- Iterate tables, drop FK constraints and tables
    declare @tablename varchar(255);
    declare cTable cursor for
    select tablename from @tables
    open cTable
    fetch next from cTable into @tableName
    while @@FETCH_STATUS=0
    begin
        -- Identify any FK constraints
        declare @fkCount int;
        SELECT @fkCount = COUNT(*)
        FROM sys.foreign_keys
        WHERE referenced_object_id = object_id(@tablename)
    
        -- Drop any FK constraints from the table
        if (@fkCount>0) begin
            declare @dropFkSql nvarchar(max);set @dropFkSql='';
            declare @fkName nvarchar(max);
            declare cDropFk cursor for  
                SELECT 'ALTER TABLE [' + OBJECT_NAME(parent_object_id) + '] DROP CONSTRAINT [' + name + ']',name
                FROM sys.foreign_keys
                WHERE referenced_object_id = object_id(@tablename)
            open cDropFk
            fetch next from cDropFk into @dropfksql,@fkName
            while @@FETCH_STATUS=0
            begin       
                exec sp_executesql @dropFkSql;
    
                select 'Dropped FK Constraint: ' + @fkName
                fetch next from cDropFk into @dropfksql,@fkName
            end
            close cDropFk
            deallocate cDropFk          
        end
    
        -- Drop the table
        declare @dropTableSql nvarchar(max);
        set @dropTableSql='DROP TABLE [' + @tablename + ']';
        exec sp_executesql @dropTableSql;
    
        select 'Dropped table: ' + @tablename
    
        fetch next from cTable into @tableName
    end
    close cTable
    deallocate cTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table B that has a foreign key to table A, and
I have a table that we just enabled FileStreams on. We created a new
I have two tables, round and event. One round has many events. create table
I have a table that has an autoincrement surrogate key. I want to use
I have created a new table including a column note. The default is varchar(255)
Which is your preference? Let's say we have a generic Product table that has
I have Plan table that has a one to many relationship with a Price
Trying to create a few new tables with foreign keys but am getting caught
I have two MySQL tables A and B . Table A has a member
I have a table that stores rejected contract proposals. CREATE TABLE STATUS_CONTRATO ( STC_ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.