From 9309a86b6e0781ae911684c0313510ae3c163582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5?= <18396581747@163.com> Date: Sun, 26 Feb 2023 16:39:38 +0000 Subject: [PATCH] =?UTF-8?q?21=20=E5=91=A8=E9=97=BB=E7=A5=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周闻祥 <18396581747@163.com> --- ...ql\345\217\212\345\256\211\350\243\205.md" | 24 ++ ...60\343\200\201\344\275\234\344\270\232.md" | 311 ++++++++++++++++++ ...60\343\200\201\344\275\234\344\270\232.md" | 216 ++++++++++++ ...60\343\200\201\344\275\234\344\270\232.md" | 108 ++++++ ...75\346\225\260\351\242\204\344\271\240.md" | 28 ++ 5 files changed, 687 insertions(+) create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230213 \344\272\206\350\247\243mysql\345\217\212\345\256\211\350\243\205.md" create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230215 \346\225\260\346\215\256\347\261\273\345\236\213\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230220 DDL\345\222\214DML\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230224 select\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230226 \345\210\206\347\273\204\345\207\275\346\225\260\351\242\204\344\271\240.md" diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230213 \344\272\206\350\247\243mysql\345\217\212\345\256\211\350\243\205.md" "b/21 \345\221\250\351\227\273\347\245\245/20230213 \344\272\206\350\247\243mysql\345\217\212\345\256\211\350\243\205.md" new file mode 100644 index 0000000..ab3c131 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230213 \344\272\206\350\247\243mysql\345\217\212\345\256\211\350\243\205.md" @@ -0,0 +1,24 @@ +# 笔记 + +DB:数据库(Database) + +DBMS:数据库管理系统 ( DataBase Management System ) + +SQL:结构化查询语言(Structure Query Language) + +## 安装mysql + +浏览器搜索mysql进入官网下载社区版 + +## 打开MySQL + +1、开始菜单mysql客户端打开 + +2、使用cmd命令连接 + +```mysql +mysql -h 主机名 -P 端口号 -u 用户名 -p密码 +``` + +3、使用第三方工具navicat连接 + diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230215 \346\225\260\346\215\256\347\261\273\345\236\213\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230215 \346\225\260\346\215\256\347\261\273\345\236\213\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..557d5d6 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230215 \346\225\260\346\215\256\347\261\273\345\236\213\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,311 @@ +# 笔记 + +```mysql +SQL语句 +DDL(数据定义语言): create(创建) alter(修改) drop(删除) show(展示) +DML(数据操作语言): insert(增加) delete(删除) update(修改) +DQL(数据查询语言): select(查询) +DCL(数据控制语言): grant(授权) commit(确认) rollback(回滚) +增加表字段: +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 = ); +数据类型 + 整形:int (int(M):无意义) + int(M) zerofill:不足M位用0补足, + 浮点型:float:小数、单浮点型 + float(M,N):M表示整个数字的长度,N表示小数部分的长度 + 双浮点型: double + double(M,N):与float相同 + 精准型:decimal + 定长字符串:char + 可变字符串:varchar(M) +``` + + + +# 作业 + +```mysql +-- ## 第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/21 \345\221\250\351\227\273\347\245\245/20230220 DDL\345\222\214DML\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230220 DDL\345\222\214DML\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..2714ea7 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230220 DDL\345\222\214DML\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,216 @@ +# 笔记 + +```mysql +创建库 create database 库名 charset utf 8 +移动字段 alter table 表名 modify 段名1 数据类型 after 段名2(移动到段名2后面) +重命名字段 alter table 表名 change 段名 新段名 数据类型 +重命名表 alter table 表名 rename to 新表名 +删字段 alter table 表名 drop 段名 +更改段内容 update 表名 set 更改内容 where 条件 +删内容 delete from 表名 where 条件 +date类型需要'' +``` + +# 作业 + +````mysql +第一题 +1、创建数据库test01_market +create database test01_market charser utf8; +2、创建表格customers +use test01_market; +create table customers; + +| 字段名 | 数据类型 | +| --------- | ----------- | +| c_num | int(11) | +| c_name | varchar(50) | +| c_contact | varchar(50) | +| c_city | varchar(50) | +| c_birth | date | + +**要求3:**将c_contact字段移动到c_birth字段后面 +alter table customers modify c_contact varchar(50) after c_birth; +**要求4:**将c_name字段数据类型改为 varchar(70) +alter table customers modify c_name varchar(70); +**要求5:**将c_contact字段改名为c_phone +alter table customers change c_contact c_phone 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; +第二题 +create database test02_library charset utf8; +use test02_library; +1) 指定所有字段名称插入第一条记录 +insert into books (b_id,b_name,authors,price,pubdate,note,num) values (1,'Tal of AAA','Dickes',23,1995,'novel',11); +select * from books; +2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura',35,1993,'joke',22); +3)同时插入多条记录(剩下的所有记录) +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','Honore Blade',30,2010,'law',0),(6,'The Battle','Upton Sara',30,1999,'medicine',40),(7,'Rose Hood','Richard haggard',28,2008,'cartoon',28); +| b_id | b_name | authors | price | pubdate | note | num | +| ---- | ------------- | --------------- | ----- | ------- | -------- | ---- | +| 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +| 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +| 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | + +4、将小说类型(novel)的书的价格都增加5。 +update books set price = price + 5 where note ='novel'; +5、将名称为EmmaT的书的价格改为40。 +update books set price = 40 where b_name ='EmmaT'; +6、删除库存为0的记录 +delete from books where num = 0; +```mysql + +``` +第三题 +## 第3题 + +-- 1、创建数据库test03_bookstore +create database test03_bookstore charset utf8; +-- 2、创建book表 +use test03_bookstore; +create table book ( +id int, +title varchar(100), +author varchar(100), +price double(11,2), +sales int, +stock int, +img_path varchar(100) +); +-- ```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 | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into book values (1,'解忧杂货店','东野圭吾',27.20,102,98,'upload/books/解忧杂货店.jpg'); +insert into book values (2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +-- ```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 | +-- +----+---------------+------------+-------+-------+-------+----------------------------+ +-- ``` +-- +-- 3、创建用户表users,并插入数据 +use test03_bookstore; +create table users( +id int, +username varchar(100), +password varchar(100), +email varchar(100) +); +-- ```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 | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into users values(1,'admin','112233','admin@mxdx.com'); +-- ```mysql +-- +----+----------+----------------------------------+--------------------+ +-- | id | username | password | email | +-- +----+----------+----------------------------------+--------------------+ +-- | 1 | admin | 112233 | admin@mxdx.com | +-- +----+----------+----------------------------------+--------------------+ +-- ``` +-- +-- 4、创建订单表orders +create table orders( +id varchar(100), +order_time datetime, +total_count int, +total_amount double(11,2), +state int, +user_id int +); +-- ```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 | | +-- +--------------+--------------+------+-----+---------+-------+ +-- ``` +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into orders values('15294258455691','2018-06-20 00:30:45',2,50.20,0,1); +-- ```mysql +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | id | order_time | total_count | total_amount | state | user_id | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- ``` +-- +-- 5、创建订单明细表order_items +create table order_items( +id int, +count int, +amount double(11,2), +title varchar(100), +author varchar(100), +price double(11,2), +img_path varchar(100), +order_id varchar(100) +); +-- ```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 | | +-- +----------+--------------+------+-----+---------+----------------+ +-- ``` +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into order_items values(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg','15294258455691'); +insert into order_items values(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg','15294258455691'); +-- ```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 | +-- +----+-------+--------+------------+----------+-------+------------+----------------+ +```` + + + diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230224 select\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230224 select\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..04c3564 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230224 select\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,108 @@ +# 笔记 + +```mysql +select * from 表格名; +select 字段名 from 表格名 where; +select name x from 表格名; #别名 +select 字段, 条件/条件 '取名' from 表格名 (条件); +#如: +select *,gdp/population '人均贡献GDP值'from countries_info where gdp/population<1000; +select distinct 字段 from 表格名 where; #查重 +select now() #实时时间 +2.运算符 +<=> != div(只取整数) +null 不能参加运算 +#区间 +where 字段 between 数值 and 数值;[1,200] +where 字段 in (字段,字段,字段) +select * from 表格名 where 字段 like '%信%' '_信_';#查询有包含信的字段 _表示第一个字符 %代表任意字符 +ifnull(条件1,条件2) 如果第一个条件为null 则返回条件2 不为null 返回条件1; +``` + + + +# 作业 + +```mysql +第一题 + +create database x charset utf8; +use x; +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); + + +-- 要求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 ='李四'; +-- **要求4:**查询出名字中带“小”的员工 + select * from employee where name like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 + select * from employee where salary >11000 and sex='男'; +-- **要求6:**查询没有登记电话号码的员工 + select * from employee where tel is null; +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + select * from employee where (salary >12000) or sex='男' and addr in ('广东深圳','广州'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name,salary from employee ; +第二题 + +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); + +select * from countries_info; + +-- 要求1:** 查询大国 的国家名称、人口和面积。 + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +-- - 面积至少为 300万平方公里(即,3000000 km2) + +-- - 人口至少为 2500 万(即 25000000) + +select name,population,area from countries_info where area>3000000 or population >25000000; +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from countries_info where continent='Asia'; +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where population<100000 and area <10000; +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where name like '%o%'; +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp >10000000000; +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select name,population,gdp,gdp/population '人均贡献GDP值' from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select *,gdp/population '人均贡献GDP值' from countries_info where gdp/population<1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name,area,population,area/population '人均国土面积值' from countries_info; +``` + diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230226 \345\210\206\347\273\204\345\207\275\346\225\260\351\242\204\344\271\240.md" "b/21 \345\221\250\351\227\273\347\245\245/20230226 \345\210\206\347\273\204\345\207\275\346\225\260\351\242\204\344\271\240.md" new file mode 100644 index 0000000..3c5e1c2 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230226 \345\210\206\347\273\204\345\207\275\346\225\260\351\242\204\344\271\240.md" @@ -0,0 +1,28 @@ +# 笔记 + +## 分组函数 + +avg() 求平均 + +sum() 求和 + +max()求最大值 + +min() 求最小值 + +count() 统计 + +```mysql +#演示分组函数如何使用 + #从table中查询salary平均值 + select avg(salary) from table; + #从table中查询salary的和 + select sum(salary) from table; + #从table中查询salary的最大值 + select max(salary) from table; + #从table中查询salary的最小值 + select min(salary) from table;; + #从talbe中查询salary的统计数 + select count(salary) from table; + +``` -- Gitee