Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-01-16 : 03:33:15
|
When I insert / delete data to two tables, (how) can I keep the links correct automatically?I have two tables, Measurements and Comments. For each measurement there is 0-n comments. I have the Measurements table like this:ID Resultwhere the ID is an autonumber and result is whatever.The Comments table is like this:ID MeasID Commentwhere the ID is an autonumber and Comment is a string, but that MeasID makes the problem.Now when I save a result like thisResult: 8Comment: Looks like we got an 8 here.Comment: Doesn't seem too serious.Comment: Keep on, that's ok.the Measurements table gets one row, like:0 8the Comments table gets three rows0 0 Looks like we got an 8 here.1 0 Doesn't seem too serious.2 0 Keep on, that's ok.How do I get that 0 (MeasID) to the comments table? Is the only way to query the Measurements table for the max ID, add one to that and use that as MeasID?Same then with deleting, do I need to delete the orphan records with someWHERE MeasID = IDtobedeleted?Or does SQL Server 2008 have some feature that does all this foreign key handling ("linking") for me automatically? |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-01-16 : 04:00:30
|
I am not getting your point. can you post the sample data of first and second tables which you want to insert (clearly)?--Chandu |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-01-16 : 05:22:26
|
See this example by using Primary key and Foreign key..........CREATE TABLE Measurements (ID int identity(0,1) PRIMARY KEY, Result varchar(10) )CREATE TABLE Comments (ID int identity(0,1) Primary key, MeasID int references Measurements(ID), Comment varchar(20))insert into Measurements values('8')SELECT * FROM MeasurementsINSERT INTO Comments (MeasID, Comment)SELECT MAX(id), 'comment1' FROM MeasurementsSELECT * FROM Comments --Chandu |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-01-16 : 05:31:20
|
Table 1 (Measurements):ID Result0 21 82 703 something4 something else5 other valueTable 2 (Comments)ID MeasID Comment0 1 Something1 1 Something else2 1 yet another value3 2 what is this comment4 4 comment herewhere that MeasID is foreign key to Measurements table. In other words, this could be made like this:Combined Table:ID Result Comment0 2 NULL1 8 Something2 8 Something else3 8 yet another value4 70 what is this comment5 something NULL6 something else comment here7 other value NULLbut clearly it shouldn't be implemented this way, because, after all, we are talking about a relational database.So now the problem is that when I add a measurement value, while the first table (Measurements) behaves correctly so that there comes a new row likeID Result6 MyNewMeasurementResultI have no idea where the second table is supposed to draw that MeasID column from. Sure, ID is 5 and comment is whatever, but how can I assign that autonumber value of 6 from Measurements to there? Is the only way to query for the max ID of Measurements table, add 1 to that and insert the row using that +1 (5+1=6) as MeasID? |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-01-16 : 05:32:04
|
quote: Originally posted by bandi See this example by using Primary key and Foreign key..........CREATE TABLE Measurements (ID int identity(0,1) PRIMARY KEY, Result varchar(10) )CREATE TABLE Comments (ID int identity(0,1) Primary key, MeasID int references Measurements(ID), Comment varchar(20))insert into Measurements values('8')SELECT * FROM MeasurementsINSERT INTO Comments (MeasID, Comment)SELECT MAX(id), 'comment1' FROM MeasurementsSELECT * FROM Comments --Chandu
Yeah, this looks good!edit. It just seems like this is mostly used for updating and deleting the data i.e. it still doesn't tell me that when I insert the record with multiple comments, that the comments would know that they are supposed to get the MeasID from the newly created autonumber of their measurement "counterpart". |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-01-16 : 06:14:08
|
see this example.. You can use SCOPE_IDENTITY property to know the last isentity value of the corresponding columncreate table testId(id int identity (0,1), name varchar(2))insert into testId VALUES('ch'),('df'), ('aw'),('sd')SELECT SCOPE_IDENTITY() --3SELECT * FROM testID--Chandu |
|
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-01-16 : 08:02:26
|
quote: Originally posted by bandi see this example.. You can use SCOPE_IDENTITY property to know the last isentity value of the corresponding columncreate table testId(id int identity (0,1), name varchar(2))insert into testId VALUES('ch'),('df'), ('aw'),('sd')SELECT SCOPE_IDENTITY() --3SELECT * FROM testID--Chandu
Ok, I think this solves it. The key thing I was missing was the certainty that I connect the right comments to the right measurements - if two happens at the very same second, this that you posted should have no problems with it. Thanks! |
|
|
|
|
|
|
|