Unable to drop foreign key constraint - sql

Table definition:
CREATE TABLE [dbo].[Visualization_ComponentLayouts]
(
[ComponentLayoutId] [int] IDENTITY(1,1) NOT NULL,
[ComponentId] [int] NOT NULL,
[LayoutId] [int] NOT NULL,
[UnitTypeId] [int] NOT NULL,
[ProjectId] [int] NOT NULL,
[CreatedBy] [int] NULL,
[ModifiedBy] [int] NULL,
CONSTRAINT [PK_Visualization_ComponentLayouts] PRIMARY KEY CLUSTERED
(
[ComponentLayoutId] 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
ALTER TABLE [dbo].[Visualization_ComponentLayouts] WITH NOCHECK
ADD CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_ComponentInfo]
FOREIGN KEY([ComponentId])
REFERENCES [dbo].[Visualization_ComponentInfo] ([ComponentId])
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] CHECK CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_ComponentInfo]
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] WITH CHECK
ADD CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_Layouts]
FOREIGN KEY([LayoutId], [ProjectId])
REFERENCES [dbo].[Visualization_Layouts] ([LayoutId], [ProjectId])
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] CHECK CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_Layouts]
GO
Query to drop constraint:
ALTER TABLE Visualization_ComponentLayouts
DROP CONSTRAINT FK_Visualization_ComponentLayouts_Visualization_Layouts
Error I get when I execute above drop query:
'FK_Visualization_ComponentLayouts_Visualization_Layouts' is not a
constraint. Could not drop constraint. See previous errors.
In SQL Server Management Studio, in Object Explorer, when I expand the table node, and within that when I expand constraints node, no constraints are being listed there.
When I execute query : EXEC sp_help 'Visualization_ComponentLayouts'
I get following:
What am I doing wrong?

Related

SQL Azure is recommending Indexes that are already on my Table

In the Azure Portal you see recommendations from SQL Azure. The weird thing is that these indexes already exist on my Table.
A query on MemberId on the Tip table also returns within I <250ms on a table of 7.5 million records so I have the feeling the index is working.
We do seem having performance problems during high traffic.
What could be going on?
TABLE
CREATE TABLE [dbo].[Tip](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MemberId] [int] NOT NULL,
[Slot] [varchar](25) NOT NULL,
[PeriodKey] [varchar](25) NOT NULL,
[LastChanged] [datetime2](7) NOT NULL,
[Invalid] [bit] NOT NULL,
[Value] [nvarchar](50) NULL,
[ListingIndex] [tinyint] NOT NULL,
CONSTRAINT [PK_Tip] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY],
CONSTRAINT [IX_Tip_Unique_Key] UNIQUE NONCLUSTERED
(
[MemberId] ASC,
[PeriodKey] ASC,
[Slot] ASC,
[ListingIndex] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_Invalid] DEFAULT ((0)) FOR [Invalid]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_ListingIndex] DEFAULT ((0)) FOR [ListingIndex]
GO
ALTER TABLE [dbo].[Tip] WITH NOCHECK ADD CONSTRAINT [FK_Tip_Member] FOREIGN KEY([MemberId])
REFERENCES [dbo].[Member] ([Id])
GO
ALTER TABLE [dbo].[Tip] NOCHECK CONSTRAINT [FK_Tip_Member]
GO
This is a really simple query on MemberId. Normal queries might be a bit more complicated:
This is the Index that it tries to create in the Azure portal:

SQL - Add constraint with foreign key in another table

I try to add a constraint but I don't manage to do what I want :).
So in my case, I have these 3 tables :
[dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Language] [nvarchar](2) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[Surname] [nvarchar](50) NOT NULL,
...
)
[dbo].[Project](
[Id] [int] NOT NULL,
[UserId] [int] NOT NULL,
[KindOfProjectId] [int] NOT NULL,
...
)
[dbo].[KindOfProject](
[Id] [int] NOT NULL,
[Language] [nvarchar](2) NOT NULL,
...
CONSTRAINT [PK__KindOfProject] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[Language] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
The table KindOfProject has columns "Id" AND "Language" as Primary Key
And I have added a first constraint to link table Project with table User like this :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([Id])
Now, what I want to do is to add a constraint to link the column "KindOfProjectId" from table Project to the column "Id" from table KindOfProject :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_KindOfProjectId] FOREIGN KEY([KindOfProjectId],[Language])
REFERENCES [dbo].[KindOfProject] ([Id],[Language])
But it does not work, as there is no column Language in table Project. But I can get it thanks to the link with the table User, as table User has a column Language.
So I have tried something like that :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_KindOfProjectId] FOREIGN KEY([KindOfProjectId],[User].[Language])
REFERENCES [dbo].[KindOfProject] ([Id],[Language])
But nothing works.
Have you an idea about it ? :D
Thanks for your help !

