Introduction
AUTO_INCREMENT helps automatically create unique numbers for new rows, so you don't have to type them yourself.
CREATE TABLE table_name ( id INT AUTO_INCREMENT PRIMARY KEY, column_name datatype );
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_id INT AUTO_INCREMENT PRIMARY KEY, product_name VARCHAR(100) );
CREATE TABLE products ( product_id INT AUTO_INCREMENT PRIMARY KEY, product_name VARCHAR(50) ); INSERT INTO products (product_name) VALUES ('Pen'); INSERT INTO products (product_name) VALUES ('Notebook'); INSERT INTO products (product_name) VALUES ('Eraser'); SELECT * FROM products;
AUTO_INCREMENT attribute do in a MySQL table?AUTO_INCREMENT column in MySQL?products created as:CREATE TABLE products (product_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
INSERT INTO products (name) VALUES ('Pen');
INSERT INTO products (name) VALUES ('Pencil');
INSERT INTO products (name) VALUES ('Eraser');product_id of the 'Eraser' row?product_id gets the next number starting from 1.CREATE TABLE orders (
order_id INT AUTO_INCREMENT,
product VARCHAR(50),
PRIMARY KEY (order_id)
);
employees has an AUTO_INCREMENT primary key emp_id. After inserting 10 rows, you delete the row with emp_id = 10. What happens when you insert a new row without specifying emp_id?Pass the test and this pattern moves unknown