Operations on tables in the database (create tables, modify tables)

1. Create a table

create table table_name(
name1 type1,
name2 type2,
name3 type3,
)character set character set collate check rule engine storage engine

Description:
1.name indicates the column name
2.type indicates the type of the column
3.charater set character set, if not specified, the character set of the database where it is located shall prevail.
4.collate validation rules, if not specified, the validation rules of the database where it is located shall prevail.
5. Storage engines include MyISAM and InnoDB.
write picture description here
View the table structure
desc table_name;
write picture description here
among them, Field (attribute name), Type (field type), NuLL (whether it is allowed to be empty), Key (index type), Default (default value), Extra (expansion).

Different storage engines create different files.
write picture description here
When the storage engine is MyISAM, such as table tt1, there are three files (.frm table structure, .MYD table data, .MYI table structure), if InnoDB, there are files (.frm, .ibd)

2. Modify the table

1. Add a field to the table.
alter table tt1 add age int;
alter table tt1 add sex int comment 'image path' after name;
write picture description here
2. Modify field size
alter table tt1 modify name varchar(10);
write picture description here
3. Delete column
alter table tt1 drop sex;
write picture description here
4. Modify table Name
Note that the name of the library cannot be directly modified, only backup first, and then delete.
alter table tt1 rename to tt2;
write picture description here
5. Modify the character set
alter table tt2 charset=gbk;
write picture description here
6. Modify the field name
alter table tt2 change name xingming varchar(20);
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643414&siteId=291194637