MySQL操作并使用Python進(jìn)行連接
一、表格與鍵概念
主鍵:可唯一表示該資料(可以設(shè)置多個列表為主鍵)
設(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 --記錄時間(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指的是最大字符長度 ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 describe `student`; -- 展示表格 drop table `student`; -- 刪除表格 alter table `student` add gpa decimal(3,2); -- 增加資料屬性 alter table `student` drop column gpa ; -- 刪除資料屬性
四、儲存資料
create table student( ? ? ? ? `student_id` int primary key, -- 第一列 ? ? `name` varchar(20), -- 第二列 ? ? `major` varchar(20) -- 第三列,20指的是最大字符長度 ); -- 創(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指這個屬性不可以為空 ? ? `major` varchar(20) unique -- 第三列,20指的是最大字符長度,unique指每個值不可以重復(fù) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table student( ? ? ? ? `student_id` int primary key auto_increment, -- 第一列,auto_increment自動會加一 ? ? `name` varchar(20), -- 第二列,not null指這個屬性不可以為空 ? ? `major` varchar(20) default '歷史' -- 第三列,20指的是最大字符長度,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指的是最大字符長度 ? ? `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` -- 更新哪個表格 set `major` = '英語文學(xué)' -- 將什么更新成什么 where `major` = '英語'; -- 將其中誰的什么進(jìn)行更新 -- 還可以進(jìn)行多個更新 update `student` -- 更新哪個表格 set `major` = '生化' -- 將什么更新成什么 where `major` = '生物' or `major` = '化學(xué)' ; -- 將其中誰的什么進(jìn)行更新 update `student` -- 更新哪個表格 set `name` = '小輝',`major` = '生化' -- 將什么更新成什么 where `student_id`=1 ; -- 將其中誰的什么進(jìn)行更新 -- 不加條件則都改 update `student` -- 更新哪個表格 set `major` = '物理'; -- 將其中誰的什么進(jìn)行更新,都改成了生化 delete from `student` where `student_id` = 3; -- 刪除表格中的數(shù)據(jù) -- 條件也可以設(shè)置多個 delete from `student` where `name` = '小白' and `major`='物理'; -- 刪除表格中的數(shù)據(jù) delete from `student`; -- 刪除所有的資料
七、取得資料
-- 取得資料 select * from `student`; -- 取得表格的全部資料 select `name` from `student`; -- 只取得表格的對應(yīng)數(shù)據(jù) select `name`, `major` from `student`; -- 取得表格的對應(yīng)多個數(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`= '英語'; -- 查找對應(yīng)資料 select * from `student` where `major`= '英語' and `student_id` = 1; -- 查找對應(yīng)資料(多條件) select * from `student` where `major` in('歷史','英語','生物'); -- 查找對應(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指的是最大字符長度 ? ? `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指的是最大字符長度 ? ? foreign key (`manager_id`) references `employee`(`emp_id`) on delete set null -- 設(shè)置好外鍵(選擇什么是并對應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 -- 補(bǔ)充外鍵(外表格對應(yīng)) alter table `employee` -- 在什么表格上進(jìn)行更新 add foreign key(`branch_id`) -- 在他的什么屬性上 references `branch`(`branch_id`) -- 從哪的什么屬性對應(yīng) on delete set null; -- 補(bǔ)充外鍵(內(nèi)表格對應(yīng)) alter table `employee` -- 在什么表格上進(jìn)行更新 add foreign key(`sup_id`) -- 在他的什么屬性上 references `employee`(`emp_id`) -- 從哪的什么屬性對應(yīng) on delete set null; create table `client`( ? ? ? ? `client_id` int primary key , -- 第一列 ? ? `client_name` varchar(20), -- 第二列 ? ? `phone` varchar(20) -- 第三列,20指的是最大字符長度 ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table `works_with`( ? ? ? ? `emp_id` int, -- 第一列 ? ? `client_id` int, -- 第二列 ? ? `total_sales` int, -- 第三列,20指的是最大字符長度 ? ? primary key(`emp_id`,`client_id`), ? ? foreign key (`emp_id`) references `employee`(`emp_id`) on delete cascade, -- 設(shè)置好外鍵(選擇什么是并對應(yīng)什么表格的什么屬性) ? ? ? ? foreign key (`client_id`) references `client`(`client_id`) on delete cascade -- 設(shè)置好外鍵(選擇什么是并對應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 -- 當(dāng)增加資料沖突的時候可以先將其設(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;
九、取得公司資料
-- 取得對應(yīng)表格所有資料 select * from `employee`; -- 取得對應(yīng)表格所有資料并排序(默認(rèn)低到高) select * from `employee` order by `salary`; -- 低為加desc -- 增加限制取出條件 select * from `employee` order by `salary` desc limit 3 ; -- 取出前三高 -- 取出對應(yīng)屬性 select ?`name` from `employee` ; -- 取出對應(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ì)對應(yīng)屬性資料個數(shù)(null不計(jì)入) -- 增加條件取數(shù) select count(*) from `employee` where `bath_date` > '1970-01-01' and `sex` = 'F'; -- 統(tǒng)計(jì)幾筆資料 -- 計(jì)算對應(yīng)的屬性的平均 select avg(`salary`) from `employee`; -- 計(jì)算對應(yīng)的屬性的總和 select sum(`salary`) from `employee`; -- 取得最高的 select max(`salary`) from `employee`; -- 取得最低的 select min(`salary`) from `employee`;
十一、萬用子元
-- %表示多個子元,_表示一個子元 -- 取得尾數(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`; -- 多個數(shù)據(jù)合并為多列(類型必須相同) select `emp_id`, `name` from `employee` union select `client_id`, `client_name` from `client`; -- 多個數(shù)據(jù)合并為多列(類型必須相同)順便改個名 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ù)刪除了(或者說對應(yīng)不到了)則會設(shè)置為null
- On delete decade 若對應(yīng)數(shù)據(jù)刪除了(或者說對應(yīng)不到了)則該表格的這條數(shù)據(jù)跟著刪掉
注:當(dāng)為主鍵時則不能設(shè)置為on delete set nullcreate table `branch`(
? ? ? ? `branch_id` int primary key , -- 第一列 ? ? `branch_name` varchar(20), -- 第二列 ? ? `manager_id` int, -- 第三列,20指的是最大字符長度 ? ? foreign key (`manager_id`) references `employee`(`emp_id`) on delete set null -- 設(shè)置好外鍵(選擇什么是并對應(yīng)什么表格的什么屬性) ); -- 創(chuàng)建表格并設(shè)計(jì)屬性 create table `works_with`( ? ? ? ? `emp_id` int, -- 第一列 ? ? `client_id` int, -- 第二列 ? ? `total_sales` int, -- 第三列,20指的是最大字符長度 ? ? primary key(`emp_id`,`client_id`), ? ? foreign key (`emp_id`) references `employee`(`emp_id`) on delete cascade, -- 設(shè)置好外鍵(選擇什么是并對應(yīng)什么表格的什么屬性) ? ? ? ? foreign key (`client_id`) references `client`(`client_id`) on delete cascade -- 設(shè)置好外鍵(選擇什么是并對應(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() # 在對mysql操作時即用該格式: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é)尾加一個指令,這樣才能提交指令生效 connection.commit() # 關(guān)閉連線 connection.close()
到此這篇關(guān)于MySQL操作并使用Python進(jìn)行連接的文章就介紹到這了,更多相關(guān)MySQL操作 Python連接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql實(shí)現(xiàn)設(shè)置定時任務(wù)的方法分析
這篇文章主要介紹了mysql實(shí)現(xiàn)設(shè)置定時任務(wù)的方法,結(jié)合實(shí)例形式分析了mysql定時任務(wù)相關(guān)的事件計(jì)劃設(shè)置與存儲過程使用等操作技巧,需要的朋友可以參考下2019-10-10mysql中數(shù)據(jù)統(tǒng)計(jì)的技巧備忘錄
mysql是常用數(shù)據(jù)庫,對于數(shù)字操作相關(guān)的東西相當(dāng)方便,這篇文章主要給大家介紹了關(guān)于mysql中數(shù)據(jù)統(tǒng)計(jì)技巧的相關(guān)資料,非常具有實(shí)用價值,需要的朋友可以參考下2018-05-05在windows環(huán)境下配置并隨意切換兩種mysql版本的方法
這篇文章主要介紹了mysql:如何在windows環(huán)境下配置并隨意切換兩種mysql版本,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01windows版本下mysql的安裝啟動和基礎(chǔ)配置圖文教程詳解
本文通過圖文并茂的形式給大家介紹了windows版本下mysql的安裝啟動和基礎(chǔ)配置圖文教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06MySQL如何查詢數(shù)據(jù)庫中所有表名及注釋等信息
這篇文章主要介紹了MySQL如何查詢數(shù)據(jù)庫中所有表名及注釋等信息問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10mysql創(chuàng)建數(shù)據(jù)庫,添加用戶,用戶授權(quán)實(shí)操方法
在本篇文章里小編給大家整理的是關(guān)于mysql創(chuàng)建數(shù)據(jù)庫,添加用戶,用戶授權(quán)實(shí)操方法相關(guān)知識點(diǎn),需要的朋友們學(xué)習(xí)下。2019-10-10