diff --git "a/07 \345\206\257\345\273\272\347\250\213/20231215.md" "b/07 \345\206\257\345\273\272\347\250\213/20231215.md" new file mode 100644 index 0000000000000000000000000000000000000000..f23de8e2e89990d6856ff342d0f9e97d29d4d58d --- /dev/null +++ "b/07 \345\206\257\345\273\272\347\250\213/20231215.md" @@ -0,0 +1,545 @@ +# 数据库管理 + +### 相关语法 + +- 查看已有数据库(可看成文件夹) + + ~~~sql + show databases; + ~~~ + +- 创建数据库(文件夹) + + ~~~sql + create database 数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ---编码为UTF—8 + ~~~ + +- 删除数据库(文件夹) + + ~~~sql + drop database 数据库名字; + ~~~ + +- 进入数据库(进入文件夹) + + ~~~sql + use 数据库名字 ; + ~~~ + +- 查看文件夹下所有的数据表(文件) + + ~~~SQL + show tables; + ~~~ + +- 创建表(文件) + + ~~~sql + create table 表名称( + 列名称 类型, + 列名称 类型, + 列名称 类型 + )default charset=utf8; + ~~~ + + 实例: + + ~~~sql + create table tb1( + id int, + name varchar(16), --字符串类型 + age int + )default charset=utf8; + ~~~ + + ~~~sql\ + create table tb1( + id int, + name varchar(16) not null, --不可为空 + age int null, -允许为空(默认) + )default charset=utf8; + ~~~ + + ~~~sql + create table tb1( + id int, + name varchar(16), + age int default 3 --插入数据时,age列的值默认为3 + )default charset=utf8; + ~~~ + + ~~~sql + create table tb1( + id int primary key, --主键(不允许为空,不允许重复) + name varchar(16), + age int + )default charset=utf8; + ~~~ + + 主键一般用于表示当前行的数据编号(类似于身份证)。 + + ~~~sql + create table tb1( + id int auto_increment primary key, --内部维护,自增。 + name varchar(16), + age int + )default charset=utf8; + ~~~ + + 一般情况下我们创建表时都会这样来写: 【标准】 + + ~~~sql + create table tb1( + id int not null auto_increment primary key, + name varchar(16), + age int + )default charset=utf8; + ~~~ + +- 查看创建表的格式 + + ~~~sql + desc 表名称; + ~~~ + + + +- 删除表 + + ~~~sql + drop table 表名称; + ~~~ + +### 常用的数据类型 + +- tinyint + + ~~~sql + 有符号,取值范围:—128~127(有正也有负)【默认】 + 无符号,取值范围;0~255(只有正) + ~~~ + + 实例: + + ​ 1.有符号 + + ~~~sql + create table tb2( + id int not null auto_increment primary key, + name varchar(16), + age tinyint + )default charset=utf8; + ~~~ + + ​ 2.无符号 + + ~~~sql + create table tb2( + id int not null auto_increment primary key, + name varchar(16), + age tinyint unsigned + )default charset=utf8; + ~~~ + + + +- int + + ~~~sql + int 表示有符号,取值范围:-2147483648~2147483647 + int unsighed 表示无符号,取值范围:0~4294967295 + ~~~ + + + +- bigint + + ~~~sql + 有符号,取值范围;-9223372036854775808~9223372036854775807 + 无符号,取值范围:0~18446744073709551615 + ~~~ + + 练习题:创建工资表 + + ~~~sql + create table tb2( + id bigint auto_increment primary key, --id自增 + salary int, + age tinyint + )default charset=utf8; + ~~~ + + 插入数据: + + ~~~sql + insert into tb2(salary,age) value(10000,18); --单行插入 + insert into tb2(salary,age) value(20000,28),(3000,38); --多行插入 + ~~~ + + 查看表: + + ~~~sql + select * from tb2(查看的表名); + ~~~ + + 效果; + + ~~~~sql + +----+--------+------+ + | id | salary | age | + +----+--------+------+ + | 1 | 10000 | 18 | + | 2 | 20000 | 28 | + | 3 | 30000 | 38 | + +----+--------+------+ + ~~~~ + +- float(不太精准) + +- double(不太精准) + +- decimal(m,d) (小数点后四舍五入) + + ~~~sql + 准确的小数值,m是数字的总个数(负号不算),d是小数点后的个数,m最大值为65,d最大值为30. + ~~~ + + 例如: + + ~~~sql + create table tb3( + id int primary key auto_increment, + salary decimal(8,2) + ) default charset=utf8; + ~~~ + + + +- char(m),速度快。 + + ~~~sql + 定长字符串。m表示字符串的长度最多容纳225个字符 + + char(11),固定用11个字符串进行存储,哪怕真没有11个字符串,也会按照11存储 + + create table tb4( + id int primary key auto_increment, + mobile char(11) ——需要加长度 + ) default charset=utf8; + ~~~ + + + +- varchar(m),节省空间。 + + ~~~sql + 变长字符串。m表示字符的长度 最大65535字节/m = 最大的m + + varchar(11),真实数据有多长按照多少存储。(在规定长度内) + + create table tb5( + id int primary key auto_increment, + mobile varchar(11) + ) default charset=utf8; + ~~~ + + + +- text + + ~~~sql + text数据类型用于保存变长的大字符串,可以容纳65535个字符。 + 一般情况下,长文本会用text类型。如:文章,新闻等。 + + create table tb6( + id int primary key auto_increment, + title varchar(128), + content text + ) default charset=utf8; + ~~~ + + + +- mediumtext + +- longtext + +- datetime(时间) + + ~~~sql + YYYY-MM-DD HH:MM:ss(1000-01-01 00:00:00/9999-12-31 23:59:59) + ~~~ + + + +- date(时间) + + ~~~sql + YYYY-MM-DD(1000-01-01/9999-12-31) + ~~~ + +### 数据行操作 + +- 新增数据 + + ~~~sql + insert into 表名(列名,列名) value(值,值); + insert into 表名(列名,列名) value(值,值),(值,值),(值,值); + ~~~ + +- 删除数据 + + ~~~sql + delete from 表名; + delete from 表名 where 条件; + ~~~ + + 实例 + + ~~~sql + delete from tb1 where id=3; + delete from tb1 where id=3 and name=dq; + delete from tb1 where id=3 or name = dq; + delete from tb1 where id>4 ; + delete from tb1 where id!=3; + delete from tb1 where id in (1,5); + ~~~ + +- 修改数据 + + ~~~sql + update 表名 set 列=值; + update 表名 set 列=值,列=值; + update 表名 set 列=值 where 条件; + ~~~ + + 实例 + + ~~~sql + update tb3 set password="哈哈"; --将password全部设置为“哈哈” + update tb3 set password="哈哈" where id>5; --将id>5的password全部修改为"哈哈" + 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 +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> +``` +