I have a question about constraints in SQL, to be exact in transact-sql. I have a database for trips. I created a procedure for adding new trip participant. I am using ms-sql server so foreign key and primary key constraints were added when I created tables. Now in my procedure for adding new participant for a trip I have
insert VoyageThemes(VoyageId,ThemeId) values (@voyageId,@themeId)
now, in VoyageThemes table both VoyageId and ThemeId are primary keys and foreign keys, so when I try to add new values that doesen’t correspond to already existing values in database constraint raises it’s head.
My question is, can I somehow check if constraint ‘said’ that I can’t add values to table, so I can stop the procedure or I need to manually check in my database if VoyageId and ThemeId exists.
I need to know if those values exists because of this line of code:
update Voyages
set Voyages.Price=Voyages.Price+@costOfTheme*@numOfParticipants
I am updating the price of a trip, so this line of code can only excecute only if there is a corresponding VoyageId and ThemeId
I guess you can use a try/catch?: