1. 程式人生 > >淺談資料庫(二)

淺談資料庫(二)

1、資料型別
  1、數值型別
  2、字元型別
  3、列舉型別
  4、日期時間型別
    1date"YYYY-MM-DD"
    2、datetime :"YYYY-MM-DD HH:MM:SS"
    3、timestamp :"YYYY-MM-DD HH:MM:SS"
    4、time :"HH:MM:SS"
    5、注意
      datetime :不給值預設返回 NULL
      timestamp :不給值預設返回系統當前時間
    6、示例
      1、建立一張表,使用者充值表
        create table t2(
    id int,
    username varchar(20
), password varchar(20), money int, birthday date, cztime timestamp )character set utf8; 2、插入記錄 insert into t2 values (1,"使用者1","123456",500,"1995-05-20","2018-08-30 09:40:30"); insert into t2 values (2,"使用者2","123456",600,"1992-02-20",now()); 2、日期時間函式 1、now() 返回伺服器當前時間 2
、curdate() 當前日期 3date("1999-09-09 09:09:09") 提取 年月日 4、time("...") 提取 時分秒 5、year("...") 提取 年 6、練習 1、查詢2018830日使用者充值的詳細資訊 select * from t2 where date(cztime)="2018-08-30"; 2、查詢20188月份所有使用者充值的資訊 select * from t2 where date(cztime)>="2018-07-01" and date(cztime)<="2018-07-31"
; 3、查詢2018083008:00-10:00之間使用者充值資訊 select * from t2 where cztime>="2018-08-30 08:00:00" and cztime<="2018-08-30 10:00:00"; 3、日期時間運算 1、語法格式 select * from 表名 where 欄位名 運算子(now()-interval 時間間隔單位); 時間間隔單位: 2 day | 3 hour | 1 minute | 2 year | 3 month 2、示例 1、查詢1天以內的充值記錄 select * from t2 where cztime>=(now()-interval 1 day); 2、查詢1年以前的充值記錄 select * from t2 where cztime<(now()-interval 1 year); 3、查詢1天以前、3天以內的充值記錄 select * from t2 where cztime>=(now()-interval 3 day) and cztime<=(now()-interval 1 day); 4、表字段操作 1、語法 :alter table 表名 ...; 2、新增欄位(add) alter table 表名 add 欄位名 資料型別; alter table 表名 add 欄位名 資料型別 first; alter table 表名 add 欄位名 資料型別 after 欄位名; 3、刪除欄位(drop) alter table 表名 drop 欄位名; 4、修改欄位資料型別(modify) alter table 表名 modify 欄位名 新資料型別; ## 會受到表中已有資料的限制 5、修改表名(rename) alter table 表名 rename 新表名; 6、修改欄位名(change) alter table 表名 change 原欄位名 新欄位名 資料型別; alter table new_t3 change name username char(15); 7、練習 1、在 db2 庫中建立表 stutab ,欄位有3個: name、age、phnumber use db2; create table stutab( name char(20), age tinyint, phnumber bigint ); 2、在表中第一列新增一個 id 欄位 alter table studab add id int first; 3、把 phnumber 的資料型別改為 char(11) alter table studab modify phnumber char(11); 4、在表中最後一列新增一個欄位 address alter table studab add address varchar(30); 5、刪除表中的 age 欄位 alter table stutab drop age; 5、表記錄操作 1、刪除表記錄(delete) 1、delete from 表名 where 條件; 2、注意 一定要加where條件,不加where條件全部刪除表記錄 2、更新表記錄(update) 1、update 表名 set 欄位1=值1,欄位2=值2 where 條件; 2、注意 一定要加where條件,不加where條件全部更新表記錄 6、運算子操作 1、數值比較&&字元比較&&邏輯比較 1、數值比較 := != > >= < <= 2、字元比較 := != 3、邏輯比較 : 1and :兩個或者多個條件同時成立 2or :有1個條件滿足即可 where country="蜀國" or country="魏國"; 4、練習 1、查詢攻擊力高於150的英雄的名字和攻擊值 select name,gongji from sanguo where gongji>150; 2、將趙雲的攻擊力設定為360,防禦力設定為88,名字改為趙子龍 update sanguo set gongji=360,fangyu=88,name="趙子龍" where name="趙雲"; 3、將吳國英雄中攻擊值為110的英雄的攻擊值改為100,防禦力改為60 update sanguo set gongji=100,fangyu=60 where country="吳國" and gongji=110; 4、查詢蜀國和魏國的英雄資訊 select * from sanguo where country="蜀國" or country="魏國"; 5、找出攻擊力高於200的蜀國英雄的名字、攻擊值和國家 select name,gongji,country from sanguo where gongji>200 and country="蜀國"; 2、範圍內比較 1、between 值1 and2 2in(值1,值2) 3not in(值1,值2) 4、練習 1、查詢攻擊值100-200之間的蜀國英雄資訊 select * from sanguo where (gongji between 100 and 200) and country="蜀國"; 2、查詢蜀國和吳國以外的國家的女英雄資訊 select * from sanguo where country not in("蜀國","吳國") and sex="女"; 3、查詢id為135的蜀國英雄 和 貂蟬的資訊 select * from sanguo where (id in(1,3,5) and country="蜀國") or name="貂蟬"; 3、匹配空 和 非空 1、空 :is null 2、非空 :is not null 3、練習 1、查詢姓名為 NULL 的蜀國男英雄資訊 select * from sanguo where name is NULL and country="蜀國" and sex="男"; 2、查詢姓名為 "" 的英雄資訊 select * from sanguo where name=""; 3、在所有蜀國英雄中查詢攻擊力大於150的並且名字不為NULL的英雄的姓名、攻擊值和國家 select name,gongji,country from sanguo where country="蜀國" and gongji>150 and name is not NULL; 4、查詢魏蜀兩國英雄中攻擊力小於200並且防禦力小於80的英雄資訊 select * from sanguo where country in("魏國","蜀國") and gongji<200 and fangyu<80; 4、注意 1、NULL :空值,只能用isis not去匹配 2"" :空字串,只能用 =、!= 去匹配 4、模糊查詢(like) 1where 欄位名 like 表示式 2、表示式 1、_ :匹配單個字元 2、% :匹配0到多個字元 3、練習 # name中有2個字元以上的 select name from sanguo where name like "_%_"; # 匹配所有,但不包括NULL select name from sanguo where name like "%"; # 匹配名字為3個字元 select name from sanguo where name like "___"; # 匹配姓趙的英雄 select name from sanguo where name like "趙%"; 7、SQL高階查詢 1、總結 3select ...聚合函式 from 表名 1where ... 2group by ... 4、having ... 5order by ... 6、limit ...; 2order by :給查詢結果排序 1order by 欄位名 ASC/DESC 2、ASC(預設) :升序 DESC :降序 3、練習 1、將所有英雄按防禦值從高到低排序 select * from sanguo order by fangyu DESC; 2、將蜀國英雄按攻擊值從高到低排序 select * from sanguo where country="蜀國" order by gongji DESC; 3、將魏蜀兩國英雄中名字為3個字的,按防禦值升序排序 select * from sanguo where country in("魏國","蜀國") and name like "___" order by fangyu; select * from sanguo where (country="蜀國" or country="魏國") and name like "___" order by fangyu; select * from sanguo where (country="蜀國" and name like "___") or (country="魏國" and name like "___") order by fangyu; 3、limit (永遠放在SQL命令的最後寫) 1、顯示查詢記錄的條數 2、用法 limit n; --> 顯示 n 條記錄 limit m,n; --> 從第 m+1 條記錄開始,顯示 n 條 limit 2,3 : 顯示第345三條記錄 3、練習 1、在蜀國英雄中,查詢防禦值倒數第2名到倒數第4名的英雄的姓名、防禦值和國家 select name,fangyu,country from sanguo where country="蜀國" order by fangyu ASC limit 1,3; 2、在所有蜀國名字不為NULL的英雄中,查詢攻擊值前3名的英雄的姓名、攻擊值和國家 select name,gongji,country from sanguo where country="蜀國" and name is not NULL order by gongji DESC limit 3; 4、分頁 每頁顯示5條記錄,顯示第4頁的內容 每頁顯示n條記錄,顯示第m頁的內容 第1頁 :limit (1-1)*5,5 # 1 2 3 4 52頁 :limit (2-1)*5,5 # 6 7 8 9 103頁 :limit (3-1)*5,5 # 11 12 13 14 15 ... 第m頁 :limit (m-1)*n,n 4、聚合函式 1、分類 avg(欄位名) : 求該欄位的平均值 sum(欄位名) : 求和 max(欄位名) : 最大值 min(欄位名) : 最小值 count(欄位名) : 統計該欄位記錄的個數 2、練習 1、所有英雄中攻擊力最大值 select max(gongji) from sanguo; 2、統計id、name兩個欄位分別有多少條記錄 select count(id),count(name) from sanguo; 5group by : 給查詢的結果進行分組 1、查詢表中都有哪些國家 select country from sanguo group by country; 2、計算每個國家的平均攻擊力 select country,avg(gongji) from sanguo group by country; 先分組 再聚合 再去重 蜀國 蜀國 蜀國 200 蜀國 魏國 魏國 200 魏國 吳國 100 吳國 3、注意 1select之後的欄位名如果沒有在group by之後出現,則必須要對該欄位進行聚合處理(聚合函式) 4、練習 1、查詢所有國家中英雄數量最多的前2名的國家名稱和英雄數量(count()) select country,count(id) as number from sanguo group by country order by number DESC limit 2; 6、having語句 1、作用 :對查詢結果進行進一步的篩選 2、練習 1、找出平均攻擊力大於105的國家的前2名,顯示國家名和平均攻擊力 1、計算每個國家的平均攻擊力 2、找到平均攻擊力 > 105select country,avg(gongji) as average from sanguo group by country having average>105 order by average DESC limit 2; 3、注意 1、having語句通常和group by語句聯合使用,過濾由group by語句返回的記錄集 2where只能操作表中實際存在欄位,having語句可操作 7、表字段、表記錄操作 表字段(alter table 表名) 表記錄 增: add insert into 表名 .. 刪: drop delete from 表名 .. 改: modify update 表名 set ... 查: desc 表名; select * from 表名 ..