user1142462
user1142462

Reputation: 43

Confusion with primary key

I am new to designing database. Maybe its a stupid question so please pardon me for that. So the thing is I am designing the database for users. After user fills registration information he gets the unique receipt number. So my question is since receipt no. is unique can I use it as a primary key in Users table or should I stick with standard method of assigning userID to each row in table and use userID as primary key?

Upvotes: 2

Views: 271

Answers (3)

nvogel
nvogel

Reputation: 25526

A table can have more than one key. If the receipt number is intended to be unique and you want the DBMS to enforce key dependencies on that attribute as a data integrity constraint then yes you should make it a key (with uniqueness implemented by a PRIMARY KEY or UNIQUE constraint or whatever mechanisms your DBMS provides).

Designating any one key to be a "primary" one isn't especially important - or at least it is only as important as you want it to be. What really matters is the full set of key(s) you choose. The requirements for any key are Uniqueness and Irreducibility. Sensible criteria for choosing a key are also: Familiarity, Simplicity and Stability

Upvotes: 2

Oleg Dok
Oleg Dok

Reputation: 21756

If your receipt no. is complicated enough - built using numbers and characters, or 20 digits in length - better use the surrogate UserId

If receipt No can be simple integer and may be generated from DB - better use UserId and assign its value to ReceiptNo

If ReceiptNo is simple integer and its identifies the user AND the only user - use it as PKey.

Upvotes: 0

mack
mack

Reputation: 1828

if your receipt number contains only integer number then, may be you can make your receipt-no field as auto-increment number,

Upvotes: 1

Related Questions