Solved

FOREIGN KEY TO FOREIGN KEY

Posted on 2013-06-07
4
428 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 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi All,

Thank you very much for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now