What is the best way to create an identifier for SQL insert?

What is the best DBMS-independent way to generate an identification number that will be used immediately in the INSERT statement, storing identifiers in approximately the same order?

+3
source share
11 answers

Regardless of the DBMS? This is problem. The two most common methods are auto-incrementing columns and sequences, and most DBMSs are one or the other, but not both. Thus, a database-independent way is to have another table with one column with one value that you lock, select, update and unlock.

Usually I say “to hell with DBMS independence” and do this with sequences in PostgreSQL or auto-increment columns in MySQL. For my purposes, supporting both is better than trying to find one way that works everywhere.

+10
source

If you can create a globally unique identifier (GUID) in your chosen programming language - consider this your identifier.

( where, INT), . GUID , , . GUID , .

+6

, ?

SQL? MySQL ,

SQL Server Top,

Oracle

, , .. ..

+1

, SQL ( ) : ) ; b) . , , - SQL , () ORM.

+1

. , . SQL - ( , ).

, , - SQL , .

, . , DML. DDL.

+1

, , . .

, int, , .

begin transaction
update tblSeq set intID = intID + 1
select @myID = intID from tblSeq

inset into tblData (intID, ...) values (@myID, ...)
end transaction

, tblSeq , ​​ tblData. , .

+1

.

0

, ? , UUID md5 (now()).

0

. -

DateTime.Now.Ticks

, - YYYYMMDDHHMMSSSS

0

, ORM, , . , Ruby ActiveRecord ( , - Ruby the Rails), Migrations. , - , , , , .

SQLite, MS SQL Server Oracle. , .

, , , , , - , .

0

SQL, :

  • , .
  • , .
  • id ( ) 3.1. ( ), 3.2 , (-).
  • - , id
  • , , , .
0

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


All Articles