Settings for automatic growth of primary keys in Mysql, SqlServer, and Oracle

Source: Internet
Author: User


Mysql, SqlServer, and Oracle primary key auto-increment settings 1. Define the primary key as auto-increment identifier type in mysql. If the primary key of the table is set to auto_increment type, the database automatically assigns a value to the primary key. Example: 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 customers table, then inserts two records, and only sets the value of the name field during insertion. Finally, query the id field in the table. The query result is www.2cto.com id12.
It can be seen that once the id is set to auto_increment type, mysql database automatically assigns a value to the primary key in ascending mode. 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: create table customers (id int identity (1, 1) primary key not null, name varchar (15); insert into customers (name) values ("name1 "), ("name2"); select id from MERs MERS; 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. 2. Get the auto-increment Identifier from the sequence. in Oracle, you can create a separate sequence for the primary key of each table, and then obtain the auto-increment Identifier from the sequence, 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.
Create sequence customer_id_seq increment by 2 start with 1 once 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 www.2cto.com nextval: First adds the value of the sequence, and then returns the value of the sequence. The following SQL statement first creates the MERs table and then inserts two records, the id and name fields are set during insertion. The id field value comes from the customer_id_seq sequence. Finally, query the id field in the MERs table. Create table MERs (id int primary key not null, name varchar (15); insert into customers values (customer_id_seq.curval, "name1"), (customer_id_seq.nextval, "name2 "); select id from MERs MERS; if the preceding statement is executed in oracle, the query result is: id13

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.