diff --git "a/20230217 MySQL\347\232\204\347\231\273\351\231\206\345\222\214\345\256\211\350\243\205/MySQL\345\256\211\350\243\205.md" "b/20230217 MySQL\347\232\204\347\231\273\351\231\206\345\222\214\345\256\211\350\243\205/MySQL\345\256\211\350\243\205.md" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git "a/20230218 MySQL\347\232\204\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/20230218 MySQL\347\232\204\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 65ce8f187e281fd2886b69f921ab4c1658cd5611..0000000000000000000000000000000000000000 --- "a/20230218 MySQL\347\232\204\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,110 +0,0 @@ -# 作业 - --- 要求3:**将c_contact字段移动到c_birth字段后面 - -| alter table customers MODIFY c_contact VARCHAR(50) AFTER c_birth;[148](https://gitee.com/class-22-class-02/mysql-base/commit/7254ba006aa7490bc78f5e14eb194f8bc5814f3b#76d16472e14795aabb14f32c4814747905a2575b_0_148) | - --- 要求4:**将c_name字段数据类型改为 varchar(70)[149] - - -- 要求5:**将c_contact字段改名为c_phone | - -| ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); | - -- 要求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; | - -```sql - -create database test01_market; -create table customers( - c_num int(11), - c_name varchar(50), - c_contact varchar(50), - c_city varchar(50), - c_birth date -); -alter table customers modify c_contact varchar(50) after c_birth; - -alter table customers modify c_name varchar(70); - -alter table customers change c_contact c_phone varchar(50); - -alter table customers add c_gender char(1); -alter table customers modify c_gender char(1) after c_name; - -alter table customers rename to customers_info; -# **要求8:**删除字段c_city -alter table customers drop c_city; -``` - -```sql - -create database test02_library; - -create table books( - b_id int(11) not null primary key comment '书编号', - b_name varchar(50) not null comment '书名', - authors varchar(100)not null comment '作者', - price float not null comment '价格', - prbdate year not null comment '出版日期', - note varchar(100) null comment '说明', - num int(11) not null comment '库存' -); - -insert into books values (1,'Tal of AAA','Dickes',23,1995,'novel',11); -insert into books values (2,'EmmaT','Jane lura',35,1993,'joke',22); -insert into books values (3,'Story of Jane','Jane Tim',40,2001,'novel',0), - (4,'Lovey Day','George Byron',20,2005,'novel',30), - (5,'Old land','Old land',30,2010,'law',0), - (6,'The Battle','The Battle',30,1999,'medicine',40), - (7,'Rose Hood','Rose Hood',28,2008,'cartoon',28); -update books set price=+5 where note = 'novel'; -update books set price=40 where b_name='EmmaT'; -delete from books where num=0; -``` - -```sql -create database test03_bookstore; -create table book( - id int(11) not null primary key auto_increment, - title varchar(100) not null , - arthor varchar(100) not null , - price double(11,2) not null , - sales int(11) not null , - stock int(11)not null , - img_path varchar(100) not null -); -insert into book values (1,'解忧杂货店','东野圭吾',27.20,102,98,' upload/books/解忧杂货店.jpg '), - (2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg '); -create table users( - id int(11)not null primary key auto_increment, - username varchar(100) not null unique , - password varchar(100) not null , - email varchar(100)null -); -insert into book value (1,'admin',112233,'admin@mxdx.com'); -create table orders( - id varchar(100) not null primary key , - order_time datetime not null , - total_count int(11) not null , - total_amount double(11,2) not null , - state int(11) not null , - user_id int(11) not null unique -); -insert into book values (15294258455691,'2018-06-20 00:30:45',2,50.20,0,1); -create table order_items( - id int(11) not null primary key auto_increment, - count int(11) not null , - amount double(11,2) not null , - title varchar(100) not null , - author varchar(100) not null , - price double(11,2) not null , - img_path varchar(100) not null , - order_id varchar(100) not null unique -); -insert into book values (1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691), - (2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); -``` diff --git "a/20230221 MySQL\347\232\204\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023.2.21.md" "b/20230221 MySQL\347\232\204\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023.2.21.md" deleted file mode 100644 index d433bf8787db46b77b0d702f2869068b19420e4f..0000000000000000000000000000000000000000 --- "a/20230221 MySQL\347\232\204\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023.2.21.md" +++ /dev/null @@ -1,432 +0,0 @@ -## 第1题 - -1、创建数据库test01_company - -```mysql -create database test01_company charset utf8; -``` - -2、创建表格offices - -```mysql -use test01_company; -create table offices( -officeCode int, -city varchar(30), -address varchar(50), -country varchar(50), -postalCode varchar(25) -); -``` - -| 字段名 | 数据类型 | -| ---------- | ----------- | -| officeCode | int | -| city | varchar(30) | -| address | varchar(50) | -| country | varchar(50) | -| postalCode | varchar(25) | - -3、创建表格employees - -```mysql -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) -); -``` - -| 字段名 | 数据类型 | -| ------------- | ---------------------------- | -| 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字段后面。 - -```mysql -alter table employees modify mobile varchar(25) after code; -``` - - - -**要求5:**将表employees的birth字段改名为birthday; - -```mysql -alter table employees change birth birthday date; -``` - - - -**要求6:**修改sex字段,数据类型为char(1)。 - -```mysql -alter table employees modify sex char(1); -``` - - - -**要求7:**删除字段note; - -```mysql -alter table employees drop note; -``` - - - -**要求8:**增加字段名favoriate_activity,数据类型为varchar(100); - -```mysql -alter table employees add favoriate_activity varchar(100); -``` - - - -**要求9:**将表employees的名称修改为 employees_info - -```mysql -alter table employees rename to employees_info; -``` - - - -## 第2题 - -1、创建数据库test02db - -```mysql -create database test02db; -``` - - - -2、创建表格pet - -```mysql -use test02db; -create table pet( -name varchar(20) comment '宠物名称', -owner varchar(20) comment '宠物主人', -species varchar(20) comment '种类', -sex char(1) comment '性别', -birth year comment '出生日期', -death year comment '死亡日期' -); -``` - -| 字段名 | 字段说明 | 数据类型 | -| ------- | -------- | ----------- | -| name | 宠物名称 | varchar(20) | -| owner | 宠物主人 | varchar(20) | -| species | 种类 | varchar(20) | -| sex | 性别 | char(1) | -| birth | 出生日期 | year | -| death | 死亡日期 | year | - -3、添加记录 - -```mysql -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。 - -```mysql -alter table pet add owner_birth date; -``` - -5、 将名称为Claws的猫的主人改为kevin - -```mysql -update pet set name='kevin' where name='Claws'; -``` - -6、 将没有死的狗的主人改为duck - -```mysql -update pet set name='duck' where species='Dog' and death is null; -``` - -7、 查询没有主人的宠物的名字; - -```mysql -select name from pet where owner is null; -``` - -8、 查询已经死了的cat的姓名,主人,以及去世时间; - -```mysql -select name,owner,death from pet where species = "Cat" and death is not null; -``` - -9、 删除已经死亡的狗 - -```mysql -delete from pet where species='Dog' and death is not null; -``` - -10、查询所有宠物信息 - -````mysql -select * from pet; -```` - - - -## 第3题 - -1、创建数据库:test03_company - -```mysql -create database test03_company; -``` - - - -2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 - -```mysql -use test03_company; -``` - - - -A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 - -```mysql -create table department( -depid int primary key, -depname varchar(10) not null, -deinfo varchar(20) null -); -``` - - - -B. 雇员表(emoloyee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 - -* ​ 雇员编号为主键; -* ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); -* ​ 性别默认为男; - -```mysql -create table emoloyee( -empid int primary key , -name varchar(10), -sex enum('男','女') not null default '男', -title varchar(10), -birthday date, -depid int, -foreign key (depid) references department(depid) on update cascade on delete set null -); -``` - -C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 - -```mysql -create table salary( -empid int primary key, -basesalary int, -titlesalary int, -deduction int null -); -``` - - - -3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) - -```mysql -alter table salary add constraint foreign key (empid) references emoloyee(empid) on update cascade on delete cascade; -``` - - - -4、添加数据如下: - -部门表: - -```mysql -insert into department values -(111,'生产部',null), -(222,'销售部',null), -(333,'人事部','人力资源管理'); -``` - -| 部门编号 | 部门名称 | 部门简介 | -| -------- | -------- | ------------ | -| 111 | 生产部 | Null | -| 222 | 销售部 | Null | -| 333 | 人事部 | 人力资源管理 | - - 雇员表: - -```mysql -insert emoloyee values (1001,'张三','男','高级工程师','1975-1-1',111), -(1002,'李四','女','助工','1985-1-1',111), -(1003,'王五','男','工程师','1978-11-11',222), -(1004,'张六', '男','工程师','1999-1-1',222); -``` - - - -| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | -| -------- | ---- | ---- | ---------- | ---------- | ------------ | -| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | -| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | -| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | -| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | - - 工资表: - -```mysql -insert into salary values(1001,2200,1100,200), -(1002,1200,200,null), -(1003,2900,700,200), -(1004,1950,700,150); -``` - - - -| 雇员编号 | 基本工资 | 职务工资 | 扣除 | -| -------- | -------- | -------- | ---- | -| 1001 | 2200 | 1100 | 200 | -| 1002 | 1200 | 200 | NULL | -| 1003 | 2900 | 700 | 200 | -| 1004 | 1950 | 700 | 150 | - - - -## 第4题 - -1、创建一个数据库:test04_school - -```mysql -CREATE DATABASE test04_school CHARSET utf8; -``` - - - -2、创建如下表格 - -````mysql -USE test04_school; -```` - - - -表1 Department表的定义 - -```mysql -create table Department( -DepNo int(10) COMMENT '部门号' PRIMARY KEY , -DepName varchar(20) COMMENT '部门名称' not null, -DepNote varchar(50) COMMENT '部门备注' null -); -``` - - - -| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | -| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | -| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | - -表2 Teacher表的定义 - -```mysql -create table Teacher( -Number int COMMENT '教工号' PRIMARY KEY, -Name varchar(30) COMMENT '姓名' not null, -Sex varchar(4) COMMENT '性别' null, -Birth date COMMENT '出生日期' null, -DepNo int COMMENT '部门号' , -Salary float COMMENT '工资' null, -Address varchar(100) COMMENT '家庭住址' null, -foreign key(DepNo) references Department(DepNo) -); -``` - - - -| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -| Number | 教工号 | int | 是 | 否 | 是 | 是 | -| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | -| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | -| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | -| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | -| Salary | 工资 | float | 否 | 否 | 否 | 否 | -| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | - -3、添加记录 - -```mysql -insert into Department VALUES(601,'软件技术系','软件技术等专业'), -(602,'网络技术系','多媒体技术等专业'), -(603,'艺术设计系','广告艺术设计等专业'), -(604,'管理工程系','连锁经营管理等专业'); - -insert into Teacher values(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'), -(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'), -(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'), -(2004,'James','女','1980-10-20',602,3500,'四川省成都市'), -(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); -``` - - - -| **DepNo** | **DepName** | **DepNote** | -| --------- | ----------- | ------------------ | -| 601 | 软件技术系 | 软件技术等专业 | -| 602 | 网络技术系 | 多媒体技术等专业 | -| 603 | 艺术设计系 | 广告艺术设计等专业 | -| 604 | 管理工程系 | 连锁经营管理等专业 | - -| **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | -| ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | -| 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | -| 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | -| 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | -| 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | -| 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | - -4、用SELECT语句查询Teacher表的所有记录。 - -```mysql -select * from Teacher; -``` - - - - - diff --git "a/20230223 MySQL\350\277\220\347\256\227\347\254\246\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" "b/20230223 MySQL\350\277\220\347\256\227\347\254\246\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" deleted file mode 100644 index a9172f2fe64d858f03ec7333afff65ab14c63c89..0000000000000000000000000000000000000000 --- "a/20230223 MySQL\350\277\220\347\256\227\347\254\246\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" +++ /dev/null @@ -1,136 +0,0 @@ -# 第1题:user_profile表脚本1 - -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'); -``` - -![](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 between 20 and 23; -``` - -## (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 ; -``` - -# 第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 -select device_id,gender,age,university,gpa from user_profile where university='北京大学'and'复旦大学'and'山东大学'; -``` - -## (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 -select device_id,gender,age,university,gpa from user_profile where (gpa>3.5 and university='山东大学')or(gpa>3.8 and university='复旦大学'); -``` - -## (8)题目:所有大学中带有北京的用户信息 - -现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。 - -![image-20220207145355354](6、运算符练习.assets/image-20220207145355354.png) - -```mysql -select device_id,age,university from user_profile where university like '%北京%'; -``` diff --git "a/20230224 \346\237\245\350\257\242\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240\344\275\234\344\270\232.md" "b/20230224 \346\237\245\350\257\242\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240\344\275\234\344\270\232.md" deleted file mode 100644 index 01805086c453b07157498636081591615d89e2b7..0000000000000000000000000000000000000000 --- "a/20230224 \346\237\245\350\257\242\344\275\234\344\270\232/\350\277\220\347\256\227\347\254\246\347\273\203\344\271\240\344\275\234\344\270\232.md" +++ /dev/null @@ -1,71 +0,0 @@ -```sql -create database z charset utf8; -use z; -CREATE TABLE( id INT, `name` VARCHAR(20), sex VARCHAR(20), tel VARCHAR(20), addr VARCHAR(50), salary FLOAT); -desc employee; -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 id,name,sex,tel,addr,salary from employee where salary >12000 and salary < 13000; --- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 -select yid,name,sex,tel,addr,salar from employee where name in ('刘') --- **要求3:**将“李四”的家庭住址改为“广东韶关” -UPDATE employee set addr='广东韶关' WHERE `name`='李四'; --- **要求4:**查询出名字中带“小”的员工 -select * from employee where name like '%小%'; --- **要求5:**查询出薪资高于11000的男员工信息 -select id,name,sex,tel,addr,salary from employee where salary > 11000- -- **要求6:**查询没有登记电话号码的员工 -select id,name,sex,tel,addr,salary from employee where tel is null; --- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 -select id,name,sex,tel,addr,salary from employee where salary >12000 or addr =( ('广东深圳'and '广东广州')and sex ='男'); --- **要求8:**查询每个员工的年薪,显示“姓名、年薪” -select name,salary from employee; - - - - -create database adde charset utf8; -use adde; -CREATE TABLE `countries_info`( - `name` VARCHAR(100), - `continent` VARCHAR(100),CASE case_value - WHEN when_value THEN - statement_list - ELSE - statement_list -END CASE; - - `area` INT, - population INT, - gdp BIGINT -); -desc `countries_info`; -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); --- 查询大国 的国家名称、人口和面积。 --- --- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : --- --- * 面积至少为 300万平方公里(即,3000000 km2) --- --- * 人口至少为 2500 万(即 25000000) -select name,population,gdp from `countries_info` where gdp > 3000000 and population > 25000000 -查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 - select * from countries_info where continent = 'Asia'; --- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 -select gdp from `countries_info` where gdp < 10000 and population < 100000 --- **要求4:**查询国家名字中包含“o“字母的国家信息 -select name,continent from `countries_info` where continent like '%o%' --- **要求5:**查询GDP值超过10000000000的国家信息 -select name ,continent,area,population,gdp from `countries_info` where gdp > 10000000000 --- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” -select name ,continent,area,population,gdp from `countries_info` where area; --- *要求7:**查询人均贡献GDP值低于1000的国家信息。 -select name ,continent,area,population,gdp from `countries_info` where area < 1000 --- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” -select name,area,population,(area/countries_info.population) from countries_info; -``` diff --git "a/20230302 \350\201\224\350\241\250\345\244\247\344\275\234\344\270\232/\350\201\224\350\241\250\345\244\247\344\275\234\344\270\232.md" "b/20230302 \350\201\224\350\241\250\345\244\247\344\275\234\344\270\232/\350\201\224\350\241\250\345\244\247\344\275\234\344\270\232.md" deleted file mode 100644 index bb54951c5d2da040d161935c1ae5d59508e6a3c6..0000000000000000000000000000000000000000 --- "a/20230302 \350\201\224\350\241\250\345\244\247\344\275\234\344\270\232/\350\201\224\350\241\250\345\244\247\344\275\234\344\270\232.md" +++ /dev/null @@ -1,243 +0,0 @@ -## 1.作业 - - - --- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息 - -select * from student; - -select * from course; - -select * from score; - - - -    SELECT - -        stu.*,cname - -    FROM - -        student stu - -        LEFT JOIN score s ON stu.sno = s.sno - -        left join course c on s.cno = c.cno; - --- 2,查询没有学生的教师的所有信息 - -    SELECT - -        t.* - -    FROM - -        teacher t - -        LEFT JOIN course c ON c.tno = t.tno - -        left join score s on s.cno=c.cno - -        WHERE sno is null; - --- ① 查询Score表中的最高分的学生学号和课程号。 - -    SELECT - -        sno, - -        cno, - -        degree - -    FROM - -        score - -    WHERE - -        degree = ( SELECT MAX( degree ) FROM score ); - --- ② 查询所有学生的Sname、Cno和Degree列。 - -    SELECT - -        sname,cno,degree - -    FROM - -        student t - -        left join score s on t.sno = s.sno; - --- ③ 查询所有学生的Sno、Cname和Degree列。 - -    SELECT - -        t.sno,cname,degree - -    FROM - -        student t - -        left join score s on t.sno = s.sno - -        inner join course c on c.cno=s.cno ; - --- ④ 查询所有学生的Sname、Cname和Degree列。 - -    SELECT - -        sname,cname,degree - -    FROM - -        student t - -        left join score s on t.sno = s.sno - -        inner join course c on c.cno=s.cno ; - --- ⑤ 查询“95033”班学生的平均分。 - -#方法一:   - -    SELECT - -        AVG( degree ) - -    FROM - -        score s - -        LEFT JOIN student t ON s.sno = t.sno - -    WHERE - -        class = '95033'; - -#方法二:   - -    SELECT - -        AVG( degree ) - -    FROM - -        score - -    WHERE - -        sno in ( SELECT sno FROM student WHERE class = '95033' ); - --- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 - -#特殊内联接+自联接 - -select a.* from score a,score b where a.cno='3-105' and a.cno=b.cno and b.sno=109 and a.degree>b.degree; - -#自联接 - -select a.* from score a inner join score b on a.cno=b.cno where a.cno='3-105' and b.sno=109  and a.degree > b.degree order by a.sno; - -#子查询 - -select * from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105'; - - - --- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 - - - -select * from score a where sno in (select sno from score group by sno having count(*)>1 -- 选学多门课程的同学 - - ) and degree=(select max(degree) from score b where a.cno=b.cno ); - - - --- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 - -select * from Score where cno='3-105' and Degree>(select degree from Score where cno='3-105' and Sno='109'); - --- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 - - - -select Sno,Sname,Sbirthday from Student where YEAR(Sbirthday)=(select YEAR(sbirthday)from Student where Sno='108'); - --- ⑩ 查询“张旭“教师任课的学生成绩。 - -Select Degree from score where cno=( Select cno from course where tno=(Select tno from teacher where tname='张旭')); - --- 11 查询选修某课程的同学人数多于5人的教师姓名。 - -Select tname from teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(*)>5)); - --- 12 查询出“计算机系“教师所教课程的成绩表。 - -select * from score where Cno in(select cno from Course where Tno in(select tno from Teacher where Depart='计算机系')); - --- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 - -select Tname,Prof from teacher where prof not in (select prof from teacher where depart = '计算机系' and prof in (select prof from teacher where depart ='电子工程系')); - - - -select Tname,Prof from teacher where prof in (select prof from teacher where depart = '计算机系') xor prof in (select prof from teacher where depart ='电子工程系'); - --- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 - -select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select MAX(degree)from Score where Cno='3-245') order by Degree desc; - --- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. - -select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select MAX(degree)from Score where Cno='3-245'); - --- 16 查询成绩比该课程平均成绩低的同学的成绩表。 - -Select degree from score a where degree<(select avg(degree) from score b where a.cno=b.cno); - --- 17 查询所有任课教师的Tname和Depart. - -Select tname,depart from teacher where tno in (select tno from course where cno in (select cno from score group by cno)); - -Select tname,depart from teacher where tno in (select tno from course where cno in (select distinct cno from score)); - --- 18 查询所有未讲课的教师的Tname和Depart. - -Select tname,depart from teacher where tno not in (select tno from course where cno in (select distinct cno from score)); - - - --- 19 查询“男”教师及其所上的课程。 - -Select tname,cname from teacher t join course c on t.tno=c.tno and t.tsex='男'; - - - --- 20 查询最高分同学的Sno、Cno和Degree列。 - -select Sno,Cno,Degree from Score where Degree in(select MAX(Degree) from Score); - - - --- 21 查询和“李军”同性别的所有同学的Sname. - -Select sname from student where ssex=(select ssex from student where sname='李军'); - - - --- 22 查询和“李军”同性别并同班的同学Sname. - -Select sname from student where ssex=(select ssex from student where sname='李军') and Class=(select class from student where sname='李军'); - - - -Select sname from student a where ssex=(select ssex from student b where sname='李军' and a.class=b.class) ; - - - --- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 - -  select degree from Score where Sno in(select Sno from Student where Ssex='男') and Cno in (select Cno from Course where cname='计算机导论'); diff --git "a/20230307 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/20230307 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" deleted file mode 100644 index c06c942389d3189d168bbf7000fccc106453c153..0000000000000000000000000000000000000000 --- "a/20230307 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" +++ /dev/null @@ -1,106 +0,0 @@ -## 1.作业 - -```sql -CREATE DATABASE td charset utf8; -use td; -CREATE table stuinfo( -stuNO VARCHAR(10) primary key, -stuName VARCHAR(10), -stuSex VARCHAR(10), -stuAge VARCHAR(10), -stuAddress VARCHAR(20), -stuSeat INT -); -CREATE table stuExam( -examNo int primary key auto_increment, -stuNO VARCHAR(10), -writtenExam int, -labExam INT -); -CREATE table stuMarks( -examNo int primary key auto_increment, -stuID VARCHAR(10), -score INT -); -alter table stuExam add foreign key (stuNO) references stuinfo(stuNO); -alter table stuMarks add foreign key (examNo) references stuExam(examNo); - - -INSERT into stuinfo VALUES -('s2501','张秋利','男',20,'美国硅谷',1), -('s2502','李斯文','女',18,'湖北武汉',2), -('s2503','马文才','男',18,'湖南长沙',3), -('s2504','欧阳俊雄','女',21,'湖北武汉',4), -('s2505','梅超风','男',20,'湖北武汉',5), -('s2506','陈旋风','男',19,'美国硅谷',6); - -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); - -INSERT into stuMarks 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,a.stuName,a.stuSex,max(score) from stuinfo a left join stumarks b on a.stuNO=b.stuID group by stuNO; - -# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) -select a.stuNO,a.stuName,a.stuSex,(b.writtenExam+b.labExam)/2 平均分 from stuinfo a left join stuexam b on a.stuNO=b.stuNO; - -# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) -select * 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.* from stuinfo a, (select stuID from stumarks group by stuID having min(score)>=60) b where a.stuNO=b.stuID; - -# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) -select a.* from stuinfo a,(select stuID from stumarks group by stuID) b where a.stuNO=b.stuID; - -# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) -select * from stuinfo where stuNO not in(select stuID from stumarks group by stuID); - -# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) -select a.* from stuinfo a,(select stuID from stumarks group by stuID having max(score)>90) b where a.stuNO=b.stuID; - -# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) -select a.* from stuinfo a,(select stuID from stumarks group by stuID having avg(score)>80) b where a.stuNO=b.stuID; - -# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) -select a.* from -stuinfo a, -(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select max(score) from stumarks where stuID='s2501')) b -where - a.stuNO=b.stuID; - -# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) -select a.* from -stuinfo a, -(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select min(score) from stumarks where stuID='s2501')) b -where - a.stuNO=b.stuID; - -# 13.查询班上比所有男生年龄都要大的女生的信息 -select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); - -# 14.查询出只是比某个男生年龄大的女生的信息 -select * from stuinfo where stuSex='女' and stuAge>(select min(stuAge) from stuinfo where stuSex='男'); - -``` - -