Solved

FOREIGN KEY TO FOREIGN KEY

Posted on 2013-06-07
4
441 Views
Last Modified: 2013-06-12
Hi All,

I have 3 tables :

1. TMUNIT
    1. UnitCode
    2. UnitName

2. TMITEM
    1. ItemCode
    2. ItemName
    3. UnitCode
    4. UnitCode1
    5. UnitCode2

3. TMPRICE
    1. ItemCode
    2. UnitCode
    3. UnitCode1
    4. UnitCode2

Is it possible to alter foreign key TMPRICE To TMITEM for UnitCode, UnitCode1, and UnitCode2. Foreign Key to Foreign Key.

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
rpkhare earned 200 total points
ID: 39231167
If I understand correctly, you want to:
     (1)  Remove Foreign Key from TMPrice
     (2)  Add new Foreign Key to TMItem for columns UnitCode1 and UnitCode 2.

To remove foreign key:
ALTER TABLE TMPrice DROP CONSTRAINT <FOREIGN_KEY_NAME>

Open in new window


To add new foreign key:

ALTER TABLE TmpItem
ADD CONSTRAINT fk_UnitCode
FOREIGN KEY (UnitCode1, UnitCode2)
REFERENCES TMUnit(UnitCode)

Open in new window


And if you want to keep Foreign Key in both tables, simply add a new Foreign Key constraint to TMUnit as shown below.

Foreign Key To Foreign Key what you are saying does not make much sense because, ideally, all Unit Codes in TMPrice definitely exist in TMItem. So just link your TMPrice with TMItem.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39232324
Hi rpkhare,

Why I am thinking of foreign key to foreign key.
Because I want to prevent TMITEM UnitCode being changed after TMPRICE has data.

How could I overcome this ?

Thank you.
0
 
LVL 2

Assisted Solution

by:vivekkumarSharma
vivekkumarSharma earned 300 total points
ID: 39240411
There is not a direct way to prevent the update in TMITEM when some data exists in TMPRICE
for UnitCode, UnitCode1, and UnitCode2.

You require to add instead of trigger on TMITEM, In which check if data exists in TMPRICE, then do not allow to update else go for update.

Alternatively, you can check it in the SP which is going to update entry of TMITEM table.
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39243316
Hi All,

Thank you very much for your help.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question