创建
创建表
create table 表名(列名1,
列名2 数据类型2,
列名3 数据类型3,
列名4 数据类型4
);
数据类型
1、int :整数
2、double :小数类型
score double(5,2)//5位小数,小数点后面保留两位小数
3、data :日期(只包含年月日的日期)yyyy-MM-dd
4、datatime :日期(包含年月日时分秒)yyyy-MM-dd HH:mm:ss
5、timestamp: 时间戳类型(包含年月日时分秒)yyyy-MM-dd HH:mm:ss
如果不给这个字段赋值或者赋值为null,则默认使用当前的系统时间
6、varchar : 字符串类型
name varchar(20)//名字最大20个字符
创建学生表
create table student(
id int,
name varchar(32),
age int,
score double(4,1),
birthday date,
insert_time timestamp
);
查询
查询数据库中的所有表名称
show tables;
查询表结构
desc 表名;
删除
drop table if exists 表名;
修改
1、修改表名
alter table 表名 rename to 新的表名;
2、修改表的字符集
//查看表的字符集
show create table stu;
//修改表的字符集
alter table stu character set utf8;
3、添加列
alter table stu add gender varchar(10);
4、修改列的名称和类型
alter table stu change gender sex varchar(20);
//只改类型
alter table stu modify sex varchar(10);
5、删除列
alter table stu drop sex;//sex是列名
复制student 表
create table stu like student;