Multiple Foreign key with a same primary key - sql

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

Related

Add a referential constraint with a constant value

I have a table of locations:
CREATE TABLE [dbo].[loca_location] (
[loca_id] [int] NOT NULL IDENTITY,
[loca_name] [nvarchar](100) NOT NULL,
[loca_address_line_1] [nvarchar](100),
[loca_address_line_2] [nvarchar](100),
[loca_address_line_3] [nvarchar](100),
[loca_address_town] [nvarchar](100),
[loca_address_county] [nvarchar](100),
[loca_post_code] [nvarchar](12),
[loca_active] [bit] NOT NULL,
[loca_created] [datetimeoffset](0) NOT NULL,
[loca_created_by] [nvarchar](50) NOT NULL,
[loca_modified] [datetimeoffset](0) NOT NULL,
[loca_modified_by] [nvarchar](50) NOT NULL,
[loca_deleted] [datetimeoffset](0),
[loca_deleted_by] [nvarchar](50),
[loca_type] [char](1),
CONSTRAINT [PK_dbo.loca_store] PRIMARY KEY ([loca_id])
)
Now some locations may be my own, others may be customers. If customer, then loca_type will be C. If I own it, it will be L. If its a supplier, then it will be S.
Now a contract should always belong to a customer, so I want to make it so the loca_type = C is a constraint as well as the loca_id.
ALTER TABLE [dbo].[cont_contract] ADD CONSTRAINT [FK_dbo.cont_contract_dbo.loca_location_cust_id] FOREIGN KEY ([loca_id_customer]) REFERENCES [dbo].[loca_location] ([loca_id], `C`)
That doesn't work. Is it possible to do what I want?
This may work
1.Declare UNIQUE ([loca_id], [loca_type] ) on [loca_location] table. That is correct as any superset of PK is unique.
2.Add constant column to [cont_contract] table.
3.Create FK.
CREATE TABLE [dbo].[loca_location] (
[loca_id] [int] NOT NULL IDENTITY,
--..
[loca_type] [char](1) CHECK ([loca_type] IN ('A','B','C')), -- change as needed
CONSTRAINT [PK_dbo.loca_store] PRIMARY KEY ([loca_id])
)
ALTER TABLE [dbo].[loca_location] ADD CONSTRAINT [u1] UNIQUE ([loca_id],[loca_type]);
CREATE TABLE [dbo].[cont_contract] (
[id] [int] NOT NULL IDENTITY,
--..
[loca_id_customer] Int,
-- Constant column
[loca_type] [char](1) DEFAULT 'C' CHECK ([loca_type] ='C')
);
ALTER TABLE [dbo].[cont_contract] ADD CONSTRAINT [FK_dbo.cont_contract_dbo.loca_location_cust_id]
FOREIGN KEY ([loca_id_customer],[loca_type]) REFERENCES [dbo].[loca_location] ([loca_id], [loca_type]);

SQL Server : How to combine multiple database into one database?

