SQL_STUDY: 12.SQL AUTO INCREMENT field

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/NumberOneStudent/article/details/102732534

Abstract:
Use auto-increment key data of the various languages

SQL AUTO INCREMENT field

Auto-increment will generate a new record is inserted into the unique number table.

AUTO INCREMENT field

We usually expect each time a new record is inserted, automatically create the value of the primary key field.

We can create an auto-increment field in the table.
MySQL syntax for
the following SQL statement to "Persons" table "P_Id" column is defined as auto-increment primary keys:

CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment task.

By default, the starting value AUTO_INCREMENT is 1, each new record is incremented by one.

Let the AUTO_INCREMENT sequence start with another value, use the following SQL syntax:

ALTER TABLE Persons AUTO_INCREMENT=100

To insert a new record in the "Persons" table, we do not have (automatically adds a unique value) is "P_Id" column Rating:

INSERT INTO Persons (FirstName,LastName)
VALUES ('Bill','Gates')

The SQL statement above would insert a new record in the "Persons" table. "P_Id" will be assigned a unique value. "FirstName" is set to "Bill", "LastName" column will be set to "Gates".


The syntax for SQL Server

The following SQL statement "Persons" table "P_Id" column is defined as the auto-increment primary key:

CREATE TABLE Persons
(
P_Id int PRIMARY KEY IDENTITY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

MS SQL to perform tasks using the auto-increment IDENTITY keyword.

By default, the starting value IDENTITY is 1, each new record is incremented by one.

To specify "P_Id" column should start at value 10 and increment, please change the identity IDENTITY (20,10)

To insert a new record in the "Persons" table, we do not have (automatically adds a unique value) is "P_Id" column Rating:

INSERT INTO Persons (FirstName,LastName)
VALUES ('Bill','Gates')

The SQL statement above would insert a new record in the "Persons" table. "P_Id" will be assigned a unique value. "FirstName" is set to "Bill", "LastName" column will be set to "Gates".


The syntax for Access

The following SQL statement "Persons" table "P_Id" column is defined as the auto-increment primary key:

CREATE TABLE Persons
(
P_Id int PRIMARY KEY AUTOINCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

MS Access using AUTOINCREMENT keyword to perform an auto-increment task.

By default, the starting value for AUTOINCREMENT is 1, each new record is incremented by one.

To specify "P_Id" column should start at value 10 and increment, please change the autoincrement AUTOINCREMENT (20,10)

To insert a new record in the "Persons" table, we do not have (automatically adds a unique value) is "P_Id" column Rating:

INSERT INTO Persons (FirstName,LastName)
VALUES ('Bill','Gates')

The SQL statement above would insert a new record in the "Persons" table. "P_Id" will be assigned a unique value. "FirstName" is set to "Bill", "LastName" column will be set to "Gates".


The syntax for Oracle

In Oracle, the code bit more complicated.

You must create auto-increment field (the object to generate a sequence of numbers) by sequence.

Please use the following CREATE SEQUENCE syntax:

CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10

The above code creates an object called seq_person sequence, which is incremented by 1 to 1 and at the start. The object cache 10 in order to improve the performance values. CACHE option provides faster access to the number of sequence values ​​to be stored.

To insert a new record in the "Persons" table, we must use nextval function (this function retrieves the next value from seq_person sequence):

INSERT INTO Persons (P_Id,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen')

The SQL statement above would insert a new record in the "Persons" table. "P_Id" assignment is the next number in sequence from seq_person. "FirstName" is set to "Bill", "LastName" column will be set to "Gates".

Guess you like

Origin blog.csdn.net/NumberOneStudent/article/details/102732534