0

enter image description here

this is my event_attended table,i have two more tables : users and events , i am importing data from csv file,from the csv file it is taking the username and event name then it is inserting the user_id and event_id in the event_attended table.

i don't want the same row values to be inserted in the table. for example: 1st row has user_id = 73 and event_id = 16 , so table should not have the same values again but it can have the same user_id again with the different event_id. for example: 4th row user_id = 73 and event_id = 18.

Akshay Rathod
  • 1,593
  • 3
  • 10
  • 17
  • Have a look at the accepted answer to see how to add a unique constraint to your table. This is probably the easiest/best way to enforce your requirement. – Tim Biegeleisen Sep 17 '18 at 07:01
  • Have you done some research? After a few minutes you should find the MySQL Documentation Entry for **unique constraints** – Tobias F. Sep 17 '18 at 07:01
  • As there's a unique value, may as well make it the PK and leave off id. – danblack Sep 17 '18 at 07:10
  • but if i add unique constraints to both the columns then i will not be able to use the same values again like the 4th row in my table. right? @TimBiegeleisen – Akshay Rathod Sep 17 '18 at 07:13
  • No...add the unique constraint on _both_ columns, `user_id` and `event_id`. Then, only a given combination of those two values can appear only once. – Tim Biegeleisen Sep 17 '18 at 07:17
  • Ok, Thank you very much . i will try that :) @TimBiegeleisen – Akshay Rathod Sep 17 '18 at 07:20
  • i added the unique constraint to both the columns and then i insert the following data: INSERT INTO `attended`(`user_id`, `event_id`) VALUES ('73','16') it got inserted. INSERT INTO `attended`(`user_id`, `event_id`) VALUES ('73','18') i am not able to insert this error: Duplicate entry '73' for key 'user_id_2 @TimBiegeleisen – Akshay Rathod Sep 17 '18 at 07:33
  • 2
    `ALTER TABLE attended ADD CONSTRAINT constr_ID UNIQUE (user_id, event_id)` ... _Also_, if you have any other unique constraints on the individual columns `user_id` or `event_id`, then you'll need to remove those. – Tim Biegeleisen Sep 17 '18 at 07:36
  • Worked :) Thanks again :) @TimBiegeleisen – Akshay Rathod Sep 17 '18 at 07:39
  • Yes i removed that :D – Akshay Rathod Sep 17 '18 at 07:40

0 Answers0