Primary key location

Should the primary key be the first field in the database table?

Will the requests be slower if I put the identifier after the name?

CREATE TABLE users ( name TEXT NOT NULL, id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, site INTEGER NOT NULL, ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
+4
source share
2 answers

I think he will follow the previous answers:

"There can be only one AUTO_INCREMENT column in the table, it must be indexed and cannot have the DEFAULT value. The AUTO_INCREMENT column works correctly only if it contains only positive values. Inserting a negative number is considered as inserting a very large positive number. This is done to to avoid problems with accuracy when numbers are β€œwrapped” from positive to negative, and also to avoid accidentally getting an AUTO_INCREMENT column containing 0. "

Thus, there are no restrictions on the position of the primary key inside the table.

I hope for this help.

+2
source

Using / placing the primary key as the first field in the database is a general agreement, but it does not matter if you set the second field (id) as the primary key and will not make requests more slowly

+4
source

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


All Articles