2.4MySQL 表連接、函數(shù)、事物、視圖等

1.連接 表連接 內(nèi)連接和外連接 自連接

(1)表連接 from后加多表,在where后加連接條件

例:

-- 查詢?nèi)肆Y源部的所有員工,顯示: 人名 職位 部門名稱 城市

select * from employee,department where deptno=id;

select ename,ejob,dname,city from employee a,department b where deptno=b.id and dname='人力資源部';

select a.username,b.name ,c.title

? ? -> from bbs_user a,bbs_category b,bbs_forum c

? ? -> where b.cid = c.cid and c.uid = a.uid;


(2)內(nèi)連接

select 字段名,... from 表1 inner join 表2 on `表1`.xxx = 表2.xxx where 條件?

例:查詢工作地點在北京的員工姓名,職位,獎金,底薪,部門名

select A.id, ename,ejob,ebonus,ebsalary,dname from employee A inner join department B on A.deptno=B.id where dname='人力資源部'; ? #給表起一個別名A、B,方便書寫


(3)右連接和左連接?

1)-- 左外連接: left JOIN ....on 左側(cè)的表為主表,主表的記錄會全部顯示,從表有匹配的顯示,沒有匹配的則顯示null,右側(cè)的表為從表

2)-- 右連接: 主表 從表 right JOIN ....on

-- 右側(cè)的表為主表,主表的記錄會全部顯示,從表有匹配的顯示,沒有匹配的則顯示null,左側(cè)的表為從表

例:-- 部門號錯誤的員工

select * from department right JOIN employee on employee.deptno=department.id where department.id is null

select * from employee LEFT JOIN department on employee.deptno=department.id where department.id is null

例:注:如下案例,employee

select * from employee LEFT JOIN department on employee.deptno=department.id

select * from department LEFT JOIN employee on employee.deptno=department.id?

(4)--全連接:左右連接綜合 sql server oracle 有, mysql無】

-- full join ... on

select * from department full join employee on employee.deptno=department.id?

(5)-- 自連接 表中有相關(guān)字段有聯(lián)系

-- 查詢部門員工以及主管名字

select b.*,a.ename as 主管名 from employee a,employee b where a.id=b.manager

2.函數(shù)

(1)內(nèi)部函數(shù)


(2)日期函數(shù)


(3)數(shù)學(xué)函數(shù)


(4)其他函數(shù)


應(yīng)用:

insert into phone2 values(3,'iphone','12 plus','黑色','6999',CURDATE())

insert into phone2 values(4,'iphone','12','黑色','5999',NOW())

select * from employee where MONTH(ehiredate)=10


3.事務(wù)

-- 多個數(shù)據(jù)庫操作看成是一個整體,整體是不可分。 原子性

-- 事務(wù)的語句:

-- 1.開啟事務(wù): begin | start TRANSACTION

-- 2. 提交事務(wù): COMMIT

-- 3. 回滾事務(wù): ROLLBACK? 還可以結(jié)合保存點使用

-- 4. 設(shè)置保存點: SAVEPOINT 名字;? (可設(shè)置多個)

-- 5. 回滾到指定的保存點: rollback to 保存點名字


4.視圖

-- 視圖: VIEW 使用視圖簡化查詢。視圖就是固化的sql語句

create view myview as # 創(chuàng)建視圖 create view 視圖名 as?

select dname from department LEFT JOIN employee on employee.deptno=department.id? where employee.id is null;

-- 使用視圖

select * from myview;

-- 刪除視圖

drop view myview;


5.索引

索引就像圖書的目錄,可以加快查詢速度

### 3.1 索引的優(yōu)點

- 可以大大加快數(shù)據(jù)的檢索速度

- 唯一索引可以保證數(shù)據(jù)的唯一性

- 可以降低分組、排序的時間

- 可以使用查詢優(yōu)化器提高系統(tǒng)性能

### 3.2 索引的缺點

- 建立索引會建立對應(yīng)索引文件,占用大量空間

- 建立索引會降低增、刪、改的效率

### 3.3 不建立索引

- 頻繁更新的字段不要建立索引

- 沒出現(xiàn)在where、having,不要建立索引

- 數(shù)據(jù)量少的表沒有必要建立索引

- 唯一性比較差的字段不要建立索引

### 3.4 索引分類

####? 普通索引

? ? create index 索引名 on 表名(字段 asc/desc) 默認(rèn)asc升序

####? 唯一索引

? 在唯一索引所在列不能有重復(fù)值,增加和修改會受影響。

~~~

create? unique index 索引名 on 表名(字段 asc/desc) 默認(rèn)asc升序

~~~

####? 主鍵索引

? 創(chuàng)建表,主鍵索引會自動添加,要求在主鍵上不能有重復(fù)值,不能有空值

#### 復(fù)合索引(聯(lián)合索引) 索引了多個列

- 使用聯(lián)合索引,必須包含左前綴。? (a,b,c)

? - a

? - a,b

? - a,b,c

#### 全文索引(了解)

? 一般會用全文索引服務(wù)器(sphinx),不會直接創(chuàng)建全文索引

create? FULLTEXT index 索引名 on 表名(字段 asc/desc)

#### 刪除索引drop index 索引名 on 表

#### 查看索引 ? show index from 表 \G

#查看sql性能

explain select sno,sname from student where class='1812'\G;

mysql> explain select sno,sname from student where sclass='1812' ;

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type | table? | partitions | type | possible_keys | key? | key_len | ref? | rows | filtered | Extra? ? ? |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

|? 1 | SIMPLE? ? ? | student | NULL? ? ? | ALL? | NULL? ? ? ? ? | NULL | NULL? ? | NULL |? 10 |? ? 10.00 | Using where |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

type:? ALL? 全表掃描

? ? ? index? 使用索引

? ? ? range? 在指定范圍內(nèi)使用索引

? ? ? const、system 常量查詢

? ~~~

#### 其它創(chuàng)建索引的方式

~~~

alter table 表 add index(字段1,字段2,...)

alter table 表 add primary key(字段1,字段2,...)

alter table 表 add unique(字段1,字段2,...)

alter table 表 add fulltext(字段1,字段2,...)

~~~

### 3.5 不使用索引的情況

- 查詢時的聯(lián)合索引沒有左前綴,不使用索引

- or條件里,如果一方字段沒有索引,則不使用索引

- 類型不對應(yīng)的不會使用索引

- like? '%tom' ,如果左邊是通配符,不會使用索引

- 使用!=、<>、not in操作,不使用索引

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容