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 315019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:15:11+00:00 2026-05-12T08:15:11+00:00

I have two tables A and B with A referencing a column in B

  • 0

I have two tables A and B with A referencing a column in B with foreign key constraint. Now, i am trying to add more columns and constraints to table A by dropping table A and creating the table A again with new columns. SQL Server Mgmt Studio provides the “Drop and Create” option to do this where i alter the create table statement to add more columns.

Executing the statements throws an error stating A is referenced by a foreign key constraint. To fix this, i had to removed the foreign key constraint from the table A and then execute “drop and create” the statement. In my case, i could do this by dropping one constraint. I can’t image doing the same with a set of tables cross referencing each other.
This should be a common occurrence for most of the SQL designers and i am wondering if there is a way to manage this situation without deleting and recreating the web of constraints across tables.

Appreciate your comments!

EXAMPLE OF SQL:
Current table:

CREATE TABLE [dbo].[TableA](
    [PhotoId] [bigint] IDENTITY(1,1) NOT NULL,
    [PhotoTypeId] [bigint] NOT NULL,
    [PhotoDescription] [nvarchar](max) NULL,
    [LastModifiedBy] [bigint] NOT NULL,
    [LastModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED 
(
    [PhotoId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[TableA]  WITH NOCHECK ADD  CONSTRAINT [FK_TableA_TableAType] FOREIGN KEY([PhotoTypeId])
REFERENCES [dbo].[TableAType] ([PhotoTypeId])
GO

ALTER TABLE [dbo].[TableA] NOCHECK CONSTRAINT [FK_TableA_TableAType]
GO

ALTER TABLE [dbo].[TableA]  WITH NOCHECK ADD  CONSTRAINT [FK_TableA_TableB1] FOREIGN KEY([LastModifiedBy])
REFERENCES [dbo].[TableB] ([UserId])
GO

ALTER TABLE [dbo].[TableA] NOCHECK CONSTRAINT [FK_TableA_TableB1]
GO

ALTER TABLE [dbo].[TableA] ADD  CONSTRAINT [DF_TableA_IsDeleted]  DEFAULT ((0)) FOR [IsDeleted]
GO

expected table

CREATE TABLE [dbo].[TableA](
    [PhotoId] [bigint] IDENTITY(1,1) NOT NULL,
    [PhotoTypeId] [bigint] NOT NULL,
    [PhotoDescription] [nvarchar](max) NULL,
    ***[PhotoWidth] [int] NOT NULL,
    [PhotoHeight] [int] NOT NULL,***
    [LastModifiedBy] [bigint] NOT NULL,
    [LastModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED 
(
    [PhotoId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[TableA]  WITH NOCHECK ADD  CONSTRAINT [FK_TableA_TableAType] FOREIGN KEY([PhotoTypeId])
REFERENCES [dbo].[TableAType] ([PhotoTypeId])
GO

ALTER TABLE [dbo].[TableA] NOCHECK CONSTRAINT [FK_TableA_TableAType]
GO

ALTER TABLE [dbo].[TableA]  WITH NOCHECK ADD  CONSTRAINT [FK_TableA_TableB1] FOREIGN KEY([LastModifiedBy])
REFERENCES [dbo].[TableB] ([UserId])
GO

ALTER TABLE [dbo].[TableA] NOCHECK CONSTRAINT [FK_TableA_TableB1]
GO

ALTER TABLE [dbo].[TableA] ADD  CONSTRAINT [DF_TableA_IsDeleted]  DEFAULT ((0)) FOR [IsDeleted]
GO
  • 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-12T08:15:11+00:00Added an answer on May 12, 2026 at 8:15 am

    Solution: Don’t use the table designer in Management Studio. Seriously. Don’t. It’s a relic from more than a decade ago, and it doesn’t know SQL very well. (Check out connect.microsoft.com, and you’ll find many, many bugs and suggestions filed against it.)

    You can (and should) add columns and constraints using SQL without dropping and recreating the table, copying data, recreating constraints, etc.

    ALTER TABLE A ADD myNewColumn int;
    ALTER TABLE A ADD CONSTRAINT ...
    

    If you have a particular situation you don’t know the SQL for, please give the CREATE TABLE/INDEX/CONSTRAINT statements and explain what you need to do.

    Added: For the example you added to your question, here’s the one line SQL. I added defaults just because you’ll need them if your table already contains data when you add the columns, which are NOT NULL.

    ALTER TABLE dbo.TableA ADD PhotoWidth INT NOT NULL DEFAULT 640, PhotoHeight INT NOT NULL DEFAULT 480;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two tables both are related with primary-foreign key ralation and i have
I have a table that has two foreign keys to two different tables with
Suppose we have a PostgreSQL database with two tables A, B. table A columns:
I have two tables, the first has a primary key that is an identity,
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: a schedule table that contains information about how an employee
I have two tables, meetings_table and items_table . items_table has three columns: id item
I have a SQL-Server 2008 database and a schema which uses foreign key constraints
I have two tables: CREATE TABLE `category` ( `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
I have a problem with my delete statement. I have two tables: table vehicule_loan(

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.