How to insert, update, and delete data
The syntax for the INSERT statement
INSERT INTO table-name [(column-list)] VALUES (value-list);
A statement that adds one row to a table
INSERT INTO products (categoryID, productCode, productName, listPrice)
VALUES (1, 'tele', 'Fender Telecaster', 599.00);
A statement that uses the MySQL NOW function to get the current date
INSERT INTO orders (customerID, orderDate)
VALUES (1, NOW());
The syntax for the UPDATE statement
UPDATE table-name
SET expression-1 [, expression-2] ...
WHERE selection-criteria;
A statement that updates a column in one row
UPDATE products
SET productName = 'Ludwig 5-Piece Kit with Zildjian Cymbals'
WHERE productCode = 'ludwig';
A statement that updates a column in multiple rows
UPDATE products SET listPrice = 299 WHERE categoryID = 1;
The syntax for the DELETE statement
DELETE FROM table-name
WHERE selection-criteria;
A statement that deletes one row from a table
DELETE FROM products
WHERE productID = 1;
A statement that deletes multiple rows from a table
DELETE FROM products
WHERE listPrice > 200;
Description
- Since the INSERT, UPDATE, and DELETE statements modify the data that’s stored in a database, they’re sometimes referred to as action queries. These statements don’t return a result set. Instead, they return the number of rows that were affected by the query.
Back