mysql 必知必會 學習筆記

– Ubuntu Linux mysql 啓動 停止
1.啓動:/etc/init.d/mysql start
2.停止:/etc/init.d/mysql stop
3.重啓:/etc/init.d/mysql restart

– MYSQL遠程連接速度慢的解決方法
後來在網上發現解決方法,my.ini裏面添加
[mysqld]
skip-name-resolve
skip-name-resolve 禁用DNS解析,連接速度會快很多。不過,這樣就不能在授權表中使用主機名,只能用ip格式。

–mysql 客戶端
1.MySQL Administrator
2.MySQL Query Browser


show columns from _tablename;
7.1

–order 排序方式 & 返回多列
select prod_id, prod_name, prod_price from products order by prod_id, prod_price;
–desc
select prod_id, prod_name, prod_price from products order by prod_id desc, prod_price;
–distinct 返回不相同的值(去重)
select distinct vend_id from products;
–limit 5,6 限制返回 從行5開始的6行
select prod_name from products limit 5,6;
–完全限定的表名
select products.prod_name from crashcourse.products;
–找到 TOP5
select prod_price from products order by prod_price desc limit 5;

–note:如果同時使用order by 和 where時,應該讓order by在where之後
select prod_name, prod_price from products where prod_price = 2.5;
select * from products where vend_id > 1002 order by vend_id;
–範圍查找
select * from products where prod_price between 5 and 10;
–空值null 查找(null值不能判斷是否匹配)
select prod_name from products where prod_price is null;
select * from customers where cust_email is null;
–操作符 and or
select * from products where vend_id = 1003 and prod_price <= 10;
select * from products where vend_id = 1003 or prod_price <= 10;
–在處理 or 操作符前,優先處理and操作符。但最好都用()確認好計算次序
select * from products where vend_id = 1002 or vend_id = 1003 and prod_price >= 10;
select * from products where (vend_id = 1002 or vend_id = 1003) and prod_price >= 10;
–操作符 in 與 純or操作符 功能相同,且in操作符更直觀、執行更快。還可包含其它select語句。
select * from products where vend_id in (1002, 1003, 1004) order by prod_name;
–操作符 not (mysql 僅支持對in、between、exists 使用not)
select * from products where vend_id not in (1002, 1003, 1004) order by prod_name;

–第8章 通配符like
–note:儘量少用通配符,因爲效率不高
–note:除非必要,不要將通配符放在搜索模式開始處,因爲這樣耗時最多
–note:正確使用通配符很有必要,也經常用到
–通配符 % 任何字符出現任意次數
select prod_id, prod_name from products where prod_name like ‘jet%’;
select * from products where prod_name like ‘%anvil%’;
select * from products where prod_name like ‘s%e’;
–通配符 _ 任何單個字符
select * from products where prod_name like ‘_ ton anvil’;

–第9章 正則表達式 regexp 表示後面接的是正則表達式
–默認不區分大小寫,可使用binary來區分大小寫
–包含1000的所有行:
select * from products where prod_name regexp ‘1000’ order by prod_name;
–’.’ 正則表達式中的特殊字符,它表示 匹配任意一個字符。
select * from products where prod_name regexp ‘.000’;
–’|’ 或(or)匹配
select * from products where prod_name regexp ‘1000|2000|3000’;
–’[]’ 匹配幾個字符之一(另一種形式的or) ‘1 ton’ ‘2 ton’ ‘3 ton’
select * from products where prod_name regexp ‘[123] ton’;
–’^’ 否定匹配符 (任意非123字符 + ton)
select * from products where prod_name regexp ‘[^123] ton’;
–’[-]’ 匹配範圍 例如:[1-6]、[a-z]
select * from products where prod_name regexp ‘[1-5] ton’;
–’\’ 匹配指定字符(轉義符)
select * from vendors where vend_name regexp ‘\.’;
–’\’ 也用來引用元字符

