|
5 | 5 | 1. 数据持久化。
|
6 | 6 | 2. 数据库发展史。
|
7 | 7 | 3. 关系型数据库特点。
|
| 8 | + - 理论基础:集合论和关系代数。 |
| 9 | + - 具体表象:用二维表(有行和列)组织数据。 |
| 10 | + - 编程语言:结构化查询语言(SQL)。 |
8 | 11 | 4. E-R图。
|
9 | 12 | 5. 关系型数据库产品。
|
| 13 | + - Oracle |
| 14 | + - DB2 |
| 15 | + - SQL Server |
| 16 | + - MySQL |
| 17 | + - SQLite |
10 | 18 |
|
11 | 19 | ### MySQL简介
|
12 | 20 |
|
|
16 | 24 | ### SQL详解
|
17 | 25 |
|
18 | 26 | 1. DDL
|
| 27 | + |
| 28 | + ```SQL |
| 29 | + |
| 30 | + -- 如果存在SRS数据库就删除 |
| 31 | + drop database if exists SRS; |
| 32 | + |
| 33 | + -- 创建学生选课系统数据库SRS并指定默认字符集为UTF8 |
| 34 | + create database SRS default charset utf8; |
| 35 | + |
| 36 | + -- 切换至SRS数据库 |
| 37 | + use SRS; |
| 38 | + |
| 39 | + -- 查看当前数据库中所有表 |
| 40 | + show tables; |
| 41 | + |
| 42 | + -- 创建学生表TbStudent |
| 43 | + create table TbStudent |
| 44 | + ( |
| 45 | + stuid integer not null, |
| 46 | + stuname varchar(20) not null, |
| 47 | + stusex bit default 1, |
| 48 | + stubirth datetime not null, |
| 49 | + stutel char(11), |
| 50 | + stuaddr varchar(255), |
| 51 | + stuphoto longblob, |
| 52 | + primary key (stuid) |
| 53 | + ); |
| 54 | + |
| 55 | + -- 修改学生表删除stutel列 |
| 56 | + alter table TbStudent drop column stutel; |
| 57 | + |
| 58 | + -- 查看学生表结构 |
| 59 | + desc TbStudent; |
| 60 | + |
| 61 | + -- 如果表TbCourse已经存在就删除它 |
| 62 | + drop table if exists TbCourse; |
| 63 | + |
| 64 | + -- 创建课程表TbCourse |
| 65 | + create table TbCourse |
| 66 | + ( |
| 67 | + cosid integer not null, |
| 68 | + cosname varchar(50) not null, |
| 69 | + coscredit tinyint not null, |
| 70 | + cosintro varchar(255) |
| 71 | + ); |
| 72 | + |
| 73 | + -- 给课程表设置主键约束 |
| 74 | + alter table TbCourse add constraint pk_course primary key (cosid); |
| 75 | + |
| 76 | + -- 创建学生选课记录表TbSC |
| 77 | + create table TbSC |
| 78 | + ( |
| 79 | + scid integer primary key auto_increment, |
| 80 | + sid integer not null, |
| 81 | + cid integer, |
| 82 | + scdate datetime not null, |
| 83 | + score float |
| 84 | + ); |
| 85 | + |
| 86 | + -- 给表TbSC添加外键约束 |
| 87 | + alter table TbSC add constraint fk_sid foreign key (sid) references TbStudent (stuid) on delete cascade on update cascade; |
| 88 | + alter table TbSC add constraint fk_cid foreign key (cid) references TBCourse (cosid); |
| 89 | + ``` |
| 90 | + |
19 | 91 | 2. DML
|
| 92 | + |
| 93 | + ```SQL |
| 94 | + |
| 95 | + -- 添加学生记录 |
| 96 | + insert into TbStudent values (1001, '张三丰', default, '1978-1-1', '成都市一环路西二段17号', null); |
| 97 | + insert into TbStudent (stuid, stuname, stubirth) values (1002, '郭靖', '1980-2-2'); |
| 98 | + insert into TbStudent (stuid, stuname, stusex, stubirth, stuaddr) values (1003, '黄蓉', 0, '1982-3-3', '成都市二环路南四段123号'); |
| 99 | + insert into TbStudent values (1004, '张无忌', 1, '1990-4-4', null, null); |
| 100 | + insert into TbStudent values |
| 101 | + (1005, '丘处机', 1, '1983-5-5', '北京市海淀区宝盛北里西区28号', null), |
| 102 | + (1006, '王处一', 1, '1985-6-6', '深圳市宝安区宝安大道5010号', null), |
| 103 | + (1007, '刘处玄', 1, '1987-7-7', '郑州市金水区纬五路21号', null), |
| 104 | + (1008, '孙不二', 0, '1989-8-8', '武汉市光谷大道61号', null), |
| 105 | + (1009, '平一指', 1, '1992-9-9', '西安市雁塔区高新六路52号', null), |
| 106 | + (1010, '老不死', 1, '1993-10-10', '广州市天河区元岗路310号', null), |
| 107 | + (1011, '王大锤', 0, '1994-11-11', null, null), |
| 108 | + (1012, '隔壁老王', 1, '1995-12-12', null, null), |
| 109 | + (1013, '郭啸天', 1, '1977-10-25', null, null); |
| 110 | + |
| 111 | + -- 删除学生记录 |
| 112 | + delete from TbStudent where stuid=1004; |
| 113 | + |
| 114 | + -- 更新学生记录 |
| 115 | + update TbStudent set stubirth='1980-12-12', stuaddr='上海市宝山区同济支路199号' where stuid=1002; |
| 116 | + |
| 117 | + -- 添加课程记录 |
| 118 | + insert into TbCourse values |
| 119 | + (1111, 'C语言程序设计', 3, '大神级讲师授课需要抢座'), |
| 120 | + (2222, 'Java程序设计', 3, null), |
| 121 | + (3333, '数据库概论', 2, null), |
| 122 | + (4444, '操作系统原理', 4, null); |
| 123 | + |
| 124 | + -- 添加学生选课记录 |
| 125 | + insert into TbSC values |
| 126 | + (default, 1001, 1111, '2016-9-1', 95), |
| 127 | + (default, 1002, 1111, '2016-9-1', 94), |
| 128 | + (default, 1001, 2222, now(), null), |
| 129 | + (default, 1001, 3333, '2017-3-1', 85), |
| 130 | + (default, 1001, 4444, now(), null), |
| 131 | + (default, 1002, 4444, now(), null), |
| 132 | + (default, 1003, 2222, now(), null), |
| 133 | + (default, 1003, 3333, now(), null), |
| 134 | + (default, 1005, 2222, now(), null), |
| 135 | + (default, 1006, 1111, now(), null), |
| 136 | + (default, 1006, 2222, '2017-3-1', 80), |
| 137 | + (default, 1006, 3333, now(), null), |
| 138 | + (default, 1006, 4444, now(), null), |
| 139 | + (default, 1007, 1111, '2016-9-1', null), |
| 140 | + (default, 1007, 3333, now(), null), |
| 141 | + (default, 1007, 4444, now(), null), |
| 142 | + (default, 1008, 2222, now(), null), |
| 143 | + (default, 1010, 1111, now(), null); |
| 144 | + ``` |
| 145 | + |
20 | 146 | 3. DQL
|
21 | 147 |
|
| 148 | + ```SQL |
| 149 | + |
| 150 | + -- 查询所有学生信息 |
| 151 | + select * from TbStudent; |
| 152 | + |
| 153 | + -- 查询所有课程名称及学分(投影和别名) |
| 154 | + select cosname as `课程名称`, coscredit as `学分` from TbCourse; |
| 155 | + |
| 156 | + -- 查询所有女学生的姓名和出生日期(筛选) |
| 157 | + select stuname, stubirth from TbStudent where stusex=0; |
| 158 | + |
| 159 | + -- 查询所有80后学生的姓名、性别和出生日期(筛选) |
| 160 | + select stuname as `姓名`, if(stusex, '男', '女') as `性别`, stubirth as `出生日期` from TbStudent where stubirth between '1980-1-1' and '1989-12-31'; |
| 161 | + |
| 162 | + -- 查询姓王的学生姓名和性别(模糊) |
| 163 | + select stuname, stusex from TbStudent where stuname like '王%'; |
| 164 | + |
| 165 | + -- 查询姓郭名字总共两个字的学生的姓名(模糊) |
| 166 | + select stuname from TbStudent where stuname like '郭_'; |
| 167 | + |
| 168 | + -- 查询姓郭名字总共三个字的学生的姓名(模糊) |
| 169 | + select stuname from TbStudent where stuname like '郭__'; |
| 170 | + |
| 171 | + -- 查询名字中有王字的学生的姓名(模糊) |
| 172 | + select stuname from TbStudent where stuname like '%王%'; |
| 173 | + |
| 174 | + -- 查询没有录入家庭住址和照片的学生姓名(多条件筛选和空值处理) |
| 175 | + select stuname from TbStudent where stuaddr is null and stuphoto is null; |
| 176 | + |
| 177 | + -- 查询学生选课的所有日期(去重) |
| 178 | + select distinct scdate from TbSC; |
| 179 | + |
| 180 | + -- 查询学生的姓名和生日按年龄从大到小排列(排序) |
| 181 | + select stuname, stubirth from TbStudent order by stubirth; |
| 182 | + |
| 183 | + -- 查询所有录入了家庭住址的男学生的姓名、出生日期和家庭住址按年龄从小到大排列(多条件筛选和排序) |
| 184 | + select stuname, stubirth, stuaddr from TbStudent where stusex=1 and stuaddr is not null order by stubirth desc; |
| 185 | + |
| 186 | + -- 查询年龄最大的学生的出生日期(聚合函数) |
| 187 | + select min(stubirth) from TbStudent; |
| 188 | + |
| 189 | + -- 查询年龄最小的学生的出生日期(聚合函数) |
| 190 | + select max(stubirth) from TbStudent; |
| 191 | + |
| 192 | + -- 查询男女学生的人数(分组和聚合函数) |
| 193 | + select if(stusex, '男', '女') as `性别`, count(stusex) as `人数` from TbStudent group by stusex; |
| 194 | + |
| 195 | + -- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数) |
| 196 | + select avg(score) as `平均成绩` from TbSC where cid=1111; |
| 197 | + |
| 198 | + -- 查询学号为1001的学生所有课程的总成绩(筛选和聚合函数) |
| 199 | + select sum(score) as `总成绩` from TbSC where sid=1001; |
| 200 | + |
| 201 | + -- 查询每个学生的学号和平均成绩, null值处理成0(分组和聚合函数) |
| 202 | + select sid as `学号`, ifnull(avg(score), 0) as `平均成绩` from TbSC group by sid; |
| 203 | + |
| 204 | + -- 查询平均成绩大于等于90分的学生的学号和平均成绩 |
| 205 | + select sid as `学号`, avg(score) as `平均成绩` from TbSC group by sid having avg(score)>=90; |
| 206 | + |
| 207 | + -- 查询年龄最大的学生的姓名(子查询) |
| 208 | + select stuname from TbStudent where stubirth=(select min(stubirth) from TbStudent); |
| 209 | + |
| 210 | + -- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算) |
| 211 | + select stuname from TbStudent where stuid in |
| 212 | + (select sid from TbSC group by sid having count(sid)>2); |
| 213 | + |
| 214 | + -- 查询选课学生的姓名和平均成绩(子查询和连接查询) |
| 215 | + -- 写法1: |
| 216 | + select stuname, avgscore from TbStudent t1 inner join |
| 217 | + (select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2 on t1.stuid=t2.sid; |
| 218 | + -- 写法2: |
| 219 | + select stuname, avgscore from TbStudent t1, |
| 220 | + (select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2 where t1.stuid=t2.sid; |
| 221 | + |
| 222 | + -- 查询学生姓名、所选课程名称和成绩(连接查询) |
| 223 | + -- 写法1: |
| 224 | + select stuname, cosname, score from |
| 225 | + TbStudent t1, TbCourse t2, TbSC t3 |
| 226 | + where t1.stuid=t3.sid and t2.cosid=t3.cid and t3.score is not null; |
| 227 | + -- 写法2: |
| 228 | + select stuname, cosname, score from TbStudent t1 inner join TbCourse t2 |
| 229 | + inner join (select sid, cid, score from TbSC where score is not null) t3 |
| 230 | + on t1.stuid=t3.sid and t2.cosid=t3.cid; |
| 231 | + |
| 232 | + -- 查询每个学生的姓名和选课数量(左外连接和子查询) |
| 233 | + select stuname as `姓名`, ifnull(coscount, 0) as `选课数` from TbStudent t1 |
| 234 | + left outer join (select sid, count(sid) as coscount from TbSC group by sid) t2 on t1.stuid=t2.sid; |
| 235 | + ``` |
| 236 | + |
| 237 | +4. DCL |
| 238 | + |
| 239 | + ```SQL |
| 240 | + |
| 241 | + -- 创建名为hellokitty的用户 |
| 242 | + create user 'hellokitty'@'localhost' identified by '123123'; |
| 243 | + |
| 244 | + -- 将对SRS数据库所有对象的所有操作权限授予hellokitty |
| 245 | + grant all privileges on SRS.* to 'hellokitty'@'localhost'; |
| 246 | + |
| 247 | + -- 召回hellokitty对SRS数据库所有对象的insert/delete/update权限 |
| 248 | + revoke insert, delete, update on SRS.* from 'hellokitty'@'localhost'; |
| 249 | + ``` |
| 250 | + |
22 | 251 | ### Python数据库编程
|
23 | 252 |
|
24 | 253 | 1. MySQLdb
|
25 | 254 | 2. PyMySQL
|
26 | 255 |
|
27 |
| -### ORM概述 |
28 |
| - |
29 | 256 |
|
0 commit comments