Note that there are some explanatory texts on larger screens.

plurals
  1. POresolving foreign constraints among connected tables
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Appreciate your comments!</p> <p>EXAMPLE OF SQL: <strong><em>Current table:</em></strong></p> <pre><code>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 </code></pre> <p><strong><em>expected table</em></strong></p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload