diff --git "a/47 \345\217\262\345\255\246\346\226\260/20220215 \345\210\233\345\273\272\346\225\260\346\215\256\345\272\223\345\222\214\350\241\250.md" "b/47 \345\217\262\345\255\246\346\226\260/20220215 \345\210\233\345\273\272\346\225\260\346\215\256\345\272\223\345\222\214\350\241\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..3d4ace21679e29dabf3633b3d0815675d0a584c4 --- /dev/null +++ "b/47 \345\217\262\345\255\246\346\226\260/20220215 \345\210\233\345\273\272\346\225\260\346\215\256\345\272\223\345\222\214\350\241\250.md" @@ -0,0 +1,15 @@ +```mysql +create database test1; +DROP TABLE IF EXISTS `student`; +CREATE TABLE student ( +student_id int(10), +student_name char(5), +student_class int(5), +student_gender varchar(1), +student_hobby varchar(10). +student_place varchar(30)student_num int(11), +student_mail varchar(20), +student_qq int(10) +); +``` + diff --git "a/47 \345\217\262\345\255\246\346\226\260/20230216 DDL\345\222\214DML\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/47 \345\217\262\345\255\246\346\226\260/20230216 DDL\345\222\214DML\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..782000455590ffb8878843c0d5ebd1e8c09dc547 --- /dev/null +++ "b/47 \345\217\262\345\255\246\346\226\260/20230216 DDL\345\222\214DML\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,236 @@ +# **笔记** + +```mysql +create database 数据库名 charset 字符集; +show databases; 查看所有的数据库 +show create database 数据库名; 查看某个数据库的详细定义语句 +ALTER DATABASE 数据库名称 CHARACTER SET 字符集名称 COLLATE 字符集对应校对规则; +修改数据库编码 +drop database 数据库名;删除数据库 +use 数据库名;使用数据库 +insert into 表名称 values(值列表);添加一条记录到某个表中 +insert into 表名称 values(值列表),(值列表),(值列表); 添加多条记录到某个表中 +update 表名称 set 字段名 = 值, 字段名 = 值; 给所有行修改 +update 表名称 set 字段名 = 值, 字段名 = 值 where 条件; 给满足条件的行修改 +delete from 表名称 where 条件;删除部分行的数据 +delete from 表名称;删除整张表的数据,但表结构留下 +``` + + + +```mysql +-- 1、创建数据库test01_market +CREATE DATABASE test01_market +alter database test01_market character set 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(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 date 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 date after c_name +-- **要求7:**将表名改为customers_info +ALTER TABLE customers RENAME customers_info +-- **要求8:**删除字段c_city +ALTER TABLE customers_info DROP c_city + +-- ## 第2题 +-- 1、创建数据库test02_library +create database test02_library +alter DATABASE test02_library character set utf8 +USE test02_library +-- 2、创建表格books +-- | 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +-- | ------- | -------- | ------------- | -------- | ---- | +-- | b_id | 书编号 | int(11) | 否 | 是 | +-- | b_name | 书名 | varchar(50) | 否 | 否 | +-- | authors | 作者 | varchar(100) | 否 | 否 | +-- | price | 价格 | float | 否 | 否 | +-- | pubdate | 出版日期 | year | 否 | 否 | +-- | note | 说明 | varchar(100) | 是 | 否 | +-- | num | 库存 | int(11) | 否 | 否 | +DROP TABLE books +CREATE TABLE books( +b_id int(11) UNIQUE,#书编号 +b_name VARCHAR(50), #书名 +authors varchar(100), #作者 +price float,#价格 +pubdate year, #出版日期 +note varchar(100) NULL, #说明 +num int(11) #库存 +); +-- 3、向books表中插入记录 +desc books; +-- 1) 指定所有字段名称插入第一条记录 +insert into books(b_id,b_name,authors,price,pubdate,note,num) values (1, 'Tal of AAA','Dickes',23, '1995','novel',11); +-- 2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura',40,'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; +select * from books; +## 第3题 +-- 1、创建数据库test03_bookstore +create database test03_bookstore charset utf8; +use test03_bookstore; +-- 2、创建book表 +create table book( +id int(11) not null auto_increment primary key, +title varchar(100) not null , +author varchar(100) not null, +price double(11,2) not null, +sales int(11) not null, +stock int(11) not null, +img_path varchar(100) not null +); +-- +----------+--------------+------+-----+---------+----------------+ +-- | 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(id,title,author,price,sales,stock,img_path)values(1,'解忧杂货店','东野圭吾',27.20,102,98,'upload/books/解忧杂货店.jpg'), +(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +select * from book; +-- +----+-------------+------------+-------+-------+-------+----------------------------+ +-- | 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,并插入数据 +create table users( +id int(11) not null auto_increment primary key, +username varchar(100) unique key not null, +password varchar (100) not null, +email varchar(100) null +); +-- +----------+--------------+------+-----+---------+----------------+ +-- | 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 (id,username,password,email)values(1,'admin',112233,'admin@mxdx.com'); +select * from users; +-- +----+----------+----------------------------------+--------------------+ +-- | id | username | password | email | +-- +----+----------+----------------------------------+--------------------+ +-- | 1 | admin | 112233 | admin@mxdx.com | +-- +----+----------+----------------------------------+--------------------+ +-- +-- 4、创建订单表orders +create table orders( +id varchar(100) not null primary key, +order_time datetime not null, +total_count int(11) not null, +total_amount double(11,2) not null, +state int(11) not null, +user_id int(11) not null +); +alter table orders add index(user_id); +desc orders; +insert into orders values (15294258455691,'2018-06-20 00:30:45',2,50.20,0,1); +select * from orders; +-- +--------------+--------------+------+-----+---------+-------+ +-- | 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 | | +-- +--------------+--------------+------+-----+---------+-------+ +-- 尝试添加部分模拟数据,参考示例如下: +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | 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(11) not null primary key auto_increment, +ount int(11) not null , +amount double(11,2) not null, +title varchar(100) not null, +author varchar(100) not null, +price double(11,2) not null, +img_path varchar(100) not null, +order_id varchar(100) not null +); +alter table orders_items add index(order_id); +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; +-- +----------+--------------+------+-----+---------+----------------+ +-- | 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 | | +-- +----------+--------------+------+-----+---------+----------------+ + +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- +----+-------+--------+------------+---------+-------+----------------+----------------+ +-- | 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/47 \345\217\262\345\255\246\346\226\260/20230222 \346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/47 \345\217\262\345\255\246\346\226\260/20230222 \346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..78cfde724c4a58e1a947d06b4d6e62a6d222133d --- /dev/null +++ "b/47 \345\217\262\345\255\246\346\226\260/20230222 \346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,214 @@ +## 笔记 + +```mysql +select 查询 +select 字段列表 from 表名; +select 字段列表 from 表名 where 条件; +查询得到的表是一张临时表,不影响原表数据 +select 字段名 [as](可省略) 别名 from 表名 [as] 别名; +列的别名有空格时,要加双引号,没有空格时,可加可不加; +表的别名不能加双引号,表的别名中间不能包含空格 +select distinct 字段列表 from 表名称 [where] 条件; 查询结果去重 +distinct 只对第一个字段生效,通常只在查询一个字段时使用 +运算符:加:+ 减:- 乘:* 除:/ +div(只保留整数部分) div:两个数相除只保留整数部分 +模:% mod +大于:> 小于:< +大于等于:>= +小于等于:<= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null(安全等于和is同理,都可以判断null) +判断不是null is not null +null参与的运算返回值都为null +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 + 模糊查询 使用时需要搭配like使用 + %:代表任意个字符 +_:代表一个字符,如果两个下划线代表两个字符 +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor +``` + + + +## 第1题:员工表 + +```mysql +create database homework charset utf8; +use homework; +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:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +```mysql +select id,name,addr from employee where name like '刘%'; +``` + +-- **要求3:**将“李四”的家庭住址改为“广东韶关” + +```mysql +update employee set addr = '广东佛山',addr='广东韶关' where name = '李四'; +``` + +-- **要求4:**查询出名字中带“小”的员工 + +```mysql +select name from employee where name like '%小%'; + +``` + +-- **要求5:**查询出薪资高于11000的男员工信息 + +```mysql +select * from employee where salary > 11000 and sex = '男'; + +``` + +-- **要求6:**查询没有登记电话号码的员工 + +```mysql +select name from employee where tel is null; + +``` + +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +```mysql +select name,sex,addr,salary from employee where (salary > 12000 or addr in ('广东深圳','广东广州')) and sex = '男'; + +``` + + **要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +select name as '姓名',salary as '年薪' from employee; +``` + +第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +``` + +表数据样例: + +```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 | ++-------------+-----------+---------+------------+--------------+ +``` + +**要求1:** 查询大国 的国家名称、人口和面积。 + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + + ```mysql + select name,population,area from countries_info where population >= 25000000 or area >= 3000000; + ``` + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +```mysql +select * from countries_info where continent = 'Asia'; +``` + +**要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +```mysql +select * from countries_info where area <=10000 and population <=100000; +``` + +**要求4:**查询国家名字中包含“o“字母的国家信息 + +```mysql +select * from countries_info where name like '%o%'; +``` + +**要求5:**查询GDP值超过10000000000的国家信息 + +```mysql +select * from countries_info where gdp >10000000000; +``` + +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +```mysql +select name '国家名',population '人口',gdp 'GDP值',gdp/population 人均贡献GDP值 from countries_info; +``` + +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + +```mysql +select * from countries_info where gdp/population; +``` + +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```mysql +select name '国家名',area '面积',population '人口',area/population '人均国土面积' from countries_info; +``` +