There are three types of database table operations, create, change and delete
First, create a table CREATE TABLE
1. There are generally two methods for creating tables: using interactive creation and management tools, and using MySQL statement manipulation
2. Use the MySQL statement to create the table:
CREATE TABLE IF NOT EXISTS customer
(
cust_id int(11) NOT NULL AUTO_INCREMENT,
cust_name char(255) NOT NULL DEFAULT 1,
cust_email char(255) NULL,
PRIMARY KEY (cust_id) USING BTREE
)ENGINE = InnoDB CHARSET=utf8;;
As you can see from the above, the table name immediately follows the CREATE TABLE keyword. IF NOT EXISTS is created if this table does not exist. CHARSET is to set the encoding format of the table
The definition of each column starts with the column name, followed by the data type of the column, the primary key of the table can be specified with the keyword PRIMARY KEY,
NULL value : NULL value means no value. Columns that allow NULL values are also allowed not to give the value of the column during insertion. Each column is either NULL or NOT NULL
Primary key : PRIMARY KEY specifies the primary key. The primary key value must be unique. If it is the primary key of multiple columns, their combination must be unique. The primary key can only use columns that are not NULL
Self-increment : The meaning of AUTO_INCREMENT is that it is automatically incremented whenever a row is added.
Specify the default value : Use the keyword DEFULT to specify, if you use the default value, then no value is given when you insert the row, MySQL will specify a default value
Engine type : ENGINE specifies the engine type. MySQL has many engines. The engine's role is to process your SQL statements.
- InnoDB is a reliable transaction processing engine
- MyISAM is an extremely high-performance engine that supports full-text search, but does not support transaction processing
Second, update the table ALTER TABLE
Update table operation: 1. Give the name of the table to be changed after ALTER TABLE 2. Change list
Add a column
ALTER TABLE customer
ADD cust_phone char(20)
Delete the column just now
ALTER TABLE customer
DROP COLUMN cust_phone
Define the foreign key:
ALTER TABLE customer
ADD CONSTRAINT fk_customer_orders
FOREIGN KEY (order_id) REFERENCES orders(order_num)
Third, delete the table DROP TABLE
Deleting a table deletes the entire table instead of deleting all its rows
DROP TABLE customer
Fourth, rename the table RENAME TABLE
RENAME TABLE customers TO customers1