Join and/or Sub QUERY with Composite Key Tables - sql

I asked a similar question a few days ago. I have three tables. The DDL for each table is found in this thread (SQL JOIN not returning any results). My needs have changed slightly (this is my shocked face) and I haven't had much success in getting to what I need; close, but not quite.
What I would like to do is hand in a users name (or part of there name, i.e. LIKE) and get back the Description (Variable.Value table) and the document filename (Documents table). Of the latest revision. I have done this to get the latest rev with great success. I have tried in a myriad of ways to plug in the authors name (variablevalue table) to get exactly what I am after. I have tried a subquery with no luck either. I think the composite keys are catching me out as I have not done a lot of work with composite keys as yet. TIA.
Select Distinct vv.ValueCache
from VariableValue vv, Documents d
where d.filename like 'ECO-%' and d.deleted = 0 and d.DocumentID = vv.DocumentID and
vv.VariableID = ( Select VariableID
from Variable
where VariableName like 'Description') and
vv.RevisionNo = ( Select Max( Vv1.RevisionNo )
From VariableValue vv1
where vv1.VariableID = vv.VariableID and
vv1.DocumentID = d.DocumentID and
vv1.ValueText is not null ) and
vv.ValueCache != ''
order by vv.ValueCache ASC
Here is the missing DDL for the Documents table.
USE [PDM_NT_Vault]
GO
/****** Object: Table [dbo].[Documents] Script Date: 9/29/2017 9:51:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Documents](
[DocumentID] [int] IDENTITY(1,1) NOT NULL,
[Filename] [nvarchar](255) NULL,
[LockProject] [int] NULL CONSTRAINT [DF__TemporaryU__Mode__3B75D760] DEFAULT ((2)),
[UserID] [int] NULL CONSTRAINT [DF__Temporary__UserI__3C69FB99] DEFAULT ((0)),
[LockDomain] [nvarchar](255) NULL,
[LockPath] [nvarchar](255) NULL,
[Busy] [bit] NOT NULL CONSTRAINT [DF__TemporaryU__Busy__3E52440B] DEFAULT ((0)),
[Flushed] [int] NULL CONSTRAINT [DF_Documents_Flushed] DEFAULT ((1)),
[DefValStored] [int] NULL CONSTRAINT [DF_Documents_DefValStored] DEFAULT ((0)),
[RevGenCounter] [int] NULL,
[LatestRevisionNo] [int] NULL,
[CurrentStatusID] [int] NULL,
[WorkingVersionModified] [int] NULL,
[ExtensionID] [int] NULL CONSTRAINT [DF_Documents_ExtensionID] DEFAULT ((1)),
[LockDate] [datetime] NULL,
[UserDocRefsModified] [bit] NULL,
[Deleted] [bit] NOT NULL CONSTRAINT [DF_Documents_Deleted] DEFAULT ((0)),
[Shared] [int] NULL CONSTRAINT [DF_Documents_Shared] DEFAULT ((0)),
[LockViewID] [uniqueidentifier] NULL,
[Link] [bit] NULL CONSTRAINT [DF_Documents_Link] DEFAULT ((0)),
[DocTypeID] [int] NULL,
[ObjectTypeID] [int] NULL CONSTRAINT [DF_Documents_ObjectTypeID] DEFAULT ((1)),
[Flags] [int] NOT NULL CONSTRAINT [DF_Documents_Flags] DEFAULT ((0)),
CONSTRAINT [aaaaaDocuments1_PK] PRIMARY KEY CLUSTERED
(
[DocumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [Documents_FK00] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [Documents_FK00]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_DocType] FOREIGN KEY([DocTypeID])
REFERENCES [dbo].[DocType] ([DocTypeID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_DocType]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_FileExtension] FOREIGN KEY([ExtensionID])
REFERENCES [dbo].[FileExtension] ([ExtensionID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_FileExtension]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_ObjectType] FOREIGN KEY([ObjectTypeID])
REFERENCES [dbo].[ObjectType] ([ObjectTypeID])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_ObjectType]
GO
ALTER TABLE [dbo].[Documents] WITH CHECK ADD CONSTRAINT [FK_Documents_Projects1] FOREIGN KEY([LockProject])
REFERENCES [dbo].[Projects] ([ProjectID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_Projects1]
GO

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:

Multiple Foreign key with a same primary key

I am using Microsoft SQL Server 2014 Express as a database server.
I have the following two tables between which I want to create PK-FK relationship.
CREATE TABLE [dbo].[Account]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[ACCOUNTNAME] [nvarchar](max) NOT NULL,
[ACCOUNTTYPE] [int] NOT NULL,
[CREATE_TIMESTAMP] [datetime] NOT NULL,
[LAST_EDIT_TIMESTAMP] [datetime] NOT NULL,
[OPENING_BALANCE] [decimal](18, 2) NOT NULL DEFAULT ((0)),
[OPENING_BALANCE_TYPE] [nvarchar](50) NULL,
CONSTRAINT [PK_dbo.Account]
PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[Voucher]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[VOUCHERTYPE] [nvarchar](50) NOT NULL,
[VOUCHERNO] [int] NOT NULL,
[DATE] [datetime] NOT NULL,
[AMOUNT] [decimal](18, 2) NOT NULL,
[DRPARTY] [int] NOT NULL,
[CRPARTY] [int] NOT NULL,
[DETAILS] [nvarchar](50) NOT NULL,
[ORIGIN] [nvarchar](50) NULL,
[ORIGINID] [int] NOT NULL,
[ORIGINDETAILS] [nvarchar](max) NULL,
[CREATE_TIMESTAMP] [datetime] NOT NULL DEFAULT ('1900-01-01T00:00:00.000'),
[LAST_EDIT_TIMESTAMP] [datetime] NOT NULL DEFAULT ('1900-01-01T00:00:00.000'),
[TRANSACTION_TYPE] [nvarchar](50) NULL,
CONSTRAINT [PK_dbo.Voucher]
PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
This is the error I am getting when creating FK relationship via Microsoft SQL Server Management Studio. Both tables are empty.
'Account' table saved successfully
'Voucher' table - Unable to create relationship 'FK_Voucher_Account1'. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Voucher_Account1". The conflict occurred in database "SKUMAR", table "dbo.Account", column 'ID'
I want to create a relation between Account.ID -> Voucher.DRPARTY and Account.ID -> Voucher.CRPARTY but I am unable to create second FK relationship.
May I know how to solve this?
There is a possibility of conflicted foreign key error because of data mismatch between Voucher(CRPARTY) column and Account(Id) column. Rows available in CRPARTY table must be in Account table.
Verify using below query.
SELECT * FROM Voucher WHERE CRPARTY NOT IN (SELECT Id FROM Account);
I was able to create 2 FKs to 1 PK. http://rextester.com/WMAT80057
You can delete the FK relationships between your tables and use this script to create them. Hopefully, your tables do not have non matching data.
ALTER TABLE [dbo]. [Voucher] WITH CHECK ADD CONSTRAINT [FK_ACCOUNT_ID_VOUCHER_DRPARTY] FOREIGN KEY ([DRPARTY]) REFERENCES [dbo]. [Account]([ID])
GO
ALTER TABLE [dbo]. [Voucher] CHECK CONSTRAINT [FK_ACCOUNT_ID_VOUCHER_DRPARTY]
GO
ALTER TABLE [dbo]. [Voucher] WITH CHECK ADD CONSTRAINT [FK_ACCOUNT_ID_VOUCHER_CRPARTY] FOREIGN KEY ([CRPARTY]) REFERENCES [dbo]. [Account]([ID])
GO
ALTER TABLE [dbo]. [Voucher] CHECK CONSTRAINT [FK_ACCOUNT_ID_VOUCHER_CRPARTY]
GO

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

2 FOREIGN KEYS to the same PRIMARY KEY

I am having problem when creating FOREIGN KEY for two of my tables (Characters) and (Factions).
Let me explain the situation:
A character normally have a faction but some can have 2, so (Characters) tabel have two columns [Faction1] and [Faction2] both must be related with the (Factions) table. So I made a FOREIGN KEY (FK_Charachters(1)_Factions) with
Set Null on delete
Cascade on update
When I tried to make the FOREIGN KEY (FK_Charachters(2)_Factions) that will use the column [Faction2] instead of [Faction1] as the previous key and also make
Set Null on delete
Cascade on update
I get the error message:
"Unable to create relationship 'FK_Characters(2)_Factions'.
Introducing FOREIGN KEY constraint 'FK_Characters(2)_Factions' on table 'Characters' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."
And of course when I set ON DELETE NO ACTION and ON UPDATE NO ACTION everything works fine but that will prevent any update for the faction.
What can I do to enforce those 2 FOREIGN KEY ?
~TIA~
(Characters) Table
USE [HDA]
GO
/****** Object: Table [dbo].[Characters] Script Date: 05/15/2014 02:38:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Characters](
[ID] [smallint] IDENTITY(1,1) NOT NULL,
[CODE] [nvarchar](5) NULL,
[Name] [nvarchar](50) NULL,
[NameID] [smallint] NULL,
[Tier] [tinyint] NULL,
[Grp1] [tinyint] NULL,
[Grp2] [tinyint] NULL,
[Rarity] [tinyint] NULL,
[Fac1] [tinyint] NULL,
[Fac2] [tinyint] NULL,
[Speciality] [tinyint] NULL,
[MaxLevel] [smallint] NULL,
[Power] [smallint] NULL,
[HP] [smallint] NULL,
[Speed] [tinyint] NULL,
[Attack] [nvarchar](20) NULL,
[AttackDesc] [nvarchar](10) NULL,
[PowerPerLevel] [tinyint] NULL,
[HPPerLevel] [tinyint] NULL,
[TS] [timestamp] NULL,
[CreationDate] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Characters] 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]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Characters] WITH CHECK ADD CONSTRAINT [FK_Characters_AttackDescription] FOREIGN KEY([AttackDesc])
REFERENCES [dbo].[AttackDescription] ([ID])
GO
ALTER TABLE [dbo].[Characters] CHECK CONSTRAINT [FK_Characters_AttackDescription]
GO
ALTER TABLE [dbo].[Characters] WITH CHECK ADD CONSTRAINT [FK_Characters_CharactersName] FOREIGN KEY([NameID])
REFERENCES [dbo].[CharactersName] ([ID])
ON UPDATE CASCADE
ON DELETE SET NULL
GO
ALTER TABLE [dbo].[Characters] CHECK CONSTRAINT [FK_Characters_CharactersName]
GO
ALTER TABLE [dbo].[Characters] WITH CHECK ADD CONSTRAINT [FK_Characters_Rarity] FOREIGN KEY([Rarity])
REFERENCES [dbo].[Rarity] ([ID])
ON UPDATE CASCADE
ON DELETE SET NULL
GO
ALTER TABLE [dbo].[Characters] CHECK CONSTRAINT [FK_Characters_Rarity]
GO
ALTER TABLE [dbo].[Characters] ADD CONSTRAINT [DF__Character__Creat__395884C4] DEFAULT (sysdatetime()) FOR [CreationDate]
GO
(Factions) Table
USE [HDA]
GO
/****** Object: Table [dbo].[Factions] Script Date: 05/15/2014 02:42:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Factions](
[ID] [tinyint] NOT NULL,
[Color] [nvarchar](5) NOT NULL,
[Description] [ntext] NULL,
[TS] [timestamp] NULL,
[CreationDate] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Factions] 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]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Factions] ADD CONSTRAINT [DF__Factions__Creati__4D5F7D71] DEFAULT (sysdatetime()) FOR [CreationDate]
GO