SQL-W3School- High: SQL AUTO INCREMENT field

ylbtech-SQL-W3School- High: SQL AUTO INCREMENT field

 

1. Back to top
1、

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.

The syntax for MySQL

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

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".

2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
1、
2、
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/11827301.html