一、数据库类型:
(一)关系型数据库
mysqloraclesqlserver(二)非关系型数据库
radismangodb二、mysql
1. mysql的安装(管理员模式打开cmd)
① 检查mysql安装成功:mysql -V
② mysql的安装:mysqld -install
③ 初始化账号:mysqld --initialize -insecure
④ 启动mysql:net start mysql
⑤ 打开数据库:mysql -u root -p
2. mysql的卸载(管理员模式打开cmd)
① 停止数据库:net stop mysql
② 删除数据库服务:mysql -remove
③ 删除数据库的初始化数据:data文件夹
④ 删除配置的环境变量
3. mysql客户端:cmd(黑框框)和navicat(可视化界面)
4. navicat
4.1 navicat的连接:输入ip、端口、用户名和密码
4.2 创建数据库
4.2.1 新建数据库
4.2.2 设计表:字段名、字段类型(int:整数;varchar:字符串;datatime:时间)、字段长度、是否必填、默认值、是否允许为空、注释、主键(不能重复不能为空)、索引
4.3 数据库四大操作:增删改查
5. cmd终端使用数据库
5.1 连接数据库:mysql -u root -p
5.2 断开数据库:exit
5.3 数据库操作
5.3.1 查看数据库:show databases;
5.3.2 选择数据库:use 数据库名;
5.3.3 创建数据库:create database test default charset utf8mb4;
5.3.4 修改数据库:只能修改数据库的charset,不能改数据库名字
5.3.5 删除数据库:drop database test;
5.4 表操作
5.4.1 查看数据库中的表:show tables;
5.4.2 查看表结构:desc 表名;
5.4.3 创建表:create table 表名 (字段信息)
字段信息:字段名、字段类型、字段长度、是否为空not null、主键primary key、默认值default ‘xxx’、注释comment、索引index、自动递增auto_increment
create table t_xx(
id int(16) not null primary key,
sname varchar(255) not null,
sex varchar(255) default '保密'
);
5.4.4 修改表:alter
① 增加字段:alter table 表名 add (字段信息);
② 删除字段:alter table 表名 drop 字段名;
③ 修改字段名:alter table 表名 change 老字段信息 新字段信息 类型;
④ 修改字段属性:alter table 表名 modify 字段信息;
⑤ 修改表名:alter table 旧表名 rename 新表名;
5.5 表操作:增删改查
5.5.1 查
①查表中全部信息:select * from t_student;
②查表中的部分信息:select id,name from t_student;
③判断符:> 、=、<、!=、in、is、like(常用于字符串)、between(用于数字)
④逻辑连接符:and、and not、or、or not
select * from t_student where address in ('上海','北京');
select * from t_student where phone is null;(不能用=null)
select * from t_student where sname like '王%';
select * from t_student where sname like '%王%';
select * from t_student where sname like '%王';
select * from t_student where age between 18 and 25;
⑤多表联查
select * from 表1 join 表2 on 表1.字段1=表2.字段2;
select * from t_student join t_grade on t_student.id=t_grade.sid;
select t_student.id,t_student.sname,t_grade.maths from t_student join t_grade on t_student.id=t_grade.sid;
select s.id,s.sname,g.maths from t_student s join t_grade g on s.id=g.sid; (取别名)
select s.id,s.sname,g.maths from t_student s join t_grade g on s.id=g.sid where s.age>18 and s.age<50;
select * from 表1 join 表2 on 表1.字段1=表2.字段2 join 表3 on 表1.字段3=表3.字段4;
select * from t_student join t_grade on t_student.id=t_grade.sid join t_class on t_student.cid=t_class.id;
⑥聚合函数:count、max、min、avg、sum
select count(*) from t_student;
select max(age), min(age), avg(age) from t_student;
select sum(maths) from t_grade;
⑦ group by:必须和聚合函数一起使用
select count(*) from t_student group by sex;
select max(age) from t_student group by sex;
⑧ having:对分组后的表做条件查询,类似于where,但是group by后面不能用where
select sname,count(sname) from t_student group by sname having count(sname)>1;
⑨ order by:对数据进行排序
select * from t_student order by age;
select * from t_student order by ag desc;
⑩ 限制数据的数目 limit
select * from t_student limit 1,5;
⑪ 条件判断:case when ... then ... end
select id,sname (case when age<30 then '青年' when age<60 and age>=30 then '中年' when age>60 then '老年' end) from t_student;
5.5.2 增
insert into 表名 (name,age,address) values ('王',18,'北京');
5.5.3 删
delete from 表名 where id=5;
5.5.4 改
update 表名 set teacher='李' where id=5;
5.6 事务
① 开启事务:begin;
② 确认事务:commit;确认操作
③ 回滚事务:rollback;撤回操作
小练习:
A. select count(*) from Student where name like '王%';
B. select Chinese.id from (select s.id,s.name,sc.score from sc join student s on sc.sid = s.id join courses c on sc.cid = c.id where c.name = '语文') Chinese, (select s.id,s.name,sc.score from sc join student s on sc.sid = s.id join courses c on sc.cid = c.id where c.name = '数学') Math where Chinese.name=Math.name and Chinese.score<Math.score;
C. select student.id,avg(score) from sc join student ON sc.sid = student.id join courses ON courses.id = sc.cid group by student.id having avg(score)>90
1) select avg(english) from mark;
2) select cust.Name,cust.Address,cust.Telno,mark.math from cust join mark on cust.Studentno=mark.Studentno;
3) select cust.Name,mark.computer from cust join mark on cust.Studentno=mark.Studentno order by mark.computer;
4) select cust.Name, sum(english+math+computer) s from cust join mark on cust.Studentno=mark.Studentno group by cust.Name having s>240 order by s desc;