Solved

Updating a row in a table with a composite primary key composed of 3 primary keys and one foreign key

Posted on 2007-11-19
8
966 Views
Last Modified: 2008-09-08
I have a table with a composite primary key composed of the fields  a,b and c and a foreign key 'd' and several other columns.
I hava a grid view for which I have dropdowns fora,b,c and d columns.
Now, my question is, when I update a row, can I update a, b, c or d individually while updating a row?

Please let me KNOW asap.
It's extremely urgent.

Thanks in advance.


0
Comment
Question by:amukta
  • 4
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20317794
>Now, my question is, when I update a row, can I update a, b, c or d individually while updating a row?
yes, that is possible.
however, you should rethink your table design, and add a dedicated column as primary key (and eventually make your current pk a unique key).
this will make things, related to this table, a lot easier.

having composite primary keys is rarely a good design, with "1 exception":
a relation table with just the 2 foreign key field could very well be implemented with the primary key on just that 2 fields...
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20317861
yes you can still update parts of composite primary keys individually. however, if it's referenced as a foreign key from another table, then it may be blocked due to constraints.

create table tab (a int, b int, c int, d int, primary key(a,b,c))
insert into tab (1,2,3,4)

these are all legal:
update tab set b = 7 where a = 1
update tab set c = 8 where a = 1
update tab set d = 9, b = b + 1where a = 1
update tab set a = 2 where a = 1
0
 

Author Comment

by:amukta
ID: 20319733
IMITCHIE,

THANK YOU!
I HAVE ONE MORE QUESTION.
IS IT FINE TO UPDATE THIS WAY?
UPDATE TAB SET C=8 WHERE A=1,B=2,C=3 AND D=4?
similarly update tab set b=3 where A=1,B=2,C=3 AND D=4?
update tab set c=4 where A=1,B=2,C=3 AND D=4?
AND SINCE D IS THE FOREIGN KEY, CAN I UPDATE IT SAYING
UPDATE TAB SET D=9 WHERE A=@A, B=@B, C=@C AND D=@D?

PLEASE LET ME know ASAP.
THANKS IN ADVANCE
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:amukta
ID: 20319760
ONE MORE QUESTION AND CN I ALSO UPDATE LIKE THIS?
UPDATE A=@A, B=@B,C=@C,D=@D WHERE A=@A,B=@B,C=@C,D=@D?
NOPE RIGHT? OR CAN I DO SO?

0
 

Author Comment

by:amukta
ID: 20319767
ONE MORE QUESTION AND CN I ALSO UPDATE LIKE THIS?
UPDATE A=@A, B=@B,C=@C,D=@D WHERE A=@A,B=@B,C=@C,D=@D?
NO RIGHT? OR CAN I DO SO?
0
 

Author Comment

by:amukta
ID: 20319774
ONE MORE QUESTION AND CN I ALSO UPDATE LIKE THIS?
UPDATE A=@A, B=@B,C=@C,D=@D WHERE A=@A,B=@B,C=@C,D=@D?
NO RIGHT? OR CAN I DO SO?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

863 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

24 Experts available now in Live!

Get 1:1 Help Now