The Oracle database uses that primary key generation strategy

Source: Internet
Author: User
Tags db2 generator uuid

The Oracle database uses that primary key generation strategy

1. Automatic Growth identity:

For MySQL, DB2, MS SQL Server, a database-generated primary key for generating unique identities for long, short, int types

Using the self-added fields of SQL Server and MySQL, this method cannot be placed in Oracle, and Oracle does not support the sequence field, which is used in MySQL and SQL Server.

The syntax in the database is as follows:

View Plaincopy to Clipboardprint?

01.mysql:create tablet_user (id int auto_increment PRIMARY key, name varchar (20));

02.SQL server:create tablet_user (id int identity (1,1) primary key, name varchar (20));

03.

04.<id name= "id" column= "id" type= "long" >

<generator class= "Identity"/>

06.</id>

Mysql:create tablet_user (id int auto_increment PRIMARY key, name varchar (20));

SQL server:create tablet_user (id int identity (1,1) primary key, name varchar (20));

<id name= "id" column= "id" type= "long" >

<generator class= "Identity"/>

</id>

2, Sequence:

A sequence supported by DB2 and Oracle to generate a unique identifier for a long, short, or int

The syntax in the database is as follows:

View Plaincopy to Clipboardprint?

01.oracle:create sequenceseq_name Increment by 1-start with 1;

Oracle:create sequenceseq_name Increment by 1-start with 1;

You can call Seq_name.nextval or seq_name.curval when you need a primary key value, and the database will help us maintain the sequence sequence, guaranteeing that the value of each fetch is unique, such as:

View Plaincopy to Clipboardprint?

01.insert into Tbl_name (ID, name) VALUES (seq_name.nextval, ' Jimliu ');

02.

03.<id name= "id" column= "id" type= "long" >

<generator class= "Sequence" >

<param name= "sequence" >seq_name</param>

</generator>

07.</id>

INSERT into Tbl_name (ID, name) VALUES (seq_name.nextval, ' Jimliu ');

<id name= "id" column= "id" type= "long" >

<generator class= "Sequence" >

<param name= "sequence" >seq_name</param>

</generator>

</id>

If we do not specify the sequence parameter, then hibernate accesses a default sequence, which is hibernate_sequence, and we also need to build this in the database sequence

In addition, sequence can also have another parameter is paramters, you can view the Hibernate API to understand its usage, see Org.hibernate.id.SequenceGenerator

Call the database sequence to generate the primary key, set the sequence name, or hibernate cannot find:

View Plaincopy to Clipboardprint?

01.<param name= "sequence" >NAME_SEQ</param> (very common in Oracle)

<param name= "sequence" >NAME_SEQ</param> (very common in Oracle)

3, Hilo:

A long, short, or int type of identifier generated using a high/low algorithm, given a table and field as the source of the high value, the default table is Hibernate_unique_key, and the default field is Next_hi. It divides the source of the ID into two parts, db+ the memory, and then produces the ID value together according to the algorithm, can produce many records in the few connection times, enhances the efficiency

View Plaincopy to Clipboardprint?

01.mysql:create tablehi_value (Next_hi integer NOT NULL);

Mysql:create table Hi_value (Next_hiinteger not NULL);

View Plaincopy to Clipboardprint?

01.insert into Hi_value (Next_hi) values (1);

02.

03.<id name= "id" column= "id" >

<generator class= "Hilo" >

<param name= "Table" >hi_value</param>

<param name= "column" >next_hi</param>

<param name= "Max_lo" >100</param>

</generator>

09.</id>

Insert into Hi_value (Next_hi) values (1);

<id name= "id" column= "id" >

<generator class= "Hilo" >

<param name= "Table" >hi_value</param>

<param name= "column" >next_hi</param>

<param name= "Max_lo" >100</param>

</generator>

</id>

When hibernate is persisted, the hibernate is responsible for generating the low value. The Hilo identifier Builder needs to remove the current value of Next_hi from the Hi_value table when it generates an identifier, and then modify the value, which is done in a separate transaction. The maximum low value is configured in attribute Max_lo, but when the low value generated in hibernate memory exceeds this value, there is a need to read the high value again in the Hi_value table of the database.

