Access to Create a Table SQL statement Create Table to set AUTOINCREMENT for Automatically increasing columns, SQL statement createtable

Source: Internet
Author: User

Access to Create a Table SQL statement Create Table to set AUTOINCREMENT for Automatically increasing columns, SQL statement createtable

Access Table creation SQL statement Create Table setting AUTOINCREMENT for Automatically increasing Columns

SQL AUTO INCREMENT Field
Uto-increment generates a unique number when the new record is inserted into the table.


Auto increment Field

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

We can create an auto-increment field in the table.


MySQL syntax

The following SQL statement defines the "P_Id" column in the "Persons" table 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))
SQL Server syntax

The following SQL statement defines the "P_Id" column in the "Persons" table 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 uses the IDENTITY keyword to execute an auto-increment task.

By default, the start value of IDENTITY is 1, and each new record increments by 1.

To specify that the "P_Id" column starts with 20 and increments by 10, change identity to IDENTITY (20, 10)


Access syntax

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

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

MS Access uses the AUTOINCREMENT keyword to execute an auto-increment task.

By default, the start value of AUTOINCREMENT is 1, and each new record increments by 1.

To specify that the "P_Id" column starts with 20 and increments by 10, change autoincrement to AUTOINCREMENT (20, 10)


Note:

1. Auto increment itself is an integer, so you do not need to set an int.
2. Automatic increment. Of course, it helps you automatically enter the content of that field, so you do not need to set it not null.
3. Automatic increment is certainly not repeated, so you should set it before setting the primary key.

The following statement fails:

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

Returned error message: syntax error in the create table statement.
It won't tell you where there is an error.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.