文章目錄
表索引創建
在已存在的表示創建索引
# 常規索引
MariaDB [my_test]> create index ename_index on emp(ename);
# 唯一索引
create unique index deptno_index on emp(deptno);
# 全文索引:
create fulltext index job_index on emp(job);
# 多列索引:
create index comm_index on emp(dept_name,comment)
MariaDB [my_test]> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| job | varchar(20) | YES | | NULL | |
| mgp | int(11) | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | NO | MUL | NULL | |
+----------+---------------+------+-----+---------+-------+
8 rows in set (0.04 sec)
MariaDB [my_test]> create index ename_index on emp(ename);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [my_test]> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | varchar(20) | NO | MUL | NULL | |
| job | varchar(20) | YES | | NULL | |
| mgp | int(11) | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | NO | MUL | NULL | |
+----------+---------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
MariaDB [my_test]>
alter table 在已存在的表上創建索引
語法:
alter table 表名 add [unique | fulltext | spatial ] index 索引名稱 (字段名[(長度)] [asc | desc]);
# 創建唯一索引示例:
alter table emp add unique index deptno_index(deptno);
管理索引
- 查看索引:show create table 表名\G;
索引刪除
- drop index 索引名 on 表名;
MariaDB [my_test]> drop index ename_index on emp;
MariaDB [my_test]> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | varchar(20) | NO | MUL | NULL | |
| job | varchar(20) | YES | | NULL | |
| mgp | int(11) | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | NO | MUL | NULL | |
+----------+---------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
MariaDB [my_test]> drop index ename_index on emp;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [my_test]> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| job | varchar(20) | YES | | NULL | |
| mgp | int(11) | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | NO | MUL | NULL | |
+----------+---------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
MariaDB [my_test]>
有索引時導入海量數據非常耗時間,這時候可以將所有刪除,數據導入成功後在創建索引
創建視圖
創建視圖
-
視圖是一個虛表,是儲存在數據庫中的SQL查詢語句,它主要處於兩種原因:安全原因,他可以隱藏一部分數據;第二個原因是是複雜的查詢已於理解和使用。
-
語法:create view 視圖名 as select語句
# 創建視圖:emp_base
MariaDB [my_test]> create view emp_base as select empno,deptno,ename,job from emp;
Query OK, 0 rows affected (0.06 sec)
MariaDB [my_test]> select * from emp_base;
+-------+--------+--------+----------+
| empno | deptno | ename | job |
+-------+--------+--------+----------+
| 7369 | 20 | SMITH | CLERK |
| 7499 | 30 | ALLEN | SALESMAN |
| 7521 | 30 | WARD | SALESMAN |
| 7566 | 20 | JOENS | MANAGER |
| 7654 | 30 | MARTIN | SALESMAN |
| 7698 | 30 | BLAKE | MANAGER |
| 7782 | 10 | CLARK | MANAGER |
| 7788 | 20 | SCOTT | ANALYST |
| 7844 | 30 | TURNER | SALESMAN |
| 7876 | 20 | ADAMS | CLERK |
| 7900 | 30 | JAMES | CLERK |
| 7902 | 20 | FORD | ANALYST |
| 7934 | 10 | MILLER | CLERK |
+-------+--------+--------+----------+
13 rows in set (0.00 sec)
MariaDB [my_test]>
當視圖不需要時,可以將其刪除
- 刪除視圖,必須擁有DROP權限
- 語法格式
DROP VIEW [IF EXISTS]
View_name [,view_name1]……
[RESTRICT | CASCADE]
MariaDB [my_test]> drop view if exists uu;
Query OK, 0 rows affected (0.00 sec)
MariaDB [my_test]> select * from uu;
ERROR 1146 (42S02): Table 'my_test.uu' doesn't exist
MariaDB [my_test]>
修改表
# 修改表名
alter table emp_tatal rename to emp_total(total int(4));
# 修改列名
alter table emp_total change tatal total int(4);