python數據庫操作-mysql數據庫


一:連接

  1:本地連接

  mysql -u用戶名 -p密碼

  2:連接遠程服務器

  mysql -u用戶名 -p密碼 -hip地址 -P端口號     線下修改遠程服務端上部署的mysql服務器    

二:創建數據庫

  create database 名字 utf8;

三:顯示數據庫

  show databases;

四:使用數據庫

  use 數據庫名;

五:刪除數據庫

  drop database if exists 數據庫名      

六:查看數據庫的結構

  show create database 數據庫名  

七:從外部導入sql文件

  導入備份的文本文件:說白了就是把文本文件重新執行一遍。

  例如從桌面導入sql.sql文件

  source C:\Users\Administrator\Desktop\sql.sql  正確導入桌面的sql語句,並執行里面的創庫語句和顯示所有庫的命令。

八:創建數據表

  create table class(id int primary key auto_increment,cname varchar(30),des varchar(100)) charset utf8;

  需要說明:表的編碼格式不指定,會默認繼承數據庫的編碼格式。

九:查看表的結構

  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;

十一:表中添加數據

  方式一:  insert into class set cname="后盾人",des="這是一個學習網站";    插入一組值

  方式二:  insert into class(cname,description) values("mysql","關系型數據庫"),("js","前端開發語言"),("python","膠水語言");  可插入一組值,也可多組值。

  # 其他的插入方式 TODO

十二:根據其他表結構生成相同的表結構

  create table copyclass like class;   根據class的表結構創建一個copyclass的表。

十三:兩個相同結構的表,數據互傳。

  insert into copyclass select * from class;   全部字段都復制過來

  insert into copyclass(cname) select cname from class; 僅僅復制cname字段。

十三:復制一個表的結構和數據的形式創建表。

  create table testclass select * from class;

十四:查詢的基本方式

  select  * from class;  全部字段查詢

  select id,cname from class; 部分字段查詢

  select cname,id from class; 部分字段查詢   說明:查詢顯示的順序和select 后面的字段順序排列一致。

  特殊情況:假如多表聯合查詢,每個表都有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函數的作用

 十八:排序操作

  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    優先級是條件1 > 條件2 > 條件3 

  找到班上最后報名的學生:

  select * from stu order by id desc;  從所有的里面找,第一個

  select * from stu order by id desc limit 1; 從所有的里面,只顯示第一個。 限制顯示條數  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 是查看表的字段信息,而不是建表信息。

二十三:刪除表中的所有數據  

  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 字段名

  

二十六:待完成

二十七:數據類型

  1.字符串類型

  

  2.字符集

    字符串:二進制和非二進制類型,二進制存儲視頻和圖片,非二進制存儲文本內容,非二進制文本受字符集校對規則影響。

    字符集(Character set)是多個字符的集合,字符集種類較多,每個字符集包含的字符個數不同。常用的字符集有GBK、BIG5、UTF8。

    UTF8字符包含文字內容更廣,如韓文、日文、德文兼容度更高,也是推薦使用的字符集。

    show character set; 查看服務器支持的字符集

    默認:表不設置字符集繼承數據庫,字段不設置字符集繼承表。

  3.校對規則

    

    數據庫支持的字符集,校對規則是字符集內,字符比較和排序的一套規則,_ci結尾的是對大小寫不敏感,_bin結尾的是不區分大小寫。

    # TODO 怎么更改字段的校對規則,區分大小寫。

  4.常用字符串處理函數

    字符串截斷函數

    

    需求:將http改為http:

    

    結果

    

    update class set cname = concat("http:",mid(cname,5)) where id >=5;

    獲取字符的區間值

    

    獲取字符的長度

    

    拼接兩個值為一個值  concat函數

    

    需求:取一個字段的內容,如果內容超過八個字符,后面有.....代替

    

  5.正則表達式在mysql中的使用技巧

    需求:cname中第二個字母是y的數據     select * from class where cname like "_y%";  _表示占位符,一個任意的字符,y后面必須加%,站位符,表示0或者多個。

    

    需求:描述字段中包含語言這兩個字的數據

    

    需求:將所有描述字段中包含語言的數據,都加上后盾人這個三個字。

    

  6.數值類型

    整型

    

    取值范圍如果加了unsigned,則最大值翻倍,如tinyint unsigned的取值范圍為(0~256)。

    m的含義不是允許字段的長度,而是顯示長度,在為字段設置 zerofill 時有效。

    需求:添加有前導零的字段

    

    浮點型

    

  7.ENUM/SET

  8.

二十八:時間日期

  1.DBeaver

  2.數據類型

  3.創建字段

  4.格式化

  5.時間戳

  6.常用函數

  7.基本查詢

  8.時間計算

二十九:摘要和排序

  1.order

  2.count

  3.min/max

  4.sum/avg

  5.distinct

  6.group

三十:多表攻略

  1.多表攻略

  2.表關聯

  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.創建外鍵

  3.選項說明

  4.創建動作

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  # group_by 一般和聚合函數一起使用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM