[MySQL] Detailed operation of tables in the database

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
insert image description here
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.
insert image description here

Second, view the table structure

View all tables in the database show tables;
insert image description here
View the table structure desc 表名;
insert image description here
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
insert image description here

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

insert image description here

3.2 Modify the fields in the table

insert image description here
The modification here is to overwrite the previous field information

3.3 Delete fields in the table

insert image description here
Note: Be careful when deleting a field, as the deleted field and its corresponding column data are gone

3.4 Modify the table name

insert image description here
to can be omitted

3.5 Modify column names

Note: new fields need to be fully defined
insert image description here

Fourth, delete the table

DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...

drop table user;

Note: delete the table with caution

Guess you like

Origin blog.csdn.net/m0_54469145/article/details/131132116