1、索引
- 什么是索引
- 一個索引是存盤在表中的資料結構,索引在表的列名上創建,索引中包含了一個列的值,這些值保存在一個資料結構中
- 索引優缺點
- 索引大大提高了查詢速度
- 會降低更新表的速度,如對表進行INSERT、UPDATE和DELETE,因為更新表時,MySQL不僅要保存資料,還要保存一下索引檔案
- 普通索引
- 普通索引是最基本的索引型別,沒有任何限制,值可以為空,僅加速查詢,普通索引是可以重復的,一個表中可以有多個普通索引
- 創建索引
- create index 索引名稱 on 表名(欄位);
- 查看索引
- show index from 表名;
- 洗掉索引
- drop index 索引名稱 on 表名;
# 給 name 欄位創建索引 aa mysql> create index aa on t_student(name); # 查看索引 mysql> show index from t_student; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-----------+------------+----------+--------------+-------------+-----------+--- | t_student | 1 | aa | 1 | name | A | 2 | NULL | NULL | YES | BTREE | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
# 洗掉索引 aa mysql> drop index aa on t_student;
- 唯一索引
- 它與前面的普通索引類似,不同的就是:索引列的值必須唯一,但允許有空值
- 創建索引
- create unique index 索引名稱 on 表名(欄位)
# 給 name 欄位創建唯一索引 mysql> create unique index bb on t_student(name); mysql> show index from t_student; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | t_student | 0 | bb | 1 | name | A | 2 | NULL | NULL | YES | BTREE | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
- 主鍵索引
- 主鍵索引是一種特殊的唯一索引,一個表只能有一個主鍵,不允許有空值;索引列的所有值都只能出現一次,即必須唯一,簡單來說:主鍵索引是加速查詢 + 列值唯一(不可以有null)+ 表中只有一個
- 主鍵是一種唯一性索引,但它必須指定為 "PRIMARY KEY"
- 每個表只有一個主鍵
- 創建索引
- alter table 表名 add primary key(欄位)
# 給 score 欄位添加主鍵索引 mysql> alter table t_course add primary key(score); mysql> show index from t_course; +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | t_course | 0 | PRIMARY | 1 | score | A | 6 | NULL | NULL | | BTREE | | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2、視圖
- 視圖的特點
- 視圖時由基本表產生的虛表
- 視圖的創建和洗掉不影響基礎表
- 視圖的更新和洗掉直接影響基礎表
- 基礎表的更新和洗掉直接影響視圖
- 視圖的作用
- 資料庫中資料的查詢非常復雜,例如多表,子查詢,撰寫陳述句較多,并且這種查詢常被重復使用,那么我們就可以創建視圖,避免每次寫 sql 陳述句會錯誤,也提高了查詢的效率
- 為了安全,在公司中,有些欄位為保密欄位,那么可以創建視圖,限制對某些欄位的操作,
- 創建視圖
- create view 視圖名 as (查詢 sql 陳述句)
# 創建視圖 v_stu mysql> create view v_stu as (select id,name,age from t_student);
- 顯示目前有哪些視圖
- show tables;
- 注意點創建的視圖都在表的最下面
# 查看創建的視圖 v_stu mysql> show tables; +-------------------+ | Tables_in_student | +-------------------+ | t_course | | t_student | | v_stu | +-------------------+ mysql> select * from v_stu; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | zhangsan | 18 | | 2 | wangwu | 20 | | 3 | zhaoliu | 19 | | 4 | lisi | 22 | +----+----------+------+
- 修改視圖
- alter view 視圖名 as (查詢陳述句)
mysql> alter view v_stu as (select id,name from t_student); mysql> select * from v_stu; +----+----------+ | id | name | +----+----------+ | 4 | lisi | | 2 | wangwu | | 1 | zhangsan | | 3 | zhaoliu | +----+----------+
- 洗掉視圖
- drop view 視圖名
# 洗掉視圖 v_cou mysql> drop view v_cou; mysql> show tables; +-------------------+ | Tables_in_student | +-------------------+ | t_course | | t_student | | v_stu | +-------------------+
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/58185.html
標籤:MySQL
