python的数据库_python数据库操作-mysql数据库
一:連接
1:本地連接
mysql -u用戶名 -p密碼
2:連接遠程服務器
mysql -u用戶名 -p密碼 -hip地址 -P端口號 線下修改遠程服務端上部署的mysql服務器
二:創(chuàng)建數(shù)據(jù)庫
create database 名字 utf8;
三:顯示數(shù)據(jù)庫
show databases;
四:使用數(shù)據(jù)庫
use 數(shù)據(jù)庫名;
五:刪除數(shù)據(jù)庫
drop database if exists 數(shù)據(jù)庫名
六:查看數(shù)據(jù)庫的結構
show create database 數(shù)據(jù)庫名
七:從外部導入sql文件
導入備份的文本文件:說白了就是把文本文件重新執(zhí)行一遍。
例如從桌面導入sql.sql文件
source C:\Users\Administrator\Desktop\sql.sql 正確導入桌面的sql語句,并執(zhí)行里面的創(chuàng)庫語句和顯示所有庫的命令。
八:創(chuàng)建數(shù)據(jù)表
create table class(id int primary key auto_increment,cname varchar(30),des varchar(100)) charset utf8;
需要說明:表的編碼格式不指定,會默認繼承數(shù)據(jù)庫的編碼格式。
九:查看表的結構
desc 表名 直接查看表的結構
十:刪除表
drop table if exists class 刪除的原因是,上面的建表語句中,cname字段都是可以為null的,這與真實情況不符。
create table class(id int primary key auto_increment,cname varchar(30) not null,des varchar(100) null) charset utf8;
十一:表中添加數(shù)據(jù)
方式一: insert into class set cname="后盾人",des="這是一個學習網(wǎng)站"; 插入一組值
方式二: insert into class(cname,description) values("mysql","關系型數(shù)據(jù)庫"),("js","前端開發(fā)語言"),("python","膠水語言"); 可插入一組值,也可多組值。
# 其他的插入方式 TODO
十二:根據(jù)其他表結構生成相同的表結構
create table copyclass like class; 根據(jù)class的表結構創(chuàng)建一個copyclass的表。
十三:兩個相同結構的表,數(shù)據(jù)互傳。
insert into copyclass select * from class; 全部字段都復制過來
insert into copyclass(cname) select cname from class; 僅僅復制cname字段。
十三:復制一個表的結構和數(shù)據(jù)的形式創(chuàng)建表。
create table testclass select * from class;
十四:查詢的基本方式
select * from class; 全部字段查詢
select id,cname from class; 部分字段查詢
select cname,id from class; 部分字段查詢 說明:查詢顯示的順序和select 后面的字段順序排列一致。
特殊情況:假如多表聯(lián)合查詢,每個表都有id字段,都要顯示怎么辦。不能都顯示成id吧,因此可以給選擇的字段進行命名,顯示時候按照命名顯示。
select id as classs_id,cname from class;
十五:條件查詢 where
select * from class where id>2; 顯示id>2的字段信息
select * from class where cname="mysql";顯示字段名字為mysql的字段信息。
select * from class where description like "%水%";顯示描述中含有水這個字信息的所有字段。 where 字段 like 模糊查詢條件。
select * from class where cname like "p%"; 顯示名字以p開頭的所有字段。 %是like查詢中的占位符,表示0個或多個任意字符。
select * from class where cname not like "p%";顯示名字不以p開頭的所有字段。 not like 否定形式
select * from class where cname not like "p%" and id>2; 顯示名字不以p開頭且id>2的字段信息and 可以連接 條件查詢的條件1和條件2,表示并且
十六:條件查詢
先準備一個表:
create table stu(id int primary key auto_increment,sname char(10),class_id int default null,age smallint not null);
insert into stu(sname,class_id,age) values("劉德華",1,18),("張學友",2,20),("郭富城",1,22),("黎明",null,24),("陳奕迅",3,26);
1:查詢學生名字包含張或者班級為2班的學生。
select * from stu where sname like "張" or class_id = 2; or 可以連接 條件查詢的條件1和條件2,表示或
2:查詢目前一共有幾個班
select class_id from stu; 顯示結果又重復,去重用 distinct
select distinct class_id from stu; 將class_id的字段進行去重。
3:查看年齡在20-24的所有學生。
select * from stu where age>20 and age <24; 不包含兩端的值
select * from stu where age between 20 and 24; 包含兩端的值
4:查看班級為2或班級為3的所有學生
select * from stu where class_id =2 or class_id =3;
select * from stu where class_id in (2,3);
十七:條件查詢:處理null的技巧
1:要查詢沒有班級號碼為null的所有學生
select * from stu where class_id = 'null"; 得不到結果
select * from stu where class_id = null; 得不到結果
null的比較:需要使用 is 和 is not 進行判斷
select * from stu where class_id is null;
select * from stu where class_id is not null;
2:查詢所有學生,如果有班級編號,顯示班級編號,沒有顯示,無。
select sname,if(class_id,class_id,"無") from stu;
select sname,if(class_id,class_id,"無") as class_id from stu;
select sname,ifnull(class_id,"無") as class_id from stu;
# TODO if函數(shù)的作用
十八:排序操作
select sname,class_id from stu order by age desc; 從大到小排列
select sname,age from stu order by age asc; 從小到大排列
select sname,age,class_id from stu order by class_id desc;
# 同時要對班級相同的年齡從大到小排序,需要進行兩次排序,先排序班級,在排序年齡。
select sname,age,class_id from stu order by class_id desc age desc;
order by 條件1,條件2,條件3 優(yōu)先級是條件1 > 條件2 > 條件3
找到班上最后報名的學生:
select * from stu order by id desc; 從所有的里面找,第一個
select * from stu order by id desc limit 1; 從所有的里面,只顯示第一個。 限制顯示條數(shù) limit
注意:limit(a,b) a---從哪里開始取 b---取幾個。
select * from stu where class_id=2 and age is not null order by age asc limit 1;
目的:是為了查找二班中年齡最小的一個人。但是存在隱患,要是班上有兩個人,年齡都最小,那么就會漏過一個人。
因此需要借助后面更加復雜的子查詢語句進行限制,后面會講到。下面的語句查詢出來更加準確。
select * from stu where age = (select age from stu where class_id =2 and age is not null order by age asc limit 1);
十九:更新表結構的使用技巧
假如:要將上表中,班級class_id為null的改為class_id=2
update stu set class_id = 2 where class_id is null; update ......set........
需求:1班中年齡小于20的給年齡增加10歲。
update stu set age=age+10 where age<20 and class_id=1;
二十:刪除操作
delete from stu where age < 30 and class_id is null;
delete from stu order by id desc limit 2; 將id最大的兩個從表中刪除。
二十一:表的維護-修改表名
方式一:alter table stu rename stus;
方式二:rename table stus to stu;
二十二:修改表的字符集和查看表的建表結構
1. 修改表的字符集
alter table stu charset "gbk";
2. 查看表的建表信息
show create table stu; 注意:desc stu 是查看表的字段信息,而不是建表信息。
二十三:刪除表中的所有數(shù)據(jù)
truncate stu;
二十四:刪除表
drop table if exists stu;
二十五:修改表的字段類型
需求:將cname的type從varchar(30)改為varchar(50) 不能為空
alter table class modify cname varchar(50) not null;
alter 是修改表的關鍵字 搭配 modify rename change 等方法使用
需求:將cname改為class_name char(50) 可以為空 默認是高級班
alter table class change cname class_name char(30) null default "高級班";
需求:給表class添加一個字段stu_num,int(30) not null default 30
alter talbe class add stu_num int(30) not null default 30;
需求:刪除class表中的stu_num字段
alter table 表明 drop 字段名
二十六:待完成
二十七:數(shù)據(jù)類型
1.字符串類型
2.字符集
字符串:二進制和非二進制類型,二進制存儲視頻和圖片,非二進制存儲文本內容,非二進制文本受字符集和校對規(guī)則影響。
字符集(Character set)是多個字符的集合,字符集種類較多,每個字符集包含的字符個數(shù)不同。常用的字符集有GBK、BIG5、UTF8。
UTF8字符包含文字內容更廣,如韓文、日文、德文兼容度更高,也是推薦使用的字符集。
show character set; 查看服務器支持的字符集
默認:表不設置字符集繼承數(shù)據(jù)庫,字段不設置字符集繼承表。
3.校對規(guī)則
數(shù)據(jù)庫支持的字符集,校對規(guī)則是字符集內,字符比較和排序的一套規(guī)則,_ci結尾的是對大小寫不敏感,_bin結尾的是不區(qū)分大小寫。
# TODO 怎么更改字段的校對規(guī)則,區(qū)分大小寫。
4.常用字符串處理函數(shù)
字符串截斷函數(shù)
需求:將http改為http:
結果:
update class set cname = concat("http:",mid(cname,5)) where id >=5;
獲取字符的區(qū)間值
獲取字符的長度
拼接兩個值為一個值 concat函數(shù)
需求:取一個字段的內容,如果內容超過八個字符,后面有.....代替
5.正則表達式在mysql中的使用技巧
需求:cname中第二個字母是y的數(shù)據(jù) select * from class where cname like "_y%"; _表示占位符,一個任意的字符,y后面必須加%,站位符,表示0或者多個。
需求:描述字段中包含語言這兩個字的數(shù)據(jù)
需求:將所有描述字段中包含語言的數(shù)據(jù),都加上后盾人這個三個字。
6.數(shù)值類型
整型
取值范圍如果加了unsigned,則最大值翻倍,如tinyint unsigned的取值范圍為(0~256)。
m的含義不是允許字段的長度,而是顯示長度,在為字段設置 zerofill 時有效。
需求:添加有前導零的字段
浮點型
7.ENUM/SET
8.
二十八:時間日期
1.DBeaver
2.數(shù)據(jù)類型
3.創(chuàng)建字段
4.格式化
5.時間戳
6.常用函數(shù)
7.基本查詢
8.時間計算
二十九:摘要和排序
1.order
2.count
3.min/max
4.sum/avg
5.distinct
6.group
三十:多表攻略
1.多表攻略
2.表關聯(lián)
3.笛卡爾積
4.inner
5.outer join
6.self join
7.多對多
8.union
9.多表刪除
三十一:事務出來
1.事務處理
2.存儲引擎
3.提交模式
4.程序控制
5.事務隔離
三十二:鎖機制
1.鎖機制
2.存儲引擎
3.事務處理
4.悲觀鎖
5.樂觀鎖
6.表鎖機制
三十三:外鍵約束
1.外鍵約束
2.創(chuàng)建外鍵
3.選項說明
4.創(chuàng)建動作
# group_by 一般和聚合函數(shù)一起使用
總結
以上是生活随笔為你收集整理的python的数据库_python数据库操作-mysql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 密度图的密度估计_不同类型的二维密度图小
- 下一篇: linux 的内核参数优化,Linux服