Database indexes and views

Data definition functions include:

  • Table definition
  • Index definition
  • View definition

index

      1. Index creation and deletion

The format of the statement to create the index is as follows:

CREATE [UNIQUE] INDEX index name 

       ON table name (column name [, column name] ……)

[Example 2-49] The emp_c table is indexed by the employee's name (ename), the index name is emp_ename_idx.

CREATE INDEX emp_ename_idx ON emp_c(ename);

[Example 2-50] The emp_c table is indexed by job and salary, and the index name is emp_job_sal_idx .

CREATE INDEX emp_job_sal_idx ON emp_c(job,sal);

 Second, view the index

         SHOW  INDEX  FROM  <表名>;

        [Example 2-51] View the index information of the table emp_c.

SHOW INDEX FROM emp_c;

       Third, delete the index

         DROP INDEX index name ON table name;

       [Example 2-52] Delete the established index emp_ename_idx in the emp_c table

DROP INDEX emp_ename_idx ON emp_c;

Note: Set the index, the system search time will be shorter ;

test:

SET profiling=1;#设置系统监控
SELECT * FROM emp_c WHERE empno=1002;
SHOW PROFILES;#查看监控信息
CREATE INDEX emp_idx ON emp_c(empno);
SELECT * FROM emp_c WHERE empno=1002;
SHOW PROFILES;#查看建立索引后的监控信息

view

           First, the reason for establishing the view

  • Provide various data representation forms to hide the logical complexity of data and simplify query statements
  • Provide certain security guarantees, simplify the management of user rights, and reconstruct the database
  • Provides some logical independence

          Second, create a view

     The statement format is as follows:

     CREATE [OR REPLACE] VIEW <view name> [(<alias> [, <alias>]…)]

     AS

    <SELECT statement>

    [WITH  CHECK  OPTION  ]

          [Example 2-53] Create a view with WITH CHECK OPTION option.  

CREATE  VIEW  v_dept_chk
AS
SELECT empno,ename,job,deptno FROM  emp
WHERE  deptno=10
WITH  CHECK  OPTION;

    Third, modify the view

     ALTER VIEW view name [(alias [, alias]…)]

             AS

             SELECT statement

             [WITH  CHECK  OPTION];

             Or C REATE [OR REPLACE] VIEW (syntax same as creation)

           

CREATE OR REPLACE VIEW vdk AS
SELECT empno,ename,job,deptno FROM emp
WHERE deptno=10
WITH CHECK OPTION;
SELECT * FROM vdk;

ALTER VIEW vdk AS
SELECT empno,ename,job FROM emp
WHERE deptno=10
WITH CHECK OPTION;
SELECT * FROM vdk;

      Fourth, delete the view         

        DROP VIEW view name [, view name,…];

       [Example 2-56] Delete the created view v_dept_chk.

DROP VIEW v_dept_chk;

      Five, the use of views, the same as TABLE

                 Known: existing view vdk

SELECT * FROM vdk;
SHOW TABLES;
DESC vdk;//查看表的结构

Note: The physical memory referencing the table is a virtual table;

 

 

 

 

 

 

 

 

 

 

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/105572122