Duplication / Unnecessary Data in SQL Database

When developing a new application / database for multiple users, a table for user management is usually required.

A table usually always has a username as well as an identifier ....

Just to check that I am not an idiot (well, you can still think it!) I just downloaded and looked at the scheme of both MediaWiki and PHPBB, and they do the same.

When I first learned about relational databases, I was always told that the big rule was never to duplicate data or do something unnecessary.

So why do we have an identifier as a primary key instead of a username?

I understand the reason if it was not unique (for example, a series of SO sites), but it is in these applications.

The only thing I could think of is faster Select * from xxx where ID="454"instead Select * from xxx where name="some_really_long_name"or because having long names will significantly increase the size of the database.

Are these the only reasons, or am I missing something here?

+3
source share
7 answers

Reasons to use an integer non-intelligent primary key instead of a unique text key:

  • Speed ​​JOIN and other requests.

  • The ability to change the text key value and maintain the integrity of the database without the need to update each table in which the key is indicated.

  • Improved code efficiency and reduced memory overhead when your application needs to create a list of key values ​​in memory.

  • , .

+9

, - .... , , , . .
, .
( ) , .

, , , FK . , , , , , , .

, , , , , , , .
.

( ), . , , . (SSAN - ). , , , (, , FK ), , , .

+7

... ?

, . , :

  • , .
  • : , .
  • , - . , , , 1234 100234. , ...
  • . .
  • , - ; , - , ( ), .

- , , , . , , :

  • , , , .

(AKA ) - ID # 1234 -.

, . ID 1234 , , , , . , - , , .

+4
  • /. .
  • / (, ).

PS: unique username.

+3

, Surrogate Key. , . , , , .

, , "" . , . , , - , , . ( ), .

+2

- , .

+1

One of the key benefits is having a separate identifier in which you can change the username without breaking any foreign key relationships.

In addition, good practice RDBMS has primary keys that do not matter - the username clearly makes sense

+1
source

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


All Articles