数据库的基本分类
关系数据库[编辑]
- MySQL
- PostgreSQL
- Microsoft Access
- Microsoft SQL Server
- Google Fusion Tables
- FileMaker
- Oracle数据库
- Sybase
- dBASE
- Clipper
- FoxPro
- foshub
几乎所有的数据库管理系统都配备了一个开放式数据库连接(ODBC)驱动程序,令各个数据库之间得以互相集成。
非关系型数据库(NoSQL)[编辑]
主条目:NoSQL
数据库连接
公司会给到一个数据库的连接信息
主机地址:rm-bp188nr95fk4l9545ao.mysql.rds.aliyuncs.com端口号:3306用户名:fanmao65密码:abc@fanmao65
选择【Mysql】

数据库查询
现有数据库表
select 查询
基本语法
select 字段名 from 表名;
查询列
查询的时候 select 后面跟上列名,如果有多个列,每个列名之间使用, 隔开;
- 查询 student表中的 name (学生姓名)
select name from student;
```sql
— 查询学生表中的所有姓名
select name from student;
— 查询学生表中的所有姓名,年龄 SELECT name,age from student;
<a name="xt06C"></a>### select * 查询所有列查询student表中所有的内容```sqlSELECT * FROM student;

练习
- 查询id,name,sex
- 查询所有的信息
-- 查询id,name,sexselect id,name,sex from student;-- 查询所有的信息SELECT * from student;
条件查询
在查询的时候可以添加对应的查询条件;
基本语法
select * from tablewhere 条件从句
- 查找所有的男生学生信息
select * from studentwhere sex="男";

where 支持条件
| 符号 | 解释 | 举例 |
|---|---|---|
| = | 表示相等 | sex=”男” |
| > | 表示大于 | age>30 |
| < | 表示小于 | age<18 |
| <= | 表示小于等于 | age<=19 |
| >= | 表示大于等于 | age>=20 |
<> 或者!= |
不等于 | name<>”张三” |
- 查找年龄(age)大于30的所有学生;
- 查找分数(score)不等于40的所有学生;
- 查找年龄(age)小于18的的学生;
- 查找分数(score)及格的学生;
- 查找分数(score)不及格的学生;
-- 查找年龄(age)大于30的所有学生;select * FROM student WHERE age>30;-- 查找分数(score)不等于40的所有学生;SELECT * from student WHERE score <> 40;-- 查找年龄(age)小于18的的学生;select * from student WHERE age < 18;-- 查找分数(score)及格的学生;select * from student WHERE score >=60;-- 查找分数(score)不及格的学生;select * from student WHERE score < 60;
and 表示并且
两个条件可以放在一起, 查询同时符合这些条件的查询;
查询age大于20的女生。
select * from student where age>20 and sex="女";
查询 age大于20 男生 score成绩及格
select * from studentwhere age>20and sex="男"and score >=60;
使用and 也可以去表示区间
-- 查询年龄在 20-30岁之间男select * from studentWHERE age>=20and age <=30and sex = "男";
and 表示并且,可以将多个条件放在一起,这些条件必须同时成立。
or 表示或者
多个条件,只要符合其中的一条就可以了。
查询年龄(age)大于30 或者 成绩(score)大于70分的同学信息;
SELECT * from studentWHERE age > 30or score > 70;
查询 年龄(age)在20-30之间 或者 成绩(score)在60-80之间的学生信息;
SELECT * from studentWHERE(age>=20 and age <=30) -- 年龄在 20-30OR(score>=60 and score<=80); -- 成绩在 60-80
between… and…
表示区间
查询20-30 岁的学生
SELECT * from studentWHERE ageBETWEEN 20 and 30;
20-30的女生学生信息;
select * from studentwhereage between 20 and 30andsex = "女";
年龄(age)在20-30之间 或者 成绩(score)在60-80之间的男学生信息;
SELECT * from studentwhere(age BETWEEN 20 and 30 OR score BETWEEN 60 and 80) -- 年龄(age)在20-30之间 或者 成绩(score)在60-80之间AND sex = "男"; -- 男学生
in 指定取值
使用方式 in (60,70,80) 只会查找 60,70 ,80 对应的数据。
查询score为 60,70,80的同学信息
select * from student where score in (60,70,80);
查询 score为 50,80,100 并且 年龄在20-30岁之间的学生信息;
select * from studentwhere score in (50,80,100)and age between 20 and 30;
not 相反
成绩不是 50,80,100 — not score in (50,80,100)
年龄不在 30-50 之间 — not age between 30 and 50
分数不在60-80 之间的学生信息
SELECT * FROM studentwhere not score BETWEEN 60 and 80
分数不在60-70 并且 年龄在20-25 之间的学生信息
SELECT * from studentWHERE score not BETWEEN 60 and 70and age BETWEEN 20 and 25;
分数不等于77分;
SELECT * from studentWHERE not score = 77;
练习

