0

If I want to make sure that there are unique columns by user_id and notification_id. Should I use UNIQUE constraint on that two columns and (eventualy) set id as PRIMARY KEY or set these two columns as PRIMARY KEY and remove id column? This is how table should look:

Column             Type       
----------------------------------
id                 int(11)    
user_id            int(11)    
notification_id    int(11)    
col3               varchar(255)
...
  • use UNIQUE constraint on two columns(user_id and notification_id) and set id as PRIMARY KEY. – Siraj ul Haq Mar 31 '17 at 13:49
  • Set the composite unique index to `user_id, notification_id`. LEAVE `id` as primary key. If you omit it, InnoDB will create a hidden key that's 8 bytes long so it can use this info for efficient writes. If you use MySQL, you want to have `auto_increment` primary key in almost all cases. It's also useful to have a single column helping you uniquely distinguish a record. – Mjh Mar 31 '17 at 13:58
  • Thank you Mjh for detailed explanation, it helps me alot. – slavkovicdjordje Mar 31 '17 at 14:01
  • Even when accepted answer is in favor of additional ID, the comments explain why is preferred the composited key. You still need make a decision, but at least will be an informed decision. – Juan Carlos Oropeza Mar 31 '17 at 14:31

0 Answers0