MySQL AUTO_INCREMENT

The AUTO_INCREMENT attribute is used to generate a unique number automatically when a new record is inserted into a table. This is commonly used for primary key columns.

Example with Tamil Kings

CREATE TABLE tamil_kings_auto_increment (
    id INT AUTO_INCREMENT PRIMARY KEY,
    king_name VARCHAR(100) NOT NULL,
    reign_period VARCHAR(50)
);

Code Explanation: The id column is set to AUTO_INCREMENT, meaning its value will automatically increase with each new record in the tamil_kings_auto_increment table.

Best Practices

  • Use AUTO_INCREMENT for primary keys when you need unique identifiers.
  • Ensure that the column is defined as PRIMARY KEY or has a unique index.

Key Takeaways

  • AUTO_INCREMENT is useful for generating unique IDs automatically.
  • It simplifies the insertion of records and ensures unique identification.