一、创建数据库和表
#如果不存在practise这个数据库,则创建数据库并且将该数据库中的字符集##设置为utf8,如果不存在那么创建该数据库就不会报错
create database if not exists practise character set utf8;
use practise; #连接你想要;连接的数据库
select database(); #查询你当前使用的数据库是哪一个
#创建部门表
create table if not exists Deparment(
department_id char(3),
department_name char(20) not null,
note text(16),
primary key(department_id)
);
#创建员工表
create table if not exists employees(
employee_id char(6),
employee_name char(10) not null,
education char(4),
birthday date not null,
sex char(2) not null,
workyear tinyint(1),
address varchar(20),
phone_numeber char(12) not null,
departmentid char(3) not null, # 这里也可以使用外键关联起来部门和员工表,部门表和员工表是一对多的关系
primary key(employee_id)
)
#创建薪资表
create table if not exists salary(
employee_id char(6) primary key, # 员工表和薪资表是一对一的关系
income float(8),
outcome float(8)
)
二、插入、删除、更新操作
1.插入
添加employees表
insert into employees values
('000001','马龙龙','初中','1999-12-12','1',8,'宁夏','12345612','2'),
('000002','王玉瑶','高中','2002-10-10','0',5,'吉林','12345678','1'),
('000003','吴春然','本科','2000-05-04','1',6,'吉林','12345697','3'),
('000004','序号白','硕士','3000-04-05','0',2,'河南','45678912','2');
上面的插入中省略了employess的字段属性,所以它要求后面的values中的值必须与employess中的全部字段一一对应。
当然也可以使用下面的这种插入方式。使用这种方式的时候直接可以给指定的字段中插入数据,想要插入数据的字段一定要与后面的一一值对应。并且有not null的字段一定要插入数据,此字段是在数据表中一定是非空的
insert into employees(employee_id,employee_name,education,birthday,sex,workyear,address,phone_number,departmentid) values('000005','马克隆','硕士','1972-10-16','1',15,'法国','45666666','2');
添加Deparment表
insert into Deparment values
('1','外交部','负责外交'),
('2','国防部','负责军事'),
('3','后勤部','负责后勤保障');
添加salary表
insert into salary values
('000001',12000,10),
('000002',15522,100),
('000003',12000,1000),
('000004',12288,102);
2.删除
删除数据太简单,我就对上面表中的数据不删除了。下面看一下delete和truncate的区别。假如使用delete删除数据表中自增字段为id的最后一条数据,假设最后一条数据的id为10,那么要是重新添加进来一条数据,它的id一定会是11,而不是10。再比如使用下面的前两个语句直接删除整个数据表中的数据,如果再重新插入数据,那么它的自增id一定是从11开始的,之前的1~10全部删除干净。但是如果使用下面的第三个语句,那么假如再插入多个数据,它的自增键id一定是从1开始的。这就是;两者之间根本的区别
delete from employees;
delete from employees where 1=1;
truncate employees; # 清空数据表employess中的数据
3.更新
alter table employees change employ_name employee_name char(10); # 这里必须有修改字段的类型,否则会报错
对于更新表这里没有什么说的,但是要注意replace的使用方法。下面是没有使用replace之前使用insert into插入数据的结果
使用replace into Deparment values('1','外交部1','负责外交');可以很清楚地看见departmen_id为‘1’的department_name的名字已经由‘外交部’变为‘外交部1’。但是如果是departmen_id为‘上面表中不存在的,查询表中的结果又是另一种情况。
很显然可以得出如果replace 中的id在之前已经存在,那么它的作用相当于update,用于更新数据库中数据的信息。但是如果id在之前没有存在。那么它的作用相当于是insert into,也就是像数据表中插入一条数据。
三、数据库查询操作
单表查询
数据库最重要的一个功能就是查询操作。一般的单表查询太过简单就不再赘述。
#查询employees表中所有员工的姓名和性别,要求sex为0的时候显示为女,为1的时候显示为男。对于命名的时候as可以加也可以不加,不影响其别名
select employee_name 员工姓名, case
when sex='1' then '男'
when sex='0' then '女'
end
as 性别
from employees;
子查询
查询在国防部工作的员工的详细信息但是要注意包含子查询的括号一定要加,否则程序就会出错
select * from employees where departmentid=(select department_id from deparment where department_name='国防部');
连接查询
#查询每个员工的情况、以及薪水的情况
select employees.*,salary.* from employees,salary where employees.employee_id =salary.employee_id;
下面的叫显式内连接。注意外连接、显式内连接和隐式内连接的区别。外连接只满足某一个方向的条件就好。但是显式内连接和隐式内连接要两边同时满足才行
#使用显式内连接的方法查询名字为“马龙龙”的员工所在的部门
select department_name from deparment join employees on deparment.department_id=employees.departmentid where employees.employee_name='马龙龙';
#使用隐式内连接的方法查询名字为“马龙龙”的员工所在的部门还是会得到上面同样的结果
select department_name from deparment,employees where deparment.department_id=employees.departmentid and employees.employee_name='马龙龙';
分组查询
#查找每个部门有几个人
select department_name 部门,count(department_name) 每个部门人数 from deparment join employees on deparment.department_id=employees.departmentid GROUP BY department_name;
四、视图操作
#创建category和route联合查询的视图,下面的语法是正确的
CREATE view view_category_route as
select route.r_id,route.price,route.r_date,route.r_name,category.cname
from category,route
where route.category_id=category.c_id;
#这种也行
CREATE view view_category_route(route_price,route_date,route_name,category_cname) as
select route.price,route.r_date,route.r_name,category.cname
from route,category
where route.category_id=category.c_id;
drop view view_category_route;
#从数据表中选择所需要的数据
select route.price,route.r_date,route.r_name,category.cname
from route,category
where route.category_id=category.c_id;
select * from view_category_route;
要个查询出来的数据属性为了不出错最好重新命名,视图的其他增、删、查、改和操作基本表的方法是一样的
五、索引与数据完整性
六、mysql语言结构
char和varchar的区别
Date,DateTime,TimeStamp和Time的区别
float、double、decimal的区别
MySQL BIT、BINARY、VARBINARY、BLOB(二进制类型)
enum的使用
geometry数据存储和计算
year,time,date,datetime,timestamp 的区别
七、存储过程与触发器
八、备份与恢复
九、总结
show variables like 'char%'; #查询当前整个mysql数据库系统中有哪些字符编码
show variables like 'collation'; #查询整个mysql数据库系统中的排序编码有哪些