Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir
i want to insert into a table through storeprocedure if a composite primary key is present having 3 attribute,if i have insert 3 same int type no into table it is raising a error,how to handle the error pls help me
Posted
Updated 26-Aug-17 4:45am

A primary key must always be unique. That holds also true for composite primary keys.
When your primary key consists of three int a,b,c then you must not insert two rows with the same values of a,b,c.
Now check your requirements: what is expected to happen when such a combination recurs? An error? Then just change the mesage to become more intellegible to the user. An update? Then check for the presence of the combination, and if it is already present, do an update query instead of an insert query.
 
Share this answer
 
If you have composite key and want to insert values into it.
Then try below code

insert ignore into HotelBooking(RoomID, RegisterDate , CustomerName , Gender , Address , BillAmount ) values ('R1002', '12/12/2018','Adam Ponting','Male', 'Sydney Australia' , '1132.11')
ON DUPLICATE KEY UPDATE RoomID =values(RoomID), RegisterDate =values(RegisterDate );
 
Share this answer
 
Comments
Dave Kreskowiak 26-Aug-17 10:59am    
Asked and answered FOUR YEARS AGO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900