[MySQL] Basic operations of database tables
Reminder: The table operation here refers to the operation of the table structure, which belongs to the DDL data definition language
1. Table creation
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
field indicates the column name
datatype indicates the type of the column
character set character set, if no character set is specified, the character set of the database shall prevail
Collate validation rule, if no validation rule is specified, the validation rule of the database shall be allow
Create two tables user1 and user2. They are the same except for the storage engine. Through
the search, it is found that user1 has three files, and user2 has two files. This involves data and indexes, so I will not describe it for now.
Second, view the table structure
View all tables in the database show tables;
View the table structure desc 表名;
View the table creation information show create table user1 \G
The found table construction information is optimized by MySQL, which is somewhat different from what we wrote
3. Modify the table structure
In the actual development of the project, the structure of a certain table is often modified, such as field name, field size, field type, character set type of the table, storage engine of the table, and so on. We also have requirements, adding fields, removing fields, and so on. Then we need to modify the table
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename DROP (column);
3.1 Add fields in the table
3.2 Modify the fields in the table
The modification here is to overwrite the previous field information
3.3 Delete fields in the table
Note: Be careful when deleting a field, as the deleted field and its corresponding column data are gone
3.4 Modify the table name
to can be omitted
3.5 Modify column names
Note: new fields need to be fully defined
Fourth, delete the table
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
drop table user;
Note: delete the table with caution