From 7c4af70362a30658fe451a619464aa1f7eb3d9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BA=9A=E8=BE=89?= <3192667214@qq.com> Date: Thu, 23 Feb 2023 10:39:29 +0800 Subject: [PATCH 1/3] =?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 --- ...30226 mysql\347\272\246\346\235\237.md.md" | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 "41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" diff --git "a/41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" "b/41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" new file mode 100644 index 0000000..16e62f3 --- /dev/null +++ "b/41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" @@ -0,0 +1,186 @@ +## mysql + +````mysql +## 第1题 +-- 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) +); +use test01_company +-- 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, +lasename 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 +## 第2题 +-- 1、创建数据库test02db +create database test02db; +use test02db; +-- 2、创建表格pet +create table pet( +`name` varchar(20), +`owner` varchar(20), +species varchar(20), +sex char(1), +birth year, +death year +); +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +desc pet; +-- 3、添加记录 +insert into pet values +('Fluffy','harold','Cat','f',2003,2010), +('Claws','gwen','Cat','m',2004,null), +('Buffy',null,'Dog','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 year after `owner`; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set `owner`='kevin' where `name`='claws'; +-- 6、 将没有死的狗的主人改为duck +update pet set `owner`='duck' where death is null and species='dog'; +-- 7、 查询没有主人的宠物的名字; +select `name` from pet where owner is null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select `name`,`owner`,death from pet where species='cat' and death is not null; +-- 9、 删除已经死亡的狗 +delete from pet where species='dog' and death is not null; +-- 10、查询所有宠物信息 +select * from pet; + +## 第3题 +## 第3题 + +1、创建数据库:test03_company + +```sql +create database test03_company charset utf8; +``` + +2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 + +A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 + +```mysql +use test03_company ; +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +) +``` + +B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 + +* ​ 雇员编号为主键; +* ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +* ​ 性别默认为男; + +```mysql +create table employee ( + empid int primary key auto_increment, + name varchar(10) not null, + sex enum('男','女') not null default '男', + title varchar(10), + birthday date, + depid int foreign key references department(depid) +) +``` + +C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 + +3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) + +4、添加数据如下: + +部门表: + +| 部门编号 | 部门名称 | 部门简介 | +| -------- | -------- | ------------ | +| 111 | 生产部 | Null | +| 222 | 销售部 | Null | +| 333 | 人事部 | 人力资源管理 | + + 雇员表: + +| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +| -------- | ---- | ---- | ---------- | ---------- | ------------ | +| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | + + 工资表: + +| 雇员编号 | 基本工资 | 职务工资 | 扣除 | +| -------- | -------- | -------- | ---- | +| 1001 | 2200 | 1100 | 200 | +| 1002 | 1200 | 200 | NULL | +| 1003 | 2900 | 700 | 200 | +| 1004 | 1950 | 700 | 150 | + +```` + + + -- Gitee From d274c8a22a50dc99126cffd51a4b791b4131d82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BA=9A=E8=BE=89?= <3192667214@qq.com> Date: Fri, 24 Feb 2023 13:10:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=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 --- ...30223 mysql\347\272\246\346\235\237.md.md" | 0 ...50\277\220\347\256\227\347\254\246.md.txt" | 123 ++++++++++++++++++ 2 files changed, 123 insertions(+) rename "41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" => "41 \345\221\250\344\272\232\350\276\211/20230223 mysql\347\272\246\346\235\237.md.md" (100%) create mode 100644 "41 \345\221\250\344\272\232\350\276\211/20230224 mysql\346\237\245\350\257\242\357\274\214\350\277\220\347\256\227\347\254\246.md.txt" diff --git "a/41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" "b/41 \345\221\250\344\272\232\350\276\211/20230223 mysql\347\272\246\346\235\237.md.md" similarity index 100% rename from "41 \345\221\250\344\272\232\350\276\211/20230226 mysql\347\272\246\346\235\237.md.md" rename to "41 \345\221\250\344\272\232\350\276\211/20230223 mysql\347\272\246\346\235\237.md.md" diff --git "a/41 \345\221\250\344\272\232\350\276\211/20230224 mysql\346\237\245\350\257\242\357\274\214\350\277\220\347\256\227\347\254\246.md.txt" "b/41 \345\221\250\344\272\232\350\276\211/20230224 mysql\346\237\245\350\257\242\357\274\214\350\277\220\347\256\227\347\254\246.md.txt" new file mode 100644 index 0000000..7041679 --- /dev/null +++ "b/41 \345\221\250\344\272\232\350\276\211/20230224 mysql\346\237\245\350\257\242\357\274\214\350\277\220\347\256\227\347\254\246.md.txt" @@ -0,0 +1,123 @@ +## MYSQL + +````MYSQL +#第一题员工表 +create database test charset utf8; +drop table if exists `employee`; +#创建employee表 +use test; +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); +#添加信息 +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | +INSERT INTO employee(id,`name`,sex,tel,addr,salary)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 sex='男' and salary > 12000 or addr = '广东深圳' or addr = '广东广州'; +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name 姓名, salary*12 年薪 from employee; +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +DROP TABLE IF EXISTS `countries_info`; +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); +``` + +#表数据样例: +use test; +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| 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 | ++-------------+-----------+---------+------------+--------------+ +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); + +-- **要求1:** 查询大国 的国家名称、人口和面积。 +-- +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +select `name`,population,area from countries_info where area>3000000 or population>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 * from countries_info where gdp/population<1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select `name` 国家名, area 面积,population 人口,area/population 人均国土面积 from countries_info; +```` + + + -- Gitee From 87688154da236c262641b3edce14fb97f731be85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BA=9A=E8=BE=89?= <3192667214@qq.com> Date: Thu, 9 Mar 2023 11:05:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=AC=E4=BA=94=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 --- ...345\255\220\347\250\213\345\272\217.md.md" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "41 \345\221\250\344\272\232\350\276\211/20230309 mysql\345\255\220\347\250\213\345\272\217.md.md" diff --git "a/41 \345\221\250\344\272\232\350\276\211/20230309 mysql\345\255\220\347\250\213\345\272\217.md.md" "b/41 \345\221\250\344\272\232\350\276\211/20230309 mysql\345\255\220\347\250\213\345\272\217.md.md" new file mode 100644 index 0000000..89beeb4 --- /dev/null +++ "b/41 \345\221\250\344\272\232\350\276\211/20230309 mysql\345\255\220\347\250\213\345\272\217.md.md" @@ -0,0 +1,130 @@ +## mysql + +### 作业 + +``` mysql +create database lianxi charset utf8; +use lianxi; +create table stuinfo( +stuno char(10), +stuname varchar(10), +stusex char(6), +stuage int, +stuaddress varchar(10), +stuseat int +); +insert into stuinfo values +('s2501','张秋利','男',20,'美国硅谷',1), +('s2502','李斯文','女',18,'美国硅谷',2), +('s2503','马文才','男',18,'美国硅谷',3), +('s2504','欧阳俊雄','女',21,'美国硅谷',4), +('s2505','梅超风','男',16,'美国硅谷',5), +('s2506','陈旋风','男',19,'美国硅谷',6); +create table stuexam( +examno int, +stuno char(10), +wittenexam int, +labexam int +); +insert into stuexam values +(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,70), +(4,'s2504',40,80), +(5,'s2505',70,85), +(6,'s2506',85,90); +create table stumarke( +examno int, +stuid char(10), +score int +); +insert into stumarke values +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); +-- 1.查询出年龄比班上平均年龄大的学生的信息 +SELECT + * +FROM + stuinfo +WHERE + stuage > ( SELECT avg( stuage ) FROM stuinfo ); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +SELECT + a.stuno,stuname,stusex,max(score) +FROM + stuinfo a + LEFT JOIN stumarke b ON a.stuno = b.stuid GROUP BY a.stuno; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT + a.stuno,stuname,stusex,round ((wittenexam+labexam)/2) +FROM + stuinfo a + LEFT JOIN stuexam b on a.stuseat = b.examno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +-- 普通查询 +SELECT + * +FROM + stuinfo +WHERE + stusex = '男' + AND stuage >= 20; +-- 子查询 +SELECT + * +FROM + stuinfo +WHERE + stuno = ( SELECT stuno FROM stuinfo WHERE stusex = '男' AND stuage >= 20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + +SELECT + * +FROM + stuinfo +WHERE + stusex = '女' + AND stuage > ( SELECT max( stuage ) FROM stuinfo WHERE stusex = '男' ); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT + a.*, + min( score ) fen +FROM + stuinfo a + LEFT JOIN stumarke b ON a.stuno = b.stuid +GROUP BY + a.stuno +HAVING + fen > 60; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +-- 表连接 +SELECT + b.* +FROM + stumarke a + LEFT JOIN stuinfo b ON a.stuid = b.stuno +GROUP BY + a.stuid; +-- in方法 +select * from stuinfo where stuno in (select stuid from stumarke group by stuid); + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +-- +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 +-- +-- 14.查询出只要比某个男生年龄大的女生的信息 +-- +``` + -- Gitee