Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FOREIGN KEY TO FOREIGN KEY

Posted on 2013-06-07
4
Medium Priority
?
443 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
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
rpkhare earned 800 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 1200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

926 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