1. Create a data table
Data table: a two-dimensional table, a table is composed of multiple columns, and each type in the table is called a field of the table
stu_num char(8) not null |
stu_name varchar(20) not null |
this_gender char(2) not null |
stu_age int not null |
this_tel char(11) not null unique |
this_qq varchar(11) unique |
---|---|---|---|---|---|
Take the above student information form as an example to create a form in the MySQL database, the statement is as follows (note: the corresponding database should be selected before creation):
create table students(
stu_num char(8) not null unique,
stu_name varchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
stu_tel char(11) not null unique,
stu_qq varchar(11) unique
);
2. Query data table
Query all tables in the current database:
show tables;
Query the specified table structure in the current database:
desc 表名字;
3. Delete the data table
## 删除数据表
drop table 数据表名;
## 当数据表存在时删除数据表
drop table if exists 数据表名;
Fourth, modify the data table and fields
## 修改表名
alter table 旧表名 rename to 新表名;
## 数据表也是有字符集的,默认字符集和数据库⼀致
alter table 表名 character set utf8;
## 添加列(字段)
alter table 表名 add 字段名 varchar(200);
## 修改列(字段)的列表和类型
alter table 表名 change 旧的字段名 新字段名 字段类型;
## 只修改列(字段)类型
alter table 表名 modify 字段名 新的字段类型;
## 删除列(字段)
alter table stus drop 字段名;