Do I need a primary key?

On database systems, should each table have a primary key?

For example, I have a table table1(foreignkey1,foreignkey2,attribute), for example, it this.table1does not have a primary key.

Should I define a primary key for this table, for example table1id?

+4
source share
4 answers

This is a subjective question, so I hope you do not mind, I answer with some opinion :)

, - 95% + - , , . ( " " ) , , , .

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

, , , , , DMBS: " - , , !"

: , . , , .

+1

, / :

  • .

Databse .

MySQL- PRIMARY KEY, , , .

Composite Primary key, :

CREATE TABLE table1(
    FK1 INT,
    FK2 INT,
    ATTRIBUTE INT,
    PRIMARY KEY (FK1, FK2)
) 

1:

ALTER TABLE table_name
ADD CONSTRAINT pk_table1 PRIMARY KEY (FK1,FK2)
+1

. , .

, :

  • . ?
  • . , , .
  • .

, (foreignkey1, foreighkey2). , . , , , .

+1

, . , , . , , , , . .

+1
source

Source: https://habr.com/ru/post/1620881/


All Articles