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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Check FK

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-02-06 : 19:04:29
Hello,

I have the following code to insert a record in a table:

INSERT dbo.Ratings (DocumentId, UserId, Rating)
VALUES (@DocumentId, UserId, @Rating)

RatingId is the PK.

DocumentId and UserId are FK in table Ratings.
DocumentId is the PK of table Documents.
UserId is the PK of table Users.

Should I check if there is a document with the given DocumentId and a user with the given UserId before I insert the new record in Ratings?

If yes, how can I do that?

Thanks,
Miguel

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-06 : 19:07:59
I prefer to just allow the constraint to do its job. So I don't validate first. If the parent data doesn't exist, then the error occurs and our application9s) handle(s) it and let the user know about the issue.

Other people do validate first.

It could be a preference thing but it also could be as a result of a business requirement.

Tara Kizer
Go to Top of Page
   

- Advertisement -