diff --git "a/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231215 MySQL\347\232\204\345\237\272\347\241\200.md" "b/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231215 MySQL\347\232\204\345\237\272\347\241\200.md" index dccefd4bf86da996a9204895bf82b84fbac222ef..fbf7a8e63da5f9d977b336c0eed61ceda03904b6 100644 --- "a/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231215 MySQL\347\232\204\345\237\272\347\241\200.md" +++ "b/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231215 MySQL\347\232\204\345\237\272\347\241\200.md" @@ -321,228 +321,6 @@ update tb3 set age=age+10; --在age原来的基础上增加10 ~~~ -- 查询数据 - - ~~~sql - select * from 表名称; --查询所有 - select 列名称,列名称 from 表名称; - select 列名称,列名称 from 表名称 where 条件; - ~~~ - - - -# 案例 (员工管理) - -## 1.创建表结构 - -~~~sql -create database unicom DEFAULT CHARSET utf8 COLLATE utf8_general_ci; -~~~ - -~~~sql -create table admin( - id int auto_increment primary key, - username varchar(16) not null, - password varchar(64) not null, - mobile varchar(16) not null - )default charset=utf8; -~~~ - -## 2.python操作mysql - -1. 创建数据 - - ~~~python - import pymysql #导包 - - #1.连接mysql - conn = pymysql.connect(host="127.0.0.1", port=3306 ,user='root',passwd="root123",charset='utf8',db='unicom') #db数据库名 - cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) - - - #2.发送指令 - cursor.execute("insert into admin(username,password,mobile) value('wupeiqi','qwe123','15579909111')") - conn.commit() - - - #3.关闭连接 - cursor.close() - conn.close() - ~~~ - - - -2. 查询数据 - - ~~~python - import pymysql - - #1.连接mysql - conn = pymysql.connect(host="127.0.0.1", port=3306 ,user='root',passwd="root123",charset='utf8',db='unicom') - cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) - - - - #2.发送指令 - cursor.execute("select * from admin") #可加条件 where - data_list = cursor.fetchall() #fetchone 符合条件的一条数据. - print(data_list) - - - #3.关闭连接 - cursor.close() - conn.close() - ~~~ - - - -3. 删除数据 - - ~~~python - import pymysql - - #1.连接mysql - conn = pymysql.connect(host="127.0.0.1", port=3306 ,user='root',passwd="root123",charset='utf8',db='unicom') - cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) - - - #2.发送指令 - cursor.execute("delete from admin where id=1") - conn.commit() - - - #3.关闭连接 - cursor.close() - conn.close() - ~~~ - - - -4. 修改数据 - - ~~~python - import pymysql - - #1.连接mysql - conn = pymysql.connect(host="127.0.0.1", port=3306 ,user='root',passwd="root123",charset='utf8',db='unicom') - cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) - - - #2.发送指令 - cursor.execute("update admin set mobile=18888888 where id=1") - conn.commit() - - - #3.关闭连接 - cursor.close() - conn.close() - ~~~ - - - -强调: - -- 在新增,修改,删除 一定记得commit 不然数据库没有数据 - - ~~~~python - cursor.execute("...") - conn.commit() - ~~~~ - - - -- 在查询时不需要commit 但需要fetchall 或者fetchone - - ~~~python - cursor.execute("...") - - v1 = cursor.fetchall() #获取全部的数据(以[{字典},{字典}]的形式获得) - - v1 = cursor.fetchone() #获取第一个符合条件的(以{字典}形式获得) - ~~~ - - ~~~mysql - create table class3( - stu_id int primary key, - stu_name varchar(5) not null, - stu_gender enum(' 男','女') not null, - stu_hobby set('唱','跳','rap','篮球'), - stu_address varchar(20) not null, - stu_mail varchar(20) not null, - stu_QQ int not null, - stu_contact int not null - )default charset=utf8; - - ~~~ - - - -~~~mysql -insert into class3 value(22443103,'菜某坤',1,'唱','江西省南昌市青山湖区50号','123465@qq.com',2130555136,110); -~~~ - -![image-20230216193958863](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230216193958863.png) - -```mysql -![屏幕截图 2023-02-18 132539](D:\homework\专业课文件夹\Mysql作业\mysql-base\23号 黄浩东\图片\屏幕截图 2023-02-18 132539.png)Microsoft Windows [版本 10.0.19044.1889] -(c) Microsoft Corporation。保留所有权利。 - -C:\Users\Administrator>mysql -u root -proot -mysql: [Warning] Using a password on the command line interface can be insecure. -Welcome to the MySQL monitor. Commands end with ; or \g. -Your MySQL connection id is 5 -Server version: 5.7.37-log MySQL Community Server (GPL) - -Copyright (c) 2000, 2022, Oracle and/or its affiliates. - -Oracle is a registered trademark of Oracle Corporation and/or its -affiliates. Other names may be trademarks of their respective -owners. - -Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. - -mysql> use bbc -Database changed -mysql> create table class3( - -> stu_id int primary key, - -> stu_name varchar(5) not null, - -> stu_gender enum(' 男','女') not null, - -> stu_hobby set('唱','跳','rap','篮球'), - -> stu_address varchar(20) not null, - -> stu_mail varchar(20) not null, - -> stu_QQ int not null, - -> stu_contact int not null - -> )default charset=utf8; -Query OK, 0 rows affected (0.05 sec) - -mysql> desc class3; -+-------------+-----------------------------+------+-----+---------+-------+ -| Field | Type | Null | Key | Default | Extra | -+-------------+-----------------------------+------+-----+---------+-------+ -| stu_id | int(11) | NO | PRI | NULL | | -| stu_name | varchar(5) | NO | | NULL | | -| stu_gender | enum(' 男','女') | NO | | NULL | | -| stu_hobby | set('唱','跳','rap','篮球') | YES | | NULL | | -| stu_address | varchar(20) | NO | | NULL | | -| stu_mail | varchar(20) | NO | | NULL | | -| stu_QQ | int(11) | NO | | NULL | | -| stu_contact | int(11) | NO | | NULL | | -+-------------+-----------------------------+------+-----+---------+-------+ -8 rows in set (0.02 sec) -mysql> insert into class3 value(22443107,'沈某祥',1,'rap','福建省龙岩监狱','666666@qq.com',1063126776,110); -Query OK, 1 row affected (0.01 sec) - -mysql> select * from class3; -+----------+----------+------------+-----------+----------------+---------------+------------+-------------+ -| stu_id | stu_name | stu_gender | stu_hobby | stu_address | stu_mail | stu_QQ | stu_contact | -+----------+----------+------------+-----------+----------------+---------------+------------+-------------+ -| 22443107 | 沈某祥 | 男 | rap | 福建省龙岩监狱 | 666666@qq.com | 1063126776 | 110 | -+----------+----------+------------+-----------+----------------+---------------+------------+-------------+ -1 row in set (0.00 sec) - -mysql> -``` - # 作业 ![](.\图片\屏幕截图 2023-02-18 132539.png) diff --git "a/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231218 MySQL DML\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" "b/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231218 MySQL DML\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1631355b2e86358eb28216d76278e570288bf8ae --- /dev/null +++ "b/23\345\217\267 \351\273\204\346\265\251\344\270\234/20231218 MySQL DML\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" @@ -0,0 +1,323 @@ +# 2DML基础 + +## 对数据的增删改 + +### (1)在列表中增加一个数据 + +完整格式 + +```MySQL +insert into 表名 (表的列名) values(列表值) #表的列名顺序应该对应列表值,必须一一对应!如果你是要把表全填满那么可以忽略表的列名。intster只是增加一行新的数据,并不会因为前面有什么数据没填而自动补上去 +``` + +例如: + +```mysql +mysql> desc teacher; ++----------+------------------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+------------------------+------+-----+---------+-------+ +| tid | int(11) | YES | | NULL | | +| tname | varchar(5) | YES | | NULL | | +| salary | double | YES | | NULL | | +| weight | double | YES | | NULL | | +| birthday | date | YES | | NULL | | +| gender | enum('男','女') | YES | | NULL | | +| blood | enum('A','B','AB','O') | YES | | NULL | | +| phone | char(11) | YES | | NULL | | ++----------+------------------------+------+-----+---------+-------+ +8 rows in set (0.00 sec); +insert into teacher valuse(1,'张三',15000,120.5,'1990-5-1','男','O','13789586859'); +``` + +```mysql +#错误示范 +/*因为位置不对出错*/insert into teacher values(1,'张三',15000,120.5,'1990-5-1','男','13789586859','O'); +/*缺少某个数据,如缺少phone*/insert into teacher values(2,'李四',15000,'1990-5-1','男','O'); +``` + +如果要同时添加多条记入则: + +```mysql +insert into 表名 (表的列名) values(列表值1),(列表值2),~~~,(列表值n);#注意事项同上 +``` + +演示: + +```mysql +create database test; +use test; +create table test_one(b_id int,b_id2 char); +insert into test_one(b_id2) values('w')#单独建一行,这一行只有b_id2不为空 +insert into test_one values(1,'w')#单独建一行,这一行为b_id=1,b_id2=w.这两行相互独立,互不影响 +``` + +### (2)删除行(数据) + +2.1删除全部行数据,但不删表 + +```mysql +delete from 表名称; +/*例如*/delete from test +``` + +2.2删除部分数据(行) + +```mysql +delete from 表名称 where 条件1 and 条件2 and ··· and 条件n +/*如*/delete from test where b_id1 = 1#只删除满足条件的行 +``` + +2.3清空表结构,只留有表结构 + +```mysql +truncate 表名称; +``` + +```mysql +truncate teacher; +``` + +### (3)修改行(数据) + +3.1修改所有行 + +与insert格式: + +```MySQL +update table 表名 set 字段名 = 新值 +#如 +update table test set b_id = 5 +``` + +3.2修改部分行: + +```mysql +update table 表名 set 字段名 = 新值 where 条件1 and 条件2 and ~~~ and 条件n +#如 +update table test set b_id = 10 where b_id2 = "w" +``` + +## 作业 + +## 第1题 + +1、创建数据库test01_market + +2、创建表格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字段后面 + +**要求4:**将c_name字段数据类型改为 varchar(70) + +**要求5:**将c_contact字段改名为c_phone + +**要求6:**增加c_gender字段到c_name后面,数据类型为char(1) + +**要求7:**将表名改为customers_info + +**要求8:**删除字段c_city + +```MySQL +CREATE DATABASE test01_market; +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); +ALTER TABLE customers MODIFY COLUMN c_contact VARCHAR(50) AFTER c_birth; +ALTER table customers change COLUMN c_name c_name varchar(70); +ALTER table customers change column c_contact c_phone varchar(50); +ALTER table customers add column c_gender char(1) after c_name; +ALTER table customers rename TO customers_info; +ALTER table customers_info drop COLUMN c_city; +DESC customers_info; +``` + +## 第2题 + +1、创建数据库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) | 否 | 否 | + +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 | + +4、将小说类型(novel)的书的价格都增加5。 + +5、将名称为EmmaT的书的价格改为40。 + +6、删除库存为0的记录 + +```mysql + +create database test02_library; +use test02_library; +create table books( +b_id int(11) NOT NULL,b_name VARCHAR(50) NOT NULL UNIQUE,`authors` VARCHAR(100) NOT NULL,price float NOT NULL,pubdate YEAR NOT NULL,note VARCHAR(100) NOT NULL,num INT(11)NOT NULL +); +DESC books; +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; + insert into books values (2,'Emmat','Jane lura',35,1993,'joke',22); + select * from books; +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,'medicne',40),(7,'Rose Hood','Richard haggard',28,2008,'cartoon',28); +UPDATE books set price = price + 5 WHERE note = 'novel'; +UPDATE books SET price = 40 WHERE b_name = 'EmmaT'; +DELETE FROM books WHERE num = 0; +select * from books; +DROP DATABASE test02_library +``` + +## 第3题 + +1、创建数据库test03_bookstore + +2、创建book表 + +```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 | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```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,并插入数据 + +```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 | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+----------+----------------------------------+--------------------+ +| id | username | password | email | ++----+----------+----------------------------------+--------------------+ +| 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 | | ++--------------+--------------+------+-----+---------+-------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```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 + +```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 | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```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 | ++----+-------+--------+------------+----------+-------+------------+----------------+ +``` + +```mysql + +CREATE database text03_bookstore; +use text03_bookstore; +CREATE table book (id int(11) NOT null,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,ing_path varchar(100) NOT null); +INSERT INTO book values (1,'解忧杂货店','东野圭吾',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg '); +select * from book; +create table users(id int(11),username varchar(100),`password` VARCHAR(100),email varchar(100)); +insert into users values (1,'admin',112233,'admin@mxdx.com'); +create table orders(id varchar(100),order_time datetime,total_count int(11),total_amount int(110),user_id int(11)); +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 +-- +``` +