SQL AUTO_INCREMENT



SQL AUTO_INCREMENT

The SQL AUTO_INCREMENT attribute is used to generate a unique number automatically when a new record is inserted into a table. This command is essential for creating unique identifiers for each record in a table without manual input.


Syntax

-- To create a table with an AUTO_INCREMENT column
CREATE TABLE table_name (
    column1 datatype AUTO_INCREMENT,
    column2 datatype,
    ...
    PRIMARY KEY (column1)
);

-- To add AUTO_INCREMENT to an existing column
ALTER TABLE table_name
MODIFY COLUMN column1 datatype AUTO_INCREMENT;
  • CREATE TABLE: This is the SQL keyword used to create a new table.
  • AUTO_INCREMENT: This attribute is used to automatically generate a unique number for the column.
  • ALTER TABLE: This is the SQL keyword used to modify an existing table.
  • MODIFY COLUMN: This command is used to change the definition of an existing column.
  • column1: This specifies the name of the column to set as AUTO_INCREMENT.
  • datatype: This specifies the type of data the column can hold, such as INTEGER.
  • table_name: This specifies the name of the table to create or modify.

Example

Let's go through a complete example that includes creating a database, creating a table with an AUTO_INCREMENT column, and inserting data into the table.

Step 1: Creating a Database

This step involves creating a new database named example_db.

CREATE DATABASE example_db;

In this example, we create a database named example_db.

Step 2: Creating a Table with an AUTO_INCREMENT Column

In this step, we create a table named employees within the previously created database, defining the id column as AUTO_INCREMENT.

USE example_db;

CREATE TABLE employees (
    id INT AUTO_INCREMENT,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    hire_date DATE,
    PRIMARY KEY (id)
);

Here, we define the employees table with columns for id, first_name, last_name, email, and hire_date. The id column is set as AUTO_INCREMENT and the primary key.

Step 3: Inserting Data into the Table

This step involves inserting some sample data into the employees table.

INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('John', 'Doe', 'john.doe@example.com', '2023-01-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Jane', 'Smith', 'jane.smith@example.com', '2023-02-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Alice', 'Johnson', 'alice.johnson@example.com', '2023-03-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Bob', 'Brown', 'bob.brown@example.com', '2023-04-01');

Here, we insert four rows of data into the employees table. The id column values are automatically generated and incremented for each new record.

Step 4: Adding AUTO_INCREMENT to an Existing Column

This step involves adding the AUTO_INCREMENT attribute to an existing column named order_id in the orders table.

ALTER TABLE orders
MODIFY COLUMN order_id INT AUTO_INCREMENT;

This command adds the AUTO_INCREMENT attribute to the order_id column of the orders table, ensuring that order_id values are automatically generated and incremented for each new record.