MySQL操作并使用Python進(jìn)行連接
一、表格與鍵概念



主鍵:可唯一表示該資料(可以設(shè)置多個(gè)列表為主鍵)

設(shè)置外鍵進(jìn)行表與表的相連,且外鍵必須是其他表的主鍵(外鍵也可以設(shè)置自己表格的主鍵)
二、創(chuàng)建資料庫
CREATE DATABASE `sql_tutorial`; ? ?--創(chuàng)建資料庫 SHOW databases; ? ?--展示資料庫 drop database `sql_tutorial`; ? ?--刪除資料庫
–為注釋
;為結(jié)束命令的格式

三、創(chuàng)建表格
MySQL的資料形態(tài):
- INT --整形
- DECIMAL(m,n) --有小數(shù)點(diǎn)的數(shù) (3,2)則是2.33,總共有m位數(shù),小數(shù)點(diǎn)站n位
- VARCHAR(n) --字串
- BLOB --圖片 影片 檔案…(二進(jìn)制的資料)
- DATE --日期(yyyy-mm-dd)
- TIMESTAMP --記錄時(shí)間(yyyy-mm-dd hh:mm:ss)
CREATE DATABASE `sql_tutorial`; ?-- 創(chuàng)建資料庫 SHOW databases; ? ? ? ?-- 展示資料庫 use `sql_tutorial`; -- 選擇使用的資料庫 create table student( ? ? ? ? `student_id` int primary key, -- 第一列 ? ? `name` varchar(20), -- 第二列 ? ? `major` varchar(20) -- 第三列,20指的是最大字符長(zhǎng)度 ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 describe `student`; -- 展示表格 drop table `student`; -- 刪除表格 alter table `student` add gpa decimal(3,2); -- 增加資料屬性 alter table `student` drop column gpa ; -- 刪除資料屬性

四、儲(chǔ)存資料
create table student(
? ? ? ? `student_id` int primary key, -- 第一列
? ? `name` varchar(20), -- 第二列
? ? `major` varchar(20) -- 第三列,20指的是最大字符長(zhǎng)度
); -- 創(chuàng)建表格并設(shè)計(jì)屬性
select * from `student`; -- 搜索表格的全部資料
insert into `student` values(1,'小白','歷史'); -- 寫入表格數(shù)據(jù)
insert into `student` values(2,'小黑','生物'); -- 寫入表格數(shù)據(jù)
insert into `student` values(3,'小綠',null); -- 寫入表格數(shù)據(jù),null為空
insert into `student`(`name`,`major`,`student_id`) values('小藍(lán)','英語','4'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`major`,`student_id`) values('英語','5'); -- 按照設(shè)置寫入表格數(shù)據(jù),沒有的數(shù)據(jù)則為空白null
五、限制約束
create table student(
? ? ? ? `student_id` int primary key, -- 第一列
? ? `name` varchar(20) not null, -- 第二列,not null指這個(gè)屬性不可以為空
? ? `major` varchar(20) unique -- 第三列,20指的是最大字符長(zhǎng)度,unique指每個(gè)值不可以重復(fù)
); -- 創(chuàng)建表格并設(shè)計(jì)屬性
create table student(
? ? ? ? `student_id` int primary key auto_increment, -- 第一列,auto_increment自動(dòng)會(huì)加一
? ? `name` varchar(20), -- 第二列,not null指這個(gè)屬性不可以為空
? ? `major` varchar(20) default '歷史' -- 第三列,20指的是最大字符長(zhǎng)度,default指預(yù)設(shè)值,如果沒有寫該屬性,則為預(yù)設(shè)值
); -- 創(chuàng)建表格并設(shè)計(jì)屬性
drop table `student`;
select * from `student`; -- 搜索表格的全部資料
insert into `student`(`name`,`major`) values('小藍(lán)','英語'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`name`) values('小黑'); -- 按照設(shè)置寫入表格數(shù)據(jù)
六、修改、刪除資料
條件:>,<,>=,<=,=,<>
set sql_safe_updates = 0; -- 把預(yù)設(shè)更新模式關(guān)閉,這樣更新操作才可以成功
create table student(
? ? ? ? `student_id` int primary key auto_increment, -- 第一列
? ? `name` varchar(20), -- 第二列
? ? `major` varchar(20), -- 第三列,20指的是最大字符長(zhǎng)度
? ? `score` int
); -- 創(chuàng)建表格并設(shè)計(jì)屬性
drop table `student`;
select * from `student`; -- 搜索表格的全部資料
insert into `student`(`name`,`major`) values('小藍(lán)','英語'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`name`,`major`) values('小白','化學(xué)'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`name`,`major`) values('小黑','生物'); -- 按照設(shè)置寫入表格數(shù)據(jù)
update `student` -- 更新哪個(gè)表格
set `major` = '英語文學(xué)' -- 將什么更新成什么
where `major` = '英語'; -- 將其中誰的什么進(jìn)行更新
-- 還可以進(jìn)行多個(gè)更新
update `student` -- 更新哪個(gè)表格
set `major` = '生化' -- 將什么更新成什么
where `major` = '生物' or `major` = '化學(xué)' ; -- 將其中誰的什么進(jìn)行更新
update `student` -- 更新哪個(gè)表格
set `name` = '小輝',`major` = '生化' -- 將什么更新成什么
where `student_id`=1 ; -- 將其中誰的什么進(jìn)行更新
-- 不加條件則都改
update `student` -- 更新哪個(gè)表格
set `major` = '物理'; -- 將其中誰的什么進(jìn)行更新,都改成了生化
delete from `student`
where `student_id` = 3; -- 刪除表格中的數(shù)據(jù)
-- 條件也可以設(shè)置多個(gè)
delete from `student`
where `name` = '小白' and `major`='物理'; -- 刪除表格中的數(shù)據(jù)
delete from `student`; -- 刪除所有的資料七、取得資料
-- 取得資料
select * from `student`; -- 取得表格的全部資料
select `name` from `student`; -- 只取得表格的對(duì)應(yīng)數(shù)據(jù)
select `name`, `major` from `student`; -- 取得表格的對(duì)應(yīng)多個(gè)數(shù)據(jù)
select * from `student` ORDER BY `score`; -- 取得表格的全部資料,并排序(默認(rèn)正序)
select * from `student` ORDER BY `score` DESC; -- 取得表格的全部資料,并排序(由高到低,asc是由低到高)
select * from `student` ORDER BY `score` ,`student_id`; -- 取得表格的全部資料,并排序(先有score做排序,score中相同的再由student_id做排序)
select * from `student` LIMIT 3 ; -- 限制資料范圍
select * from `student` ORDER BY `score` LIMIT 3 ; -- 排序并限制資料范圍
select * from `student` where `major`= '英語'; -- 查找對(duì)應(yīng)資料
select * from `student` where `major`= '英語' and `student_id` = 1; -- 查找對(duì)應(yīng)資料(多條件)
select * from `student` where `major` in('歷史','英語','生物'); -- 查找對(duì)應(yīng)資料(多條件)1八、創(chuàng)建公司資料庫
CREATE DATABASE `sql_tutorial`; ?-- 創(chuàng)建資料庫 SHOW databases; ? ? ? ?-- 展示資料庫 use `sql_tutorial`; -- 選擇使用的資料庫 create table `employee`( ? ? ? ? `emp_id` int primary key, -- 第一列 ? ? `name` varchar(20), -- 第二列,20指的是最大字符長(zhǎng)度 ? ? `bath_date` date, -- 第三列 ? ? `sex` varchar(1), ? ? `salary` int, ? ? `branch_id` int, ? ? `sup_id` int ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table `branch`( ? ? ? ? `branch_id` int primary key , -- 第一列 ? ? `branch_name` varchar(20), -- 第二列 ? ? `manager_id` int, -- 第三列,20指的是最大字符長(zhǎng)度 ? ? foreign key (`manager_id`) references `employee`(`emp_id`) on delete set null -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 -- 補(bǔ)充外鍵(外表格對(duì)應(yīng)) alter table `employee` -- 在什么表格上進(jìn)行更新 add foreign key(`branch_id`) -- 在他的什么屬性上 references `branch`(`branch_id`) -- 從哪的什么屬性對(duì)應(yīng) on delete set null; -- 補(bǔ)充外鍵(內(nèi)表格對(duì)應(yīng)) alter table `employee` -- 在什么表格上進(jìn)行更新 add foreign key(`sup_id`) -- 在他的什么屬性上 references `employee`(`emp_id`) -- 從哪的什么屬性對(duì)應(yīng) on delete set null; create table `client`( ? ? ? ? `client_id` int primary key , -- 第一列 ? ? `client_name` varchar(20), -- 第二列 ? ? `phone` varchar(20) -- 第三列,20指的是最大字符長(zhǎng)度 ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table `works_with`( ? ? ? ? `emp_id` int, -- 第一列 ? ? `client_id` int, -- 第二列 ? ? `total_sales` int, -- 第三列,20指的是最大字符長(zhǎng)度 ? ? primary key(`emp_id`,`client_id`), ? ? foreign key (`emp_id`) references `employee`(`emp_id`) on delete cascade, -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ? ? ? ? foreign key (`client_id`) references `client`(`client_id`) on delete cascade -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 -- 當(dāng)增加資料沖突的時(shí)候可以先將其設(shè)置為null然后再更新 insert into `branch` values(1,'研發(fā)',null); insert into `employee` values(206,'xiaohuang','1998-10-08','F',50000,1,null); update `branch` set `manager_id` = 206 where `branch_id` = 1;
九、取得公司資料
-- 取得對(duì)應(yīng)表格所有資料 select * from `employee`; -- 取得對(duì)應(yīng)表格所有資料并排序(默認(rèn)低到高) select * from `employee` order by `salary`; -- 低為加desc -- 增加限制取出條件 select * from `employee` order by `salary` desc limit 3 ; -- 取出前三高 -- 取出對(duì)應(yīng)屬性 select ?`name` from `employee` ; -- 取出對(duì)應(yīng)屬性的內(nèi)容(且不重復(fù)) select distinct `name` from `employee` ;
十、聚合函數(shù)
-- 取得人數(shù) select count(*) from `employee`; -- 統(tǒng)計(jì)幾筆資料 select count(`sup_id`) from `employee`; -- 統(tǒng)計(jì)對(duì)應(yīng)屬性資料個(gè)數(shù)(null不計(jì)入) -- 增加條件取數(shù) select count(*) from `employee` where `bath_date` > '1970-01-01' and `sex` = 'F'; -- 統(tǒng)計(jì)幾筆資料 -- 計(jì)算對(duì)應(yīng)的屬性的平均 select avg(`salary`) from `employee`; -- 計(jì)算對(duì)應(yīng)的屬性的總和 select sum(`salary`) from `employee`; -- 取得最高的 select max(`salary`) from `employee`; -- 取得最低的 select min(`salary`) from `employee`;
十一、萬用子元
-- %表示多個(gè)子元,_表示一個(gè)子元 -- 取得尾數(shù)335的數(shù)據(jù) select * from `client` where `phone` like '%335'; -- 取得姓艾的 select * from `client` where `client_name` like '艾%'; -- 取得生日是10月的 select * from `employee` where `bath_date` like '_____10%';
十二、聯(lián)集
-- 員工與客戶合并為一列(類型必須相同) select `name` from `employee` union select `client_name` from `client`; -- 多個(gè)數(shù)據(jù)合并為多列(類型必須相同) select `emp_id`, `name` from `employee` union select `client_id`, `client_name` from `client`; -- 多個(gè)數(shù)據(jù)合并為多列(類型必須相同)順便改個(gè)名 select `emp_id` as `total_id`, `name` as `total_name` from `employee` union select `client_id`, `client_name` from `client`;
十三、連接
-- 連接 -- 取得所有部門經(jīng)理名字,這就需要先確定部門再確定經(jīng)理(二表相連) select * from `employee` join `branch` on `emp_id` = `manager_id`; -- 還可以寫成 select * from `employee` join `branch` on `employee`.`emp_id` = `branch`.`manager_id`; -- 附加條件 select * from `employee` left join `branch` on `employee`.`emp_id` = `branch`.`manager_id`; -- 左邊的表格(join的左邊)返回全部數(shù)據(jù),右邊的必須滿足才可
十四、子查詢
-- 查詢套查詢 select `name` from `employee` where `emp_id` = ( ? ? ? ? select `manager_id` ? ? from `branch` ? ? where `branch_name` = '研發(fā)' ); -- 查找研發(fā)部門的經(jīng)理名字 select `emp_id` from `employee` where `emp_id` in ( ? ? ? ? select `emp_id`? ? ? ? ? from `works_with` ? ? ? ? where `total_sales` > 50000 ); -- 銷售資金超50000的人有哪些
十五、On delete
- On delete set null 的作用是若該數(shù)據(jù)刪除了(或者說對(duì)應(yīng)不到了)則會(huì)設(shè)置為null
- On delete decade 若對(duì)應(yīng)數(shù)據(jù)刪除了(或者說對(duì)應(yīng)不到了)則該表格的這條數(shù)據(jù)跟著刪掉
注:當(dāng)為主鍵時(shí)則不能設(shè)置為on delete set nullcreate table `branch`(
? ? ? ? `branch_id` int primary key , -- 第一列 ? ? `branch_name` varchar(20), -- 第二列 ? ? `manager_id` int, -- 第三列,20指的是最大字符長(zhǎng)度 ? ? foreign key (`manager_id`) references `employee`(`emp_id`) on delete set null -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table `works_with`( ? ? ? ? `emp_id` int, -- 第一列 ? ? `client_id` int, -- 第二列 ? ? `total_sales` int, -- 第三列,20指的是最大字符長(zhǎng)度 ? ? primary key(`emp_id`,`client_id`), ? ? foreign key (`emp_id`) references `employee`(`emp_id`) on delete cascade, -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ? ? ? ? foreign key (`client_id`) references `client`(`client_id`) on delete cascade -- 設(shè)置好外鍵(選擇什么是并對(duì)應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性
十六、Python連接MySQL
# 導(dǎo)入功能包(mysql-connector-python)
import mysql.connector
# 創(chuàng)入連線
connection = mysql.connector.connect(host='localhost', ?# mysql的位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?port='3306', ?# 連接通道
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?user='root', ? # 使用者名稱
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?password='123456') ?# 使用者密碼
'''connection = mysql.connector.connect(host='localhost', ?# mysql的位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?port='3306', ?# 連接通道
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?user='root', ? # 使用者名稱
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?password='123456', ? # 使用者密碼
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?database='sql_tutorial') ?# 直接打開目標(biāo)資料庫'''
# 告知開始使用操作
cursor = connection.cursor()
# 在對(duì)mysql操作時(shí)即用該格式:cursor.execute("你要執(zhí)行的mysql語句命令")
# 創(chuàng)建資料庫
# cursor.execute("CREATE DATABASE `qq`;")
# 取得所有資料庫名稱
cursor.execute("show databases;")
records = cursor.fetchall() ?# 取出回傳資料庫
for r in records:
? ? print(r) ?# 因?yàn)榛貍髁斜恚瑸楸阌谟^察,進(jìn)行循環(huán)打印
# 選擇資料庫
cursor.execute("use `sql_tutorial`;")
# 創(chuàng)建表格
# 告知關(guān)閉操作
cursor.close()
# 若要懂資料進(jìn)行修改需要結(jié)尾加一個(gè)指令,這樣才能提交指令生效
connection.commit()
# 關(guān)閉連線
connection.close()到此這篇關(guān)于MySQL操作并使用Python進(jìn)行連接的文章就介紹到這了,更多相關(guān)MySQL操作 Python連接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql一次將多條不同sql查詢結(jié)果并封裝到一個(gè)結(jié)果集的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于mysql一次將多條不同sql查詢結(jié)果并封裝到一個(gè)結(jié)果集的實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
MYSQL關(guān)聯(lián)關(guān)系查詢方式
文章詳細(xì)介紹了MySQL中如何使用內(nèi)連接和左外連接進(jìn)行表的關(guān)聯(lián)查詢,并展示了如何選擇列和使用別名,文章還提供了一些關(guān)于查詢優(yōu)化的建議,并鼓勵(lì)讀者參考和支持腳本之家2025-02-02
MySQL Slave 觸發(fā) oom-killer解決方法
這篇文章主要介紹了MySQL Slave 觸發(fā) oom-killer解決方法,需要的朋友可以參考下2016-07-07
mysql 5.7.17 安裝配置方法圖文教程(ubuntu 16.04)
這篇文章主要為大家分享了ubuntu 16.04下mysql 5.7.17 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
MySQL多表查詢與7種JOINS的實(shí)現(xiàn)舉例