Use the Hilo build policy to create an extra table in the database, with the default table name Hibernate_unique_key, the default field of type Integer, and the name Next_hi (less)

We can also set our own custom table names and field names.

View Plaincopy to Clipboardprint?

01.<id name= "id" type= "integer" >

<column name= "id"/>

<generator class= "Hilo" >

<param name= "My_unique_key"/>

<param column= "Next_hi"/>

</generator>

07.</id>

<id name= "id" type= "integer" >

<column name= "id"/>

<generator class= "Hilo" >

<param name= "My_unique_key"/>

<param column= "Next_hi"/>

</generator>

</id>

4, native:

Depending on the capabilities of the underlying database, choose one from identity, sequence, Hilo, and more flexibility, but at this point, if you select sequence or Hilo, all of the table's primary keys will be taken from the hibernate default sequence or Hilo table. Also, some databases are not very efficient in support of default primary key generation tests

For Oracle using the Sequence method, for MySQL and SQL Server using identity (the generation of the main key generating mechanism), native is the primary key generation work to the database to complete, hibernate regardless (very often)

View Plaincopy to Clipboardprint?

01.<id name= "id" column= "id" >

<generator class= "native"/>

03.</id>

<id name= "id" column= "id" >

<generator class= "native"/>

</id>

5, Seqhilo:

The combination of sequence and Hilo, Hilo High is generated by sequence, so it also needs the support of the underlying database

Implemented through the Hilo algorithm, but the primary key history is stored in Sequence and is suitable for databases that support Sequence, such as Oracle (less)

View Plaincopy to Clipboardprint?

01.<id name= "id" column= "id" >

<generator class= "Seqhilo" >

<param name= "sequence" >seq_name</param>

<param name= "Max_lo" >100</param>

</generator>

06.</id>

<id name= "id" column= "id" >

<generator class= "Seqhilo" >

<param name= "sequence" >seq_name</param>

<param name= "Max_lo" >100</param>

</generator>

</id>

6, Increment:

This is generated by hibernate in memory the primary key, each increment is 1, does not depend on the underlying database, so all the database can be used, but the problem also comes with, because it is hibernate generated, so only

Can have a hibernate application process access to the database, or it will create a primary key conflict, can not be used in the cluster when inserting data hibernate will add an additional primary key to the primary key, but a hibernate instance will maintain a counter, So you can't use this method when multiple instances are running

View Plaincopy to Clipboardprint?

01.<id name= "id" column= "id" >

<generator class= "Increment"/>

03.</id>

<id name= "id" column= "id" >

<generator class= "Increment"/>

</id>

7, Uuid.hex:

Generates a string-type identifier using a 128-bit UUID algorithm, which is encoded as a string of 32-bit 16-digit digits. The UUID contains: IP address, JVM startup time, System time (accurate to 1/4 seconds), and a

Counter values (unique in the JVM)

Hibernate will figure out a 128-bit unique value insertion

View Plaincopy to Clipboardprint?

01.<id name= "id" column= "id" >

<generator class= "Uuid.hex"/>

03.</id>

<id name= "id" column= "id" >

<generator class= "Uuid.hex"/>

</id>

Uuid.string

Hibernate will figure out a 16-bit value to insert

8, Assigned:

The application is responsible for generating the primary key identifier, often using the primary key associated with the business in the database without a proxy primary key, such as:

View Plaincopy to Clipboardprint?

01.<id name= "id" column= "id" type= "string" >

<generator class= "Assigned"/>

03.</id>

<id name= "id" column= "id" type= "string" >

<generator class= "Assigned"/>

</id>

This type of primary key generation is not recommended, in the database table design should use the proxy primary key (surrogate key), should not use the natural primary key (natural key has business meaning), in the absence of the designation <generator> label,

The default is the way assigned primary keys are generated

When inserting data, the primary key is added by the user himself, and hibernate doesn't care

9, Foreign

Use fields from external tables as primary keys

10, select

Using triggers to generate primary keys (primarily for early database primary key generation mechanisms, less)

In addition, some tables do not have proxy primary keys, the use of joint primary keys, multiple fields unique, which is also not recommended table design,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.