MySQL 操做總結

1. 數據庫級別操做

1.1 建立數據庫mysql

CREATE DATABASE db1 default charset utf8 collate utf8_general_ci;

 

1.2 刪除數據庫sql

DROP DATABASE db1;

 

 

2. 用戶級別操做

 

2.1 單首創建用戶並受權數據庫

CREATE USER 'hukey'@'%';

 

2.2 刪除用戶服務器

DROP USER 'hukey'@'%';

 

2.3 修改用戶ide

RENAME USER 'hukey'@'%' TO 'superman'@'%';

 

2.4 修改密碼測試

SET PASSWORD FOR 'superman'@'%' = PASSWORD('111111');

 

 

3. 受權管理操做

 

3.1 爲已有用戶受權spa

GRANT SELECT, UPDATE, DELETE ON db1.* TO 'superman'@'%';

 

3.2 查看用戶受權3d

SHOW GRANTS FOR 'superman'@'%';

 

3.3 刪除用戶受權調試

REVOKE DELETE ON db1.* FROM 'superman'@'%';

 

    all privileges          除grant外的全部權限
    select                  僅查權限
    select,insert           查和插入權限
    ...
    usage                   無訪問權限
    alter                   使用alter table
    alter routine           使用alter procedure和drop procedure
    create                  使用create table
    create routine          使用create procedure
    create temporary tables 使用create temporary tables
    create user             使用create userdrop user、rename user和revoke??all privileges
    create view             使用create view
    delete                  使用delete
    drop                    使用drop table
    execute                 使用call和存儲過程
    file                    使用select into outfile 和 load data infile
    grant option            使用grant 和 revoke
    index                   使用index
    insert                  使用insert
    lock tables             使用lock table
    process                 使用show full processlist
    select                  使用select
    show databases          使用show databases
    show view               使用show view
    update                  使用update
    reload                  使用flush
    shutdown                使用mysqladmin shutdown(關閉MySQL)
    super                   使用change master、kill、logs、purge、master和set global。還容許mysqladmin????????調試登錄
    replication client      服務器位置的訪問
    replication slave       由複製從屬使用
對於權限
    對於目標數據庫以及內部其餘:
        數據庫名.*          數據庫中的全部
        數據庫名.表         指定數據庫中的某張表
        數據庫名.存儲過程   指定數據庫中的存儲過程
        *.*                 全部數據庫
對於數據庫
    用戶名@IP地址       用戶只能在改IP下才能訪問
    用戶名@192.168.1.%  用戶只能在改IP段下才能訪問(通配符%表示任意)
    用戶名@%            用戶能夠再任意IP下訪問(默認IP地址爲%)
對於用戶和IP地址

 

PS:修改完權限須要執行:code

FLUSH PRIVILEGES;

 將數據讀取到內存中,從而當即生效.

 

4. 經常使用數據類型

MySQL數據類型能夠分爲 3 大類:
(1)數值類型:int float
(2)字符串類型 char varchar text
(3)時間類型 datetime

 

重點問題:
    char 和 varchar 的區別?
    char: 定長,效率高
    varchar:不定長,效率偏低

 

5. 表內容操做

 

5.1 增

insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from

 

5.2 刪

delete fromdelete fromwhere id=1 and name='alex'
truncate table tb2;   清空數據並將自增值設置爲 1

 

5.3 改

updateset name = 'alex' where id>1

 

5.4 查

select * fromselect * fromwhere id > 1
select nid,name,gender as gg fromwhere id > 1

 

5.5 其餘

5.5.1 條件
    select * fromwhere id > 1 and name != 'alex' and num = 12;
 
    select * fromwhere id between 5 and 16;
 
    select * fromwhere id in (11,22,33)
    select * fromwhere id not in (11,22,33)
    select * fromwhere id in (select nid from 表)

5.5.2 通配符
    select * fromwhere name like 'ale%'  - ale開頭的全部(多個字符串)
    select * fromwhere name like 'ale_'  - ale開頭的全部(一個字符)

5.5.3 限制
    select * from 表 limit 5;            - 前5行
    select * from 表 limit 4,5;          - 從第4行開始的5行
    select * from 表 limit 5 offset 4    - 從第4行開始的5行

5.5.4 排序
    select * fromorder byasc              - 根據 「列」 從小到大排列
    select * fromorder bydesc             - 根據 「列」 從大到小排列
    select * fromorder by 列1 desc,列2 asc    - 根據 「列1」 從大到小排列,若是相同則按列2從小到大排序

5.5.5 分組
    select num fromgroup by num
    select num,nid fromgroup by num,nid
    select num,nid fromwhere nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) fromgroup by num,nid
 
    select num fromgroup by num having max(id) > 1021
 
    特別的:group by 必須在where以後,order by以前

