sql - In MySQL does a UNIQUE varchar have to be a PRIMARY KEY as well? -
i have 2 unique values under 1 table in mysql.
|id (pk)|varchar(255) unique| ----------------------------- | 1 | 1234abcde | | 2 | 5678fghij | | 3 | 9012klmno |
do have set unique varchar pk or can still use auto_incremented id (int)? or bad practice?
no not need primary key.
additionally: cannot have more 1 'primary key' (different compound primary keys), , makes sense have auto-increment id field convention alone , frameworks (e.g. orm's hibernate , entity framework).
to answer second question, there many business cases many enforce unique constraint on multiple columns without making them primary key - example may have unique, may need able edit/change them:
i.e.
- email addresses usernames - forced unique, users need update them
- password salts (if generation sound unlikely require enforcement)
- random strings used generate one-time or time sensitive links (think bit.ly)
so, point being done time.
Comments
Post a Comment