排序查詢:select 字段列表 from [表名]?order by?[字段名1] [asc升序/desc降序,默認(rèn)值為升序],[字段名2] [排序方式];//字段名1為優(yōu)先級排序,如果字段名1有相同的,再以字段名2排序
聚合函數(shù):
count | 統(tǒng)計數(shù)量(一般不選null的列) |
max | 最大值 |
min | 最小值 |
sum | 總和 |
avg | 平均值 |
Select 聚合函數(shù)(字段名) from 表名;//null值不參與所有聚合函數(shù)運(yùn)算 ! !
分組查詢:selcet 字段列表 from [where 分組前條件限定] group by 分組字段名 [having 分組后條件過濾];//分組后查詢的字段為聚合函數(shù)和分組字段,查詢其他字段無意義,執(zhí)行順序:where>聚合函數(shù)>having
eg:select sex,avg(score),count(*) from where score>60 student group by sex?having count(*)>1;//查詢student表格中以sex(性別)為分組的score值大于60的人的平均score(成績)并且前面加上sex的數(shù)據(jù)后面加上分組后每個組的數(shù)量,如果人數(shù)小于2則過濾掉
AS的用法: select [字段] [聚合函數(shù)] as [your_name] from ... :as就是將聚合函數(shù)的內(nèi)容的字段有個名字
分頁查詢:select 字段列表 from limit 起始索引 , 查詢條目數(shù);//起始索引=(當(dāng)前頁碼-1)*每頁顯示的條數(shù)
—多表查詢—
基礎(chǔ):select *from 表名1,表名2; 這種查會顯示表1和表2的集合乘,即展示的表會有表1數(shù)量乘表2數(shù)量的乘積。
隱式內(nèi)連接:select 字段列表 from 表1,表2… where 條件;
顯示內(nèi)連接:select 字段列表 from 表1 [inner] join 表2 on 條件;//這兩種差不多,只是表達(dá)不一樣。
左右連接:select 字段列表 from 表1 left/right join 表2 on 條件;//顯示左/右邊的所有數(shù)據(jù)(因為有些條件會不顯示部分?jǐn)?shù)據(jù)或有些數(shù)據(jù)為null)
索引index
索引是對數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)。MySQL索引的建立對于MySQL的高效運(yùn)行是很重要的,索引可以大大提高M(jìn)ySQL的檢索速度
- 主鍵索引:一張表只能有一個主鍵索引,不允許重復(fù)、不允許為 NULL;
ALTER TABLE TableName ADD PRIMARY KEY(column_list);
- 唯一索引:數(shù)據(jù)列不允許重復(fù),允許為 NULL 值,一張表可有多個唯一索引,索引列的值必須唯一,但允許有空值。如果是組合索引,則列值的組合必須唯一。
CREATE UNIQUE INDEX IndexName ON `TableName`(`字段名`(length));
# 或者
ALTER TABLE TableName ADD UNIQUE (column_list);
- 普通索引:一張表可以創(chuàng)建多個普通索引,一個普通索引可以包含多個字段,允許數(shù)據(jù)重復(fù),允許 NULL 值插入;
CREATE INDEX IndexName ON `TableName`(`字段名`(length));
# 或者
ALTER TABLE TableName ADD INDEX IndexName(`字段名`(length));
視圖view
視圖是虛擬表,本身不存儲數(shù)據(jù),而是按照指定的方式進(jìn)行查詢。所以借助視圖,來執(zhí)行相同或相似的查詢。
使用視圖和使用表完全一樣,只需要把視圖當(dāng)成一張表就OK了。視圖是一張?zhí)摂M表
操作指令 | 代碼 |
---|---|
創(chuàng)建視圖 | CREATE VIEW 視圖名(列1,列2...) AS SELECT (列1,列2...) FROM ...; |
使用視圖 | 當(dāng)成表使用就好 |
修改視圖 | CREATE OR REPLACE VIEW 視圖名 AS SELECT [...] FROM [...]; |
查看數(shù)據(jù)庫已有視圖 |
>SHOW TABLES [like...]; (可以使用模糊查找) |
查看視圖詳情 |
DESC 視圖名 或者SHOW FIELDS FROM 視圖名
|
視圖條件限制 | [WITH CHECK OPTION] |
?文章來源:http://www.zghlxwxcb.cn/news/detail-667293.html
授權(quán)與回收權(quán)限
MySQL 賦予用戶權(quán)限命令的簡單格式可概括為:
?1 grant 權(quán)限 on 數(shù)據(jù)庫對象 to 用戶 ?
一、grant 普通數(shù)據(jù)用戶,查詢、插入、更新、刪除 數(shù)據(jù)庫中所有表數(shù)據(jù)的權(quán)利
1 grant select on testdb.* to common_user@'%'
2 grant insert on testdb.* to common_user@'%'
3 grant update on testdb.* to common_user@'%'
4 grant delete on testdb.* to common_user@'%'
或者,用一條 MySQL 命令來替代:
?1 grant select, insert, update, delete on testdb.* to common_user@'%' ?
二、grant 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)等權(quán)限
grant 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結(jié)構(gòu)權(quán)限。
1 grant create on testdb.* to developer@'192.168.0.%';
2 grant alter on testdb.* to developer@'192.168.0.%';
3 grant drop on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 外鍵權(quán)限:
?1 grant references on testdb.* to developer@'192.168.0.%'; ?
grant 操作 MySQL 臨時表權(quán)限:
?1 grant create temporary tables on testdb.* to developer@'192.168.0.%'; ?
grant 操作 MySQL 索引權(quán)限:
?1 grant index on testdb.* to developer@'192.168.0.%'; ?
grant 操作 MySQL 視圖、查看視圖源代碼權(quán)限:
1 grant create view on testdb.* to developer@'192.168.0.%';
2 grant show view on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 存儲過程、函數(shù)權(quán)限:
1 grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
2 grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
3 grant execute on testdb.* to developer@'192.168.0.%';
三、grant 普通 DBA 管理某個 MySQL 數(shù)據(jù)庫的權(quán)限
?1 grant all privileges on testdb to dba@'localhost' ?
其中,關(guān)鍵字 “privileges” 可以省略。
四、grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權(quán)限:
?1 grant all on *.* to dba@'localhost' ?
五、MySQL grant 權(quán)限,分別可以作用在多個層次上
1. grant 作用在整個 MySQL 服務(wù)器上:
1 grant select on *.* to dba@localhost; -- dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。
2 grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
1 grant select on testdb.* to dba@localhost; -- dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
1 grant select, insert, update, delete on testdb.orders to dba@localhost;
這里在給一個用戶授權(quán)多張表時,可以多次執(zhí)行以上語句。例如:
1 grant select(user_id,username) on smp.users to mo_user@'%' identified by '123345';
2 grant select on smp.mo_sms to mo_user@'%' identified by '123345';
4. grant 作用在表中的列上:
1 grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存儲過程、函數(shù)上:
1 grant execute on procedure testdb.pr_add to 'dba'@'localhost'
2 grant execute on function testdb.fn_add to 'dba'@'localhost'
六、查看 MySQL 用戶權(quán)限
查看當(dāng)前用戶(自己)權(quán)限:
1 show grants;
查看其他 MySQL 用戶權(quán)限:
1 show grants for dba@localhost;
七、撤銷已經(jīng)賦予給 MySQL 用戶權(quán)限的權(quán)限。
revoke 跟 grant 的語法差不多,只需要把關(guān)鍵字 “to” 換成 “from” 即可:
1 grant all on *.* to dba@localhost;
2 revoke all on *.* from dba@localhost;
八、MySQL grant、revoke 用戶權(quán)限注意事項
1. grant, revoke 用戶權(quán)限后,該用戶只有重新連接 MySQL 數(shù)據(jù)庫,權(quán)限才能生效。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-667293.html
到了這里,關(guān)于MySQL數(shù)據(jù)庫學(xué)習(xí)筆記(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!