Still FK conflict, even after investigation

4.857 million rows went through the flow OK, and only 38.000 rows sent to error output. This due to a FK conflict. ---> "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FactTransactions_DimCustomer".
The conflict occurred in database "", table "dbo.DimCustomer", column 'CustomerNr'.".
My problem is, after some investigation, i can't identify any conflict in the primary table dbo.dimcustomer.
Lets take Mid(Named CustomerNr in the DB) "60534658" for an example and let us see.
Picture NR
1: This is a data-view draft of some of the rows that got sent to error output for analysis.
2: This is the table where its supposed to insert, notice that rows with the same CustomerNr already exists, because some rows of the same CustomerNr, for a strange reason, got inserted while others did not
3: And last. This is the actual primary table(Customer table), where the Mid(CustomerNr) reference clearly exists!
Am i missing something here? why is it still in conflict?
ty for any answers!
TABLE STRUCTURE:
CREATE TABLE [dbo].[DimCustomer](
[CustomerNr] [int] NOT NULL,
[CustomerID] [int] NULL,
[GeographyKey] [int] NULL,
[OrgNum] [nvarchar](50) NULL,
[CustomerName] [nvarchar](255) NULL,
[Adress] [nvarchar](255) NULL,
[ZipCode] [nvarchar](255) NULL,
[MCC_Code] [float] NULL,
CONSTRAINT [PK_Dim.Customer_1] PRIMARY KEY CLUSTERED
(
[CustomerNr] 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
ALTER TABLE [dbo].[DimCustomer] WITH CHECK ADD CONSTRAINT [FK_DimCustomer_DimGeography] FOREIGN KEY([GeographyKey])
REFERENCES [dbo].[DimGeography] ([GeographyKey])
GO
ALTER TABLE [dbo].[DimCustomer] CHECK CONSTRAINT [FK_DimCustomer_DimGeography]
GO
CREATE TABLE [dbo].[FactTransactions](
[TransactionKey] [int] IDENTITY(1,1) NOT NULL,
[Reportdate] [date] NULL,
[CustomerNr] [int] NULL,
[SchemeID] [smallint] NULL,
[PriceType] [int] NULL,
[Count] [int] NULL,
[Amount] [float] NULL,
[Commission] [float] NULL,
[InterchangeFee] [float] NULL,
[Currency] [nvarchar](3) NULL,
[FeeType] [int] NULL,
CONSTRAINT [PK_FactTransactions] PRIMARY KEY CLUSTERED
(
[TransactionKey] 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
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimCardScheme] FOREIGN KEY([SchemeID])
REFERENCES [dbo].[DimCardScheme] ([SchemeID])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCardScheme]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimCustomer] FOREIGN KEY([CustomerNr])
REFERENCES [dbo].[DimCustomer] ([CustomerNr])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCustomer]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimDate] FOREIGN KEY([Reportdate])
REFERENCES [dbo].[DimDate] ([Date])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimDate]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimPriceType] FOREIGN KEY([PriceType])
REFERENCES [dbo].[DimPriceType] ([PriceType])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimPriceType]
GO
Example error output line:
2015-05-01,60534658,1,1,57,484,5280,3000000000002,78,340000000000003,0,EUR,57,1
Maybe you have some FactTrancsactions pointing to a customer which doesn't exist.
Try this statement to check if there are some transaction pointing to the wrong customer. Each row returned by this statement will bounce if you create the foreign key.
SELECT ft.*
FROM dbo.FactTransactions as ft
LEFT JOIN dbo.DimCustomer as dc
ON ft.CustomerNr = dc.CustomerNr
WHERE dc.CustomerNr IS NULL

Unique Constraint doesn't appear to be constraining anything

