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.