- 查询所有同学的所有信息;
- 查询所有男生同学的信息;
- 查询所有女生同学的体育信息;
- 查询所有数学课成绩大于60分同学的信息;
- 查询年龄在25岁以下的男生同学的体育成绩;
- 查询年龄在18,19,23 岁的同学信息;
SELECT * from students;SELECT * FROM students WHERE sex="男";SELECT * from studentsWHERE sex="女"AND course = "体育";SELECT * from studentsWHERE course = "数学"AND score > 60;SELECT username, age, course, sex, score from studentsWHERE age < 25and sex = "男"and course = "体育";SELECT * from studentsWHERE age in (18,19,23);
like 模糊匹配
在进行查询的时候,可以使用模糊匹配,比如 查找姓张的同学 使用 like 进行模糊匹配。like 支持两种匹配方式
- % 匹配0个或者多个字符。
- _ 匹配一个字符。
% 匹配
查询学生表中的姓张的同学
-- 查找姓张的同学SELECT * from studentWHERE name LIKE "张%";
_ 表示1个字符
查询姓张,名字为1个字。
select * from studentwhere name like "张_";
查询姓张 ```sql
— 名字为1个字符 SELECT * from student WHERE name like “张_”;
— 名字为两个字符 SELECT * from student WHERE name like “张__”;
---1. 查找姓名为3个字的信息;```sqlselect * from studentwhere name like "___";
查找姓名中包含
小的信息;SELECT * from studentWHERE name LIKE "%小%";
is Null 为空
查询score(成绩)为Null 的学生信息;
SELECT * from studentWHERE score IS NULL;

查询score 不为Null.
SELECT * from studentWHERE NOT score IS NULL;
总结
作业
连接数据库表— 【heros】
数据表中这 24 个字段(除了 id 以外),分别代表的含义见下图。
完成如下操作:
- 查询最大生命(hp_max) 在5000-8000的所有英雄;
- 查询主要定位(role_main)为
战士或者射手的英雄信息; 查询上线时间(birthdate)为2016年的英雄名字(name),英雄上线时间(birthdate),英雄最大生命(hp_max);
select name,hp_max,birthdate from heroswhere birthdate ?
查询姓名为4个字的所有英雄信息;
- 查询主要定位(role_main)为
坦克,次要定位(role_assist)为战士或者辅助的英雄信息; - 查询上线时间(birthdate) 不为Null, 英雄名字(name)为3个字英雄信息。
- 查询所有最大生命(hp_max)值在 5399 到 6811 之间的英雄。
- 查询主要定位(role_main)是
法师或者次要定位(role_assist)是射手的英雄,同时英雄的上线时间(birthdate)不在2016-01-01到2017-01-01之间(不包含Null时间)。 - 英雄中的最大的生命值(hp_max)是多少?(需要预习排序,选做)
- 上线时间(birthdate)最早的英雄姓名(需要预习排序,选做)
-- 查询最大生命(hp_max) 在5000-8000的所有英雄;select * from heros where hp_max between 5000 and 8000;-- 查询主要定位(role_main)为战士 或者 射手 的英雄信息;select * from heros where role_main="战士" or role_main="射手";select * from heros where role_main in ("战士","射手");-- 查询上线时间(birthdate)为2016年的英雄名字(name),英雄上线时间(birthdate),英雄最大生命(hp_max);select name,hp_max,birthdate from heroswhere birthdate like "2016%";-- 查询姓名为4个字的所有英雄信息;select * from heros where name like "____";-- 查询主要定位(role_main)为 坦克 ,次要定位(role_assist)为 战士 或者 辅助 的英雄信息;SELECT * FROM heros WHERE role_main="坦克" and role_assist in ("战士","辅助");-- 查询上线时间(birthdate) 不为Null, 英雄名字(name)为3个字英雄信息。SELECT * FROM heros WHERE birthdate is not NULL and name like "___";-- 查询主要定位(role_main)是法师或者次要定位(role_assist)是射手的英雄,同时英雄的上线时间(birthdate)不在 2016-01-01 到 2017-01-01 之间(不包含Null时间)。SELECT * FROM heros WHERENOT birthdate BETWEEN "2016-01-01" and "2017-01-01"AND role_main="法师" OR role_assist="射手";
-- 英雄中的最大的生命值(hp_max)是多少?SELECT MAX(hp_max) FROM heros;SELECT*FROM heros ORDER BY hp_max DESC LIMIT 1;-- 上线时间(birthdate)最早的英雄姓名1. 先找时间,最小的时间 就是最早的 。select min(birthdate) from heros;2. 根据时间找对应的人。SELECT NAME FROM heros WHERE birthdate=(SELECT MIN(birthdate) FROM heros);
