Complete the code to create a table with an auto-incrementing primary key.
CREATE TABLE users (id INT [1] PRIMARY KEY, name VARCHAR(100));
The AUTO_INCREMENT keyword makes the id column automatically increase by 1 for each new row.
Complete the code to insert a new user without specifying the auto-increment column.
INSERT INTO users (name) VALUES ([1]);When inserting, you do not specify the id column; the database assigns it automatically. Here, we insert the name 'Alice'.
Fix the error in the insert statement to let auto-increment work correctly.
INSERT INTO users (id, name) VALUES ([1], 'Bob');
To let MySQL auto-generate the id, insert NULL or omit the column. Using NULL tells MySQL to assign the next auto-increment value.
Fill both blanks to reset the auto-increment counter to 1 for the table.
ALTER TABLE users [1] = [2];
To reset the auto-increment counter, use ALTER TABLE users AUTO_INCREMENT = 1;. This sets the next auto-increment value to 1.
Fill all three blanks to create a table with an auto-increment column, insert a row, and select the auto-generated id.
CREATE TABLE products (product_id INT [1] PRIMARY KEY, name VARCHAR(50)); INSERT INTO products (name) VALUES ([2]); SELECT [3] FROM products WHERE name = 'Table';
First, create the table with AUTO_INCREMENT on product_id. Then insert a product named 'Table'. Finally, select the product_id for that product.