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

  • SEARCH
  • Home
  • 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 1055593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:35:12+00:00 2026-05-16T17:35:12+00:00

If you drop a table in SQL Server that has Keys, Indexes, Contraints, etc

  • 0

If you drop a table in SQL Server that has Keys, Indexes, Contraints, etc on it will it drop those as well? I was just wondering when I am building my scripts if I need to create drop scripts for all of those as well or if I can simply drop the tables?

Thanks,

S

  • 1 1 Answer
  • 1 View
  • 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-05-16T17:35:13+00:00Added an answer on May 16, 2026 at 5:35 pm

    Yes, although you can’t drop a table that is referenced by a foreign key in another table.

    Here’s a procedure for dropping a table with foreign keys that reference it in SQL Server 2008:

    create table TA ( AID int identity(1, 1), OtherId int, Name varchar(512), constraint PK_TA primary key (AID))
    create table TB ( BID int identity(1, 1), OtherId int, Name varchar(512), constraint PK_TB primary key (BID))
    alter table TA add constraint FK_TA_TB foreign key (OtherId) references TB (BID)
    alter table TB add constraint FK_TB_TA foreign key (OtherId) references TA (AID)
    
    drop table ta -- doesn't work
    drop table tb -- doesn't work
    
    create procedure my_DropTable @tableName varchar(512) as
    begin
        if OBJECT_ID(@tableName) is null begin print 'OBJECT DOES NOT EXIST' return end
        declare @sql nvarchar(max)
        while exists (select * from sys.foreign_keys where referenced_object_id = object_id(@tableName))
        begin
            select @sql = 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' DROP CONSTRAINT ' + OBJECT_NAME(object_id)
            from sys.foreign_keys where referenced_object_id = object_id(@tableName)
    
            exec sp_executesql @sql
        end
        set @sql = 'DROP TABLE ' + @tableName
        exec sp_executesql @sql
    end
    
    exec my_DropTable 'TA'
    exec my_DropTable 'TB'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I first drop a table in SQL Server 2008 (after that it shows the
I have a Products table in SQL Server database, it has an ID column
I'm working with a legacy SQL Server database which has a core table with
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
I have a script that drops a load of tables using DROP TABLE IF
I'm writing a function which will drop a table if it already exists. It
I have an existing MVC3 application and database that is on a SQL Server
Back story i have a table that stores cached times, and currently has about
EDIT: I am using MS SQL server 2008 R2 as well. Well, on a
I have a SQL Server 2005 database that stores data for multiple users. Each

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.