Should each MySQL table have an auto-incrementing primary key?

  • I understand the meaning of primary keys.
  • I understand the meaning of indices.

Should each MySQL table have an automatically incrementing primary key (ideally with an INT field type)?

Update

@Raj More answer seems most effective. However, the problem when I think about this is how this auto-incrementing primary key identifier will relate to other tables. For instance:

Table 1

ID  |   firstname  |   lastname | email
----------------------------------------
1   |    john      |   doe      | 1@email.com
2   |    sarah     |   stow     | 2@email.com
3   |    mike      |   bro      | 3@email.com

table 2

ID  | memberid |    display      |    address
--------------------------------------------
1   |    1     | funtime zone    |   123 street
2   |    3     | silly place llc |  944 villa dr 

/. , ( 2) .. , , , " ". 2 "" 1, " ". , 2, 1, 2 .

+5
3

. , .

.

: surrogate-vs-natural-business-keys


( ), .

, , . , , , - , .. , , " MySQL ", , , , .

OP , .

, , , . , table1 table2 1:1 , memberid Primary Key, Foreign Key table1.

, (, InnoDB PK), memberid. , auto-incrementing id, JOIN 2 id (JOINs 1:n 2), memberid ( JOINs 1:n table1). memberid, JOIN memberid.

+6

. , .

.

/-

Edit

, :

, :

DimDate (20151031, 20151101, 20151102....) DimZipCode (10001, 10002, 10003...)

.

+3

, :

, n: m , , , , . .

, .

, ; - ...

EDIT: Added more in the previous sentence.

0
source

All Articles