1、索引
# 給 name 字段創(chuàng)建索引 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;
# 給 name 字段創(chuàng)建唯一索引 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 | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
# 給 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、視圖
# 創(chuàng)建視圖 v_stu mysql> create view v_stu as (select id,name,age from t_student);
# 查看創(chuàng)建的視圖 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 | +----+----------+------+
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 | +----+----------+
# 刪除視圖 v_cou mysql> drop view v_cou; mysql> show tables; +-------------------+ | Tables_in_student | +-------------------+ | t_course | | t_student | | v_stu | +-------------------+
聯(lián)系客服