網(wǎng)站建設(shè)收費(fèi)標(biāo)準(zhǔn)網(wǎng)站seo最新優(yōu)化方法
文章目錄
- 數(shù)據(jù)庫(kù)概述
- Mysql概述
- Mysql安裝與使用
- Navicat安裝和使用
- Mysql終端指令操作
- Mysql和python交互
- 訂單管理案例實(shí)現(xiàn)
數(shù)據(jù)庫(kù)概述
-
數(shù)據(jù)庫(kù)的由來(lái)
發(fā)展歷程 說(shuō)明 人工管理階段 用紙帶等進(jìn)行數(shù)據(jù)的存儲(chǔ) 文件系統(tǒng)階段 數(shù)據(jù)存儲(chǔ)在文件中 數(shù)據(jù)庫(kù)階段 解決了文件系統(tǒng)問(wèn)題 高級(jí)數(shù)據(jù)庫(kù)階段 分布式數(shù)據(jù)庫(kù) -
數(shù)據(jù)庫(kù)的分類
數(shù)據(jù)庫(kù)分類 說(shuō)明 常用庫(kù) 關(guān)系型數(shù)據(jù)庫(kù) 采用了關(guān)系模型來(lái)組織數(shù)據(jù)的數(shù)據(jù)庫(kù):關(guān)系模型指的就星二維表格模型 Oracle
Microsoft SQL Server
MySQL
SQLite非關(guān)系型數(shù)據(jù)庫(kù) Not Only SQL
強(qiáng)調(diào)Key-Value的方式存儲(chǔ)數(shù)據(jù)Mongodb
redis -
關(guān)系型數(shù)據(jù)庫(kù)的使用場(chǎng)景
Web網(wǎng)站系統(tǒng)、日志記錄系統(tǒng)、數(shù)據(jù)倉(cāng)庫(kù)系統(tǒng)、嵌入式系統(tǒng)
Mysql概述
MySQL是一個(gè) 關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng) \color{red}{關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)} 關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)軟件。
-
關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)-RDBMS
MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)軟件,在WEB應(yīng)用方面,MySQL是最好的RDBMS(Relational Database Management System, 關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng))應(yīng)用軟件,它是由瑞典MySQL AB司開發(fā),目前屬于Oracle旗下產(chǎn)品,MySQL是最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)中的一個(gè)。 -
關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)可以分為:
- 關(guān)系型數(shù)據(jù)庫(kù)服務(wù)端軟件-Server
- 關(guān)系型數(shù)據(jù)庫(kù)客戶端軟件-Client
-
關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)的流程圖:
-
關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)的通信流程效果圖:
-
SQL介紹
SQL(Structured Query Language)是 結(jié)構(gòu)化查詢語(yǔ)言 \color{red}{結(jié)構(gòu)化查詢語(yǔ)言} 結(jié)構(gòu)化查詢語(yǔ)言,是一種用來(lái)操作 R D B M S \color{red}{RDBMS} RDBMS的數(shù)據(jù)庫(kù)的語(yǔ)言,也就是說(shuō)通過(guò)SQL可以操作oracle, sql server, mysql, sqlite等關(guān)系型的數(shù)據(jù)庫(kù)。 -
SQL語(yǔ)言的分類
簡(jiǎn)寫 語(yǔ)義 說(shuō)明 DQL 數(shù)據(jù)查詢語(yǔ)句 select DML 數(shù)據(jù)操作語(yǔ)句 insert
update
deleteDDL 數(shù)據(jù)定義語(yǔ)句 creat
dropTPL 事務(wù)處理語(yǔ)言 begin transaction
commit
rollbackDCL 數(shù)據(jù)控制語(yǔ)言 grant
revoke -
MySQL的特點(diǎn)
- MySQL是開源的,所以你不需要支付額外的費(fèi)用。
- M小ySQL支持大型的數(shù)據(jù)庫(kù)??梢蕴幚?yè)碛猩细扇f(wàn)條記錄的大型數(shù)據(jù)庫(kù)。
- MySQL使用標(biāo)準(zhǔn)的SQL數(shù)據(jù)語(yǔ)言形式。
- MySQL可以安裝在不同的操作系統(tǒng),并且提供多種編程語(yǔ)言的操作接口。這些編程語(yǔ)言包括C、C++、Python、Java、Ruby等等。
-
MySQL的數(shù)據(jù)類型
數(shù)據(jù)類型 說(shuō)明 int, bit 整數(shù) decimal 小數(shù) varchar, char 字符串 date, time, datetime 日期和時(shí)間 enum 枚舉類型 -
MySQL的字段約束
約束參數(shù) 說(shuō)明 primary key 主鍵約束 not null 非空約束 unique 唯一約束 default 默認(rèn)約束
Mysql安裝與使用
- Mysql安裝
MySQL數(shù)據(jù)庫(kù) 服務(wù) \color{red}{服務(wù)} 服務(wù)端軟件的安裝:sudo apt-get install mysql-server
MySQL數(shù)據(jù)庫(kù) 客戶 \color{red}{客戶} 客戶端軟件的安裝:sudo apt-get insatll mysql-client
- Mysql數(shù)據(jù)庫(kù)服務(wù)端啟動(dòng)
查看MySQL服務(wù)狀態(tài):sudo service mysql status
停止MySQL服務(wù):sudo service mysql stop
啟動(dòng)MySQL服務(wù):sudo service mysql start
重啟MySQL服務(wù):sudo service mysql restart
- Mysql的配置文件
- mqsql配置文件的路徑:/etc/mysql/mysql.conf.d/
- 配置項(xiàng)介紹
<1> port表示端口,默認(rèn)為3306;
<2> bind-address表示服務(wù)器綁定的ip,默認(rèn)為127.0.0.1;
<3> datadir表示數(shù)據(jù)庫(kù)保存路徑,默認(rèn)為/var/lib/mysql;
<4> log_error表示錯(cuò)誤日志,默認(rèn)為/var/log/mysql/error.log;
- mqsql配置文件的路徑:/etc/mysql/mysql.conf.d/
Navicat安裝和使用
- Navicat介紹
Navicat是一款圖形化界面的數(shù)據(jù)庫(kù)客戶端軟件。 - Navicat的安裝——基于Linux操作系統(tǒng)
- 官網(wǎng)下載安裝包:https://www.navicat.com.cn
- 解壓navicat安裝包:
tar -zxvf navicat112_mysql_cs_x64.tar.gz
- cd到navicat安裝包文件夾目錄:
cd navicate112_mysql_cs_x64
- 運(yùn)行navicat:
./start_navicat
安裝包及破解工具下載: 百度網(wǎng)盤 【提取碼:4KaE】
破解教程:Navicat Premium15永久破解版安裝教程 - Navicat的連接mysql服務(wù)
Mysql終端指令操作
- Mysql登錄登出客戶端
連接mysql服務(wù)端指令:mysql -uroot -p
顯示當(dāng)前時(shí)間:select now();
退出連接:exit/quit/[control+d]
- Mysql數(shù)據(jù)庫(kù)操作
查看所有數(shù)據(jù)庫(kù):show databases;
創(chuàng)建數(shù)據(jù)庫(kù):create database 數(shù)據(jù)庫(kù)名 charset=utf-8;
使用數(shù)據(jù)庫(kù):use 數(shù)據(jù)庫(kù)名;
查看當(dāng)前使用的數(shù)據(jù)庫(kù):select database();
刪除數(shù)據(jù)庫(kù):drop database 數(shù)據(jù)庫(kù)名;
- Mysql表操作
查看所有當(dāng)前庫(kù)中的所有表:show tables;
創(chuàng)建表:create table 表名(字段名稱 數(shù)據(jù)類型 可選的約束條件, column1 datatype contral, ...);
修改表字段類型:alter table 表名 modify 列名 類型 約束;
刪除表:drop table 表名;
查看表結(jié)構(gòu):desc 表名;
- Mysql -CRUD操作
- 查詢數(shù)據(jù)
查詢所有列:select * from 表名;
查詢指定列:select 列名1, 列名2,... from 表名;
- 增加數(shù)據(jù)
全列插入 - 值的順序必須和字段順序完全一致:insert into 表名 values(...);
部分列插入 - 值的順序和給出的列的順序?qū)?yīng):insert into 表名(列1, 列2,...) values(值1, 值2,...)
全列多行插入:insert into 表名 values(...), (...), (...);
部分列多行插入:insert into 表名(列1, 列2,...) values(值1,...), (值1,...), (值1,...)
- 修改數(shù)據(jù):
update 表名 set 列1=值1, 列2=值2,... where 條件;
- 刪除數(shù)據(jù):
delete from 表名 where 條件;
- 查詢數(shù)據(jù)
- Mysql數(shù)據(jù)庫(kù)備份和恢復(fù)
-
備份導(dǎo)出:
mysqldump -u用戶名 -p密碼 數(shù)據(jù)庫(kù)名 表名 > data.sql
(注意:不寫表名默認(rèn)導(dǎo)出所有數(shù)據(jù)表)
-
恢復(fù)導(dǎo)入:
cd 到數(shù)據(jù)文件路徑下 mysql -u用戶名 -p密碼 use 數(shù)據(jù)庫(kù) source data.sql
-
Mysql和python交互
-
pymysql的安裝
安裝pymysql:sudo pip3 install pymysql
查看安裝情況:pip show pymysql / pip3 list
卸載pymysql:sudo pip3 uninstall pymysql
-
pymysql的使用
- 導(dǎo)包:
import pymysql
- 創(chuàng)建和mysql服務(wù)端的連接對(duì)象:
conn = pymysql.connect(參數(shù)列表)
- 獲取游標(biāo)對(duì)象:
cursor = conn.cursor()
- 執(zhí)行sql語(yǔ)句:
row_count = cursor.execute(sql)
- 獲取查詢結(jié)果集:
result = cursor.fetchall()
- 將增加和修改操作提交到數(shù)據(jù)庫(kù):
conn.commit()
- 回滾數(shù)據(jù):
conn.rollback()
- 關(guān)閉游標(biāo)對(duì)象:
cursor.close()
- 關(guān)閉連接:
conn.close()
- 導(dǎo)包:
-
pymysql查詢數(shù)據(jù)
# 查詢數(shù)據(jù)庫(kù)goods下student表中的所有數(shù)據(jù) # 1. 導(dǎo)包 import pymysqltry:# 2. 連接mysql數(shù)據(jù)庫(kù)的服務(wù)conn = pymysql.Connect(# mysql服務(wù)端的IP,默認(rèn)127.0.0.1/localhost,也可填真實(shí)iphost='192.168.90.172',user='root',password='mysql',database='goods',port=3306,charset='utf8')# 3. 創(chuàng)建游標(biāo)對(duì)象cur = conn.cursor()# 4. 編寫sql語(yǔ)句sql = 'select * from students;'# 5. 使用游標(biāo)對(duì)象去調(diào)用sqlcur.execute(sql)# 6. 獲取查詢的結(jié)果并打印result = cur.fetchall()print(result)# 7. 關(guān)閉游標(biāo)對(duì)象cur.close()# 8. 關(guān)閉連接conn.close() except Exception as e:print(e)
結(jié)果如下:
-
pymysql增刪改數(shù)據(jù)
''' 對(duì)數(shù)據(jù)庫(kù)goods下的students表進(jìn)行如下操作: 1. 增加數(shù)據(jù):李磊 35 男 2. 修改數(shù)據(jù):李磊的名字改為王磊 3. 刪除數(shù)據(jù):王磊 '''# 1. 導(dǎo)包 import pymysql# 2. 連接mysql數(shù)據(jù)庫(kù)的服務(wù) conn = pymysql.Connect(# mysql服務(wù)端的IP,默認(rèn)127.0.0.1/localhost,也可填真實(shí)iphost='192.168.90.172',user='root',password='mysql',database='goods',port=3306,charset='utf8' ) # 3. 創(chuàng)建游標(biāo)對(duì)象 cur = conn.cursor() try:# 4. 編寫增加、修改、刪除的sql語(yǔ)句add_sql = 'insert into students values(%s, %s, %s, %s);'add_data = [0, '李磊', 35, '男']update_sql = 'update students set name=%s where name = "李磊";'update_data = ['王磊']delete_sql = 'delete from students where name=%s;'delete_data = ['王磊']# 5. 使用游標(biāo)對(duì)象執(zhí)行sqlcur.execute(add_sql, add_data)cur.execute(update_sql, update_data)cur.execute(delete_sql, delete_data)# 6. 提交操作conn.commit() except Exception as e:print(e)# 數(shù)據(jù)回滾conn.rollback() finally:# 7. 關(guān)閉游標(biāo)對(duì)象cur.close()# 8. 關(guān)閉連接conn.close()
訂單管理案例實(shí)現(xiàn)
-
需求分析
① 創(chuàng)建數(shù)據(jù)庫(kù)orders
② 創(chuàng)建orders訂單表
③ 插入測(cè)試數(shù)據(jù)orders.sql
④ Python和Mysql交互
⑤ 查詢訂單數(shù)據(jù)
⑥ 增加訂單數(shù)據(jù)
⑦ 刪除訂單數(shù)據(jù)
⑧ 修改訂單數(shù)據(jù) -
實(shí)現(xiàn)步驟
-
建庫(kù)和建表
--創(chuàng)建數(shù)據(jù)庫(kù) create database orders charset=utf8;--創(chuàng)建數(shù)據(jù)表 create table orders(id int not null auto_increment primary key,count int not null,price decimal(10,2) not null,freight decimal(10,2) not null,user varchar(50) not null,status enum('待支付','待發(fā)貨','待收貨') default '待支付' not null,time date not null);
-
插入測(cè)試數(shù)據(jù)
① cd素材的目錄下
② 登錄mysql客戶端:mysql -uroot -p
③ 切換數(shù)據(jù)庫(kù):use orders
④ 導(dǎo)入測(cè)試數(shù)據(jù):source orders.sql
-
實(shí)現(xiàn)訂單查詢功能
① 導(dǎo)包pymysql
② 連接mysql服務(wù)
③ 創(chuàng)建游標(biāo)對(duì)象
④ 編寫查詢所有訂單SQL語(yǔ)句
⑤ 執(zhí)行SQL語(yǔ)句
⑥ 獲取所有查詢的結(jié)果
⑦ 將獲取結(jié)果轉(zhuǎn)換成列表字典格式
⑧ 對(duì)Mysql的操作加Try處理# 1 導(dǎo)包 import pymysqldef order()# 2 鏈接mysql的服務(wù)端conn = pymysql.Connect(user='root',password='mysql',host='192.168.90.172',database='orders',port=3306,charset='utf8')# 3 創(chuàng)建游標(biāo)對(duì)象cur = conn.cursor()try:# 4 編寫 查詢orders表的所有數(shù)據(jù)SQLsql = 'select * from orders;'# 5 使用游標(biāo)對(duì)象執(zhí)行SQLcur.execute(sql)# 6 獲取查詢的所有數(shù)據(jù) fetchall() ==>元組result = cur.fetchall()print('查詢數(shù)據(jù):', result)# 打印結(jié)果如下:# 查詢數(shù)據(jù):((1, 2, Decimal('100.00'), Decimal('10.00'), '老王', ‘待收貨', datetime.data(2020, 1, 1)), (2, 3, Decimal('200.00'), Decimal('25.00'), '錢紅', ’待支付', datatime.date(2020, 4,1)))# 7 將數(shù)據(jù)轉(zhuǎn)換成[{}, {}]data_lst = []for row in result:data_lst.append({'id': row[0],'count': row[1],'price': str(row[2]),'freight': str(row[3]),'user': row[4],'status': row[5],'time': str(row[6])})# 8 加個(gè)try優(yōu)化下except Exception as e:print('報(bào)錯(cuò)信息:', e)finally:# 關(guān)閉游標(biāo)對(duì)象cur.close()# 關(guān)閉連接conn.close()
-
實(shí)現(xiàn)訂單增加功能
① 導(dǎo)包pymysql
② 連接mysql服務(wù)
③ 創(chuàng)建游標(biāo)對(duì)象
④ 編寫增加訂單的SQL語(yǔ)句
⑤ 執(zhí)行SQL語(yǔ)句并提交
⑥ 對(duì)Mysql的操作加Try處理# 1 導(dǎo)包 import pymysqldef add(data):# 2 鏈接mysql的服務(wù)端conn = pymysql.Connect(user='root',password='mysql',host='192.168.90.172',database='orders',port=3306,charset='utf8')# 3 創(chuàng)建游標(biāo)對(duì)象cur =conn.cursor()try:# 4 編寫 增加數(shù)據(jù)的SQLsql = 'insert into orders values(%s,%s,%s,%s,%s,%s,%s)'# 5 使用游標(biāo)對(duì)象執(zhí)行SQL并提交cur.execute(sql, data)conn.commit()# 6 加個(gè)try優(yōu)化下except Exception as e:print('報(bào)錯(cuò)信息:', e)# 回滾數(shù)據(jù)conn.rollback()finally:# 關(guān)閉游標(biāo)對(duì)象cur.close()# 關(guān)閉連接conn.close()
-
實(shí)現(xiàn)封裝功能函數(shù)
查詢和增加的功能重復(fù)度比較高,均有:導(dǎo)包、連接mysql、創(chuàng)建游標(biāo)對(duì)象、執(zhí)行SQL語(yǔ)句、Try語(yǔ)句。實(shí)現(xiàn)封裝函數(shù)的操作:
① 定義封裝函數(shù)的名字
② 粘貼重復(fù)度高的代碼
③ 提交參數(shù)
④ 測(cè)試# 1 導(dǎo)包 import pymysql# 封裝一個(gè)執(zhí)行CRUD的函數(shù) def execute_crud_sql(sql,data):# 2 鏈接mysql的服務(wù)端conn = pymysql.Connect(user='root',password='mysql',host='192.168.90.172',database='orders',port=3306,charset='utf8')# 3 創(chuàng)建游標(biāo)對(duì)象cur = conn.cursor()try:# 4 編寫 查詢orders表的所有數(shù)據(jù)SQL# 5 使用游標(biāo)對(duì)象執(zhí)行SQLcur.execute(sql, data)# 6 提交操作conn.commit()# 8 加個(gè)try優(yōu)化下except Exception as e:print('報(bào)錯(cuò)信息:', e)# 回滾數(shù)據(jù)conn.rollback()finally:# 關(guān)閉游標(biāo)對(duì)象cur.close()# 關(guān)閉連接conn.close()def add(data):sql = 'insert into orders values(%s,%s,%s,%s,%s,%s,%s)'execute_crud_sql(sql, data)
-
實(shí)現(xiàn)訂單修改和刪除功能
# 修改的SQL語(yǔ)句 update orders set count=%s, price=%s, freight=%s, user=%s, status=%s, time=%s where id=%s; # 將參數(shù)中id的順序從第一個(gè)顛倒到最后一個(gè) data.append(data.pop(0))# 刪除的SQL語(yǔ)句 delete from orders where id=%s;
# 1 導(dǎo)包 import pymysql# 封裝一個(gè)執(zhí)行CRUD的函數(shù) def execute_crud_sql(sql,data):# 2 鏈接mysql的服務(wù)端conn = pymysql.Connect(user='root',password='mysql',host='192.168.90.172',database='orders',port=3306,charset='utf8')# 3 創(chuàng)建游標(biāo)對(duì)象cur = conn.cursor()try:# 4 編寫 查詢orders表的所有數(shù)據(jù)SQL# 5 使用游標(biāo)對(duì)象執(zhí)行SQLcur.execute(sql, data)# 6 提交操作conn.commit()# 8 加個(gè)try優(yōu)化下except Exception as e:print('報(bào)錯(cuò)信息:', e)# 回滾數(shù)據(jù)conn.rollback()finally:# 關(guān)閉游標(biāo)對(duì)象cur.close()# 關(guān)閉連接conn.close()# 修改訂單數(shù)據(jù) def update(data):print('修改的數(shù)據(jù):', data) # ['7', '99', '9.90', '1.00', '明明','待收貨', '2020-03-29']data.append(data.pop(0))# 1. 修改的SQL語(yǔ)句sql = 'update orders set count=%s, price=%s, freight=%s, user=%s, status=%s, time=%s where id=%s'# 2. 執(zhí)行調(diào)用execute_crud_sql(sql, data)# 刪除訂單數(shù)據(jù) def delete(data):# 1. 刪除的SQL數(shù)據(jù)sql = 'delete from orders where id=%s;'# 2. 執(zhí)行execute_crud_sql(sql, data)
-