workbench
关系模型:表格
- 选择数据类型的时候,要根据业务规则选择合适的类型。通常来说,BIGINT能满足整数存储的需求,==VARCHAR(N)==能满足字符串存储的需求,这两种类型是使用最广泛的。
- 主流的数据库
- SQL具有的基本能力:
- 数据定义
- 数据操作(增添,修改,删除)
- 数据查询
- “NULL”表示不存在,不是0或者空字符串
- 某个字段唯一区分出不同的记录,这个字段被称为主键, 主键的值不可能重复,是唯一标识。
- 选取主键的一个基本原则是:不使用任何业务相关的字段作为主键。身份证号、手机号、邮箱地址这些看上去可以唯一的字段,均不可用作主键。因为一旦这些规则变化了,后果很复杂。
查询
- 虽然SELECT可以用作计算,但它并不是SQL的强项。但是,不带FROM子句的SELECT语句有一个有用的用途,就是用来判断当前到数据库的连接是否有效。许多检测工具会执行一条SELECT 1,来测试数据库连接。
#
select 1
select * from students
select 100+200
- where 查询限制
SELECT * FROM students WHERE score >= 80 AND gender = 'M';
SELECT * FROM students WHERE score >= 80 OR gender = 'M';
SELECT * FROM students WHERE NOT class_id = 2;
SELECT * FROM students WHERE class_id <> 2;
SELECT * FROM students WHERE name LIKE 'ab%' # %ab%
SELECT * FROM students WHERE score >= 80 AND score<=90;
SELECT * FROM students WHERE score BETWEEN 80 AND 90;
SELECT * FROM students WHERE score IN (10,20,30)
- select * 是查询, select col1,col2…这种是投影查询
可以选完后取别名: SELECT 列1 别名1, 列2 别名2, 列3 别名3 FROM …。
SELECT id, score, name FROM students;
SELECT id 'xixixi', score '哈哈哈', name 'lala' FROM students WHERE gender = 'M';
- 排序
SELECT id, name, gender, score FROM students ORDER BY score DESC;
SELECT id, name, gender, score FROM students ORDER BY score DESC, gender;
- 分页查询
使用LIMIT OFFSET 可以对结果集进行分页,每次查询返回结果集的一部分;
分页查询需要先确定每页的数量和当前页数,然后确定LIMIT和OFFSET的值
SELECT id, name, gender, score
FROM students
ORDER BY score DESC
LIMIT 3 OFFSET 1;
- 聚合函数
- 注意:聚合查询的列中,只能放入分组的列。
- mysql中,当我们用到聚合函数,如sum,count后,又需要筛选条件时,having就派上用场了,因为WHERE是在聚合前筛选记录的,having和group by是组合着用的
SELECT COUNT(*) boys FROM students WHERE gender = 'M';
SELECT AVG(score) average FROM students WHERE gender = 'M';
SELECT MAX(score) average FROM students WHERE gender = 'M'
SELECT MIN(score) average FROM students WHERE gender = 'M'
SELECT SUM(score) average FROM students WHERE gender = 'M'
#分组聚合
SELECT COUNT(*) num FROM students GROUP BY class_id;
SELECT class_id, COUNT(*) num FROM students GROUP BY class_id;
SELECT class_id,gender,avg(score) from students group by class_id,gender;
select cid,count(id) nums from xzyd_question group by cid HAVING nums>2
- 多表查询
SELECT
students.id sid,
students.name,
students.gender,
students.score,
classes.id cid,
classes.name cname
FROM students, classes;
SELECT
s.id sid,
s.name,
s.gender,
s.score,
c.id cid,
c.name cname
FROM students s, classes c
WHERE s.gender = 'M' AND c.id = 1;
- 连接查询
INNER JOIN
只返回同时存在于两张表的行数据RIGHT OUTER JOIN
返回右表都存在的行LEFT OUTER JOIN
则返回左表都存在的行FULL OUTER JOIN
小结:
JOIN查询需要先确定主表,然后把另一个表的数据“附加”到结果集上;
INNER JOIN是最常用的一种JOIN查询,它的语法是SELECT … FROM <表1> INNER JOIN <表2> ON
<条件…>;
JOIN查询仍然可以使用WHERE
条件和ORDER BY
排序
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
INNER JOIN classes c
ON s.class_id = c.id;
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
RIGHT OUTER JOIN classes c
ON s.class_id = c.id;
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
LEFT OUTER JOIN classes c
ON s.class_id = c.id;
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
FULL OUTER JOIN classes c
ON s.class_id = c.id;
插入
INSERT INTO students (class_id, name, gender, score) VALUES
(1, '大宝', 'M', 87),
(2, '二宝', 'M', 81);
删除
DELETE FROM students WHERE id>=5 AND id<=7;
更新
UPDATE students SET score=score+10 WHERE score<80;