建表语句
create table tb_emp(
id int primary key auto_increment comment '主键ID',
username varchar(20) not null comment '用户名',
password varchar(32) default '123456' null comment '密码',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 1 男, 2 女',
image varchar(300) null comment '图像url',
job tinyint unsigned null comment '职位, 1 班主任 , 2 讲师 , 3 学工主管, 4 教研主管',
entrydate date null comment '入职日期',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间',
constraint tb_emp_username_uindex unique (username)
) comment '员工表';
查询
当前数据库下的表
show tables;
查看指定表结构
desc tb_user;
数据库的建表语句
show create table tb_user;
修改
- 添加字段:
alter table
表名 add
字段名 类型(长度) [comment
注释] [约束]; - 修改字段类型:
alter table
表名 modify
字段名 新数据类型(长度); - 修改字段名和字段类型:
alter table
表名 change
旧字段名 新字段名 类型 (长度) [comment
注释] [约束]; - 删除字段:
alter table
表名 drop column
字段名; - 修改表名:
rename table
表名 to
新表名;
添加字段
alter table tb_emp add qq varchar(11) comment 'QQ';
修改字段类型
修改字段名和字段类型
alter table tb_emp modify qq varchar(13) comment 'QQ';
alter table tb_emp change qq qq_num varchar(13) comment 'QQ';
删除字段
alter table tb_emp drop column qq_num;
修改表名
rename table tb_emp to emp;
删除
- 删除表语法:
drop table [ if exists ] 表名;
drop table if exists emp;