17. Insert data
1. Data Insertion
Keywords: INSERT
Insertion can be used in several ways:
- insert complete row
- insert part of a row
- insert multiple lines;
- Insert the results of some queries
2. Insert a complete line
requires specifying the table name and the value to be inserted into the new row
Example: INSERT INTO customers
VALUES(NULL,
'Pep E. LaPew',
‘100 Main Street’,
'The Angels',
'THAT',
‘90046’,
‘USA’,
NULL,
NULL);
This example inserts a new customer into the table customers. A value must be provided for each column, if a column has no value, NULL is used instead. This syntax is simple but unsafe because it is highly dependent on the order in which the columns in the table are defined, and also on readily available information about their order.
A safer way to write an INSERT statement is as follows:
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_eamil)
VALUES(‘Pep E. LaPew’,
‘100 Main Street’,
'The Angels',
'THAT',
‘90046’,
‘USA’,
NULL,
NULL);
VALUES must match the specified column names in their specified order, not necessarily the order in which the columns appear in the actual table
3. Insert multiple rows
Multiple INSERT statements can be used, even committing them all at once, ending each statement with a semicolon
例如:INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
VALUES(‘Pep E. LaPew’,
‘100 Main Street’,
'The Angels',
'THAT',
‘90046’,
‘USA’);
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
VALUES(‘M. Martian’,
’42 Galaxy Way’,
‘New York’,
'NEW',
‘11213’,
‘USA’);
Alternatively, as long as the column names (and order) are the same in each INSERT statement, the statements can be combined as follows:
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
VLAUES(
'Pep E. LaPew',
‘100 Main Street’,
'The Angels',
'THAT',
‘90046’,
‘USA’),
(
‘M. Martian’,
’42 Galaxy Way’,
‘New York’,
'NEW',
‘11213’,
‘USA’);
4. Insert the retrieved data
Use INSERT to insert the results of a SELECT statement into a table. This is the INSERT SELECT statement
For example: INSERT INTO customers(cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
SELECT cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
FROM custnew;
The SELECT statement in INSERT SELECT can contain a WHERE clause to filter the inserted data.
18. Update and delete data
1. Update data
keyword: UPDATE
There are two ways to use UODATE:
- Specific row in the update table;
- Update all rows in the table.
The basic UODATE statement consists of three parts:
- table to update
- column names and their new values;
- Determine filter criteria for rows to update
Example: Update the email address of customer 10005
UPDATE customers
SET cust_email=’[email protected]’
WHERE cust_id = 10005;
UPDATE statements always start with the name of the table to be updated. The SET command is used to assign new values to the updated columns. The UPDATE statement ends with a WHERE clause, which tells MySQL which row to update.
Only a single SET command is required when updating multiple columns, with each "column=value" pair separated by a comma (no comma after the last column)
Example: Update email and name of customer 10005
UPDATE customers
SET cust_email = ‘[email protected]’,
Cust_name = ‘The Fudds’
WHERE cust_id = 10005;
2. Delete data
keyword: DELETE
DELETE is used in two ways:
- delete a specific row from the table;
- delete all rows from the table
For example: delete a row from the customers table:
DELETE FROM customers
WHERE cust_id = 10006;
If the WHERE clause is omitted, it will delete every customer in the table
3. Guidelines for updating and deleting
- Never use UPDATE and DELETE statements without a WHERE clause unless you really intend to update and delete every row
- Guarantee that every table has a primary key, use it like a WHERE clause whenever possible
- Before using a WHERE clause on an UPDATE or DELETE statement, you should test with SELECT to ensure that it filters the correct records, in case the WHERE clause is written incorrectly
- Use a database that enforces referential integrity
19. Creating and manipulating tables
1. Create a table
There are generally two ways to create a table:
- Use tools with interactive table creation and management
- Tables can also be manipulated directly with MySQL statements
Keywords: CREATE TABLE
To create a table with CREATE TABLE, the following information must be given:
- The name of the new table, given after the keyword CREATE TABLE.
- Table column names and definitions, separated by commas
For example: to create the customers table:
CREATE TABLE customers
(
cust_id int NOT NULL AUTO_INCREMENT,
cust_name char(50) NOT NULL,
cust_address char(50) NULL,
cust_city char(50) NULL,
cust_state char(5) NULL,
cust_zip char(10) NULL,
cust_country char(50) NULL,
cust_contact char(50) NULL,
cust_email char(255) NULL,
PRIMARY_KEY (cust_id)
) ENGINE=InnoDB;
A NULL value is no value or a missing value. Columns that allow NULL values also allow inserting rows without giving that column's value. A column that does not allow NULL values does not accept rows with no value for that column.
Primary keys must be unique. If multiple columns are used as primary keys, each column name should be given as a comma-separated list:
PRIMARY_KEY(order_num, order_item);
The role of AUTO_INCREMENT: AUTO_INCREMENT tells MySQL that this column is automatically incremented whenever a row is added. Each time an INSERT operation is performed, MySQL automatically increments the column, giving the column the next available value. Only one AUTO_INCREMENT column is allowed per table, and it must be indexed.
If no value is given when inserting a row, MySQL allows specifying a default value to be used at that time. The default value is specified with the DEFAULT keyword in the column definition of the CREATE TABLE statement:
quantity int NOT NULL DEFAULT 1,
2. Update table
Keywords: ALTER TABLE
Instructions:
- after ALTER TABLE give the name of the table to be changed
- List of changes made.
For example: add a column to a table
ALTER TABLE vendors
ADD vend_phone CHAR(20);
To remove the column you just added, you can do this:
ALTER TABLE vendors
DROP COLUMN vend_phone;
3. Delete table
Dropping a table (deleting the entire table instead of its contents) is as easy as using the DROP TABLE statement
Example: DROP TABLE customers;
4. Rename the table
Keywords: RENAME TABLE
例如:RENAME TABLE backup_customers TO customers,
backup_vendors TO vendors,
backup_products TO products;