溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL怎么使用Python進(jìn)行連接

發(fā)布時(shí)間:2022-06-06 15:09:31 來源:億速云 閱讀:208 作者:iii 欄目:開發(fā)技術(shù)

這篇“MySQL怎么使用Python進(jìn)行連接”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“MySQL怎么使用Python進(jìn)行連接”文章吧。

一、表格與鍵概念

MySQL怎么使用Python進(jìn)行連接

MySQL怎么使用Python進(jìn)行連接

MySQL怎么使用Python進(jìn)行連接

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

MySQL怎么使用Python進(jìn)行連接

設(shè)置外鍵進(jìn)行表與表的相連,且外鍵必須是其他表的主鍵(外鍵也可以設(shè)置自己表格的主鍵)

二、創(chuàng)建資料庫

CREATE DATABASE `sql_tutorial`;    --創(chuàng)建資料庫
SHOW databases;    --展示資料庫
drop database `sql_tutorial`;    --刪除資料庫

–為注釋
;為結(jié)束命令的格式

MySQL怎么使用Python進(jìn)行連接

三、創(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指的是最大字符長度
); -- 創(chuàng)建表格并設(shè)計(jì)屬性

describe `student`; -- 展示表格
drop table `student`; -- 刪除表格

alter table `student` add gpa decimal(3,2); -- 增加資料屬性
alter table `student` drop column gpa ; -- 刪除資料屬性

MySQL怎么使用Python進(jìn)行連接

四、儲(chǔ)存資料

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

MySQL怎么使用Python進(jìn)行連接

五、限制約束

create table student(
        `student_id` int primary key, -- 第一列
    `name` varchar(20) not null, -- 第二列,not null指這個(gè)屬性不可以為空
    `major` varchar(20) unique -- 第三列,20指的是最大字符長度,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指的是最大字符長度,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ù)

MySQL怎么使用Python進(jìn)行連接

六、修改、刪除資料

條件:>,<,>=,<=,=,<>

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` -- 更新哪個(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指的是最大字符長度
    `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è)置好外鍵(選擇什么是并對(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指的是最大字符長度
); -- 創(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è)置好外鍵(選擇什么是并對(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指的是最大字符長度
    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指的是最大字符長度
    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)行連接”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI