MySQL
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
- Purpose
- The
AUTO_INCREMENTattribute 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.
- The
- Syntax
- To define an
AUTO_INCREMENTcolumn, useCOLUMN_NAME DATA_TYPE AUTO_INCREMENTin theCREATE TABLEstatement. - The
AUTO_INCREMENTcolumn must be defined as a key column, typically aPRIMARY KEY.
- To define an
Code Samples
- Creating the
companyTable with AUTO_INCREMENTCREATE 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 beNULL).established: Year the company was established (cannot beNULL).revenue: Annual revenue (defaults to 0.00 if not specified).
- Creating the
employeesTable with AUTO_INCREMENTCREATE 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_nameandlast_name: Required fields (cannot beNULL).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 beNULL).status: Employment status (defaults to 'Active').
- Creating the
departmentsTable with AUTO_INCREMENTCREATE 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 beNULL).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).
- Creating the
branchesTable with AUTO_INCREMENTCREATE 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, andstate: Required fields (cannot beNULL).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 forAUTO_INCREMENTusing theAUTO_INCREMENTtable option.ALTER TABLE table_name AUTO_INCREMENT = starting_value;Unique Constraint: The column withAUTO_INCREMENTmust be defined as a key (usually aPRIMARY KEYorUNIQUE).Resetting: You can reset theAUTO_INCREMENTvalue 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.
To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found