I have a Table called Models set up as follows:
ID - PK, Identity
Manufacturer FK - Int32
PriModel FK - Int32
SecModel FK - Int32
FormFactor FK - Int32
Active Char(1)
What I want is Columns 2,3,4,5 to only accept a unique set of values. I ran:
ALTER TABLE dbo.Models ADD CONSTRAINT uc_Models UNIQUE (Manufacturer, PriModel, SecModel, FormFactor)
The command reported completed successfully and I see an index was added, but not a constraint. I am also able to create a duplicate of cols 2-5 with Insert and Update commands. I'm not sure if the issue is the foreign keys or something else altogether.
USE [inventory_v2]
GO
/****** Object: Table [dbo].[Models] Script Date: 4/8/2013 2:01:41 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Models](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Manufacturer] [int] NOT NULL,
[PriModel] [int] NOT NULL,
[SecModel] [int] NOT NULL,
[FormFactor] [int] NOT NULL,
[Active] [char](1) NOT NULL,
CONSTRAINT [PK_Models] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [uc_Models] UNIQUE NONCLUSTERED
(
[Manufacturer] ASC,
[PriModel] ASC,
[SecModel] ASC,
[FormFactor] 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].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_FormFactor] FOREIGN KEY([FormFactor])
REFERENCES [dbo].[Models_FormFactor] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_FormFactor]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_OEMs] FOREIGN KEY([Manufacturer])
REFERENCES [dbo].[Models_OEMs] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_OEMs]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_PriModels] FOREIGN KEY([PriModel])
REFERENCES [dbo].[Models_PriModels] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_PriModels]
GO
ALTER TABLE [dbo].[Models] WITH CHECK ADD CONSTRAINT [FK_Models_Models_SecModels] FOREIGN KEY([SecModel])
REFERENCES [dbo].[Models_SecModels] ([ID])
GO
ALTER TABLE [dbo].[Models] CHECK CONSTRAINT [FK_Models_Models_SecModels]
GO

1:1 table relation can't insert new row

I have a table inheritance in my database. I have table "Item" and Table "Something". Item has ItemId which is primary key and auto increment. In table Something I have ItemId which is primary key (not autoincrement). Theese tables are in 1:1 relation.
So I have tried to insert data in those tables but this doesn't work:
...
DECLARE #itemId int
INSERT INTO dbo.Items
(ItemTypeId,UserId,CreatedOnDate,Title,Description)
VALUES
(#p_ItemTypeId,#p_UserId,#p_CreatedOnDate,#p_Title,#p_Description)
SELECT #itemId = SCOPE_IDENTITY()
INSERT INTO dbo.Something
(ItemsId)
VALUES
(#itemId)
...
This is the error that I get:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Items_Somethings".
Tables create script:
CREATE TABLE [dbo].[Items](
[ItemdId] [int] IDENTITY(1,1) NOT NULL,
[ItemTypeId] [int] NULL,
[UserId] [int] NULL,
[CreatedOnDate] [smalldatetime] NULL,
[Title] [nvarchar](50) NULL,
[Description] [nvarchar](max) NULL,
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED
(
[ItemId] 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
ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Somethings] FOREIGN KEY([ItemId])
REFERENCES [dbo].[Somethings] ([ItemId])
GO
ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Somethings]
GO
CREATE TABLE [dbo].[Somethings](
[ItemId] [int] NOT NULL,
CONSTRAINT [PK_Somethings] PRIMARY KEY CLUSTERED
(
[ItemId] 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
ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Somethings] FOREIGN KEY([ItemId])
REFERENCES [dbo].[Somethings] ([ItemId])
Bad Direction of foreign key.
You should add foreign key to [dbo].[Somethings] and reference [dbo].[Items].ItemId
Like this:
ALTER TABLE [dbo].[Somethings] WITH CHECK ADD CONSTRAINT [FK_Somethings_Items] FOREIGN KEY([ItemId])
REFERENCES [dbo].[Items] ([ItemId])
In this script, you have inverted the inheritance between the tables. If Something table inherits from Item table then the FK has to be created at Table `Something' like this :
ALTER TABLE [dbo].[Somethings] WITH CHECK ADD CONSTRAINT [FK_Somethings_Items] FOREIGN KEY([ItemId])
REFERENCES [dbo].[Items] ([ItemId])
GO
ALTER TABLE [dbo].[Somethings] CHECK CONSTRAINT [FK_Somethings_Items]
GO