From 9b76629851c7f94fa046fe90dd1a8f24764b1fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Mon, 20 Feb 2023 15:53:04 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- .../2023MySQL\345\237\272\347\241\200.md" | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 "01\351\231\210\346\242\246\346\242\246/2023MySQL\345\237\272\347\241\200.md" diff --git "a/01\351\231\210\346\242\246\346\242\246/2023MySQL\345\237\272\347\241\200.md" "b/01\351\231\210\346\242\246\346\242\246/2023MySQL\345\237\272\347\241\200.md" new file mode 100644 index 0000000..4fb3c46 --- /dev/null +++ "b/01\351\231\210\346\242\246\346\242\246/2023MySQL\345\237\272\347\241\200.md" @@ -0,0 +1,188 @@ +# 1库。 + +## 数据库操作 + +1.创建库 + +~~~mysql +create database 【if not exists (若不存在则创建,存在则跳过)】 数据库名称; +~~~ + +2.查看库 + +~~~mysql +show databases; +#查看库,当要查看另外一个库里的表格 +#show tables from 表格名称; +~~~ + + + +3.查看创建文件某个数据库的sql语句(就是看一下他是怎么定义的) + +~~~mysql +show create database 数据库名称; +~~~ + +4.使用库 + +~~~mysql +use 数据库名称; +~~~ + +5.修改库(不能修改名称。只能修改编码个校对规则) + +~~~mysql +alter database 数据库名称 charcater set 新的编码 collate 新校对规则; +~~~ + +6.删除库 + +~~~mysql +drop database 【if exists (若存在则删除,不在则跳过)】数据库名称; +~~~ + + + +# 2.表 + +#### 表和字段不能单独存放,他们必须一起出现 + +~~~mysql +#使用库(建表之前一定要先用库) +#1.创建表 +create table 表名( +字段1 数据类型, +字段2 数据类型, +字段3 数据类型 +......最后一个字段结尾不用, +); +#2.查看当前数据库的某个表的结构 +desc 表名; +例如:desc fans; +#3.查看某个数据库的某个表格的结构 +desc 数据库 表名; +例如:decs mxdx.student; +#4.修改表 +修改名 +例如:alter table fans rename to fensi; +#5删表 +drop tbale if exists stu; + +~~~ + +#### 222修改表 + +~~~mysql +修改表的字符集和校对规则 +alter table 表名称 charset=新字符 collate=新校对规则; +增加一个字段 +ALTER RABLE 表名称 add [column] 字段名 数据类型; +删除一个字段 +alter tbale 表名称 drop colimun 字段名; +修改一个字段名称 +alter tbale 表名称 change 旧字段名称 新的字段名称 数据类型; +修改一个字段的数据类型 +alter table 表名称 modify 字段名称 新数据类型; +修改字段的位置和顺序 +alter table 表名称 modify 字段名称 数据类型 after 另一个字段;(移到xxx后这个位置) +alter tbale 表名称 mofidy 字段名称 数据类型 first; +~~~ + +### 作业 + +~~~mysql +#1、创建数据库test01_market +create database if not EXISTS test01_market default charset utf8; +use test01_market; +#2、创建表格customers +create table customers( + c_num int(11), + c_name varchar(50), + c_contact varchar(50), + c_city varchar(50), + c_birth date +); + +#| 字段名 | 数据类型 | +#| --------- | ----------- | +#| c_num | int(11) | +#| c_name | varchar(50) | +#| c_contact | varchar(50) | +#| c_city | varchar(50) | +#| c_birth | date | + +#**要求3:**将c_contact字段移动到c_birth字段后面 +alter table customers MODIFY c_contact varchar(50) after c_birth; + +#**要求4:**将c_name字段数据类型改为 varchar(70) +alter table customers modify c_name varchar(70); + +#**要求5:**将c_contact字段改名为c_phone +alter table customers change c_contact c_phone; + +#**要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +alter table customers add c_gender char(1) after c_name; + +#**要求7:**将表名改为customers_info +alter table customers rename to customers_info; + +#**要求8:**删除字段c_city +alter table customers_info drop c_city; +~~~ + +~~~mysql +#1、创建数据库test02_library +create database if not exists test02_library default charset utf8; +use test02_library; +#2、创建表格books +create table books( + b_id int(11) comment'书编号', + b_name varchar(50) comment'书名', + authors varchar(100) comment'作者', + price float comment'价格', + pubdate year comment'出版日期', + note varchar(100)comment'说明', + num int(11) comment'库存' +); + +#| 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +#| ------- | -------- | ------------- | -------- | ---- | +#| b_id | 书编号 | int(11) | 否 | 是 | +#| b_name | 书名 | varchar(50) | 否 | 否 | +#| authors | 作者 | varchar(100) | 否 | 否 | +#| price | 价格 | float | 否 | 否 | +#| pubdate | 出版日期 | year | 否 | 否 | +#| note | 说明 | varchar(100) | 是 | 否 | +#| num | 库存 | int(11) | 否 | 否 | + +#3、向books表中插入记录 +insert into books (b_id,b_name,authors,price,pubdate,note,num) values (1,'Tal of AAA','Dickes',23,1995,'novel',11); + +#1) 指定所有字段名称插入第一条记录 +insert into books values (2,'EmmaT','Jane lura',35,1993,'joke',22); + +#2)不指定字段名称插入第二记录 +insert into books values (3,'Story of Jane','Jane Tim',40,2001,'nove',10), + +#3)同时插入多条记录(剩下的所有记录) +insert into books values (4,'Lovey DayGeorge','Byron',20,2005,'novel',30),(5,'Old land','Honore Blade',30,2010,'law',0),(5,'Old land','Honore Blade',30,2010,'law',0),(7,'Rose Hood','Richard haggard',28,2008,'cartoon',28); + +#| b_id | b_name | authors | price | pubdate | note | num | +#| ---- | ------------- | --------------- | ----- | ------- | -------- | ---- | +#| 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +#| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +#| 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +#| 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +#| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +#| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +#| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | + +#4、将小说类型(novel)的书的价格都增加5。 +update books set price=price+5 where note='novel'; +#5、将名称为EmmaT的书的价格改为40。 +update books set price=40 where b_name='EmmaT'; +#6、删除库存为0的记录 +delete from books where num=0; +~~~ + -- Gitee From 8489d5fcc9c9567421e263c8837d42913610421d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Thu, 23 Feb 2023 05:08:18 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- .../20230223.md" | 296 ++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 "01\351\231\210\346\242\246\346\242\246/20230223.md" diff --git "a/01\351\231\210\346\242\246\346\242\246/20230223.md" "b/01\351\231\210\346\242\246\346\242\246/20230223.md" new file mode 100644 index 0000000..5d87f3d --- /dev/null +++ "b/01\351\231\210\346\242\246\346\242\246/20230223.md" @@ -0,0 +1,296 @@ +## 唯一键约束 + +### 唯一键约束的作用 + +单列唯一:用来限制某个字段/某列的值不能重复。 + +组合唯一:用来限定几个字段的值组合不能重复。 + +### 关键字:unique key + +### 特点 + +```mysql +(1)一个表可以有很多个唯一键约束, +(2)每一个唯一键约束字段都会自动创建索引。 +(3)唯一键约束允许为空 +(4)唯一键约束也可以是复合唯一 +(5)删除唯一键约束的索引来删除唯一键约束 +索引名默认是字段名,复合唯一默认是第一个字段名。 +``` + +### 如何指定唯一键约束 + +#### 建表时 + +```mysql +#在建表时,可以指定唯一键约束 +create table 表名称( + 字段名 数据类型 unique key, + 字段名 数据类型 unique key, + 字段名 数据类型 +); + +create table 表名称( + 字段名 数据类型, + 字段名 数据类型, + 字段名 数据类型, + unique key(字段名), + unique key(字段名) +); +``` + +### 复合唯一 + +```mysql +create table 表名称( + 字段名 数据类型, + 字段名 数据类型, + 字段名 数据类型, + unique key(字段列表) #字段列表中写的是多个字段名,多个字段名用逗号分隔,表示那么是复合唯一,即多个字段的组合是唯一的 +); +``` + +## 主键约束(重要) + +### 主键约束的作用 + +用来唯一的确定一条记录 + +### 关键字:primary key + +### 特点 + +(1)唯一并且非空 + +(2)一个表最多只能有一个主键约束 + +(3)如果主键是由多列组成,可以使用复合主键 + +(4)主键列会自动创建索引(能够根据主键查询的,就根据主键查询,效率更高) + +主键列的唯一并且非空是约束的概念,但是mysql会给每个表的主键列创建索引,会开辟单独的物理空间来存储每一个主键的目录表(Btree结构)。这样设计的意义,可以根据主键快速查询到某一行的记录。 + +(5)如果删除主键约束了,主键约束对应的索引就自动删除了。 + +### 唯一键约束和主键约束区别 + +```mysql +4、唯一键约束和主键约束的区别 +(1)唯一键约束一个表可以有好几个, +但是主键约束只有一个 +(2)唯一键约束本身不带非空限制,如果需要非空,需要单独定义。 +主键约束不用再定义NOT NULL,自身就带非空限制。 +``` + +### 如何指定主键约束 + +#### 建表时指定主键约束 + +```mysql +create table 表名称( + 字段名 数据类型 primary key, + 字段名 数据类型, + 字段名 数据类型 +); +create table 表名称( + 字段名 数据类型, + 字段名 数据类型, + 字段名 数据类型, + primary key(字段名) +); +``` + + + +## 默认值约束 + +### 作用 + +给某个字段/某列指定默认值,当添加时或修改时,可以使用默认值。 + +### 关键字:default + +### 如何给字段加默认值 + +#### 建表时 + +```mysql +create table 表名称( + 字段名 数据类型 primary key, + 字段名 数据类型 unique key not null, + 字段名 数据类型 unique key, + 字段名 数据类型 not null default 默认值, +); +create table 表名称( + 字段名 数据类型 default 默认值 , + 字段名 数据类型 not null default 默认值, + 字段名 数据类型 not null default 默认值,, + primary key(字段名), + unique key(字段名) +); + +说明:默认值约束一般不在唯一键和主键列上加 +``` + +## 自增属性 + +### 1、作用 + +作用:给某个字段自动赋值,这个值是一直往上增加,如果没有特意干扰的,每次自增1. + +### 2、关键字:auto_increment + +### 3、特点和要求 + +```mysql +(1)一个表只能有一个自增字段,因为一个表只有一个AUTO_INCREMENT属性记录自增字段值 +(2)并且自增字段只能是key字段,即定义了主键、唯一键等键约束的字段。 +一般都是给主键和唯一键加自增。 +(3)自增字段应该是数值类型,一般都是整数类型。 +(4)AUTO_INCREMENT属性值 必须 > 当前自增字段的最大值 +(5)如果自增列指定了 0 和 null,会在当前最大值的基础上自增, +如果自增列手动指定了具体值,直接赋值为具体值。 +``` + +### 4、如何指定自增约束 + +#### 建表时 + +```mysql +create table 表名称( + 字段名 数据类型 primary key auto_increment, + 字段名 数据类型 unique key not null, + 字段名 数据类型 unique key, + 字段名 数据类型 not null default 默认值, +); +create table 表名称( + 字段名 数据类型 default 默认值 , + 字段名 数据类型 unique key auto_increment, + 字段名 数据类型 not null default 默认值,, + primary key(字段名) +); +``` + +## 检查约束 + +### 关键字:check + +# 作业 + +```mysql +-- 1、创建数据库test01_company + CREATE DATABASE test01_company; + USE test01_company; +-- 2、创建表格offices +-- | 字段名 | 数据类型 | +-- | ---------- | ----------- | +-- | officeCode | int | +-- | city | varchar(30) | +-- | address | varchar(50) | +-- | country | varchar(50) | +-- | postalCode | varchar(25) | + CREATE TABLE offices( + officeCode INT, + city VARCHAR(30), + address VARCHAR(50), + country VARCHAR(50), + postalCode VARCHAR(25) + ); +-- 3、创建表格employees +-- | 字段名 | 数据类型 | +-- | --------- | ------------- | +-- | empNum | int(11) | +-- | lastName | varchar(50) | +-- | firstName | varchar(50) | +-- | mobile | varchar(25) | +-- | code | int | +-- | jobTitle | varchar(50) | +-- | birth | date | +-- | Note | varchar(255) | +-- | Sex | varchar(5) | +CREATE TABLE employees( +empnum INT(11), +lastname VARCHAR(50), +firstname VARCHAR(50), +mobile VARCHAR(25), +`code` INT, +jobtitle VARCHAR(50), +birth date, +note VARCHAR(255), +sex VARCHAR(5) +); +-- **要求4:**将表employees的mobile字段修改到code字段后面。 +ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER `code`; +-- **要求5:**将表employees的birth字段改名为birthday; +ALTER TABLE employees CHANGE birth birthday date; +-- **要求6:**修改sex字段,数据类型为char(1)。 +ALTER TABLE employees MODIFY sex char(1); +-- **要求7:**删除字段note; +ALTER TABLE employees DROP note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +ALTER TABLE employees ADD favoriate_activity VARCHAR(100); +-- **要求9:**将表employees的名称修改为 employees_info +ALTER TABLE employees RENAME TO employees_info; +``` + + + +```mysql +-- ## 第2题 +-- 1、创建数据库test02db +CREATE DATABASE if NOT EXISTS test02db; +USE test02db; +-- 2、创建表格pet +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +CREATE TABLE IF NOT EXISTS pet( +`name` VARCHAR(20) COMMENT'宠物名称', +`owner` VARCHAR(20) COMMENT'宠物主人', +`spcies` VARCHAR(20) COMMENT'种类', +`sex` char(1) COMMENT'性别', +`birth` YEAR COMMENT '出生日期', +`death` YEAR COMMENT '死亡日期' +); +-- 3、添加记录 +INSERT INTO pet (`name`,`owner`,spcies,sex,birth,death) VALUES +('fluffy','harold','cat','f','2003','2010'), +('claws','gwen','cat','m','2004',null), +('buffy','null','god','f','2009',null), +('fang','benny','dog','m','2000',null), +('bowser','diane','dog','m','2003','2009'), +('chirpy',null,'bird','f','2008',null); +-- | name | owner | species | sex | birth | death | +-- | ------ | ------ | ------- | ---- | ----- | ----- | +-- | Fluffy | harold | Cat | f | 2003 | 2010 | +-- | Claws | gwen | Cat | m | 2004 | | +-- | Buffy | | Dog | f | 2009 | | +-- | Fang | benny | Dog | m | 2000 | | +-- | bowser | diane | Dog | m | 2003 | 2009 | +-- | Chirpy | | Bird | f | 2008 | | +-- +-- 4、 添加字段主人的生日owner_birth。 +ALTER TABLE pet ADD owner_birth VARCHAR(15); +alter table pet drop owner_birth; +desc pet; +-- 5、 将名称为Claws的猫的主人改为kevin +UPDATE pet SET owner='kevin' WHERE owner='claws'; + +-- 6、 将没有死的狗的主人改为duck +UPDATE pet SET owner='duck' WHERE death is NULL; +-- 7、 查询没有主人的宠物的名字; +SELECT name FROM pet WHERE OWNER is NULL; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +SELECT name,owner,death from pet WHERE death is NOT null AND spcies='cat'; +-- 9、 删除已经死亡的狗 +DELETE FROM pet where death is NOT NULL AND spcies='dog'; +-- 10、查询所有宠物信息 +SELECT * FROM pet; +``` + -- Gitee From 980cb1ff1d4bcaefc04870a560ef21f74e3ac2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Mon, 27 Feb 2023 14:42:03 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- ...72\347\241\200\347\273\203\344\271\240.md" | 152 +++++++++++++++ ...27\347\254\246\347\273\203\344\271\240.md" | 176 ++++++++++++++++++ 2 files changed, 328 insertions(+) create mode 100644 "01\351\231\210\346\242\246\346\242\246/5\343\200\201SELECT\350\257\255\345\217\245\345\237\272\347\241\200\347\273\203\344\271\240.md" create mode 100644 "01\351\231\210\346\242\246\346\242\246/6\343\200\201\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" diff --git "a/01\351\231\210\346\242\246\346\242\246/5\343\200\201SELECT\350\257\255\345\217\245\345\237\272\347\241\200\347\273\203\344\271\240.md" "b/01\351\231\210\346\242\246\346\242\246/5\343\200\201SELECT\350\257\255\345\217\245\345\237\272\347\241\200\347\273\203\344\271\240.md" new file mode 100644 index 0000000..b24f3ca --- /dev/null +++ "b/01\351\231\210\346\242\246\346\242\246/5\343\200\201SELECT\350\257\255\345\217\245\345\237\272\347\241\200\347\273\203\344\271\240.md" @@ -0,0 +1,152 @@ +# 第1组:user_profile表 + +user_profile表的sql脚本: + +```mysql +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`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'); +``` + +```mysql +CREATE DATABASE bg charset utf8; +USE bg; +drop table if exists user_1; +CREATE TABLE user_1( +`id` int, +`device_id` int(4), +`gender` varchar(14), +`age` int(2) , +`university` varchar(32), +`province` varchar(32)); +); +INSERT INTO user_1 VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_1 VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_1 VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_1 VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_1 VALUES(5,5432,'male',25,'山东大学','Shandong'); +DESC user_1; +``` + + + +![](5、SELECT语句基础练习.assets/image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +## (1)题目:从用户信息表中取出学校的去重数据 + +现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。 + +![image-20220207141745581](5、SELECT语句基础练习.assets/image-20220207141745581.png) + +```mysql +SELECT distinct university FROM user_1; +``` + + + +## (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' + +现在你需要查看用户明细设备ID数据,并将列名显示为 'user_infos_example',请你从用户信息表取出相应结果。 + +![image-20220207143458749](5、SELECT语句基础练习.assets/image-20220207143458749.png) + +```mysql +SELECT device_id user_infos_example FROM user_1; +``` + +## (3)题目:查询university是北京大学的设备ID + +现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。 + +![image-20220207144253185](5、SELECT语句基础练习.assets/image-20220207144253185.png) + +```mysql +SELECT device_id,university FROM user_1 WHERE university='北京大学'; +``` + + + +## (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 + +现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。 + +![image-20220207144628582](5、SELECT语句基础练习.assets/image-20220207144628582.png) + +```mysql +SELECT device_id,gender,age,university FROM user_1 WHERE age>24; +``` + + + +## (5)题目:查询所有用户的设备id、性别、年龄、学校 + +现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据 + +![image-20220207172816753](5、SELECT语句基础练习.assets/image-20220207172816753.png) + +```mysql +SELECT device_id,gender,age,university FROM user_1 WHERE age='25'; +``` + + + +## (6)题目:查询所有用户的数据 + +现在运营想要查看用户信息表中所有的数据,请你取出相应结果 + +![image-20220207172840781](5、SELECT语句基础练习.assets/image-20220207172840781.png) + +```mysql +SELECT * FROM user_1; +``` + +## (7)题目:查询省份是"shanghai"的用户信息 + +现在运营想要查看上海市用户的信息,请你取出相应的结果。 + +根据示例,你的查询应返回以下结果: + +![image-20220210171353387](5、SELECT语句基础练习.assets/image-20220210171353387.png) + +```mysql +SELECT * FROM user_1 WHERE province='Shanghai'; +``` + +## (8)题目:查询所有男性用户的设备ID、年龄、学校 + +现在运营想要查看所有男性用户的设备ID、年龄、学校,便于后期做数据分析。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171628263](5、SELECT语句基础练习.assets/image-20220210171628263.png) + +```mysql +SELECT device_id,age,university FROM user_1 WHERE gender='male'; +``` + +## (9)题目:从用户信息表中取出省份的去重数据 + +现在运营想要查看所有用户的省份分布情况,请从用户信息表中取出省份的去重数据。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171835938](5、SELECT语句基础练习.assets/image-20220210171835938.png) + +```mysql +SELECT DISTINCT province FROM user_1; +``` + diff --git "a/01\351\231\210\346\242\246\346\242\246/6\343\200\201\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" "b/01\351\231\210\346\242\246\346\242\246/6\343\200\201\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" new file mode 100644 index 0000000..2160202 --- /dev/null +++ "b/01\351\231\210\346\242\246\346\242\246/6\343\200\201\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" @@ -0,0 +1,176 @@ +# 第1题:user_profile表脚本1 + +user_profile表的sql脚本: + +```mysql +CREATE DATABASE if NOT EXISTS user_profile charset utf8; +USE user_profile; +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`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'); +``` + +![](6、运算符练习.assets/image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +## (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 + +现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。 + +![image-20220207144855013](6、运算符练习.assets/image-20220207144855013.png) + +```mysql +SELECT device_id,gender, age FROM user_profile where age<=23 AND age>=20; +``` + + + +## (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 + +现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据 + +![image-20220207144942510](6、运算符练习.assets/image-20220207144942510.png) + +```mysql +SELECT device_id,gender, age,university from user_profile WHERE !(university='复旦大学'); +``` + + + +## (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 + +现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。 + +![image-20220207145017152](6、运算符练习.assets/image-20220207145017152.png) + +```mysql +SELECT device_id,gender, age,university FROM user_profile WHERE age IS NOT NULL; +``` + +```mysql +CREATE DATABASE if NOT EXISTS user_profile charset utf8; +USE user_profile; +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`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 device_id,gender, age FROM user_profile where age<=23 AND age>=20; +SELECT device_id,gender, age,university from user_profile WHERE !(university='复旦大学'); +SELECT device_id,gender, age,university FROM user_profile WHERE age IS NOT NULL; +``` + + + +# 第2题:user_profile表脚本2 + +user_profile表sql脚本 + +```mysql +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`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_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +``` + +![image-20220207145932876](6、运算符练习.assets/image-20220207145932876.png) + +解释: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 + +现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。 + +![image-20220207145044808](6、运算符练习.assets/image-20220207145044808.png) + +```mysql +SELECT device_id,gender,age, university,gpa FROM user_profile WHERE gpa>3.5; + +``` + + + +## (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现) + +![image-20220207145119374](6、运算符练习.assets/image-20220207145119374.png) + +```mysql +SELECT device_id,gender,age, university,gpa FROM user_profile WHERE university='北京大学' OR gpa<3.7; +``` + + + +## (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。 + +![image-20220207145152255](6、运算符练习.assets/image-20220207145152255.png) + +```mysql + +``` + + + +## (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 + +现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据。 + +![image-20220207145321717](6、运算符练习.assets/image-20220207145321717.png) + +```mysql + +``` + + + +## (8)题目:所有大学中带有北京的用户信息 + +现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。 + +![image-20220207145355354](6、运算符练习.assets/image-20220207145355354.png) + +```mysql + +``` + -- Gitee From eb29fe46f52a8b4b82184b58de421c97b3057f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Thu, 2 Mar 2023 02:34:18 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- .../\344\275\234\344\270\2322023 0302.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "01\351\231\210\346\242\246\346\242\246/\344\275\234\344\270\2322023 0302.md" diff --git "a/01\351\231\210\346\242\246\346\242\246/\344\275\234\344\270\2322023 0302.md" "b/01\351\231\210\346\242\246\346\242\246/\344\275\234\344\270\2322023 0302.md" new file mode 100644 index 0000000..146a690 --- /dev/null +++ "b/01\351\231\210\346\242\246\346\242\246/\344\275\234\344\270\2322023 0302.md" @@ -0,0 +1,60 @@ +```mysql +drop database class2; +create database class2 charset utf8; +use class2; +create table Student( + `Sno` varchar(20) not null, + Sname varchar(20) not null, + Ssex varchar(20) not null, + Sbirthday datetime, + Class varchar(20) +); +create table Course( + Cno varchar(20) not null, + Cname varchar(20) not null, + Tno varchar(20) not null +); +create table Score( + Sno varchar(20) not null, + Cno varchar(20) not null, + Degree Decimal(4,1) +); +create table Teacher( + Tno varchar(20) not null, + Tname varchar(20) not null, + Tsex varchar(20) not null, + Tbirthday datetime, + Prof varchar(20), + Depart varchar(20) not null +); +insert into Student values ('108','曾华','男','1977-9-1','95033'),('105','匡明','男','1975-10-2','95031'),('107','王丽','女','1976-1-23','95033'),('101','李军','男','1976-2-20','95033'),('109','王芳','女','1975-2-10','95031'),('103','陆君','男','1974-6-3','95031'); +insert into Course values ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831'); +insert into Score values ('103','3-245',86),('105','3-245',75),('109','3-245',68),('103','3-105',92),('105','3-105',88),('109','3-105',76),('101','3-105',64),('107','3-105',91),('108','3-105',78),('101','6-166',85),('107','6-166',79),('108','6-166',81); +insert into Teacher values ('804','李诚','男','1958-12-2','副教授','计算机系'),('856','张旭','男','1969-3-12','讲师','电子工程系'),('825','王萍','女','1972-5-5','助教','计算机系'),('831','刘冰','女','1977-8-14','助教','电子工程系'); +select Student.Sno,Sname,Ssex,Sbirthday,class,Cname from Student,Course,Score where Student.Sno = Score.Sno and Course.Cno = Score.Cno; +select Teacher.* from Teacher where Tname not in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno); +select Sno,Cno from Score where Degree=(select Max(Degree) from Score); +select Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno; +select Student.Sno,Cname,Degree from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno; +select Sname,Cname,Degree from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno; +select AVG(Degree) from Student,Score where Student.Sno=Score.Sno and class=95033; +select Student.* from Student,Score where Student.Sno=Score.Sno and Cno='3-105' and Degree>(select Degree from Score where Sno=109 and Cno='3-105'); +select * from Score cmm where Degree <(select MAX(Degree) from Score ct where cmm.Cno=ct.Cno) and Sno in(select Sno from Score group by Sno having count(*)>1); +select * from Student,Score where Student.Sno=Score.Sno and Score.Degree>(select Degree from Score where Cno='3-105' and Sno='109'); +select Sno,Sname,Sbirthday from Student where year(Sbirthday)=(select year(Sbirthday) from Student where Sno=108); +select Sname,Degree from Student,Score,Teacher,Course where Score.cno=Course.Cno and Course.Tno=Teacher.Tno and Teacher.Tname='张旭' and Student.Sno = Score.Sno; +select distinct Tname from Course,Score,Teacher where Course.Tno=Teacher.Tno and Course.Cno=(Select cno from score group by cno having count(*)>5); +select * from Score where Cno in(select Cno from Course where Tno in(select Tno from Teacher where Depart='计算机系')); +select Tname,Prof from Teacher where Depart='电子工程系' and Prof not in(select Prof from Teacher where Depart='计算机系') or Depart='计算机系' and Prof not in(select Prof from Teacher where Depart='电子工程系'); +select Cno,Sno,Degree from Score where Cno='3-105' and Degree >any(select Degree from Score where Cno='3-245') order by Degree desc; +select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno='3-105' and b.Sno=a.Sno)>(select Degree from Score c where Cno='3-245' and c.Sno=a.Sno); +select * from Score a where Degree <(select avg(Degree) from Score b where a.Cno=b.Cno); +select Tname,Depart from Teacher where Tno in (select Tno from Course); +select Tname,Depart from Teacher where Tname not in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno); +select Tname,Cname from Teacher,Course where Tsex='男' and Teacher.Tno=Course.Tno; +select Sno,Cno,Degree from Score where Degree=(select MAX(Degree)from Score); +select Sname from Student where Ssex=(select Ssex from Student where Sname='李军') and Sname not in ('李军'); +select Sname from Student where Ssex=(select Ssex from Student where Sname='李军') and Sname not in ('李军') and Class=(select Class from Student where Sname='李军'); +select * from Score where Sno in (select Sno from Student where Ssex='男') and Cno in (select Cno from Course where Cname='计算机导论'); +``` + -- Gitee