By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Database table data modifications with PK/FK relationship

P: n/a
I have two tables (T1 and T2). In T1 I have a field FT1 that is a
primary key in T2 I have a field FT2 that is a foreign key linked to
FT1. These fields have been populated with data. Lets say that in one
row of data I have in T1 under FT1 "my cell" as the data entry,
similarly with T2 under FT2 I have 2 rows of data that also have "my
cell" as the data entry. What is the best line of action is I wanted
to change "my cell" to "my data"?

Mar 21 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 21 Mar, 21:48, ebade2...@gmail.com wrote:
I have two tables (T1 and T2). In T1 I have a field FT1 that is a
primary key in T2 I have a field FT2 that is a foreign key linked to
FT1. These fields have been populated with data. Lets say that in one
row of data I have in T1 under FT1 "my cell" as the data entry,
similarly with T2 under FT2 I have 2 rows of data that also have "my
cell" as the data entry. What is the best line of action is I wanted
to change "my cell" to "my data"?
-- Method 1.
INSERT INTO T1 (FT1) VALUES ('MY DATA');
UPDATE T2 SET FT2 = 'MY DATA' WHERE FT2 = 'MY CELL';
DELETE FROM T1 WHERE FT1 = 'MY CELL';

-- Method 2.
ALTER TABLE T2 ADD CONSTRAINT fk_t2_t1
FOREIGN KEY (ft2) REFERENCES T1 (FT1) ON UPDATE CASCADE;
UPDATE T1 SET FT1 = 'MY DATA' WHERE FT1 = 'MY CELL';

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Mar 21 '07 #2

P: n/a
eb*******@gmail.com wrote:
I have two tables (T1 and T2). In T1 I have a field FT1 that is a
primary key in T2 I have a field FT2 that is a foreign key linked to
FT1. These fields have been populated with data. Lets say that in one
row of data I have in T1 under FT1 "my cell" as the data entry,
similarly with T2 under FT2 I have 2 rows of data that also have "my
cell" as the data entry. What is the best line of action is I wanted
to change "my cell" to "my data"?
Revise the table structures so that the primary key is something that
never changes (e.g. INT IDENTITY) and "my cell" / "my data" is a non-PK
field in T1.

Side note: Please use real table/column names (e.g. table = Customers,
column = CustomerKey) rather than confusingly similar abbreviations
(table = T1, column = FT1).
Mar 21 '07 #3

P: n/a
Sorry for not specifying extra details, I thought it was a general
issue, I am running MS SQL 2000.

Thanks for your help, I very much prefer method 1. After looking at
your solution, it seemed so obvious. I probably need some rest LOL.

PS: Doesnt make any sense to post the error now but I will incase it
helps someone elses search
Server: Msg 547, Level 16, State 1, Line 1
UPDATE statement conflicted with TABLE REFERENCE constraint
'FK_T2_T1'. The conflict occurred in database 'DB_Test', table 'T2'.
The statement has been terminated.

Server: Msg 547, Level 16, State 1, Line 1
UPDATE statement conflicted with TABLE FOREIGN KEY constraint
'FK_T2_T1'. The conflict occurred in database 'DB_Test', table 'T1'.
The statement has been terminated.
Mar 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.