Unix Technical Forum

Database table data modifications with PK/FK relationship

This is a discussion on Database table data modifications with PK/FK relationship within the SQL Server forums, part of the Microsoft SQL Server category; --> I have two tables (T1 and T2). In T1 I have a field FT1 that is a primary key ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:44 PM
ebade2000@gmail.com
 
Posts: n/a
Default Database table data modifications with PK/FK relationship

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"?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:44 PM
David Portas
 
Posts: n/a
Default Re: Database table data modifications with PK/FK relationship

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
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:44 PM
Ed Murphy
 
Posts: n/a
Default Re: Database table data modifications with PK/FK relationship

ebade2000@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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:44 PM
ebade2000@gmail.com
 
Posts: n/a
Default Re: Database table data modifications with PK/FK relationship

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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:55 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com