–9.2.6 匹配字符類
[:alnum:] 任意字母和數字 同[a-zA-Z0-9]
[:alpha:] 任意字母 同[a-zA-Z]
[:lower:] 任意小寫字母 同[a-z]
[:upper:] 任意大寫字母 同[A-Z]
[:blank:] 空格和製表 同[\t]
[:digit:] 任意數字 同[0-9]
[:xdigit:] 16進制數字 同[a-fA-F0-9]
[:print:] 任意可打印字符
[:graph:] 同[:print:]但不包括空格
[:cntrl:] ASCII控制字符 ASCII 0到31和127
[:punct:] 既不在[:alnum:]也不在[:cntrl:]中的任意字符
[:space:] 空格在內的任意空白字符 同[\f\n\r\t\v]

–9.2.7 匹配多個實例

–元字符
–* 0個或多個匹配
–+ 1個或多個匹配 等於{1,}
–? 匹配它前面字符的0個或1個匹配 等於{0,1} sticks or stick
select * from products where prod_name regexp ‘\([0-9] sticks?\)’;
–{n} 指定數目的匹配
select * from products where prod_name regexp ‘[[:digit:]]{4}’;
select * from products where prod_name regexp ‘[0-9][0-9][0-9][0-9]’;
–{n,} 不少於指定數目的匹配
–{n,m} 匹配數目的範圍 m<=255

