一、数据库练习
1、登陆数据库
①代码
mysql -uroot -p'密码'
②结果
2、创建数据库zoo
①代码
create database zoo;
②结果
3、修改数据库zoo字符集为gbk
①代码
#查看字符集类型
show variables like 'character%';
#修改字符集
alter database zoo character set gbk;
②结果
4、选择当前数据库为zoo
①代码
use zoo;
②结果
5、查看创建数据库zoo信息
①代码
show create database zoo;
②结果
6、删除数据库zoo
①代码
drop database if exists zoo;
②结果
二、创建表练习
1、创建一个名称为db_system的数据库
①代码
create database db_system;
②结果
2、在该数据库下创建两张表,具体要求如下
员工表 user
字段 类型 约束 备注
id 整形 主键,自增长 id
NAME 字符型 非空 姓名
gender 字符 非空 性别
birthday 日期型 生日
entry_date 日期型 非空 入职时间
job 字符型 非空 职位员工绩效表 salary
字段 类型 约束 备注
id 整形 主键,自增长 id
userId 字符型 非空,外键,关联的是user表的id字段 用户id
baseSalary 小数 非空 基本工资
month 整数 非空 月份
allowances 小数 非空,默认为0 补贴
①员工表代码
create table user (
id int auto_increment primary key,
name varchar(11) not null comment '姓名',
gender char(1) not null comment '性别',
birthday date comment '生日',
entry_date date not null comment '入职时间',
job varchar(255) not null comment '职位'
);
②结果
③员工绩效表代码
create table salary (
id int primary key auto_increment,
userld int not null,
baseSalary decimal(10,2) not null comment '基本工资',
month int not null comment '月份',
allowances decimal(10,2) not null default 0 comment'补贴',
foreign key (userld) references user(id) on delete cascade
);
②结果
三、修改表练习
1、在上面员工表的基本上增加一个image列,类型是blob,长度255
①代码
alter table user add column image blob comment'员工照片';
②结果
2、修改job列,使其长度为60
①代码
alter table user modify column job varchar(60) comment '职位';
②结果
3、删除gender列
①代码
alter table user drop column gender;
②结果
4、表名salary改为usersalary
①代码
rename table salary to usersalary;
②结果
5、修改表的字符集为utf8
①代码
alter table usersalary convert to character set utf8 collate utf8_general_ci;
②结果
6、列名name修改为username
①代码
alter table user change name username varchar(255) comment '用户名';
②结果