MySQL

Chapter 5 - DDL (Data Definition Language)

AUTO INCREMENT

The AUTO_INCREMENT attribute in MySQL is used to automatically generate a unique value for a column, typically used for primary keys. This feature is useful for creating sequential numbers for records, ensuring that each new record gets a unique identifier without manual input. The AUTO_INCREMENT attribute simplifies the insertion process and helps avoid duplicate primary key values. Below is a guide on using the AUTO_INCREMENT attribute in MySQL, with code samples for tables related to a company database, including company, employees, departments, and branches.

Understanding the AUTO_INCREMENT Attribute

  1. Purpose
    • The AUTO_INCREMENT attribute automatically increments the value of a column with each new row inserted.
    • Ensures unique values for the column, which is especially useful for primary keys.
  2. Syntax
    • To define an AUTO_INCREMENT column, use COLUMN_NAME DATA_TYPE AUTO_INCREMENT in the CREATE TABLE statement.
    • The AUTO_INCREMENT column must be defined as a key column, typically a PRIMARY KEY.

Code Samples

  1. Creating the company Table with AUTO_INCREMENT
    CREATE TABLE company (
      company_id INT AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(100) NOT NULL,
      established YEAR NOT NULL,
      revenue DECIMAL(15, 2) DEFAULT 0.00
    );
    • company_id: Unique identifier for each company, automatically incremented with each new record.
    • name: Name of the company (cannot be NULL).
    • established: Year the company was established (cannot be NULL).
    • revenue: Annual revenue (defaults to 0.00 if not specified).
  2. Creating the employees Table with AUTO_INCREMENT
    CREATE TABLE employees (
      employee_id INT AUTO_INCREMENT PRIMARY KEY,
      first_name VARCHAR(50) NOT NULL,
      last_name VARCHAR(50) NOT NULL,
      email VARCHAR(100) UNIQUE NOT NULL,
      salary DECIMAL(10, 2) DEFAULT 30000.00,
      hire_date DATE DEFAULT CURDATE(),
      department_id INT DEFAULT NULL,
      status VARCHAR(10) DEFAULT 'Active'
    );
    • employee_id: Unique identifier for each employee, automatically incremented with each new record.
    • first_name and last_name: Required fields (cannot be NULL).
    • email: Employee’s email address (must be unique; no two employees can have the same email).
    • salary: Salary of the employee (defaults to 30,000.00 if not specified).
    • hire_date: Date the employee was hired (defaults to the current date if not specified).
    • department_id: Reference to the department (can be NULL).
    • status: Employment status (defaults to 'Active').
  3. Creating the departments Table with AUTO_INCREMENT
    CREATE TABLE departments (
      department_id INT AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(100) NOT NULL,
      budget DECIMAL(12, 2) DEFAULT 100000.00,
      num_employees INT DEFAULT 0
    );
    • department_id: Unique identifier for each department, automatically incremented with each new record.
    • name: Name of the department (cannot be NULL).
    • budget: Budget allocated to the department (defaults to 100,000.00 if not specified).
    • num_employees: Number of employees in the department (defaults to 0 if not specified).
  4. Creating the branches Table with AUTO_INCREMENT
    CREATE TABLE branches (
      branch_id INT AUTO_INCREMENT PRIMARY KEY,
      address VARCHAR(255) NOT NULL,
      city VARCHAR(100) NOT NULL,
      state VARCHAR(100) NOT NULL,
      postal_code VARCHAR(20) UNIQUE NOT NULL,
      opening_hours VARCHAR(50) DEFAULT '9am-5pm'
    );
    • branch_id: Unique identifier for each branch, automatically incremented with each new record.
    • address, city, and state: Required fields (cannot be NULL).
    • postal_code: Postal code (must be unique; no two branches can have the same postal code).
    • opening_hours: Description of the branch's opening hours (defaults to '9am-5pm').

Important Considerations

  • Initial Value: You can specify the starting value for AUTO_INCREMENT using the AUTO_INCREMENT table option.
  • ALTER TABLE table_name AUTO_INCREMENT = starting_value;
  • Unique Constraint: The column with AUTO_INCREMENT must be defined as a key (usually a PRIMARY KEY or UNIQUE).
  • Resetting: You can reset the AUTO_INCREMENT value by altering the table or truncating it.

By using the AUTO_INCREMENT attribute as shown in these examples, beginners can easily manage unique identifiers in their MySQL tables, simplifying the process of data entry and maintaining data integrity.

Tansy SQL Course | AUTO INCREMENT | Chapter 5 | Lesson 10 - Video Thumbnail
Comments(0 comments)

Comments Not Found