Introduction
AUTO_INCREMENT helps automatically give a unique number to each new row, so you don't have to type it yourself.
CREATE TABLE table_name ( id INT AUTO_INCREMENT, column_name datatype, PRIMARY KEY (id) );
CREATE TABLE users ( user_id INT AUTO_INCREMENT, username VARCHAR(50), PRIMARY KEY (user_id) );
INSERT INTO users (username) VALUES ('Alice'); INSERT INTO users (username) VALUES ('Bob');
CREATE TABLE orders ( order_number INT AUTO_INCREMENT PRIMARY KEY, order_date DATE );
CREATE TABLE books ( book_id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100) ); INSERT INTO books (title) VALUES ('Book A'); INSERT INTO books (title) VALUES ('Book B'); INSERT INTO books (title) VALUES ('Book C'); SELECT * FROM books;
AUTO_INCREMENT attribute do in a SQL table?AUTO_INCREMENT column in SQL?products with id INT AUTO_INCREMENT PRIMARY KEY and name VARCHAR(50), what will be the id values after these inserts?INSERT INTO products (name) VALUES ('Pen');
INSERT INTO products (name) VALUES ('Pencil');
INSERT INTO products (name) VALUES ('Eraser');id gets the next unique number starting from 1 by default.id values.orders with order_id INT AUTO_INCREMENT PRIMARY KEY. You try to insert a row with order_id = 0 manually, but get an error. What is the likely cause?employees has emp_id INT AUTO_INCREMENT PRIMARY KEY. After deleting the row with emp_id = 3, you insert a new row without specifying emp_id. What will be the emp_id of the new row?Pass the test and this pattern moves unknown