標籤:
一、串連資料庫
mysql -u root -p 預設串連到本地的資料庫mysql -h 192.168.12.12 -u root -p 串連遠端的資料庫
二、基本命令
1、show databases;---------------------------顯示資料庫中的庫2、create database 資料庫名-------------------建立一個資料庫3、use 表名稱;-------------------------------使用表4、show tables;------------------------------顯示當前資料庫中所有的表5、creat table 表名(-------------------------建立表 列名 資料類型 是否為空白 預設值 自增列 設定主鍵 列名 資料類型 是否為空白 設定主鍵)
6、drop table 表名-----------------------------刪除表
7、delete from 表名----------------------------清空表
truncate table 表名
8、修改表的操作:
alter table 表名 add 列名 屬性------------------添加列
alter table 表名 drop 列名 屬性------------------刪除列
alter table 表名 change 舊列名 新列名 類型---修改列
alter table 表名 add primary key(列名)
alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);
alter table 表名 drop foreign key---------------刪除外鍵
自增,如果為某列設定自增列,插入資料時無需設定此列,預設將自增(表中只能有一個自增列)
注意:1、對於自增列,必須是索引(含主鍵)
2、對於自增可以設定步長和起始值
自增列的倆種寫法 :1、在建立的列後面直接加 primary key 2、寫完列後在最後加primary key (列名,列名)
主鍵:一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
外鍵:一個特殊的索引,只能是指定內容
constraint fk_cc foreign key (color_id) references color(nid
三、基礎資料型別 (Elementary Data Type)
資料類型:整數:tinyint ----0-255:int(m)----m表示顯示的位元:bigint ------大整數小數:decimal(m,d)-----精確的小數,其中m=<65,n=<30,m表示數字總長度,n表示小數後的幾位:flout(m,n)-------單精確度浮點數,數字越大越不準確:double(m,n)------雙精確度浮點數,數字越大越不準確字串:char(m)----------定長字串,m表示字串的總長度----------查詢速度快:varchar(m)-------變長字串,m表示字串的總長度:text(m)----------儲存變長的大字串:enum(1,2,3)----枚舉,表示自能從裡面擷取:set--------------集合時間:data----------2016-8-2016-8-2:time----------21:48:datatime-------2016-8-2 21:49:33
四、表內容的操作
1、增insert into 表名(欄位1,欄位2....) values(值1,值2.....),(值1,值2....)2、刪delete from 表名 where 條件3、改updata 表名 set 欄位 = 值 條件4、查條件:select * form 表名 條件select * from 表名 where nid > 1 and name != ‘alex‘;select * from 表名 where nid between 5 and 12;----------------查nid為5-12之間的值select * from 表名 where nid not in (select nid from 表名)select * from 表名 where nid in(1,2,3,4)萬用字元:select * from 表名 where name like ‘ale%‘----------------------以ale開頭的所有字串 %表示多個字元select * from 表名 where name like ‘ale_‘----------------------以ale開頭的所有字串 %表示單個字元限制:select * from 表名 limit 5;------------------------------------找前5行資料select * from 表名 limit 1,2;----------------------------------找從第1行開始以後的2行資料select * from 表名 limit 2 offset 1;----------------------------------找從第1行開始以後的2行資料排序:select * from 表名 order by 欄位1,欄位2;------先按欄位1排序,欄位1相同的值再按欄位2進行排序這裡的後面也可以規定asc(從小到大)和desc(從大到小)進行排序分組:select 欄位1 from 表名 group by 欄位1------------以欄位1中的資料進行分組,顯示欄位1中的種類select count(欄位1) from 表名 group by 欄位1------以欄位1進行分組,並顯示分組後欄位1中資料種類的個數select 欄位1 from 表名 group by 欄位2--------以欄位2進行分組的時候,查詢的是欄位1中的資料,預設是把欄位中小的值取出來select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nidselect count(欄位1),sum(欄位1),max(欄位1),min(欄位1),avg(欄位1) from 表名 grop by 欄位2select count(欄位1),sum(欄位1),max(欄位1),min(欄位1),avg(欄位1) from 表名 grop by 欄位2 having max(欄位1)>1;||----------->如果想用彙總條件作為條件的話,必須使用having
五、pymysql操作資料庫
建立串連conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)建立遊標cursor = conn.cursor()執行SQL,並返回收影響行數effect_row = cursor.execute("update hosts set host = ‘1.1.1.2‘")執行SQL,並返回受影響行數effect_row = cursor.execute("update hosts set host = ‘1.1.1.2‘ where nid > %s", (1,))執行SQL,並返回受影響行數-----------插入多行資料executemany("sql語句",[(),().....])effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])提交,不然無法儲存建立或者修改的資料conn.commit()-----------------查詢的時候不用new_id = cursor.lastrowid-----------擷取自增id ,預設擷取最後一行的關閉遊標cursor.close()# 關閉串連conn.close()
cursor.execute("select * from hosts")cursor.fetchall()--------以元祖套元祖的方式擷取得到的所有資料cursor.fetchone() ----------擷取一行資料,後面再有的話,則擷取下一行資料cursor.fetchmany(m) ----------擷取前幾行資料cursor.scroll(1,mode=‘relative‘) 相對當前位置向下移動一個位置cursor.scroll(1,mode=‘absolute‘) 移動到1的位置cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)-------這樣設定以後,返回的結果是列表套字典的形式
mysql基礎知識總結