《MYSQL必知必會》

來源:互聯網
上載者:User

標籤:模式   客戶   and   between   country   custom   情境   部分   group by   

1、 同一個資料庫中不允許出現同名表;不同的資料庫中可以出現同名表
2、 每一行記錄都用有一個key(一列或一組列作為key)
3、 作為key的列不允許值為空白(NULL)
4、 多個列作為key時,多個列的組合必須唯一,但其中單個列的值可以不唯一
5、 好習慣:作為key的列的取值最好不要變
6、 SQL是操作資料庫的結構化查詢語言 (SQL);MYSQL是一種資料庫管理系統,即是一種資料庫軟體
7、 MYSQL、 Oralcle、SQL Server等資料庫都是基於客戶機-伺服器的資料庫
8、 查看所有資料庫:show databases;
切換資料庫:use 資料庫名;
切換到某個資料庫後,看資料庫中的所有表:show tables;
查看錶中所有列:show columns from 表名;或者:describe 表名
顯示允許的show語句:help show
9、 SQL語言不區分大小寫 如select和SELECT 意思是一樣的
10、除非確實需要表中的每個列,否則別用*,檢視不需要的列會降低效能
11、去掉重複的行:select distinct vend_id from products;
Distinct 放在列的前面;distinct應用於所有列,不僅是前置它的列
12、只要查詢結果中的前幾行:select name from produdct limit 5 order by time;
只要查詢結果中的第4~9行:Select name form product limit 5,5 order by time;
Limite + 開始行+行數
13、一般order by後面接的是查詢的列,其實用非檢索的列也是可以的!!!
14、預設按升序排序;明確指定按升序排:asc
15、按降序排:select price,name for product order by price desc,name;
Distinct是對所有列有效,desc只對其前面的那一列有效
Distinct加在列名前面,desc加在列名後面
若多個列想按降序排,則多個列都要在後面加上desc
16、Select name,price from prodct where name = ‘fuses’;
用來篩選的列是字串類型,所以要加單引號
17、Select price from product where price between 5 and 10;
18、若列中不包含值,則稱其包含空值NULL
空值檢查:select price from product where price is null;
19、NULL與不匹配:在通過過濾選擇不具有特定值的行時,你可能希望返回具有NULL值的行,但是,不行
20、where中的字句可以用and 或or 方式串連
select * from product where price>10 and id = 1003;
select * from product where id =1003 or id =1006;
21、where子句中即有and也有or時,應該用圓括弧明確的進行分組
where (id =1002 or id=1003) and price >=10;
22、in和or的功能相當,但in更快,且in可以包含其他select語句
where id in (1002,1003)
23、not 常與 in、between、exists 一起用
where id not in (1003,1004)
24、% 表示任何字元出現任意次數(當然報錯0次)
-底線只匹配單個字元
Select name form product where name like ‘jet%’;
Where name like ‘jet-‘;
25、若資料庫中的anvil後面有空格,用’%anvil’是匹配不上的
26、盡量不要使用萬用字元,因為有了萬用字元,搜尋速度會很慢;
把萬用字元置於搜尋模式的開始處,搜尋速度最慢
27、第9章Regex沒怎麼看
28、將欄位進行拼接時,多數資料庫管理系統使用+或||來實現拼接,但MYSQL則使用concat函數來拼接
Select concat(name,country) as title from vendors;
用AS定義列名,亦可用AS修改當前的列名(重新命名)
29、去掉查詢結果中欄位的空格:RTrim、LTrim、Trim
30、Select quantity ,price,quantity*price as expand_price from order;
MYSQL支援 + - * /
31、對日期操作時,建議使用yyyy-mm-dd這種格式,消除了多義性
32、資料庫中存的是2005-09-01 11:30:05,用date()擷取日誌部分
Where date(order_date)=’2001-09-01’
可以通time()擷取時間部分
33、檢索2005年9月下的訂單:where year(order_date)=2005 and month(order_date)=9
34、SQL常用聚集合函式:AVG、COUNT、MAX、MIN、SUM
35、COUNT(*)不忽略值為NULL的列;COUNT(列名)忽略值為NULL的列
36、統計數量和:select sum(quantity) as test from order
統計總價:select sum(price*quantiry) as totalprice from order
37、計算各個不同價格的平均值:select sum(distinct price) form order
38、分組:group by ; 過濾分組having
按id進行分組,計算每組的行數:Select id,count(*) from order group by id
39、Having類似where,以為差別是where過濾的是行,having過濾的是分組
Where在分組前進行過濾;having在分組後進行過濾
列出具有2個以上,價格為10以上的產品供應商:
Select id,cout(*) from order where price>10 group by id having count(*)>2;
40、Select id, sum(quantiry*price) as total from order group by id order by total
41、Select 子句順序:
Select ->from ->where ->group by ->having ->order by->limit
42、外鍵:外鍵為某表中的一列,它包含另一個表的主索引值,定義了兩個表之間的關係
43、連接:從多個表中查資料:select vend_name,product_name price from vend, product where vend.vend_id=product.vend_id;
所有連接都應該有where語句,否則返回的是笛卡爾積
44、連接是耗時的,連接的表越多,效能下降的越厲害!
45、外部連接:連接包含了那些在相關表中沒有關聯線的行
如查詢每個客戶下單數量,包括尚未下過單的,一個是客戶表一個是訂單表,有的客戶在訂單表中無相應記錄
Select customers.id ,orders.num from customers left outer join orders on orders.id=customers.id
46、使用外部連接outer join,必需包含關鍵字right或left
47、union組合查詢等價於多一個where條件,這兩種技術在不同的查詢中效能也不通;但where代替不了union all
48、Union中的每個查詢必需包含相同的列、運算式或聚集合函式
49、Union主要用於如下兩種情境:
1) 從不同的表返回類似資料
2) 對同張表執行多個查詢
Select id from order where id in(1002,1005) union select id from order where price >100;
50、Union從查詢結果中自動去掉了重複的行,若不想去掉重複行,可以用union all
51、第18章,全文檢索搜尋 需要再看
52、全文檢索搜尋類似like,但比like要快,因為它建立了索引
53、當資料庫被多個客戶訪問時,若select是最重要的,則可以在insert和into之間天愛關鍵詞low_priority,指示MYSQL降低insert語句的優先順序,update、delete也是
54、Insert into 表名(列名)values(列值),(列值)
Update tbl_order set name =’11’ where id =0;
Delete from tbl_order where id = 0;
55、從一個表中讀資料,插入到另一個表中:Insert select
Insert into order (name,age) select id,old from customers;
56、更新多行時,若某一行更新失敗,則整改更新失敗,錯誤之前更新的行也被恢複成原來的值。
即使某條更新錯誤,也要繼續更新,可以使用ignore關鍵字,如:
Update ignore tbl_name ……..
57、刪除表中所有行,可以用truncate table;這個速度更快。他是刪掉原表,再建一個新表
58、建立表:create table 表名
(列名 列的屬性 是否可以為空白 預設值 ,
Id int not null default 1 ,
…….,
Primary key (id))
59、僅在不存在時建立:creat table if not exists 表名

《MYSQL必知必會》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.