最近在學習scrapy redis,在複習redis的同時打算把mysql和mongodb也複習一下,本篇爲mysql篇,實例比較簡單,學習sql仍是要動手實操記的比較牢。mysql
安裝:sudo apt-get install mysql-server
查看服務:ps ajx | grep mysql
中止服務:sudo service mysql stop
開啓服務:sudo service mysql start
重啓服務:sudo service mysql restart
連接數據庫:mysql -uroot -p後輸入密碼
查看版本:select version();正則表達式
查看數據庫:show database;
建立數據庫:create database 庫名 [charset = UTF8];
查看建庫語句:show create database 庫名;
使用數據庫:use 庫名;
刪除數據庫:drop 庫名;redis
查看錶:show table;
查看錶結構:desc 表名;
建立表:sql
CREATE TABLE table_name( column1 datatype contrai, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY(one or more columns) );
建立表經常使用屬性字段:mongodb
-- auto_inorement 表示自動增加 -- not null 表示不爲空 -- primary key 表示爲主鍵 -- defaul 表示默認值
刪除表:drop table;
修改表結構:數據庫
添加字段:alter table 表名 add 列名 類型; 修改字段(重命名):alter table 表名 change 原名 新名 類型及約束; 修改字段(不重命名):alter table 表名 modify 列名 類型及約束; 刪除字段:alter table 表名 drop 列名;
查看全部列:select * from 表名;
查看指定列:select 列1,列2,... from 表名;安全
全列插入:insert into 表名 values(...); --須要給主鍵留下佔位符,用0或null皆可。
部分列插入:insert into 表名(列1,...) values(值1,...);
插入多行全列數據:insert into 表名 values(...),(...)...;
插入多行部分列數據:insert into 表名(列1,...) values(值1,...),(值1,...)...;scrapy
更新操做:update 表名 set 列1=值1,列2=值2... where 條件;ide
刪除操做(不推薦):delete from 表名 where 條件;
邏輯刪除(推薦):update 字段名 set isvalid=0 where id=1; --設置刪除字段,執行刪除字段的操做即對該字段更新。函數
查詢消除重複行:select distinct 列1,... from 表名;
where條件查詢:select * from 表名 where 條件;
where能夠與比較運算符、邏輯運算符、模糊查詢、範圍查詢、空判斷搭配使用
比較運算符
等於: = 大於: > 大於等於: >= 小於: < 小於等於: <= 不等於: != 或 <>
舉個栗子:
select * from students where id > 1; select * from students where id <= 2; select * from students where name != '鹹魚'; select * from students where is_delete=0;
邏輯運算符
and or not
舉個栗子:
select * from students where id > 3 and gender=0; select * from students where id < 4 or is_delete=0; select * from students where id not 4;
模糊查詢
like % 表示任意多個任意字符 _ 表示一個任意字符 rlike
舉個栗子:
select * from students where name like '鹹%'; --查詢以鹹字開頭的內容 select * from students where name like '鹹_'; --查詢以鹹字開頭且後面只有一個字的內容 select * from students where name like '鹹%' or name like '%魚'; -- 查詢以鹹字開頭或以魚字結尾的內容
範圍查詢
in 表示在一個非連續的範圍內 no in 表示不在一個非連續的範圍內 between ... and ... 表示在一個連續的範圍內 rlike 表示正則查詢,可使用正則表達式查詢數據
舉個栗子:
select * from students where id in(1,3,8); -- 查詢 id 在 1,3,8 當中的內容 select * from students where id not in(1,3,8); -- 查詢 id 不在 1,3,8 當中的內容 select * from students where id between 3 and 8; -- 查詢 id 在3到8之間的內容 select * from students where name rlike "^鹹"; -- 查詢 name 是以鹹字開頭的內容
空判斷
判斷是否爲空 is null
舉個栗子:
select * from students where height is null;
以上幾種預算符優先級爲:
優先級由高到低的順序爲:小括號、not、比較運算符、邏輯運算符。
and比or先運算,若是同時出現並但願先算or,須要結合()使用。
排序
asc 升序 desc 降序
舉個栗子:
select * from students order by age desc,height desc; --顯示全部的學生信息,先按照年齡從大到小排序,當年齡相同時 按照身高從高到矮排序
聚合函數
count(*)查詢總數 max(列)表示求此列的最大值 min(列)表示求此列的最小值 sum(列)表示求此列的和 avg(列)表示求此列的平均值
舉個栗子:
select count(*) from students; select max(id) from students where gender=2; select min(id) from students where is_delete=0; select sum(age) from students where gender=1; select sum(age)/count(*) from students where gender=1; --求平均年齡 select avg(id) from students where is_delete=0 and gender=2;
分組
group by 將查詢結果按照1個或多個字段進行分組,字段值相同的爲一組 group_concat 表示分組以後,根據分組結果,使用group_concat()來放置每一組的某字段的值的集合
舉個栗子:
select gender from students group by gender; -- 將學生按照性別分組 輸出結果: +--------+ | gender | +--------+ | 男 | | 女 | | 中性 | | 保密 | +--------+ select gender,group_concat(name) from students group by gender; 輸出結果: +--------+-----------------------------------------------------------+ | gender | group_concat(name) | +--------+-----------------------------------------------------------+ | 男 | 小彭,小劉,小周,小程,小郭 | | 女 | 小明,小月,小蓉,小賢,小菲,小香,小杰 | | 中性 | 小金 | | 保密 | 小鳳 | +--------+-----------------------------------------------------------+
分頁
select * from 表名 limit start,count
舉個栗子:
select * from students where gender=1 limit 0,3; --查詢前三行的數據
鏈接查詢
語法:
select * from 表1 inner/left/right join 表2 on 表1.列 = 表2.列
其中:
inner join(內鏈接查詢):查詢的結果爲兩個表匹配到的數據 right join(右鏈接查詢):查詢的結果爲兩個表匹配到的數據,右表特有的數據,對於左表中不存在的數據使用null填充 left join(左鏈接查詢):查詢的結果爲兩個表匹配到的數據,左表特有的數據,對於右表中不存在的數據使用null填充
舉個栗子:
select from students inner join classes on students.cls_id = classes.id;
select from students as s left join classes as c on s.cls_id = c.id;
select * from students as s right join classes as c on s.cls_id = c.id;
子查詢
在一個 select 語句中,嵌入了另一個 select 語句, 那麼被嵌入的 select 語句稱之爲子查詢語句。
子查詢能夠和 in 搭配使用
主查詢 where 條件 in (子查詢)
mysqldump –uroot –p 數據庫名 > 備份文件名.sql;
mysql -uroot –p 新數據庫名 < 備份文件名.sql
Python與mysql交互流程
安裝相關庫:pip install pymysql
導入:from pymysql import *
connection = connect(host, port, database, user, password, charset)
其中參數以下:
host:鏈接的mysql主機,若是本機是'localhost' port:鏈接的mysql主機的端口,默認是3306 database:數據庫的名稱 user:鏈接的用戶名 password:鏈接的密碼 charset:通訊採用的編碼方式,推薦使用utf8
connection對象方法以下:
close()關閉鏈接 commit()提交 cursor()返回Cursor對象,用於執行sql語句並得到結果
cursor=connection.cursor()
其中經常使用方法:
close():關閉cursor execute(operation [, parameters ]):執行語句,返回受影響的行數,主要用於執行insert、update、delete語句,也能夠執行create、alter、drop等語句。 fetchone():執行查詢語句時,獲取查詢結果集的第一個行數據,返回一個元組 fetchall():執行查詢時,獲取結果集的全部行,一行構成一個元組,再將這些元組裝入一個元組返回
舉個栗子:
from pymysql import * def main(): conn = connect(host='localhost',port=3306,database='xianyuplus',user='root',password='mysql',charset='utf8') cs1 = conn.cursor() count = cs1.execute('insert into xianyufans(name) values("666")') conn.commit() cs1.close() conn.close() if __name__ == '__main__': main()
視圖是對若干張基本表的引用,一張虛表,查詢語句執行的結果,不存儲具體的數據。
建立視圖:create view 視圖名稱 as select語句; --建議視圖以v_開頭 查看視圖:show tables; 使用視圖:select * from 視圖名稱; 刪除視圖:drop view 視圖名稱;
提升了重用性,就像一個函數
對數據庫重構,卻不影響程序的運行
提升了安全性能,能夠對不一樣的用戶
事務,它是一個操做序列,這些操做要麼都執行,要麼都不執行,它是一個不可分割的工做單位。
原子性,一個事務必須被視爲一個不可分割的最小工做單元,整個事務中的全部操做要麼所有提交成功,要麼所有失敗回滾,對於一個事務來講,不可能只執行其中的一部分操做,這就是事務的原子性。
一致性,數據庫老是從一個一致性的狀態轉換到另外一個一致性的狀態。
隔離性,一個事務所作的修改在最終提交之前,對其餘事務是不可見的。
開啓事務:start transaction; 或者 begin; 提交事務:commit; 回滾事務:rollback;
數據庫索引比如是一本書前面的目錄,能加快數據庫的查詢速度
建立索引:create index 索引名稱 on 表名(字段名稱(長度)) --當指定索引的字段類型爲字符串時,應填寫長度 查看索引:show index from 表名; 刪除索引:drop index 索引名稱 on 表名;
創建太多的索引將會影響更新和插入的速度,由於它須要一樣更新每一個索引文件。對於一個常常須要更新和插入的表格,就沒有必要爲一個不多使用的where字句單獨創建索引了,對於比較小的表,排序的開銷不會很大,也沒有必要創建另外的索引。
以上就是關於mysql的一些用法,實際上是比較基礎的,重點部分是關於mysql的查詢部分,畢竟在業務應用中主要仍是查詢爲主。