How to insert rows
The syntax of the INSERT
statement
INSERT INTO table_name [(column_list)]
VALUES (expression_1 [, expression_2]...)[, (expression_1 [, expression_2]...)]...
The table definition
CREATE TABLE products (
productID INT NOT NULL AUTO_INCREMENT
categoryID INT NOT NULL,
productCode VARCHAR(10) NOT NULL,
productName VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
listPrice DECIMAL(10,2) NOT NULL,
discountPercent DECIMAL(10,2) NOT NULL DEFAULT 0.00,
dateAdded DATETIMENOT NULL
);
Add a single row without using a column list
INSERT INTO products
VALUES (DEFAULT, 1, 'tele', 'Fender Telecaster', 'NA', '949.99', DEFAULT, NOW());
Add a single row using a column list
INSERT INTO products (categoryID, productCode, productName, description, listPrice, dateAdded)
VALUES (1, 'tele', 'Fender Telecaster', 'NA', '949.99', NOW());
Add multiple rows
INSERT INTO categories (categoryID, categoryName)
VALUES (4, 'Keyboards'), (5, 'Brass'), (6, 'Woodwind');
Description
- You use the
INSERT
statement to add a new row to a table.
- In the
INSERT
clause, you specify the name of the table that you want to add a row to, along with an optional column list.
- If you don’t include a column list, you must specify the column values in the same order as they appear in the table, and you must code a value for each column in the table.
- If you include a column list, you must specify the column values in the same order as they appear in the column list. You can omit columns with default values and columns that accept NULL values.
- To insert a default value, you can use the
DEFAULT
keyword.
- To insert a NULL value into a column, you can use the NULL keyword.
Back