From 057b59574c8b2c38d4846d14cdb95e9e9f214a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9?= Date: Tue, 28 Feb 2023 14:35:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20230213 MySQL\345\256\211\350\243\205.md" | 20 -- ...60\346\215\256\347\261\273\345\236\213.md" | 23 -- ...50\344\270\216\345\241\253\345\200\274.md" | 281 ------------------ .../20230220 \347\272\246\346\235\237.md" | 266 ----------------- ...16\350\277\220\347\256\227\347\254\246.md" | 157 ---------- ...04\344\271\240\347\254\224\350\256\260.md" | 75 ----- ...24\350\256\260\346\225\264\347\220\206.md" | 131 -------- ...32\350\241\250\346\237\245\350\257\242.md" | 250 ++++++++++++++++ 8 files changed, 250 insertions(+), 953 deletions(-) delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" delete mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" "b/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" deleted file mode 100644 index fcaa22f..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" +++ /dev/null @@ -1,20 +0,0 @@ -# 1、笔记 - -```sql -浏览器搜索MySQL,进入官方网页(https://www.mysql.com/downloads/)进行下载,按安装提示进行安装 - -MySQL启动 -在任务栏右键任务管理器,点击服务,找到MySQL57,右键启动 - -环境变量 -在我的电脑中找到MySQL的文件夹,进入bin文件夹,复制路径,打开控制面板,点击系统,高级系统设置,环境变量,在系统变量里的Path文件中新建,将复制的路径粘贴进去,在后面+\,保存退出 - -导出数据 -Windows键+R键打开运行面板,键入cmd,进入运行面板 ->mysql -u root -p 密码 要导出的数据库名 >要保存的路径\脚本名.sql - -导入数据 -在运行面板中登录mysql数据库 -msyql>source 要导入的数据库路径\脚本名.sql -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" "b/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" deleted file mode 100644 index 50205f8..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" +++ /dev/null @@ -1,23 +0,0 @@ -# 1、笔记 - -```sql -SQL语句 -DDL(数据定义语言): create(创建) alter(修改) drop(删除) show(展示) -DML(数据操作语言): insert(增加) delete(删除) update(修改) -DQL(数据查询语言): select(查询) -DCL(数据控制语言): grant(授权) commit(确认) rollback(回滚) -``` - -```sql -数据类型 - 整形:int (int(M):无意义) - int(M) zerofill:不足M位用0补足, - 浮点型:float:小数、单浮点型 - float(M,N):M表示整个数字的长度,N表示小数部分的长度 - 双浮点型: double - double(M,N):与float相同 - 精准型:decimal - 定长字符串:char - 可变字符串:varchar(M) -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" "b/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" deleted file mode 100644 index e32e40f..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" +++ /dev/null @@ -1,281 +0,0 @@ -# 1、笔记 - -```sql -增加表字段: -alter table 表名称 add 字段名 数据类型; -删除表字段: -alter table 表名称 drop 字段名; -修改字段名称: -alter table 表名称 change 旧字段名 新字段名 数据类型; -修改数据类型: -alter table 表名称 modify 字段名 新数据类型; -修改字段顺序: -alter table 表名称 modify 字段名1 数据类型 after 字段名2;(字段1移动到字段2后面) -alter table 表名称 modify 字段名 数据类型 first;(将字段移动到第一位) -修改表名称 -alter table 旧标明 rename to 新表名; -删除列的定义 -alter table 表名 drop 字段名; - -修改表数据(无条件) -update 表名 set 字段名 = 新值; -修改表数据(指定条件) -update 表名 set 字段名 = 新值 where 条件(如 name = 'XXX'); -update 表名 set 字段名 = 新值 where 条件字段名 in (如 where name in('XXX','XXX')); - -删除表数据(无条件) -delete from 表名; -删除表数据(指定条件 行) -delete from 表名 where 条件(如 id = ); - -数据表填值 -insert into 表名称[字段名] values (插入内容); -``` - -# 2、作业 - -```sql --- ## 第1题 -drop database test01_market; --- 1、创建数据库test01_market -create database test01_market charset utf8; --- 2、创建表格customers -use test01_market; --- | 字段名 | 数据类型 | --- | --------- | ----------- | --- | c_num | int(11) | --- | c_name | varchar(50) | --- | c_contact | varchar(50) | --- | c_city | varchar(50) | --- | c_birth | date | -create table customers( -c_num int, -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 varchar(50); --- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) -alter table customers add c_gender char(1); -alter table customers modify 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; - - - --- ## 第2题 --- drop database test02_library; --- 1、创建数据库test02_library -create database test02_library charset utf8; --- 2、创建表格books -use test02_library; --- | 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | --- | ------- | -------- | ------------- | -------- | ---- | --- | b_id | 书编号 | int(11) | 否 | 是 | --- | b_name | 书名 | varchar(50) | 否 | 否 | --- | authors | 作者 | varchar(100) | 否 | 否 | --- | price | 价格 | float | 否 | 否 | --- | pubdate | 出版日期 | year | 否 | 否 | --- | note | 说明 | varchar(100) | 是 | 否 | --- | num | 库存 | int(11) | 否 | 否 | -create table books( -b_id int not null comment '书编号', -b_name varchar(50) not null comment '书名', -`authors` varchar(100) not null comment '作者', -price float not null comment '价格', -pubdate year not null comment '出版日期', -note varchar(100) comment '说明', -num int not null comment '库存' -); --- 3、向books表中插入记录 - --- 1) 指定所有字段名称插入第一条记录 --- --- 2)不指定字段名称插入第二记录 - --- 3)同时插入多条记录(剩下的所有记录) --- --- | 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 | -insert into books(b_id,b_name,`authors`,price,pubdate,note,num) 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','Jant 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'; -select * from books; - - --- ## 第3题 --- drop database test03_bookstore; --- 1、创建数据库test03_bookstore -create database test03_bookstore charset utf8; --- 2、创建book表 -use test03_bookstore; --- ```mysql --- +----------+--------------+------+-----+---------+----------------+ --- | Field | Type | Null | Key | Default | Extra | --- +----------+--------------+------+-----+---------+----------------+ --- | id | int(11) | NO | PRI | NULL | auto_increment | --- | title | varchar(100) | NO | | NULL | | --- | author | varchar(100) | NO | | NULL | | --- | price | double(11,2) | NO | | NULL | | --- | sales | int(11) | NO | | NULL | | --- | stock | int(11) | NO | | NULL | | --- | img_path | varchar(100) | NO | | NULL | | --- +----------+--------------+------+-----+---------+----------------+ --- ``` --- drop table book; -create table book( -id int not null auto_increment, -title varchar(100) not null, -author varchar(100), -price double(11,2) not null, -sales int not null, -stock int not null, -img_path varchar(100) not null, -primary key(`id`) -); --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+-------------+------------+-------+-------+-------+----------------------------+ --- | id | title | author | price | sales | stock | img_path | --- +----+-------------+------------+-------+-------+-------+-----------------------------+ --- | 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | --- | 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | --- +----+---------------+------------+-------+-------+-------+----------------------------+ --- ``` -insert into book values(1,'解忧杂货铺','东野圭吾',27.20,102,98,'upload/books/解忧杂货铺.jpg'), -(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); --- 3、创建用户表users,并插入数据 --- --- ```mysql --- +----------+--------------+------+-----+---------+----------------+ --- | Field | Type | Null | Key | Default | Extra | --- +----------+--------------+------+-----+---------+----------------+ --- | id | int(11) | NO | PRI | NULL | auto_increment | --- | username | varchar(100) | NO | UNI | NULL | | --- | password | varchar(100) | NO | | NULL | | --- | email | varchar(100) | YES | | NULL | | --- +----------+--------------+------+-----+---------+----------------+ --- ``` --- drop table users; -create table users( -id int not null auto_increment, -username varchar(100) not null, -`password` varchar(100) not null, -email varchar(100), -primary key(`id`), -unique key(`username`) -); --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+----------+----------------------------------+--------------------+ --- | id | username | password | email | --- +----+----------+----------------------------------+--------------------+ --- | 1 | admin | 112233 | admin@mxdx.com | --- +----+----------+----------------------------------+--------------------+ --- ``` -insert into users values(1,'admin','112233','admin@mxdx.com'); --- 4、创建订单表orders --- --- ```mysql --- +--------------+--------------+------+-----+---------+-------+ --- | Field | Type | Null | Key | Default | Extra | --- +--------------+--------------+------+-----+---------+-------+ --- | id | varchar(100) | NO | PRI | NULL | | --- | order_time | datetime | NO | | NULL | | --- | total_count | int(11) | NO | | NULL | | --- | total_amount | double(11,2) | NO | | NULL | | --- | state | int(11) | NO | | NULL | | --- | user_id | int(11) | NO | MUL | NULL | | --- +--------------+--------------+------+-----+---------+-------+ --- ``` -create table orders( -id varchar(100) not null, -order_time datetime not null, -total_count int not null, -total_amount double(11,2) not null, -state int not null, -user_id int not null, -primary key(`id`) -); --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----------------+---------------------+-------------+--------------+-------+---------+ --- | id | order_time | total_count | total_amount | state | user_id | --- +----------------+---------------------+-------------+--------------+-------+---------+ --- | 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | --- +----------------+---------------------+-------------+--------------+-------+---------+ --- ``` -insert into orders values('15294258455691','2018-06-20 00:30:45',2,50.20,0,1); --- 5、创建订单明细表order_items --- --- ```mysql --- +----------+--------------+------+-----+---------+----------------+ --- | Field | Type | Null | Key | Default | Extra | --- +----------+--------------+------+-----+---------+----------------+ --- | id | int(11) | NO | PRI | NULL | auto_increment | --- | count | int(11) | NO | | NULL | | --- | amount | double(11,2) | NO | | NULL | | --- | title | varchar(100) | NO | | NULL | | --- | author | varchar(100) | NO | | NULL | | --- | price | double(11,2) | NO | | NULL | | --- | img_path | varchar(100) | NO | | NULL | | --- | order_id | varchar(100) | NO | MUL | NULL | | --- +----------+--------------+------+-----+---------+----------------+ --- ``` -create table order_items( -id int not null auto_increment, -count int 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, -primary key(`id`) -); --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+-------+--------+---------+---------+-------+----------------+----------------+ --- | id |count| amount| title | author | price | img_path | order_id | --- +----+-------+--------+------------+----------+-------+----------------+----------------+ --- | 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | --- | 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | --- +----+-------+--------+------------+----------+-------+------------+----------------+ -insert into order_items values(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg','15294258455691'), -(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg','15294258455691'); -select * from order_items; -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" "b/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" deleted file mode 100644 index e34db4a..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" +++ /dev/null @@ -1,266 +0,0 @@ -# 1、笔记 - -1. ```sql - 唯一键约束 - unique key;(不重复,但可为空,可以有多个列设置) - 主键约束 - primary key;(不重复,不为空,不能多列) - 复合主键 - primary key(XXX,XXX); - 默认建约束 - not null default 默认值 - 自增属性 - auto_increment primary key(后面必须加主键) - 外键约束 - foreign key (从表的字段) references 主表(被依赖的字段); - foreign key (从表的字段) references 主表(被依赖的字段) on update cascade on delete set null(从表数据随主表数据变换) - alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade; - ``` - - - -# 2、作业 - -```sql -#1、创建数据库test01_company -create database test01_company; -alter database test01_company charset utf8; -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(50), -address varchar(50), -country varchar(50), -postalCode varchar(25) -); -desc offices; -select*from offices; - -#3、创建表格employees -#| 字段名 | 数据类型 | -#| --------- | ------------- | -#| empNum | int(11) | -#| lastName | varchar(50) | -#| firstName | varchar(50) | -#| mobile | varchar(25) | -#| code | int | -#| jobTitle | varchar(50) | -#| birth | date | -#| Note | varchar(255) |l -#| Sex | varchar(5) | -create table employees( -empNum int(11), -lastNum varchar(50), -firstName varchar(50), -mobile varchar(25), -`code` int, -jobTitle varchar(50), -birth date, -Note varchar(255), -Sex varchar(5) -); -desc employees; -select*from employees; -#要求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_in -alter table employees rename employees_info; - -#1、创建数据库test02db -create database test02db; -alter database test02db charset utf8; -use test02db; -#2、创建表格pet -#| 字段名 | 字段说明 | 数据类型 | -#| ------- | -------- | ----------- | -#| name | 宠物名称 | varchar(20) | -#| owner | 宠物主人 | varchar(20) | -#| species | 种类 | varchar(20) | -#| sex | 性别 | char(1) | -#| birth | 出生日期 | year | -#| death | 死亡日期 | year | -create table pet( -name varchar(20), -`owner` varchar(20), -species varchar(20), -sex char(1), -birth year, -death year -); -#3、添加记录 -#| 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 | | -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'), -('Chitpy',null,'Bird','f','2008',null); -desc pet; -select*from pet; -#4、 添加字段主人的生日owner_birth。 -alter table pet add owner_birth year; -#5、 将名称为Claws的猫的主人改为kevin -update pet set name='kevin' where name='Claws'; -#6、 将没有死的狗的主人改为duck -update pet set `owner`='duck' where species='Dog'and death is null; -#7、 查询没有主人的宠物的名字; -select name from pet where owner is null; -#8、 查询已经死了的cat的姓名,主人,以及去世时间; -select*from pet where death is not null; -#9、 删除已经死亡的狗 -delete from pet where death='2009'; -#10、查询所有宠物信息 -select*from pet; - -#1、创建数据库:test03_company -create database test03_company; -alter database test03_company charset utf8; -use test03_company; -#2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 -#A.部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 -create table department( - depid int primary key auto_increment, - depname char(10) not null unique key, - deinfo varchar(200) -); -#| 部门编号 | 部门名称 | 部门简介 | -#| -------- | -------- | ------------ | -#| 111 | 生产部 | Null | -#| 222 | 销售部 | Null | -#| 333 | 人事部 | 人力资源管理 | -insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源部'); -#B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 -#*​雇员编号为主键; -#*部门编号为外键,外键约束等级为(on update cascade 和on delete set null); -#*性别默认为男; -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 -); - #雇员表: -#| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | -#| -------- | ---- | ---- | ---------- | ---------- | ------------ | -#| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | -#| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | -#| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | -#| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | -insert into employee values -(1001,'张三','男','高级工程师','1975-1-1',111), -(1002,'李四','女','助工','1985-1-1',111), -(1003,'王五','男','工程师','1978-11-11',222), -(1004,'张六','男','工程师','1999-1-1',222); -alter table employee add constraint kf_employee_depid foreign key (depid) references department(depid) on update cascade on delete cascade; -#C工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 -#3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) -#| empid |basesalary|titlesarlary| deduction | -#| -------- | -------- | -------- | ---- | -#| 1001 | 2200 | 1100 | 200 | -#| 1002 | 1200 | 200 | NULL | -#| 1003 | 2900 | 700 | 200 | -#| 1004 | 1950 | 700 | 150 | -create table salary( -empid int primary key auto_increment, -basesalary int, -titlesalary int, -deduction int -); -insert into salary values -(1001,2200,1100,200), -(1002,1200,200,null), -(1003,2900,700,200), -(1004,1950,700,150); -desc salary; -select*from salary; -alter table employee add constraint kf_salary_empid foreign key(empid) references salary(empid) on update cascade on delete cascade; - -#1、创建一个数据库:test04_school -create database test04_school; -alter database test04_school charset utf8; -use test04_school; -#2、创建如下表格 -#表1 Department表的定义 -#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -#| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | -#| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | -#| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | -create table Deparment( -DepNo int(10) primary key not null, -DepName varchar(20) not null, -DepNote varchar(20) -); -#3、添加记录 -#| **DepNo** | **DepName** | **DepNote** | -#| --------- | ----------- | ------------------ | -#| 601 | 软件技术系 | 软件技术等专业 | -#| 602 | 网络技术系 | 多媒体技术等专业 | -#| 603 | 艺术设计系 | 广告艺术设计等专业 | -#| 604 | 管理工程系 | 连锁经营管理等专业 | -insert into Deparment values(601,'软件技术系','软件技术等专业'), -(602,'网络技术系','多媒体技术等专业'), -(603,'艺术设计系','广告艺术设计等专业'), -(604,'管理工程系','连锁经营管理等专业'); -#表2 Teacher表的定义 -#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -#| Number | 教工号 | int | 是 | 否 | 是 | 是 | -#| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | -#| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | -#| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | -#| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | -#| Salary | 工资 | float | 否 | 否 | 否 | 否 | -#| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | -create table Teacher( -Number int primary key, -Name varchar(30) not null, -Sex varchar(4), -Birth date, -DepNo int, -Salary float, -Address varchar(100) -); -#| **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 | 重庆市南岸区 | -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,'重庆市南岸区'); -#4、用SELECT语句查询Teacher表的所有记录。 -alter table Teacher add constraint ky_teacher_depno foreign key (DepNo) references Deparment(DepNo) on update cascade on delete cascade; -select*from Teacher; -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" "b/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" deleted file mode 100644 index d1bfab7..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" +++ /dev/null @@ -1,157 +0,0 @@ -# 1、笔记 - -1. ```sql - 查询去重 - select distinct 字段名 from 表名 [where]; - 查询%表示多个字符 - -- 查询名字中有“熊”的员工 - select 字段名 from 表名 where 字段名 like '%熊%'; - -- 查询姓“熊”的员工 - select 字段名 from 表名 where 字段名 like '熊%'; - 查询_表示一个字符 - -- 查询叫“熊某”的员工 - select 字段名 from 表名 where 字段名 like '熊_'; - 查询异或(只能符合其中一种条件) - select 字段名 from 表名 where 条件 xor 条件; - - - 运算null - 字段名 is null;/字段名 <=> null; - 取整 - div - 求余 - % / mod - 区间范围(即x<=值<=y) - between x and y; - not between x and y; - 集合范围(即(x,y,z)) - in (x,y,z); - not in(x,y,z); - 运算实发工资 - select 字段名,(运算式) as 别名 from 表名; - 如果有null参与运算 - select 字段名,字段*(ifnull(字段,代替的值)) as 别名 from 表名; - ``` - - - -# 2、作业 - -```sql -use mockQ; --- ## 第1题:员工表 --- --- ```mysql -drop table if exists `employee`; --- #创建employee表 -CREATE TABLE employee( - id INT, - `name` VARCHAR(20), - sex VARCHAR(20), - tel VARCHAR(20), - addr VARCHAR(50), - salary FLOAT -); --- --- #添加信息 -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); --- ``` --- | **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 | --- --- **要求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 and sex = '男') or (addr = '广东深圳,广州' and sex = '男'); --- **要求8:**查询每个员工的年薪,显示“姓名、年薪” -select `name` as `姓名`,salary as `年薪` from employee; --- ```mysql --- --- ``` --- --- ## 第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); --- ``` --- --- 表数据样例: --- --- ```mysql --- +-------------+-----------+---------+------------+--------------+ --- | 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 | --- +-------------+-----------+---------+------------+--------------+ --- ``` --- countries_info表中存储了国家名称(name)、所属大陆(continent)、面积(area)、人口(population)和 GDP 值。 --- **要求1:** 查询大国 的国家名称、人口和面积。 --- --- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : --- --- - 面积至少为 300万平方公里(即,3000000 km2) --- --- - 人口至少为 2500 万(即 25000000) -select `name`,area,population 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` as `国家名`,population as `人口`,gdp as `GDP值`,gdp/population `人均贡献GDP值` from countries_info; --- **要求7:**查询人均贡献GDP值低于1000的国家信息。 -select * from countries_info where gdp/population < 1000; --- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” -select `name` as `国家名`,area as `面积`,population as `人口`,area/population `人均国土面积值` from countries_info; --- ```mysql --- --- ``` --- --- -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" "b/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" deleted file mode 100644 index 2ae9e42..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" +++ /dev/null @@ -1,75 +0,0 @@ -# 1笔记 - -## 1.1 分组函数 - -```sql --- 计算平均值 -avg(x) - select avg(字段名) from 表名; --- 计算和 -sum(x) - #简单求和 - select sum(字段名) from 表名; - #计算表达式求和 - select sum(字段名*(1+ifnull(字段名,代替值))); --- 计算最大值 -max(x) - select max(字段名) from 表名; --- 计算最小值 -min(x) - select min(字段名) from 表名; --- 统计记录 -count(x) - #统计实际的行数 - select count(*) from 表名; - #只统计"字段/表达式"部分非空值得行数 - select count(字段/表达式) from 表名; -``` - -## 1.2 单行函数 - -```sql --- 返回绝对值 -abs(x) --- 返回比x大的最小整数 -ceil(x) --- 返回比x小的最小整数 -floor(x) --- 求(x/y)的模 -mod(x,y) --- 随机数(0-1) -``` - -## 1.3 窗口函数(属于单行函数的一种) - -```sql --- 顺序排序 -row_number() --- 并列排序 - #排序字段出现重复值时,跳过重复序号,如1,1,3.... - rank() - #排序字段出现重复值时,不跳过重复序号,如1,1,2.... - dense_rank() - --- 排名百分比(rank-1)/(rows-1) -percent_rank() --- 累积分布值 -cume_dist() - --- 返回当前行的前面n行的expr值 -lag(expr,n) --- 返回当前行的后面n行的expr值 -lead(expr,n) - --- 返回当前分组第一行的expr值 -first_value(expr) --- 返回当前分组每一个rank最后一行的expr值 -last_value(expr) --- 返回当前分组第n行的expr值 -nth_value(expt) - --- 窗口函数语法格式 -函数名([参数列表]) over () -函数名([参数列表]) over (子句) -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" "b/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" deleted file mode 100644 index c332f32..0000000 --- "a/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" +++ /dev/null @@ -1,131 +0,0 @@ -# 1、MySQL安装 - -```sql -浏览器搜索MySQL,进入官方网页(https://www.mysql.com/downloads/)进行下载,按安装提示进行安装 - -MySQL启动 -在任务栏右键任务管理器,点击服务,找到MySQL57,右键启动 - -环境变量 -在我的电脑中找到MySQL的文件夹,进入bin文件夹,复制路径,打开控制面板,点击系统,高级系统设置,环境变量,在系统变量里的Path文件中新建,将复制的路径粘贴进去,在后面+\,保存退出 - -导出数据 -Windows键+R键打开运行面板,键入cmd,进入运行面板 ->mysql -u root -p 密码 要导出的数据库名 >要保存的路径\脚本名.sql - -导入数据 -在运行面板中登录mysql数据库 -msyql>source 要导入的数据库路径\脚本名.sql -``` - - - -# 2 、SQL语句 数据类型 - -```sql -SQL语句 -DDL(数据定义语言): create(创建) alter(修改) drop(删除) show(展示) -DML(数据操作语言): insert(增加) delete(删除) update(修改) -DQL(数据查询语言): select(查询) -DCL(数据控制语言): grant(授权) commit(确认) rollback(回滚) - -数据类型 - 整形:int (int(M):无意义) - int(M) zerofill:不足M位用0补足, - 浮点型:float:小数、单浮点型 - float(M,N):M表示整个数字的长度,N表示小数部分的长度 - 双浮点型: double - double(M,N):与float相同 - 精准型:decimal - 定长字符串:char - 可变字符串:varchar(M) -``` - -# 3、DDL建表与填值 - -```sql -增加表字段: -alter table 表名称 add 字段名 数据类型; -删除表字段: -alter table 表名称 drop 字段名; -修改字段名称: -alter table 表名称 change 旧字段名 新字段名 数据类型; -修改数据类型: -alter table 表名称 modify 字段名 新数据类型; -修改字段顺序: -alter table 表名称 modify 字段名1 数据类型 after 字段名2;(字段1移动到字段2后面) -alter table 表名称 modify 字段名 数据类型 first;(将字段移动到第一位) -修改表名称 -alter table 旧标明 rename to 新表名; -删除列的定义 -alter table 表名 drop 字段名; - -修改表数据(无条件) -update 表名 set 字段名 = 新值; -修改表数据(指定条件) -update 表名 set 字段名 = 新值 where 条件(如 name = 'XXX'); -update 表名 set 字段名 = 新值 where 条件字段名 in (如 where name in('XXX','XXX')); - -删除表数据(无条件) -delete from 表名; -删除表数据(指定条件 行) -delete from 表名 where 条件(如 id = ); - -数据表填值 -insert into 表名称[字段名] values (插入内容); -``` - -# 4、约束 - -```sql -唯一键约束 -unique key;(不重复,但可为空,可以有多个列设置) -主键约束 -primary key;(不重复,不为空,不能多列) -复合主键 -primary key(XXX,XXX); -默认建约束 -not null default 默认值 -自增属性 -auto_increment primary key(后面必须加主键) -外键约束 -foreign key (从表的字段) references 主表(被依赖的字段); -foreign key (从表的字段) references 主表(被依赖的字段) on update cascade on delete set null(从表数据随主表数据变换) -alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade; -``` - -# 5、基础查询与运算符 - -```sql -查询去重 -select distinct 字段名 from 表名 [where]; -查询%表示多个字符 --- 查询名字中有“熊”的员工 -select 字段名 from 表名 where 字段名 like '%熊%'; --- 查询姓“熊”的员工 -select 字段名 from 表名 where 字段名 like '熊%'; -查询_表示一个字符 --- 查询叫“熊某”的员工 -select 字段名 from 表名 where 字段名 like '熊_'; -查询异或(只能符合其中一种条件) -select 字段名 from 表名 where 条件 xor 条件; - - -运算null -字段名 is null;/字段名 <=> null; -取整 -div -求余 -% / mod -区间范围(即x<=值<=y) -between x and y; -not between x and y; -集合范围(即(x,y,z)) -in (x,y,z); -not in(x,y,z); -运算实发工资 -select 字段名,(运算式) as 别名 from 表名; -如果有null参与运算 -select 字段名,字段*(ifnull(字段,代替的值)) as 别名 from 表名; -``` - diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" "b/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" new file mode 100644 index 0000000..5873ceb --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" @@ -0,0 +1,250 @@ +# 1、笔记 + +```sql +-- 计算平均值 +avg(x) + select avg(字段名) from 表名; +-- 计算和 +sum(x) + #简单求和 + select sum(字段名) from 表名; + #计算表达式求和 + select sum(字段名*(1+ifnull(字段名,代替值))); +-- 计算最大值 +max(x) + select max(字段名) from 表名; +-- 计算最小值 +min(x) + select min(字段名) from 表名; +-- 统计记录 +count(x) + #统计实际的行数 + select count(*) from 表名; + #只统计"字段/表达式"部分非空值得行数 + select count(字段/表达式) from 表名; + +-- 内联查询 +select 字段名 from 表1 [inner] join 表2 on 条件; -- 显式内连接 +select 字段名 from 表1,表2 where 条件; -- 隐式内连接 +select 字段名 from 表1 natural join 表2; -- 自然连接 +-- 左外联查询 +select 字段名 from 表1 left join 表2 on 条件; +-- 右外联查询 +select 字段名 from 表1 right join 表2 on 条件; +-- 多表查询 +select 字段名 from 表1,表2,表3... where 表1.关联字段 = 表2.关联字段 and 表1.关联字段 = 表3.关联字段 ...; +select 字段名 from 表1 inner join 表2 on 条件 + inner join 表3 on 条件...; +``` + + + +# 2、作业 + +```sql +use mockq; +-- 1.设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 +-- 2.数据库的表结构 +-- 表(一)Student (学生表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(主码) +-- Sname varchar (20) 否 学生姓名 +-- Ssex varchar (20) 否 学生性别 +-- Sbirthday datetime 可 学生出生年月 +-- Class varchar (20) 可 学生所在班级 +create table student( + Sno varchar(20) not null, + Sname varchar(20) not null, + Ssex varchar(20) not null, + Sbirthday datetime, + Class varchar(20), + primary key(Sno) +); + +-- 表(二)Course(课程表) +-- 属性名 数据类型 可否为空 含义 +-- Cno varchar (20) 否 课程号(主码) +-- Cname varchar (20) 否 课程名称 +-- Tno varchar (20) 否 教工编号(外码) +-- drop table score; +create table course( + Cno varchar(20) not null, + Cname varchar(20) not null, + Tno varchar(20) not null, + primary key(Cno), + foreign key(Tno) references teacher(Tno) on update cascade +); +-- alter table course add constraint fk_course_Tno foreign key(Tno) references teacher(Tno); +-- alter table course drop foreign key fk_course_Tno; +-- 表(三)Score(成绩表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(外码) +-- Cno varchar (20) 否 课程号(外码) +-- Degree Decimal(4,1) 可 成绩 +-- 主码:Sno+ Cno +create table score( + Sno varchar(20) not null, + Cno varchar(20) not null, + Degree decimal(4,1), + foreign key(Sno) references student(Sno) on update cascade, + foreign key(Cno) references course(Cno) on update cascade +); +alter table score add primary key(Sno,Cno); +-- 表(四)Teacher(教师表) +-- 属性名 数据类型 可否为空 含义 +-- Tno varchar (20) 否 教工编号(主码) +-- Tname varchar (20) 否 教工姓名 +-- Tsex varchar (20) 否 教工性别 +-- Tbirthday datetime 可 教工出生年月 +-- Prof varchar (20) 可 职称 +-- Depart varchar (20) 否 教工所在部门 +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, + primary key(Tno) +); +-- 3.数据库中的数据: +-- -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select * from student inner join course; +-- -- 2,查询没有学生的教师的所有信息 +select * from student,score,course,teacher where student.Sno = score.Sno and course.Cno = score.Cno and course.Tno = teacher.Tno; + +select * from student inner join score on student.Sno = score.Sno + inner join course on course.Cno = score.Cno + inner join teacher on course.Tno = teacher.Tno; + +-- +-- 表(一)Student +-- Sno Sname Ssex Sbirthday class +-- 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 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'); +-- 表(二)Course +-- Cno Cname Tno +-- 3-105 计算机导论 825 +-- 3-245 操作系统 804 +-- 6-166 数字电路 856 +-- 9-888 高等数学 831 +insert into course VALUES + ('3-105','计算机导论','825'), + ('3-245','操作系统','804'), + ('6-166','数字电路','856'), + ('9-888','高等数学','831'); +-- 表(三)Score +-- Sno Cno Degree +-- 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 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','64'), + ('108','3-105','78'), + ('101','6-166','85'), + ('107','6-166','79'), + ('108','6-166','81'); +-- 表(四)Teacher +-- Tno Tname Tsex Tbirthday Prof Depart +-- 804 李诚 男 1958-12-2 副教授 计算机系 +-- 856 张旭 男 1969-3-12 讲师 电子工程系 +-- 825 王萍 女 1972-5-5 助教 计算机系 +-- 831 刘冰 女 1977-8-14 助教 电子工程系 +insert into teacher values + ('804','李诚','男','1958-12-2','副教授','计算机系'), + ('856','张旭','男','1969-3-12','讲师','电子工程系'), + ('825','王萍','女','1972-5-5','助教','计算机系'), + ('831','刘冰','女','1977-8-14','助教','电子工程系'); +-- 4.查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +select Sno,Cno from score where Degree = (select max(Degree) from score); +-- ② 查询所有学生的Sname、Cno和Degree列。 +select student.Sname,score.Cno,score.Degree from student,score where score.Sno = student.Sno; -- 隐式内连接 + +select student.Sname,score.Cno,score.Degree from student inner join score on score.Sno = student.Sno; -- 显式内连接 + +select student.Sname,score.Cno,score.Degree from student natural join score; -- 自然连接 +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select score.Sno,course.Cname,score.Degree from score inner join course on score.Cno = course.Cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select student.Sname,course.Cname, score.Degree from student,course,score where student.Sno = score.Sno and course.Cno = score.Cno; +-- ⑤ 查询“95033”班学生的平均分。 +select avg(score.Degree) from score inner join student on student.Sno = score.Sno and student.Class = '95033'; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Degree = (select !(max(score.Degree)) from score); +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +-- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from student where year(Sbirthday) = '1977'; +-- ⑩ 查询“张旭“教师任课的学生成绩。 +select score.Degree from score inner join course on score.Cno = course.Cno + inner join teacher on course.Tno = teacher.Tno + and teacher.Tname = '张旭'; +-- ⑪ 查询选修某课程的同学人数多于5人的教师姓名。 +select teacher.Tname from teacher inner join course on teacher.Tno = course.Tno + inner join score on course.Cno = score.Cno + and score.Cno = (select count(Cno) > 5 from score); +-- ⑫ 查询出“计算机系“教师所教课程的成绩表。 +select score.Sno,score.Cno,score.Degree from course inner join score on course.Cno = score.Cno + inner join teacher on course.Tno = teacher.Tno and teacher.Depart = '计算机系'; +-- ⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from teacher where Prof in (select distinct Prof from teacher); +-- select distinct Prof from teacher; +-- ⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select score.Cno,score.Sno,score.Degree from score inner join course on score.Cno = course.Cno + and score.Cno = '3-105' + and score.Degree > 75 order by score.Degree desc; +-- ⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select score.Cno,score.Sno,score.Degree from score inner join course on score.Cno = course.Cno + and score.Cno = '3-105' + and score.Degree > 75; +-- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 +select * from score where Degree < (select avg(Degree) from score); +-- ⑰ 查询所有任课教师的Tname和Depart. +select Tname,Depart from teacher; +-- ⑱ 查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from teacher inner join course on teacher.Tno = course.Tno and course.Cno is null; +-- ⑲ 查询“男”教师及其所上的课程。 +select teacher.Tname,course.Cname from course inner join teacher on teacher.Tno = course.Tno and teacher.Tsex = '男'; +-- ⑳ 查询最高分同学的Sno、Cno和Degree列。 +select * from score where Degree = (select max(Degree) from score); +-- 21 查询和“李军”同性别的所有同学的Sname. +select Sname from student where Ssex = '男' and Sname != '李军'; +-- 22 查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex = '男' and Sname != '李军' and Class = '95033'; +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select score.Cno,score.Sno,score.Degree from score inner join student on score.Sno = student.Sno + inner join course on score.Cno = course.Cno + and course.Cname = '计算机导论' + and student.Ssex = '男'; +``` + -- Gitee From c08442a6914c06370bd1a3b9abbdeddbe8eb5d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9?= Date: Tue, 28 Feb 2023 14:37:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20230213 MySQL\345\256\211\350\243\205.md" | 20 ++ ...60\346\215\256\347\261\273\345\236\213.md" | 23 ++ ...50\344\270\216\345\241\253\345\200\274.md" | 281 ++++++++++++++++++ .../20230220 \347\272\246\346\235\237.md" | 266 +++++++++++++++++ ...16\350\277\220\347\256\227\347\254\246.md" | 157 ++++++++++ ...04\344\271\240\347\254\224\350\256\260.md" | 75 +++++ ...24\350\256\260\346\225\264\347\220\206.md" | 131 ++++++++ 7 files changed, 953 insertions(+) create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" create mode 100644 "10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" "b/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" new file mode 100644 index 0000000..fcaa22f --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230213 MySQL\345\256\211\350\243\205.md" @@ -0,0 +1,20 @@ +# 1、笔记 + +```sql +浏览器搜索MySQL,进入官方网页(https://www.mysql.com/downloads/)进行下载,按安装提示进行安装 + +MySQL启动 +在任务栏右键任务管理器,点击服务,找到MySQL57,右键启动 + +环境变量 +在我的电脑中找到MySQL的文件夹,进入bin文件夹,复制路径,打开控制面板,点击系统,高级系统设置,环境变量,在系统变量里的Path文件中新建,将复制的路径粘贴进去,在后面+\,保存退出 + +导出数据 +Windows键+R键打开运行面板,键入cmd,进入运行面板 +>mysql -u root -p 密码 要导出的数据库名 >要保存的路径\脚本名.sql + +导入数据 +在运行面板中登录mysql数据库 +msyql>source 要导入的数据库路径\脚本名.sql +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" "b/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" new file mode 100644 index 0000000..50205f8 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230215 SQL\350\257\255\345\217\245 \346\225\260\346\215\256\347\261\273\345\236\213.md" @@ -0,0 +1,23 @@ +# 1、笔记 + +```sql +SQL语句 +DDL(数据定义语言): create(创建) alter(修改) drop(删除) show(展示) +DML(数据操作语言): insert(增加) delete(删除) update(修改) +DQL(数据查询语言): select(查询) +DCL(数据控制语言): grant(授权) commit(确认) rollback(回滚) +``` + +```sql +数据类型 + 整形:int (int(M):无意义) + int(M) zerofill:不足M位用0补足, + 浮点型:float:小数、单浮点型 + float(M,N):M表示整个数字的长度,N表示小数部分的长度 + 双浮点型: double + double(M,N):与float相同 + 精准型:decimal + 定长字符串:char + 可变字符串:varchar(M) +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" "b/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" new file mode 100644 index 0000000..e32e40f --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230216 DDL\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" @@ -0,0 +1,281 @@ +# 1、笔记 + +```sql +增加表字段: +alter table 表名称 add 字段名 数据类型; +删除表字段: +alter table 表名称 drop 字段名; +修改字段名称: +alter table 表名称 change 旧字段名 新字段名 数据类型; +修改数据类型: +alter table 表名称 modify 字段名 新数据类型; +修改字段顺序: +alter table 表名称 modify 字段名1 数据类型 after 字段名2;(字段1移动到字段2后面) +alter table 表名称 modify 字段名 数据类型 first;(将字段移动到第一位) +修改表名称 +alter table 旧标明 rename to 新表名; +删除列的定义 +alter table 表名 drop 字段名; + +修改表数据(无条件) +update 表名 set 字段名 = 新值; +修改表数据(指定条件) +update 表名 set 字段名 = 新值 where 条件(如 name = 'XXX'); +update 表名 set 字段名 = 新值 where 条件字段名 in (如 where name in('XXX','XXX')); + +删除表数据(无条件) +delete from 表名; +删除表数据(指定条件 行) +delete from 表名 where 条件(如 id = ); + +数据表填值 +insert into 表名称[字段名] values (插入内容); +``` + +# 2、作业 + +```sql +-- ## 第1题 +drop database test01_market; +-- 1、创建数据库test01_market +create database test01_market charset utf8; +-- 2、创建表格customers +use test01_market; +-- | 字段名 | 数据类型 | +-- | --------- | ----------- | +-- | c_num | int(11) | +-- | c_name | varchar(50) | +-- | c_contact | varchar(50) | +-- | c_city | varchar(50) | +-- | c_birth | date | +create table customers( +c_num int, +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 varchar(50); +-- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +alter table customers add c_gender char(1); +alter table customers modify 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; + + + +-- ## 第2题 +-- drop database test02_library; +-- 1、创建数据库test02_library +create database test02_library charset utf8; +-- 2、创建表格books +use test02_library; +-- | 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +-- | ------- | -------- | ------------- | -------- | ---- | +-- | b_id | 书编号 | int(11) | 否 | 是 | +-- | b_name | 书名 | varchar(50) | 否 | 否 | +-- | authors | 作者 | varchar(100) | 否 | 否 | +-- | price | 价格 | float | 否 | 否 | +-- | pubdate | 出版日期 | year | 否 | 否 | +-- | note | 说明 | varchar(100) | 是 | 否 | +-- | num | 库存 | int(11) | 否 | 否 | +create table books( +b_id int not null comment '书编号', +b_name varchar(50) not null comment '书名', +`authors` varchar(100) not null comment '作者', +price float not null comment '价格', +pubdate year not null comment '出版日期', +note varchar(100) comment '说明', +num int not null comment '库存' +); +-- 3、向books表中插入记录 + +-- 1) 指定所有字段名称插入第一条记录 +-- +-- 2)不指定字段名称插入第二记录 + +-- 3)同时插入多条记录(剩下的所有记录) +-- +-- | 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 | +insert into books(b_id,b_name,`authors`,price,pubdate,note,num) 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','Jant 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'; +select * from books; + + +-- ## 第3题 +-- drop database test03_bookstore; +-- 1、创建数据库test03_bookstore +create database test03_bookstore charset utf8; +-- 2、创建book表 +use test03_bookstore; +-- ```mysql +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | title | varchar(100) | NO | | NULL | | +-- | author | varchar(100) | NO | | NULL | | +-- | price | double(11,2) | NO | | NULL | | +-- | sales | int(11) | NO | | NULL | | +-- | stock | int(11) | NO | | NULL | | +-- | img_path | varchar(100) | NO | | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +-- drop table book; +create table book( +id int not null auto_increment, +title varchar(100) not null, +author varchar(100), +price double(11,2) not null, +sales int not null, +stock int not null, +img_path varchar(100) not null, +primary key(`id`) +); +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- ```mysql +-- +----+-------------+------------+-------+-------+-------+----------------------------+ +-- | id | title | author | price | sales | stock | img_path | +-- +----+-------------+------------+-------+-------+-------+-----------------------------+ +-- | 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | +-- | 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | +-- +----+---------------+------------+-------+-------+-------+----------------------------+ +-- ``` +insert into book values(1,'解忧杂货铺','东野圭吾',27.20,102,98,'upload/books/解忧杂货铺.jpg'), +(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +-- 3、创建用户表users,并插入数据 +-- +-- ```mysql +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | username | varchar(100) | NO | UNI | NULL | | +-- | password | varchar(100) | NO | | NULL | | +-- | email | varchar(100) | YES | | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +-- drop table users; +create table users( +id int not null auto_increment, +username varchar(100) not null, +`password` varchar(100) not null, +email varchar(100), +primary key(`id`), +unique key(`username`) +); +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- ```mysql +-- +----+----------+----------------------------------+--------------------+ +-- | id | username | password | email | +-- +----+----------+----------------------------------+--------------------+ +-- | 1 | admin | 112233 | admin@mxdx.com | +-- +----+----------+----------------------------------+--------------------+ +-- ``` +insert into users values(1,'admin','112233','admin@mxdx.com'); +-- 4、创建订单表orders +-- +-- ```mysql +-- +--------------+--------------+------+-----+---------+-------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +--------------+--------------+------+-----+---------+-------+ +-- | id | varchar(100) | NO | PRI | NULL | | +-- | order_time | datetime | NO | | NULL | | +-- | total_count | int(11) | NO | | NULL | | +-- | total_amount | double(11,2) | NO | | NULL | | +-- | state | int(11) | NO | | NULL | | +-- | user_id | int(11) | NO | MUL | NULL | | +-- +--------------+--------------+------+-----+---------+-------+ +-- ``` +create table orders( +id varchar(100) not null, +order_time datetime not null, +total_count int not null, +total_amount double(11,2) not null, +state int not null, +user_id int not null, +primary key(`id`) +); +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- ```mysql +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | id | order_time | total_count | total_amount | state | user_id | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- ``` +insert into orders values('15294258455691','2018-06-20 00:30:45',2,50.20,0,1); +-- 5、创建订单明细表order_items +-- +-- ```mysql +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | count | int(11) | NO | | NULL | | +-- | amount | double(11,2) | NO | | NULL | | +-- | title | varchar(100) | NO | | NULL | | +-- | author | varchar(100) | NO | | NULL | | +-- | price | double(11,2) | NO | | NULL | | +-- | img_path | varchar(100) | NO | | NULL | | +-- | order_id | varchar(100) | NO | MUL | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +create table order_items( +id int not null auto_increment, +count int 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, +primary key(`id`) +); +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- ```mysql +-- +----+-------+--------+---------+---------+-------+----------------+----------------+ +-- | id |count| amount| title | author | price | img_path | order_id | +-- +----+-------+--------+------------+----------+-------+----------------+----------------+ +-- | 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | +-- | 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | +-- +----+-------+--------+------------+----------+-------+------------+----------------+ +insert into order_items values(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg','15294258455691'), +(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg','15294258455691'); +select * from order_items; +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" "b/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" new file mode 100644 index 0000000..e34db4a --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230220 \347\272\246\346\235\237.md" @@ -0,0 +1,266 @@ +# 1、笔记 + +1. ```sql + 唯一键约束 + unique key;(不重复,但可为空,可以有多个列设置) + 主键约束 + primary key;(不重复,不为空,不能多列) + 复合主键 + primary key(XXX,XXX); + 默认建约束 + not null default 默认值 + 自增属性 + auto_increment primary key(后面必须加主键) + 外键约束 + foreign key (从表的字段) references 主表(被依赖的字段); + foreign key (从表的字段) references 主表(被依赖的字段) on update cascade on delete set null(从表数据随主表数据变换) + alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade; + ``` + + + +# 2、作业 + +```sql +#1、创建数据库test01_company +create database test01_company; +alter database test01_company charset utf8; +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(50), +address varchar(50), +country varchar(50), +postalCode varchar(25) +); +desc offices; +select*from offices; + +#3、创建表格employees +#| 字段名 | 数据类型 | +#| --------- | ------------- | +#| empNum | int(11) | +#| lastName | varchar(50) | +#| firstName | varchar(50) | +#| mobile | varchar(25) | +#| code | int | +#| jobTitle | varchar(50) | +#| birth | date | +#| Note | varchar(255) |l +#| Sex | varchar(5) | +create table employees( +empNum int(11), +lastNum varchar(50), +firstName varchar(50), +mobile varchar(25), +`code` int, +jobTitle varchar(50), +birth date, +Note varchar(255), +Sex varchar(5) +); +desc employees; +select*from employees; +#要求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_in +alter table employees rename employees_info; + +#1、创建数据库test02db +create database test02db; +alter database test02db charset utf8; +use test02db; +#2、创建表格pet +#| 字段名 | 字段说明 | 数据类型 | +#| ------- | -------- | ----------- | +#| name | 宠物名称 | varchar(20) | +#| owner | 宠物主人 | varchar(20) | +#| species | 种类 | varchar(20) | +#| sex | 性别 | char(1) | +#| birth | 出生日期 | year | +#| death | 死亡日期 | year | +create table pet( +name varchar(20), +`owner` varchar(20), +species varchar(20), +sex char(1), +birth year, +death year +); +#3、添加记录 +#| 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 | | +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'), +('Chitpy',null,'Bird','f','2008',null); +desc pet; +select*from pet; +#4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth year; +#5、 将名称为Claws的猫的主人改为kevin +update pet set name='kevin' where name='Claws'; +#6、 将没有死的狗的主人改为duck +update pet set `owner`='duck' where species='Dog'and death is null; +#7、 查询没有主人的宠物的名字; +select name from pet where owner is null; +#8、 查询已经死了的cat的姓名,主人,以及去世时间; +select*from pet where death is not null; +#9、 删除已经死亡的狗 +delete from pet where death='2009'; +#10、查询所有宠物信息 +select*from pet; + +#1、创建数据库:test03_company +create database test03_company; +alter database test03_company charset utf8; +use test03_company; +#2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +#A.部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +); +#| 部门编号 | 部门名称 | 部门简介 | +#| -------- | -------- | ------------ | +#| 111 | 生产部 | Null | +#| 222 | 销售部 | Null | +#| 333 | 人事部 | 人力资源管理 | +insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源部'); +#B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +#*​雇员编号为主键; +#*部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +#*性别默认为男; +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 +); + #雇员表: +#| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +#| -------- | ---- | ---- | ---------- | ---------- | ------------ | +#| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +#| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +#| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +#| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +insert into employee values +(1001,'张三','男','高级工程师','1975-1-1',111), +(1002,'李四','女','助工','1985-1-1',111), +(1003,'王五','男','工程师','1978-11-11',222), +(1004,'张六','男','工程师','1999-1-1',222); +alter table employee add constraint kf_employee_depid foreign key (depid) references department(depid) on update cascade on delete cascade; +#C工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +#3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +#| empid |basesalary|titlesarlary| deduction | +#| -------- | -------- | -------- | ---- | +#| 1001 | 2200 | 1100 | 200 | +#| 1002 | 1200 | 200 | NULL | +#| 1003 | 2900 | 700 | 200 | +#| 1004 | 1950 | 700 | 150 | +create table salary( +empid int primary key auto_increment, +basesalary int, +titlesalary int, +deduction int +); +insert into salary values +(1001,2200,1100,200), +(1002,1200,200,null), +(1003,2900,700,200), +(1004,1950,700,150); +desc salary; +select*from salary; +alter table employee add constraint kf_salary_empid foreign key(empid) references salary(empid) on update cascade on delete cascade; + +#1、创建一个数据库:test04_school +create database test04_school; +alter database test04_school charset utf8; +use test04_school; +#2、创建如下表格 +#表1 Department表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +#| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +#| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +create table Deparment( +DepNo int(10) primary key not null, +DepName varchar(20) not null, +DepNote varchar(20) +); +#3、添加记录 +#| **DepNo** | **DepName** | **DepNote** | +#| --------- | ----------- | ------------------ | +#| 601 | 软件技术系 | 软件技术等专业 | +#| 602 | 网络技术系 | 多媒体技术等专业 | +#| 603 | 艺术设计系 | 广告艺术设计等专业 | +#| 604 | 管理工程系 | 连锁经营管理等专业 | +insert into Deparment values(601,'软件技术系','软件技术等专业'), +(602,'网络技术系','多媒体技术等专业'), +(603,'艺术设计系','广告艺术设计等专业'), +(604,'管理工程系','连锁经营管理等专业'); +#表2 Teacher表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| Number | 教工号 | int | 是 | 否 | 是 | 是 | +#| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +#| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +#| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +#| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +#| Salary | 工资 | float | 否 | 否 | 否 | 否 | +#| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +create table Teacher( +Number int primary key, +Name varchar(30) not null, +Sex varchar(4), +Birth date, +DepNo int, +Salary float, +Address varchar(100) +); +#| **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 | 重庆市南岸区 | +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,'重庆市南岸区'); +#4、用SELECT语句查询Teacher表的所有记录。 +alter table Teacher add constraint ky_teacher_depno foreign key (DepNo) references Deparment(DepNo) on update cascade on delete cascade; +select*from Teacher; +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" "b/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..d1bfab7 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230222 \346\237\245\350\257\242\344\270\216\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,157 @@ +# 1、笔记 + +1. ```sql + 查询去重 + select distinct 字段名 from 表名 [where]; + 查询%表示多个字符 + -- 查询名字中有“熊”的员工 + select 字段名 from 表名 where 字段名 like '%熊%'; + -- 查询姓“熊”的员工 + select 字段名 from 表名 where 字段名 like '熊%'; + 查询_表示一个字符 + -- 查询叫“熊某”的员工 + select 字段名 from 表名 where 字段名 like '熊_'; + 查询异或(只能符合其中一种条件) + select 字段名 from 表名 where 条件 xor 条件; + + + 运算null + 字段名 is null;/字段名 <=> null; + 取整 + div + 求余 + % / mod + 区间范围(即x<=值<=y) + between x and y; + not between x and y; + 集合范围(即(x,y,z)) + in (x,y,z); + not in(x,y,z); + 运算实发工资 + select 字段名,(运算式) as 别名 from 表名; + 如果有null参与运算 + select 字段名,字段*(ifnull(字段,代替的值)) as 别名 from 表名; + ``` + + + +# 2、作业 + +```sql +use mockQ; +-- ## 第1题:员工表 +-- +-- ```mysql +drop table if exists `employee`; +-- #创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); +-- +-- #添加信息 +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); +-- ``` +-- | **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 | +-- +-- **要求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 and sex = '男') or (addr = '广东深圳,广州' and sex = '男'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select `name` as `姓名`,salary as `年薪` from employee; +-- ```mysql +-- +-- ``` +-- +-- ## 第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); +-- ``` +-- +-- 表数据样例: +-- +-- ```mysql +-- +-------------+-----------+---------+------------+--------------+ +-- | 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 | +-- +-------------+-----------+---------+------------+--------------+ +-- ``` +-- countries_info表中存储了国家名称(name)、所属大陆(continent)、面积(area)、人口(population)和 GDP 值。 +-- **要求1:** 查询大国 的国家名称、人口和面积。 +-- +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +select `name`,area,population 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` as `国家名`,population as `人口`,gdp as `GDP值`,gdp/population `人均贡献GDP值` from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from countries_info where gdp/population < 1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select `name` as `国家名`,area as `面积`,population as `人口`,area/population `人均国土面积值` from countries_info; +-- ```mysql +-- +-- ``` +-- +-- +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" "b/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" new file mode 100644 index 0000000..2ae9e42 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230223 \345\207\275\346\225\260\351\242\204\344\271\240\347\254\224\350\256\260.md" @@ -0,0 +1,75 @@ +# 1笔记 + +## 1.1 分组函数 + +```sql +-- 计算平均值 +avg(x) + select avg(字段名) from 表名; +-- 计算和 +sum(x) + #简单求和 + select sum(字段名) from 表名; + #计算表达式求和 + select sum(字段名*(1+ifnull(字段名,代替值))); +-- 计算最大值 +max(x) + select max(字段名) from 表名; +-- 计算最小值 +min(x) + select min(字段名) from 表名; +-- 统计记录 +count(x) + #统计实际的行数 + select count(*) from 表名; + #只统计"字段/表达式"部分非空值得行数 + select count(字段/表达式) from 表名; +``` + +## 1.2 单行函数 + +```sql +-- 返回绝对值 +abs(x) +-- 返回比x大的最小整数 +ceil(x) +-- 返回比x小的最小整数 +floor(x) +-- 求(x/y)的模 +mod(x,y) +-- 随机数(0-1) +``` + +## 1.3 窗口函数(属于单行函数的一种) + +```sql +-- 顺序排序 +row_number() +-- 并列排序 + #排序字段出现重复值时,跳过重复序号,如1,1,3.... + rank() + #排序字段出现重复值时,不跳过重复序号,如1,1,2.... + dense_rank() + +-- 排名百分比(rank-1)/(rows-1) +percent_rank() +-- 累积分布值 +cume_dist() + +-- 返回当前行的前面n行的expr值 +lag(expr,n) +-- 返回当前行的后面n行的expr值 +lead(expr,n) + +-- 返回当前分组第一行的expr值 +first_value(expr) +-- 返回当前分组每一个rank最后一行的expr值 +last_value(expr) +-- 返回当前分组第n行的expr值 +nth_value(expt) + +-- 窗口函数语法格式 +函数名([参数列表]) over () +函数名([参数列表]) over (子句) +``` + diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" "b/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" new file mode 100644 index 0000000..c332f32 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230223 \347\254\224\350\256\260\346\225\264\347\220\206.md" @@ -0,0 +1,131 @@ +# 1、MySQL安装 + +```sql +浏览器搜索MySQL,进入官方网页(https://www.mysql.com/downloads/)进行下载,按安装提示进行安装 + +MySQL启动 +在任务栏右键任务管理器,点击服务,找到MySQL57,右键启动 + +环境变量 +在我的电脑中找到MySQL的文件夹,进入bin文件夹,复制路径,打开控制面板,点击系统,高级系统设置,环境变量,在系统变量里的Path文件中新建,将复制的路径粘贴进去,在后面+\,保存退出 + +导出数据 +Windows键+R键打开运行面板,键入cmd,进入运行面板 +>mysql -u root -p 密码 要导出的数据库名 >要保存的路径\脚本名.sql + +导入数据 +在运行面板中登录mysql数据库 +msyql>source 要导入的数据库路径\脚本名.sql +``` + + + +# 2 、SQL语句 数据类型 + +```sql +SQL语句 +DDL(数据定义语言): create(创建) alter(修改) drop(删除) show(展示) +DML(数据操作语言): insert(增加) delete(删除) update(修改) +DQL(数据查询语言): select(查询) +DCL(数据控制语言): grant(授权) commit(确认) rollback(回滚) + +数据类型 + 整形:int (int(M):无意义) + int(M) zerofill:不足M位用0补足, + 浮点型:float:小数、单浮点型 + float(M,N):M表示整个数字的长度,N表示小数部分的长度 + 双浮点型: double + double(M,N):与float相同 + 精准型:decimal + 定长字符串:char + 可变字符串:varchar(M) +``` + +# 3、DDL建表与填值 + +```sql +增加表字段: +alter table 表名称 add 字段名 数据类型; +删除表字段: +alter table 表名称 drop 字段名; +修改字段名称: +alter table 表名称 change 旧字段名 新字段名 数据类型; +修改数据类型: +alter table 表名称 modify 字段名 新数据类型; +修改字段顺序: +alter table 表名称 modify 字段名1 数据类型 after 字段名2;(字段1移动到字段2后面) +alter table 表名称 modify 字段名 数据类型 first;(将字段移动到第一位) +修改表名称 +alter table 旧标明 rename to 新表名; +删除列的定义 +alter table 表名 drop 字段名; + +修改表数据(无条件) +update 表名 set 字段名 = 新值; +修改表数据(指定条件) +update 表名 set 字段名 = 新值 where 条件(如 name = 'XXX'); +update 表名 set 字段名 = 新值 where 条件字段名 in (如 where name in('XXX','XXX')); + +删除表数据(无条件) +delete from 表名; +删除表数据(指定条件 行) +delete from 表名 where 条件(如 id = ); + +数据表填值 +insert into 表名称[字段名] values (插入内容); +``` + +# 4、约束 + +```sql +唯一键约束 +unique key;(不重复,但可为空,可以有多个列设置) +主键约束 +primary key;(不重复,不为空,不能多列) +复合主键 +primary key(XXX,XXX); +默认建约束 +not null default 默认值 +自增属性 +auto_increment primary key(后面必须加主键) +外键约束 +foreign key (从表的字段) references 主表(被依赖的字段); +foreign key (从表的字段) references 主表(被依赖的字段) on update cascade on delete set null(从表数据随主表数据变换) +alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade; +``` + +# 5、基础查询与运算符 + +```sql +查询去重 +select distinct 字段名 from 表名 [where]; +查询%表示多个字符 +-- 查询名字中有“熊”的员工 +select 字段名 from 表名 where 字段名 like '%熊%'; +-- 查询姓“熊”的员工 +select 字段名 from 表名 where 字段名 like '熊%'; +查询_表示一个字符 +-- 查询叫“熊某”的员工 +select 字段名 from 表名 where 字段名 like '熊_'; +查询异或(只能符合其中一种条件) +select 字段名 from 表名 where 条件 xor 条件; + + +运算null +字段名 is null;/字段名 <=> null; +取整 +div +求余 +% / mod +区间范围(即x<=值<=y) +between x and y; +not between x and y; +集合范围(即(x,y,z)) +in (x,y,z); +not in(x,y,z); +运算实发工资 +select 字段名,(运算式) as 别名 from 表名; +如果有null参与运算 +select 字段名,字段*(ifnull(字段,代替的值)) as 别名 from 表名; +``` + -- Gitee