diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230220 \347\254\254\344\270\211\346\254\241\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" "b/38 \351\273\204\346\255\243\347\204\225/20230220 \347\254\254\344\270\211\346\254\241\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" index 98755750ed48a1b1c2d36776c314c215ddda7ff1..6a22b268939f5af21c8f85a5bb15cf6475efbacc 100644 --- "a/38 \351\273\204\346\255\243\347\204\225/20230220 \347\254\254\344\270\211\346\254\241\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" +++ "b/38 \351\273\204\346\255\243\347\204\225/20230220 \347\254\254\344\270\211\346\254\241\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" @@ -302,6 +302,67 @@ alter table 表名称 modify 字段名 数据类型 auto_increment;#给这个字 alter table 表名称 modify 字段名 数据类型; #去掉auto_increment相当于删除 ``` +## 外键约束foreign key + +### 作用 + +限定某个表的某个字段的引用完整性, + +比如:员工表的员工所在部门的选择,必须在部门表能找到对应的部分。 + +### 主表和从表/父表和子表 + +主表(父表):被引用的表,被参考的表 + +从表(子表):引用别人的表,参考别人的表 + +例如:员工表的员工所在部门这个字段的值要参考部门表, + +​ 部门表是主表,员工表是从表。 + +### 特点 + +(1)在“从表”中指定外键约束,并且一个表可以建立多个外键约束 + +(2)创建(create)表时就指定外键约束的话,先创建主表,再创建从表 + +(3)删表时,先删从表(或先删除外键约束),再删除主表 + +(4)从表的外键列,必须引用/参考主表的键列(主键或唯一键) + +为什么?因为被依赖/被参考的值必须是唯一的 + +(5)从表的外键列的数据类型,要与主表被参考/被引用的列的数据类型一致,并且逻辑意义一致。 + +例如:都是表示部门编号,都是int类型。 + +(6)外键列也会自动建立索引(根据外键查询效率很高,很多) + +(7)外键约束的删除,所以不会自动删除,如果要删除对应的索引,必须手动删除 + +建表时 + +```mysql +create table 主表名称( + 字段1 数据类型 primary key, + 字段2 数据类型 +); + +create table 从表名称( + 字段1 数据类型 primary key, + 字段2 数据类型, + foreign key (从表的某个字段) references 主表名(被参考字段:一定是主键) +); +#(从表的某个字段)的数据类型必须与主表名(被参考字段)的数据类型一致,逻辑意义也一样 +#(从表的某个字段)的字段名可以与主表名(被参考字段)的字段名一样,也可以不一样 +``` + +建表后 + +```mysql +alter table 从表名称 add foreign key (从表的字段) references 主表(被引用字段) 【on update xx】【on delete xx】; +``` + # 作业 diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230222 \347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" "b/38 \351\273\204\346\255\243\347\204\225/20230222 \347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..a94540579089e529f3358eacf20e16a6c1487b58 --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230222 \347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" @@ -0,0 +1,331 @@ +## DQL + +SELECT语句是用于查看计算结果、或者查看从数据表中筛选出的数据的。 + +SELECT语句的基本语法: + +```mysql +SELECT 常量; +SELECT 表达式; +SELECT 函数; + +例如: +SELECT 1; +SELECT 9/2; +SELECT NOW(); +``` + +要从数据表中筛选数据 + +```mysql +需要加FROM子句。FROM指定数据来源。字段列表筛选列。 +SELECT 字段列表 FROM 表名称; +需要加FROM和WHERE子句。WHERE筛选行。 +SELECT 字段列表 FROM 表名称 WHERE 条件; +``` + +## 别名 + +在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称,便于当前select语句的编写和理解。这个临时名称称为别名。 + +```mysql +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +``` + +- 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 + +- ==**表的别名不能加双引号**==,表的别名中间不能包含空格。 + +- as大小写都可以,as也完全可以省略。 + + ## 去重 + + mysql可以在查询结果中使用distinct关键字去重。 + + ```mysql + select distinct 字段列表 from 表名称 【where 条件】; + select distinct did from t_employee; + ``` + + ## 运算符 + +### 算数运算符 + +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod + +mysql中没有 +=等运算符 + +### 比较运算符 + +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null +判断不是null is not null + +### 区间或集合范围比较运算符 + +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 + +### 模糊匹配比较运算符 + +%:代表任意个字符 + +_:代表一个字符,如果两个下划线代表两个字符 + +```mysql +例如: +#查询名字中包含'冰'字 +select * from t_employee where ename like '%冰%'; + +#查询名字以‘雷'结尾的 +select * from t_employee where ename like '%远'; + +#查询名字以’李'开头 +select * from t_employee where ename like '李%'; + +#查询名字有冰这个字,但是冰的前面只能有1个字 +select * from t_employee where ename like '_冰%'; +``` + +### 逻辑运算符 + +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor + +例如: + +```mysql +#查询薪资高于15000,并且性别是男的员工 +select * from t_employee where salary>15000 and gender='男'; +select * from t_employee where salary>15000 && gender='男'; + +select * from t_employee where salary>15000 & gender='男';#错误得不到值 &按位与 +select * from t_employee where (salary>15000) & (gender='男'); + +#查询薪资高于15000,或者did为1的员工 +select * from t_employee where salary>15000 or did = 1; +select * from t_employee where salary>15000 || did = 1; + +#查询薪资不在[15000,20000]范围的 +select * from t_employee where salary not between 15000 and 20000; +select * from t_employee where !(salary between 15000 and 20000); + +#查询薪资高于15000,或者did为1的员工,两者只能满足其一 +select * from t_employee where salary>15000 xor did = 1; +select * from t_employee where (salary>15000) ^ (did = 1); +``` + +### 关于null值的问题 + +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 + +## 练习 + +select语句练习 + +```mysql +use zuoye; +drop table if exists user_profile; +CREATE TABLE `` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +select * from user_profile; +## (1)题目:从用户信息表中取出学校的去重数据 +select distinct university from user_profile; +-- (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' +select device_id user_infos_example from user_profile; +-- (3)题目:查询university是北京大学的设备ID +select device_id,university from user_profile where university = '北京大学'; +-- (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 +select device_id,gender,age,university from user_profile where age > 24; +-- (5)题目:查询所有用户的设备id、性别、年龄、学校 +select device_id,gender,age,university from user_profile; +-- 题目:查询所有用户的数据 +select * from user_profile; +-- (7)题目:查询省份是"shanghai"的用户信息 +select * from user_profile where province = 'shanghai'; +-- (8)题目:查询所有男性用户的设备ID、年龄、学校 +select device_id,age,university from user_profile where gender = 'male'; +-- (9)题目:从用户信息表中取出省份的去重数据 +select distinct province from user_profile; +``` + +运算符练习 + +```mysql +use zuoye; +drop table if exists user_profile1; +CREATE TABLE `user_profile1` ( +`id` int, +`` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); +INSERT INTO user_profile1 VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile1 VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile1 VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile1 VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile1 VALUES(5,5432,'male',25,'山东大学','Shandong'); +-- (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 +select device_id,gender,age from user_profile1 where age between 20 and 23; +-- (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 +select device_id,gender,age,university from user_profile1 where !(university = '复旦大学'); +-- (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 +select device_id,gender,age,university from user_profile1 where age is not null; +drop table if exists user_profile2; +CREATE TABLE `user_profile2` ( +`id` int , +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`gpa` float, +`active_days_within_30` float, +`question_cnt` float, +`answer_cnt` float +); +INSERT INTO user_profile2 VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile2 VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile2 VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile2 VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile2 VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile2 VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +-- 解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份),gpa(平均成绩)、active_days_within_30(30天内活跃天数)、question_cnt(发帖数量)、answer_cnt(回答数量) +-- +-- 例如:第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4,在过去的30天里面活跃了7天,发帖数量为2,回答数量为12 +-- (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile2 where gpa > 3.5; +-- (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile2 where university = '北京大学' or gpa > 3.7; +-- (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile2 where university in('北京大学','复旦大学','山东大学'); +-- (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 +select * from user_profile2 where gpa > 3.5 and university = '山东大学' or gpa > 3.8 and university = '复旦大学'; +-- (8)题目:所有大学中带有北京的用户信息 +select * from user_profile2 where university like '%北京%'; + +``` + + + +## 作业 + +第一题 + +```mysql +DROP DATABASE zouye; +create database zouye CHARSET utf8; +USE zouye; +show DATABASES; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); +#添加信息 +INSERT INTO employee VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +-- **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary between 12000 and 13000; +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,name,addr from employee where name like '刘%'; +-- **要求3:**将“李四”的家庭住址改为“广东韶关” +update employee set addr = '广东韶关' where name = '李四'; +select * from employee; +-- **要求4:**查询出名字中带“小”的员工 +select * from employee where name like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where salary > 11000 and sex = '男'; +-- **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null; +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +-- select * from employee where salary >12000 or sex '男'and addr = '广东深圳' or addr = '广东广州' ; +SELECT * FROM employee WHERE salary>12000 || addr in('广东深圳','广东广州')and sex = '男'; +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name 姓名,salary 年薪 from employee; +``` + +第二题 + +```mysql +create database zuoye charset utf8; +use zuoye; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000);、 +select * from countries_info; +-- **要求1:** 查询大国 的国家名称、人口和面积。 +select name 国家,population 人口,area 面积 from countries_info where area >=3000000 || population >=25000000; +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +-- +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from countries_info where continent = 'Asia'; +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where area <10000 and population <100000; +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where name like '%o%'; +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp > 10000000000; +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select name 国家, population 人口,gdp GDP值,gdp/population 人均贡献GDP值 from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select name 国家,continent 国家所属地,population 人口,gdp GDP值,gdp/population 人均贡献GDP值 from countries_info where gdp/population < 1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name 国家,area 面积,population 人口,area/population 人均国土面积值 from countries_info; +``` +