Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
udaaf
Starting Member
22 Posts |
Posted - 2014-01-15 : 01:04:39
|
I have problem for create relation between table. I have one table refer to 2 table with same field. Could someone help me how to solve this problem. Here's the code.create table TopHeadConfTbl (PartNumber varchar(14) primary key not null,TpHdTypeID varchar(4) not null)create table BotHeadConfTbl (PartNumber varchar(14) primary key not null,BotHdTypeID varchar(4) not null)create table QuoDetTbl(MainAsID varchar(14)not null,PartNumber varchar(14) not null references (TopHeadConfTbl,BotHeadConfTbl) ,ItemNumber varchar(14) notnull) Thanks,Afri |
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2014-01-15 : 03:10:02
|
OK can't be completed like you are asking. Will you always have a QuoDetTbl record? If so, swap you references and say they have to exist in that table to be able to exist in the others.If you think what you have is right, then you will need to do this in your middle level logic (stored procs, triggers etc) instead as it seems you have to have the PartNumber existing in Top or Bot tables. |
 |
|
udaaf
Starting Member
22 Posts |
Posted - 2014-01-15 : 04:06:58
|
quote: Originally posted by RickD OK can't be completed like you are asking. Will you always have a QuoDetTbl record? If so, swap you references and say they have to exist in that table to be able to exist in the others.If you think what you have is right, then you will need to do this in your middle level logic (stored procs, triggers etc) instead as it seems you have to have the PartNumber existing in Top or Bot tables.
@RickD,Thanks for your reply.I'm try for this problem with LEFT Joint and here's the code :create table TopHeadConfTbl (PartNumber varchar(14) primary key not null,TpHdTypeID varchar(4) not null)create table BotHeadConfTbl (PartNumber varchar(14) primary key not null,BotHdTypeID varchar(4) not null)create table QuoDetTbl(MainAsID varchar(14)not null,PartNumber varchar(14) not null ,ItemNumber varchar(14) not null)insert into TopHeadConfTbl (PartNumber ,TpHdTypeID ) values ('TH001','001'), ('TH002','002')insert into BotHeadConfTbl (PartNumber ,BotHdTypeID ) values ('BH001','003'), ('BH002','004')insert into QuoDetTbl (MainAsID ,PartNumber ,ItemNumber ) values ('M001','TH001','1.1'), ('M001','BH002','1.2') select MainAsID ,coalesce (TopHeadConfTbl .PartNumber, BotHeadConfTbl.PartNumber) as PartNumber, coalesce (TopHeadConfTbl .TpHdTypeID, BotHeadConfTbl.BotHdTypeID) as HeadType,ItemNumber from QuoDetTblleft join TopHeadConfTbl on TopHeadConfTbl .PartNumber = QuoDetTbl .PartNumber left join BotHeadConfTbl on BotHeadConfTbl .PartNumber = QuoDetTbl .PartNumber |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-15 : 05:07:48
|
quote: Originally posted by udaaf I have problem for create relation between table. I have one table refer to 2 table with same field. Could someone help me how to solve this problem. Here's the code.create table TopHeadConfTbl (PartNumber varchar(14) primary key not null,TpHdTypeID varchar(4) not null)create table BotHeadConfTbl (PartNumber varchar(14) primary key not null,BotHdTypeID varchar(4) not null)create table QuoDetTbl(MainAsID varchar(14)not null,PartNumber varchar(14) not null references (TopHeadConfTbl,BotHeadConfTbl) ,ItemNumber varchar(14) notnull) Thanks,Afri
you cant have single column related to two tables columns using foreign key constraint in sql serverThere are two workarounds1. create two NULL able fields in your table TopHeadPartNumber and BotHeadPartNumber and link them vis foreign keys to respective tables ie TopHeadConfTbl and BotHeadConfTbl. Add a CHECK constrint on the QuoDetTbl table likeALTER TABLE QuoDetTbl ADD CONSTRAINT Chk_PartNumber CHECK (TopHeadPartNumber IS NOT NULL OR BotHeadPartNumber IS NOT NULL)2. Using trigger as suggested by Rick------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|