From my original database, I made changes to some tables with columns in the table, I want to merge them into a single database. New database just add some table and old table add some columns.
How to merge multiple database into one database?
SQL example:
CREATE TABLE [dbo].[Item]
(
[ItemID] [nchar](10) NOT NULL,
[Money] [bigint] NOT NULL,
[ItemName] [bigint] NOT NULL,
[MoneyType] [bigint] NOT NULL,
CONSTRAINT [PK_Item]
PRIMARY KEY CLUSTERED ([ItemID] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Account]
(
[Index] [int] IDENTITY(1,1) NOT NULL,
[AccountID] [nchar](10) NOT NULL,
[AccountName] [int] NOT NULL,
[ItemList] [int] NOT NULL,
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Money]
(
[AccountID] [nchar](10) NOT NULL,
[Money] [bigint] NOT NULL,
[MoneyType] [bigint] NOT NULL,
CONSTRAINT [PK_Money]
PRIMARY KEY CLUSTERED ([AccountID] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO
-> Nick.McDermaid: use the schema compare tool in Visual Studio (various free editions) which will create a change script!
This will combine them onto DBCombined.Account if the Account table does NOT exist yet: the SELECT INTO code creates the target table. You would then need to add any indexes from the original tables. Also, "SELECT *" should really be broken out, listing each field, because if you have an ID field it will contain duplicates. Better to leave ID off during the insert and then go back and add an identity column.
USE DBCombined
GO
SELECT *
INTO Account
FROM (
SELECT *
FROM DB1.dbo.Account
UNION ALL
SELECT *
FROM DB2.dbo.Account
) Acct

Complex foreign key

I have a table similar to this:
CREATE TABLE [dbo].[Table1](
[Option_PK] [bigint] IDENTITY(1,1) NOT NULL,
[Option_1] [varchar](10) NULL,
[Option_2] [varchar](10) NULL,
[Option_3] [varchar](10) NULL)
What I am attempting to do, is add a table driven constraint which can effectively restrict valid entries on a per-column basis. For example, if I made a second table:
CREATE TABLE [dbo].[Table2](
[FK_Name] [varchar](10) NOT NULL,
[FK_Value] [varchar](10) NOT NULL)
I would then want to check that the value stored in Table1, column "Option_1", existed in Table2, column "FK_Value", where the value of "FK_Name" was "Option_1".
Is this possible with either a check or an FK?
** Edit to make the column datatypes match; I hand typed the example table declarations and typo'd, this wasn't relevant to the problem. I know how to do a FK, I don't know how to do an FK like what I'm describing.
Could you not just have 3 tables and three FK?
A FK needs to match types.
CREATE TABLE [dbo].[Option1]([FK_Value] [nchar](10) NOT NULL)
CONSTRAINT [PK_Option1] PRIMARY KEY CLUSTERED ([FK_Value] ASC)
ALTER TABLE [dbo].[Table1] WITH CHECK ADD CONSTRAINT [FK_Table1_Option1] FOREIGN KEY([Option_1])
REFERENCES [dbo].[Table2] ([FK_Value])
GO
Or you could have a column Option1 that defaults to a value of option1
I tried hard coding in a value for Option1 but would not go.
ALTER TABLE [dbo].[FKtest1] WITH CHECK ADD CONSTRAINT [FK_FKtest1_FKtest1] FOREIGN KEY([Option1],[ValueFK])
REFERENCES [dbo].[FKtest1FK] ([PKoption],[PKvalue])
GO
CREATE TABLE [dbo].[Table1](
[Option_PK] [bigint] IDENTITY(1,1) NOT NULL,
[Option_1] [varchar](10) NULL,
[Option_1_FK] [varchar](8) NOT NULL DEFAULT 'OPTION_1',
[Option_2] [varchar](10) NULL,
[Option_2_FK] [varchar](8) NOT NULL DEFAULT 'OPTION_2',
[Option_3] [varchar](10) NULL,
[Option_3_FK] [varchar](8) NOT NULL DEFAULT 'OPTION_3'
)
CREATE TABLE [dbo].[Options](
[FK_Name] [nchar](8) NOT NULL,
[FK_Value] [nchar](10) NOT NULL,
CONSTRAINT [PK_Option1] PRIMARY KEY CLUSTERED ([FK_Name], [FK_Value] ASC)
)
ALTER TABLE [dbo].[Table1] WITH CHECK ADD
CONSTRAINT [FK_Table1_Option1] FOREIGN KEY([Option_1], [Option_1_FK) REFERENCES [dbo].[Options] ([[FK_Value], [FK_Name])
CONSTRAINT [FK_Table1_Option2] FOREIGN KEY([Option_2], [Option_2_FK) REFERENCES [dbo].[Options] ([[FK_Value], [FK_Name])
CONSTRAINT [FK_Table1_Option3] FOREIGN KEY([Option_3], [Option_3_FK) REFERENCES [dbo].[Options] ([[FK_Value], [FK_Name])
GO
Which is untested, unnormalized and ugly. You should probably add constraints to ensure the value of Option_X_FK does not change. Actually, this being T-SQL, you might be able to use computed columns for that, but I'm not sure if including them in a foreign key is allowed.

Get value from other foreign key table based on current table value

I am working on a project where I need to extract the data from excel sheet to SQL Server
, well that bit have done successfully. Now my problem is that for a particular column
called product size, I want to update current table based on product size in other table, I am really very confused , please help me out
Please find the table structure
CREATE TABLE [dbo].[T_Product](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
[PartNo] [nvarchar](255) NULL,
[CategoryID] [int] NULL,
[MaterialID] [float] NULL,
[WireformID] [float] NULL,
[ProductName] [nvarchar](50) NULL,
[ProductSize] [nvarchar](50) NULL,
[ProductLength] [varchar](20) NULL,
[ProductActive] [bit] NULL,
[ProductImage] [varchar](60) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[T_ProductSize](
[Code] [int] IDENTITY(1,1) NOT NULL,
[ProductSize] [nvarchar](50) NULL,
[Length] [nchar](20) NULL
) ON [PRIMARY]
GO
OK, so ignore the previous answer, I got the wrong end of the stick!!
You want something like this I think:
UPDATE T_Product
SET [ProductLength] = ps.[Length]
FROM T_Product p
INNER JOIN T_ProductSize ps
ON p.[ProductSize] = ps.[ProductSize]
That will take the Length value from T_ProductSize and place it in T_Product.ProductLength based on the value of T_Product.ProductSize
You mention a foreign key but you haven't included a definition for it. Is it between the two tables in your example? If so, which columns make the key? Is product size the key? If so, then your question doesn't make a lot of sense as the value will be the same in both tables.
Is it possible that you mean the product size is to be stored in a separate table and not in the T_Product table? In that case then instead of ProductSize in T_Product you will want the code from the T_ProductSize table (can I also suggest that instead of 'code' you call it 'ProductSizeCode' or better yet 'ProductSizeId' or similar? having columns simply called code can be very confusing as you have no simple way of know what table that value is in). Also, you should always create a primary key on each table: You cannot have a foreign key without one. They don't have to be clustered, that will depend upon hwo your search the table, but I am using a clustered PK for this example. That would give you something like this:
CREATE TABLE [dbo].[T_Product](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
[PartNo] [nvarchar](255) NULL,
[CategoryID] [int] NULL,
[MaterialID] [float] NULL,
[WireformID] [float] NULL,
[ProductName] [nvarchar](50) NULL,
[ProductSizeId] [int] NOT NULL,
[ProductLength] [varchar](20) NULL,
[ProductActive] [bit] NULL,
[ProductImage] [varchar](60) NULL
CONSTRAINT [PK_T_Product] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[T_ProductSize](
[ProductSizeId] [int] IDENTITY(1,1) NOT NULL,
[ProductSize] [nvarchar](50) NULL,
[Length] [nchar](20) NULL
CONSTRAINT [PK_T_ProductSize] PRIMARY KEY CLUSTERED
(
[ProductSizeId] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
--Now add your foreign key to T_Product.
ALTER TABLE [T_Product] WITH NOCHECK ADD CONSTRAINT [FK_Product_ProductSize] FOREIGN KEY([ProductSizeId])
REFERENCES [T_ProductSize] ([ProductSizeId])
GO
ALTER TABLE [T_Product] CHECK CONSTRAINT [FK_Product_ProductSize]
GO
Now, to retrieve your product along with the product size use something like this:
SELECT p.[ProductID], p.[PartNo], p.[CategoryID], p.[MaterialID], p.[WireformID], p.[ProductName],
ps.[ProductSize], ps.[Length], p.[ProductLength], p.[ProductActive], p.[ProductImage]
FROM [T_Product] p
INNER JOIN [T_ProductSize] ps
ON ps.[ProductSizeId] = p.[ProductSizeId]
If I have understood you correctly, then this is what I think you're after. If not, then have another go at explaining what it is you need and I'll try again.
Try this...
UPDATE t2
SET t2.ProductLength = t1.Length
FROM dbo.T_ProductSize t1
INNER JOIN dbo.T_Product t2 ON t1.ProductSize = t2.ProductSize

FluentNHibernate HasManyToMany syntax

CREATE TABLE [dbo].[User](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[Password] [varchar](50) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[Module](
[ModuleID] [int] NOT NULL,
[ModuleName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Module] PRIMARY KEY CLUSTERED
(
[ModuleID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[Role](
[RoleID] [int] NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED
(
[RoleID] ASC
) ON [PRIMARY]
CREATE TABLE [dbo].[UserRoleSetting](
[UserID] [int] NOT NULL, /* FK to User table */
[ModuleID] [int] NOT NULL, /* FK to Module table */
[RoleID] [int] NOT NULL, /* FK to Role table */
CONSTRAINT [PK_UserRoleSetting] PRIMARY KEY CLUSTERED
(
[UserID] ASC,
[ModuleID] ASC
) ON [PRIMARY]
GO
I have a schema like this to define the users have different roles under different modules. I know if UserRoleSetting table is just a simple many to many relationship table, it is easy to define. But that table actually contains relationship from 3 different tables, so what might be the correct syntax to load user role settings into user object?
Thanks
Hardy
What you would like to map is a three-way many-to-many.
You can't define it directly. You instead create a separate entity UserRoleSetting and map it using: one-to-manys in User, Module, Role and many-to-one in UserRoleSetting. In FluentNHibernate it translates into HasMany() and References() respectively.
You can omit one side of the mapping of each connection if bi-directionality is not needed.
Does that solve your problem?