Menu

SQL AUTO INCREMENT


In SQL Auto-increment is used to generate a unique number when a new record is inserted into a table.

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

SQL INSERT INTO Syntax

CREATE TABLE table_name
(
ID int NOT NULL AUTO_INCREMENT,
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
PRIMARY KEY (ID)
);

SQL Auto Increment Example

CREATE TABLE Student
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(50),
FirstName varchar(50),
RollNo varchar(50),
City varchar(50),
PRIMARY KEY (ID)
);

When we insert a new record into the "Student" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically).








Connection failed: SQLSTATE[28000] [1045] Access denied for user 'studentdb'@'localhost' (using password: YES)

Subscribe