–定位元字符
–^ 文本的開始 (找以’.'開始的數)
select * from products where prod_name regexp ‘1’;
–$ 文本的結尾
select * from products where prod_name regexp ‘[0-9]$’;
–[[:<:]] 詞的開始 例:文本 ‘jet 110’ 中有兩個詞
select ‘jet 1000’ regexp ‘[[:<:]]je’;
–[[:>:]] 詞的結尾
select ‘jet 1000’ regexp ‘[0-9][[:>:]]’;

–簡單的正則表達式 測試
select ‘hello’ regexp ‘[0-9]’; --沒有匹配:0,匹配:1

–10.1 計算字段 是運行時在select語句內創建的
–10.2 拼接字段
–concat 拼接字段 多數dbms使用+或||來拼接
select concat(vend_name, ’ (’, vend_country, ‘)’) from vendors;
–rtrim() 函數去掉右邊所有空格 ltrim(左邊空格) trim(兩邊空格)
select concat(rtrim(vend_name), ’ (’, rtrim(vend_country), ‘)’) from vendors;
–as 使用別名(導出列)
select concat(rtrim(vend_name), ’ (’, rtrim(vend_country), ‘)’) as vend_title from vendors;
–10.3 算術操作符 + - * /
select prod_id, quantity, item_price, quantity*item_price as ex_price from orderitems where order_num = 20005;

–11.1 函數
–upper 轉換成大寫
select vend_name, upper(vend_name) as vend_name_upcase from vendors;
–11.2.1 文本處理函數
–left() 返回串左邊的字符
–length() 返回串的長度
–locate() 找出串的一個子串
–lower() 將串轉換爲小寫
–ltrim() 去掉串左邊的空格
–rtrim() 去掉串右邊的空格
–right() 返回串右邊的字符
–soundex() 返回串的soundex值 (‘Y Lie’ 與 ‘Y Lee’ 發音相似)
select * from customers where soundex(cust_contact) = soundex(‘Y Lie’);
–substring() 返回子串的字符
–upper() 將串轉換成大寫

–11.2.2 日期和時間處理函數
AddDate() --增加一個日期
AddTime() --增加一個時間
CurDate() --返回當前日期
CurTime() --返回當前時間
Date() --增加日期時間的日期部分
DateDiff() --計算兩個日期之差
Date_Add() --高度靈活的日期運算函數
Date_Format() --格式化的日期或時間串
Day() --日期的天數部分
DayOfWeek() --返回一個日期是星期幾
Hour() --一個時間的小時部分
Minute() --一個時間的分鐘部分
Month() --一個日期的月份部分
Now() --當前日期和時間
Second() --一個時間的秒部分
Time() --一個日期時間的時間部分
Year() --日期的年份部分
– 指定某天時間內
select * from orders where date(order_date) = ‘2005-09-01’;
– 指定某段時間內
select * from orders where date(order_date) between ‘2005-09-01’ and ‘2005-09-30’;
– 指定某月時間內
select * from orders where year(order_date) = 2005 and month(order_date) = 9;

–11.2.3 數值處理函數
Abs() --絕對值
Cos() --角度的餘弦
Exp() --數的指數值
Mod() --餘數
Pi() --圓周率
Rand() --隨機數
Sin() --角度的正弦
Sqrt() --平方根
Tan() --角度的正切

–12.1 聚集函數 將數據彙總而不用實際檢索出來(高效的,一般比程序中計算更快)
–avg() --平均值
select avg(prod_price) as avg_price from products (where xx = …);
–count() --行數
select count() as mun_cust from customers;
select count(cust_email) as mun_cust from customers;
–max() --最大值
–min() --最小值
select max(prod_price) as max_price from products;
–sum() --和
select sum(quantity) as timers_ordered from orderitems where order_num = 20005;
select sum(quantity
item_price) as timers_ordered from orderitems where order_num = 20005;
–distinct 【去重】 必須使用列名,不能用於計算或表達式
–如果指定列名,則distinct只能用於count()且不能用於count()
select avg(distinct prod_price) as avg_price from products
–返回(物品數目、最高價格、最低價格、平均價格)
select count(
) as num_items,
min(prod_price) as price_min,
max(prod_price) as price_max,
avg(prod_price) as price_avg
from products;

–13.1 數據分組
–group by 除聚集計算語句外,select中的每列都必須在group by子句中
–group by 必須在where之後,order by之前
select vend_id, count() as num_prods from products group by vend_id order by num_prods;
–with rollup 會將所有分組再進行彙總
select vend_id, count(
) as num_prods from products group by vend_id with rollup;
–13.3 過濾分組
–having 分組語法中的where
–where數據分組前進行過濾,having的數據分組後進行過濾
select cust_id, count() as orders from orders group by cust_id having count() >= 2;
–13.4 分組和排序
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(ordertotal) >= 50;
–13.5 select 子句順序如下
select --1. 要返回的列表或表達式
from --2. 從中檢索數據的表
where --3. 行級過濾
group by --4. 分組說明
having --5. 組級過濾
order by --6. 輸出排序順序
limit --7. 要檢索的行數

–14.1 子查詢 一般與:IN = <> 配合使用
select order_num from orderitems where prod_id = ‘TNT2’;
select cust_id from orders where order_num in(20005, 20007);
– 上面兩句等於:
select cust_id from orders where order_num in(
select order_num from orderitems where prod_id = ‘TNT2’
);
– 子查詢中,需保證select具有與where子句中相同數目的列。
select * from customers where cust_id in (
select cust_id from orders where order_num in (
select order_num from orderitems where prod_id = ‘TNT2’));

–14.3
select cust_name, cust_state, (select count(*) from orders
where orders.cust_id = customers.cust_id) as orders from customers order bycust_name;

–15.1 join聯結 sql最強大最重要的特性
–!!!sql最強大的功能之一就是:在數據檢索查詢的執行中聯結(join)

–15.1.1 關係表 :將信息分成多個獨立的表,以避免相同數據出現多次.各表之間通過常用值互相關聯
–note:外鍵:定義了兩表之間的關係(外部主鍵)
–note:關係數據庫 可以有效地存儲和方便處理,因此它的可伸縮性遠比非關係數據庫要好。
–note:笛卡兒積 聯結後的行最大數目是兩個表的行數積
–note:叉聯結
select vend_name, prod_name, prod_price from vendors, products
where vendors.vend_id = products.vend_id

–15.2.2 內部聯結 inner join 前後兩個表順序可換
select vend_name, prod_name, prod_price from vendors inner join products
on vendors.vend_id = products.vend_id;

–15.2.3 聯結多個表
select prod_name, vend_name, prod_price, quantity from orderitems, products, vendors
where products.vend_id = vendors.vend_id and orderitems.prod_id = products.prod_id and order_num = 20005;

–note:外聯結與內聯結的區別:內聯結需兩邊都有元素才聯結,外聯結只需其中一邊有元素就可聯結,另一邊若無則爲null
–16 高級聯結:自聯結、自然聯結和外部聯結
–16.1 使用表別名 表別名跟列別名不一樣,它不返回到客戶機
select cust_name, cust_contact from customers as c, orders as o, orderitems as oi
where c.cust_id = o.cust_id and oi.order_num = o.order_num and prod_id = ‘TNT2’;
–16.3.1 自聯結 有時自聯結會比子查詢快得多,雖然結果一樣
–方式1:子查詢
select prod_id, prod_name from products where vend_id = (select vend_id from products where prod_id =‘DTNTR’);
–方式2:自聯結
select p1.prod_id, p1.prod_name from products as p1, products as p2
where p1.vend_id = p2.vend_id and p2.prod_id = ‘DTNTR’;
–16.2.2 自然聯結 用where實現
select c.*, o.order_num, o.order_date, oi.prod_id, oi.quantity, oi.item_price
from customers as c, orders as o, orderitems as oi
where c.cust_id = o.cust_id and oi.order_num = o.order_num and oi.prod_id = ‘FB’;
–16.2.3 外部聯結 left outer join \ right outer join
select customers.cust_id, orders.order_num from customers
left outer join orders on customers.cust_id= orders.cust_id;


–16.3 使用帶聚集函數的聯結
select customers.cust_name, customers.cust_id, count(orders.order_num) as num_ord from customers
inner join orders on customers.cust_id = orders.cust_id group by customers.cust_id;
select customers.cust_name, customers.cust_id, count(orders.order_num) as num_ord from customers
left outer join orders on customers.cust_id = orders.cust_id group by customers.cust_id;
–16.4 使用聯結和聯結條件

–17.1 組合查詢 下面兩種情況時需要用到
–(1)在單個查詢中從不同的表返回類似結構的數據
–(2)對單個表執行多個查詢,按單個查詢返回數據

–17.2 創建組合查詢 union 可將多條select語句的結果 組合成單個結果集
–17.2.1 union 雖然看起來union比where更繁瑣,但處理多表時更簡單
select vend_id, prod_id, prod_price from products where prod_price <= 5
union
select prod_id, vend_id, prod_price from products where vend_id in (1001, 1002);
–17.2.2 union規則
–(1)n條select語句就有需要n-1個union分隔
–(2)union和每個查詢必須包含相同的列、表達式或聚集函數(次序不必相同)
–(3)列數據類型必須兼容,不必完全相同(可隱式轉換的類型)
–17.2.3 包含或取消重複的行 union 默認自動從結果中去重,若相顯示所有結果,可使用union all
–17.2.4 對組合查詢結果排序 order by 只能出現一次且出現在最後一條select語句之後

–18.1 理解全文本搜索 不是所有引擎都支持全文本搜索,MyISAM(支持)InnoDB(不支持)
–且有幾個限制
–(1)性能問題
–(2)明確控制:用通配符和正則表達式,很難明確控制匹配和不匹配什麼。
–(3)智能化的結果:同義詞等需求
–18.2 使用全文本搜索 索引後,select可與match()和against()一起使用來搜索
–18.2.1 啓用全文本搜索支持 fulltext
–note:應先導入數據,再定義fulltext,這樣導入數據更快
create table productnoteMy
(
note_id int not null auto_increment,
prod_id char(10) not null,
note_date datetime not null,
note_text text null,
primary key(note_id),
fulltext(note_text)
)engine=MyISAM;
–18.2.2 全文本搜索 因數據是索引的,全文本搜索相當快。
select note_id, note_text from productnotes where match(note_text) against(“rabbit”);
select match(note_text) against(‘rabbit’) as rank, note_text from productnotes order by rank desc;
–18.2.3 查詢擴展 返回了有’anvils’所在行上任意單詞的所有行
select note_id, note_text from productnotes where match(note_text) against(‘anvils’ with query expansion);
–18.2.4 布爾文本搜索 沒有fulltext索引也可以使用。是一種非常緩慢的操作
select note_text from productnotes where match(note_text) against(‘heavy’ in boolean mode);
–包含heavy 但沒有rope開始的詞(rope*)
select note_text from productnotes where match(note_text) against(‘heavy -rope*’ in boolean mode);
–全文布爾操作符
– + 包含,詞必須存在
– - 包含,詞必須不出現
– > 包含,且增加等級值
– < 包含,且減少等級值
– () 把詞組成子表達式
– ~ 取消一個詞的排序值
– * 詞尾通配符
– “” 定義一個短語
–18.2.5 全文本搜索的使用說明 若干…


  1. 0-9\. ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章