Go deep into Mysql, SqlServer, and Oracle primary key auto-increment settings

Source: Internet
Author: User

1. Define the primary key as the automatic growth identifier type
MySql
In mysql, if the table's primary key is set to auto_increment type, the database automatically assigns a value to the primary key. For example: Copy codeThe Code is as follows: create table customers (id int auto_increment primary key not null, name varchar (15 ));
Insert into MERs (name) values ("name1"), ("name2 ");
Select id from MERs MERS;

The preceding SQL statement first creates the MERs table, then inserts two records, and only sets the value of the name field during the insertion. Finally, query the id field in the table. The query result is:

It can be seen that once the id is setAuto_incrementType. The mysql database automatically assigns values to the primary key in ascending mode.
SQL Server
In MS SQLServer, if the table's primary key is set to the identity type, the database automatically assigns a value to the primary key. For example:

Copy codeThe Code is as follows: create table customers (id int identity (1, 1) primary key not null, name varchar (15 ));
Insert into MERs (name) values ('name1'), ('name2 ');
Select id from MERs MERS;

Note: strings in sqlserver are expanded with single quotation marks, while double quotation marks can be used in mysql.
The query result is the same as that of mysql.

It can be seen that once the id is set to the identity type, the MS SQLServer database automatically assigns a value to the primary key in ascending mode. Identity contains two parameters. The first parameter indicates the start value and the second parameter indicates the increment.
PS: 2013-6-4
In the past, we often encountered such a problem. When we delete a record with the auto-growth column 1, the record with the re-inserted auto-growth column is 2. We cannot insert a record with the auto-growth column 1. During discussions with colleagues today, we found that we can SET IDENTITY_INSERT <table_name> ON; To cancel auto-growth. We will disable this function after inserting data. The experiment is as follows:

Copy codeThe Code is as follows: use TESTDB2
-- Step 1: Create a table
Create table customers (
Id int identity primary key not null,
Name varchar (15)
);
-- Step 2: Execute the insert operation.
Insert into MERs (id, name) values (1, 'name1 ');
-- Error: An explicit value for the identity column in table 'customer' can only be specified when a column list is used and IDENTITY_INSERT is ON.
-- Step 3: Release auto-increment of primary key columns
SET IDENTITY_INSERT customers ON;
-- Step 4: insert two records with the primary keys 1 and 3. Inserted successfully
Insert into MERs (id, name) values (1, 'name1 ');
Insert into MERs (id, name) values (3, 'name1 ');
-- Step 5: Insert a record with a primary key of 2 again. Inserted successfully
Insert into MERs (id, name) values (2, 'name1 ');
-- Step 6: insert duplicate primary keys,
-- Error: Violation of primary key constraint 'pk _ customer _ 3213E83F00551192 '. Cannot insert duplicate key in object 'dbo. customer '.
Insert into MERs (id, name) values (3, 'name1 ');
-- Step 7: Disable IDENTITY_INSERT
SET IDENTITY_INSERT customers OFF;

2. Get the auto-increment Identifier from the sequence
Oracle
In Oracle, you can create a separate sequence for the primary key of each table, obtain the automatically added Identifier from the sequence, and assign it to the primary key. For example, the following statement creates a sequence named mermer_id_seq. The starting value of this sequence is 1 and the increment is 2.Copy codeThe Code is as follows: create sequence customer_id_seq increment by 2 start with 1

Once the customer_id_seq sequence is defined, the curval and nextval attributes of the sequence can be accessed.
• Curval: returns the current value of the sequence.
• Nextval: first increase the sequence value and then return the sequence value.
The following SQL statement first creates the MERs table, then inserts two records, and sets the values of the id and name fields during insertion. The value of the id field comes from the customer_id_seq sequence. Finally, query the id field in the MERs table.Copy codeThe Code is as follows: create table MERs (id int primary key not null, name varchar (15 ));
Insert into MERs values (customer_id_seq.nextval, 'name1 ');
Insert into MERs values (customer_id_seq.nextval, 'name2 ');
Select id from MERs MERS;

If the preceding statement is executed in oracle, the query result is:

Automatically add id fields through triggers
From the preceding insert statement, we can find that it is very cumbersome and troublesome to insert the customer_id_seq.nextval value every time. Therefore, you can use a trigger to complete this step.
Create trigger trg_mers MERs

Copy codeThe Code is as follows: create or replace
Trigger trg_customers before insert on customers for each row
Begin
Select CUSTOMER_ID_SEQ.nextval into: new. id from dual;
End;

Insert a recordCopy codeThe Code is as follows: insert into MERs (name) values ('test ');

We will find that this record is inserted into the database, and the id is still growing.

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.