MySQL如何使用Python進行連接

今天小編給大家分享一下MySQL如何使用Python進行連接的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

貴德ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

一、表格與鍵概念

MySQL如何使用Python進行連接

MySQL如何使用Python進行連接

MySQL如何使用Python進行連接

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

MySQL如何使用Python進行連接

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

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

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

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

MySQL如何使用Python進行連接

三、創(chuàng)建表格

MySQL的資料形態(tài):

  • INT --整形

  • DECIMAL(m,n) --有小數(shù)點的數(shù) (3,2)則是2.33,總共有m位數(shù),小數(shù)點站n位

  • VARCHAR(n) --字串

  • BLOB --圖片 影片 檔案…(二進制的資料)

  • 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è)計屬性

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

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

MySQL如何使用Python進行連接

四、儲存資料

create table student(
        `student_id` int primary key, -- 第一列
    `name` varchar(20), -- 第二列
    `major` varchar(20) -- 第三列,20指的是最大字符長度
); -- 創(chuàng)建表格并設(shè)計屬性
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('小藍','英語','4'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`major`,`student_id`) values('英語','5'); -- 按照設(shè)置寫入表格數(shù)據(jù),沒有的數(shù)據(jù)則為空白null

MySQL如何使用Python進行連接

五、限制約束

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è)計屬性

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è)計屬性
drop table `student`;
select * from `student`; -- 搜索表格的全部資料

insert into `student`(`name`,`major`) values('小藍','英語'); -- 按照設(shè)置寫入表格數(shù)據(jù)
insert into `student`(`name`) values('小黑'); -- 按照設(shè)置寫入表格數(shù)據(jù)

MySQL如何使用Python進行連接

六、修改、刪除資料

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

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è)計屬性
drop table `student`;
select * from `student`; -- 搜索表格的全部資料

insert into `student`(`name`,`major`) values('小藍','英語'); -- 按照設(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` = '英語'; -- 將其中誰的什么進行更新
-- 還可以進行多個更新
update `student` -- 更新哪個表格
set `major` = '生化' -- 將什么更新成什么
where `major` = '生物' or `major` = '化學(xué)' ; -- 將其中誰的什么進行更新
update `student` -- 更新哪個表格
set `name` = '小輝',`major` = '生化' -- 將什么更新成什么
where `student_id`=1 ; -- 將其中誰的什么進行更新
-- 不加條件則都改
update `student` -- 更新哪個表格
set `major` = '物理'; -- 將其中誰的什么進行更新,都改成了生化

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`; -- 取得表格的全部資料,并排序(默認正序)
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è)計屬性
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è)計屬性
-- 補充外鍵(外表格對應(yīng))
alter table `employee` -- 在什么表格上進行更新
add foreign key(`branch_id`) -- 在他的什么屬性上
references `branch`(`branch_id`) -- 從哪的什么屬性對應(yīng)
on delete set null;
-- 補充外鍵(內(nèi)表格對應(yīng))
alter table `employee` -- 在什么表格上進行更新
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è)計屬性
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è)計屬性
-- 當(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)表格所有資料并排序(默認低到高)
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)計幾筆資料
select count(`sup_id`) from `employee`; -- 統(tǒng)計對應(yīng)屬性資料個數(shù)(null不計入)
-- 增加條件取數(shù)
select count(*)
from `employee`
where `bath_date` > '1970-01-01' and `sex` = 'F'; -- 統(tǒng)計幾筆資料
-- 計算對應(yīng)的屬性的平均
select avg(`salary`) from `employee`;
-- 計算對應(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è)計屬性
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è)計屬性

十六、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')  # 直接打開目標資料庫'''

# 告知開始使用操作
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)  # 因為回傳列表,為便于觀察,進行循環(huán)打印

# 選擇資料庫
cursor.execute("use `sql_tutorial`;")

# 創(chuàng)建表格

# 告知關(guān)閉操作
cursor.close()
# 若要懂資料進行修改需要結(jié)尾加一個指令,這樣才能提交指令生效
connection.commit()
# 關(guān)閉連線
connection.close()

以上就是“MySQL如何使用Python進行連接”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:MySQL如何使用Python進行連接
轉(zhuǎn)載注明:http://muchs.cn/article8/pihgip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)虛擬主機、營銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司