5.5.6 連表
    無對應關係則不顯示
    select A.num, A.name, B.name
    from A,B
    Where A.nid = B.nid
 
    無對應關係則不顯示
    select A.num, A.name, B.name
    from A inner join B
    on A.nid = B.nid
 
    A表全部顯示,若是B中無對應關係,則值爲null
    select A.num, A.name, B.name
    from A left join B
    on A.nid = B.nid
 
    B表全部顯示,若是B中無對應關係,則值爲null
    select A.num, A.name, B.name
    from A right join B
    on A.nid = B.nid

5.5.7 組合
    組合,自動處理重合
    select nickname
    from A
    union
    select name
    from B
 
    組合,不處理重合
    select nickname
    from A
    union all
    select name
    from B

5.5.8 連表查詢

left join 表名 on 
right join 表名 on 
inner join  將出現null時一行隱藏

 

 

6. 補充知識

6.1 mysql left join 中 where 和 on 條件的區別?

left join中關於where和on條件的幾個知識點:

    1. 多表left join是會生成一張臨時表,並返回給用戶
    2. where條件是針對最後生成的這張臨時表進行過濾,過濾掉不符合where條件的記錄,是真正的不符合就過濾掉。
    3. on條件是對left join的右表進行條件過濾,但依然返回左表的全部行,右表中沒有的補爲NULL
    4. on條件中若是有對左表的限制條件,不管條件真假,依然返回左表的全部行,可是會影響右表的匹配值。也就是說on中左表的限制條件隻影響右表的匹配內容,不影響返回行數。

 

測試:
    建立兩張表:

-- t1 表
CREATE TABLE t1(id INT,name VARCHAR(20));
insert  into `t1`(`id`,`name`) values (1,'a11');
insert  into `t1`(`id`,`name`) values (2,'a22');
insert  into `t1`(`id`,`name`) values (3,'a33');
insert  into `t1`(`id`,`name`) values (4,'a44');

-- t2 表
CREATE TABLE t2(id INT,local VARCHAR(20));
insert  into `t2`(`id`,`local`) values (1,'beijing');
insert  into `t2`(`id`,`local`) values (2,'shanghai');
insert  into `t2`(`id`,`local`) values (5,'chongqing');
insert  into `t2`(`id`,`local`) values (6,'tianjin');

 

 

測試01:返回左表全部行,右表符合 on 條件的原樣匹配,不知足條件的補 NULL

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id;
+------+------+----------+
| id   | name | local    |
+------+------+----------+
|    1 | a11  | beijing  |
|    2 | a22  | shanghai |
|    3 | a33  | NULL     |
|    4 | a44  | NULL     |
+------+------+----------+

 

 

測試02:on後面增長對右表的限制條件:t2.local='beijing'

結論02:左表記錄所有返回,右表篩選條件生效

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id and t2.local='beijing';
+------+------+---------+
| id   | name | local   |
+------+------+---------+
|    1 | a11  | beijing |
|    2 | a22  | NULL    |
|    3 | a33  | NULL    |
|    4 | a44  | NULL    |
+------+------+---------+

 

 

測試03:只在 where 後面增長對右表的限制條件:t2.local = 'beijing'

結論03:針對右表,相同條件,在where後面對最後的臨時表進行記錄篩選,行數可能會減小;在on後面是做爲匹配條件進行篩選,篩選的是右表的內容。

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t2.local='beijing';
+------+------+---------+
| id   | name | local   |
+------+------+---------+
|    1 | a11  | beijing |
+------+------+---------+

 

 

測試04:t1.name = 'a11' 或者 t1.name = 'a33'

結論04:on中對左表的限制條件,不影響返回的行數,隻影響右表的匹配內容

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id and t1.name='a11';  
+------+------+---------+
| id   | name | local   |
+------+------+---------+
|    1 | a11  | beijing |
|    2 | a22  | NULL    |
|    3 | a33  | NULL    |
|    4 | a44  | NULL    |
+------+------+---------+

MariaDB [db2]> select t1.id, t1.name, t2.local from t1 LEFT JOIN t2 on t1.id = t2.id and t1.name = 'a33';
+------+------+-------+
| id   | name | local |
+------+------+-------+
|    1 | a11  | NULL  |
|    2 | a22  | NULL  |
|    3 | a33  | NULL  |
|    4 | a44  | NULL  |
+------+------+-------+

 

 

測試05:where t1.name = 'a33' 或者 where t1.name = 'a22'

結論05:where條件是在最後臨時表的基礎上進行篩選, 顯示只符合最後where條件的行

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t1.name='a33';  
+------+------+-------+
| id   | name | local |
+------+------+-------+
|    3 | a33  | NULL  |
+------+------+-------+

MariaDB [db2]> SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t1.name='a22'; 
+------+------+----------+
| id   | name | local    |
+------+------+----------+
|    2 | a22  | shanghai |
+------+------+----------+
相關文章
相關標籤/搜索