View (Profile, create, view, modify, update, delete)

I. INTRODUCTION view

Or is a view from the guide table out of a plurality of tables, a table of virtual presence. The view is like a window, you can see the data system provided exclusively through the window. The user can not see the data in the database, but only care about their own useful data. It allows the user to view the operation more convenient, but also can protect the security of the database system.

1 , meaning views

A view is a virtual table, a table can be defined from one or more database tables guided by the table, the view from the base view of the already existing. Database store only the definition of the view, and are not stored in the data view. The data stored in the original table. When a query using the view data corresponding to the database system data is extracted from the original table. Thus, view data is dependent on the original table. Once the data in the table is changed, the data displayed in the view may also change.

2 , the effect of view

A view is a virtual table based on the original table or view on redefined, which the user can select useful information from the original table. View plays a role similar to the screening.

2.1 using simple operation, the view object is seen to be achieved that is desired.

2.2 increase data security, through the view, users can query and modify specified data.

2.3 improve the independence of the logic table, the impact of view can shield the original table structure changes.

 

Second, create a view

 

Create a view refers to a view on the establishment of an existing database. Views can be built on a table or multiple tables.

 

1 , create a view of grammatical forms

 

MySQL , create a view through SQL statements CREATE VIEW achieve. The basic syntax is as follows:

 

CREATE  [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]

 

  VIEW   view name   [( property list )]

 

  AS SELECT   statement

 

  [WITH  [CASCADED | LOCAL]  CHECK  OPTION];

 

Wherein, the ALGORITHM is an optional parameter represents the view selection algorithm; view name parameter represents the name of the view created; list of attributes is an optional parameter, which specifies the view in terms of each attribute, by default the SELECT statement query the same property; the SELECT statement is a complete parameter query, expressed identify certain records to meet the conditions from a table, these records into view; the WITH CHECK OPTION is optional, pledged to ensure that when you update the view within the purview of the view.

 

ALGORITHM   includes 3 options   UNDEFINED , MERGE and the TempTable . Wherein, UNDEFINED option represents MySQL automatic selection algorithm to be used; the MERGE option means defined using the view statement is a view of combined, such that a partial view of the definition of substituents corresponding portion of the statement; TempTable option shows a result of the view is stored temporary table, and then execute the statement using temporary tables.

 

CASCADED is an optional parameter update condition represents all views and tables to view satisfied, the default parameters; the LOCAL indicates when the view is updated to the condition itself to the view definition.

 

CREATE VIEW create a view, plus the best WITH CHECK OPTION parameter. And the best plus CASCADED parameters. In this way, the view derived from the new view, a new view updates need to consider the constraints of its parent view. It's more stringent, we can guarantee the security of data.

 

When you create a view, you need to have CREATE VIEW privilege. At the same time, it should have involved the query column SELECT privileges. SELECT statement queries as follows:

 

SELECT   Select_priv,Create_view_priv  FROM  mysql.user  WHERE  user='用户名'

 

Which, Select_priv attribute indicates whether the user has the SELECT privilege, the Y- representation has SELECT permissions, N means no; , Create_view_priv attribute indicates whether the user has the CREATE VIEW privilege; the mysql.user represents MySQL database following user table; parameter indicates the user name to be queried that the user has permission previously cited, this parameter causes enclosed in single quotes.

 

1.1 to create a view on a single table

 

mysql> CREATE VIEW index2_view AS SELECT * FROM index2; in index2 create a name for the table index2_view view.

 

mysql> CREATE VIEW index1_view (v_id, name, sex) AS SELECT id, name, sex FROM index1; at index1 created table index1_view view.

 

mysql> desc index1_view; see just index1_view view.

 

1.2 to create a view on multiple tables

 

mysql> CREATE ALGORITHM=MERGE VIEW

 

    -> worker_view(name,department,sex,age,address)

 

    -> AS SELECT name,department.d_name,sex,2009-brithday,address

 

    -> FROM worker,department WHERE worker.d_id= department.d_id

 

    -> WITH LOCAL CHECK OPTION;

 

Third, view view

View view is a defined view of view the database already exists. See the view must have the SHOW VIEW privileges, MySQL in the database user table holds the information. See the view of the method include DESCRIBE statement, SHOW TABLE the STATUS statement, SHOW the CREATE VIEW statements and queries information_schema in the database views lists.

1 , DESCRIBE statement to see the view basic information

Because the view is also a table. But this table is rather special, is a virtual table. Also you can use DESCRIBE statement to see the view. The basic syntax is as follows:

DESCRIBE   view name ; where the view name   argument refers to the name of the view you want to view.

DESC worker_view; see worker_view view basic information.

2 , SHOW TABLE the STATUS   statement to see the view basic information

The basic syntax:  SHOW TABLE the STATUS the LIKE ' view '   ; wherein, the LIKE   represents a string matching the rear; name of the view parameter refers to the name of the view to view, need be enclosed in single quotes.

mysql> SHOW TABLE STATUS LIKE 'index1_view ' \ G See view index1_view , plus \ G format view neater.

3 , SHOW the CREATE VIEW statement to view View Details

The basic syntax: SHOW the CREATE VIEW   view name

mysql> SHOW CREATE VIEW index2_view1; this view shows the data bit messy View

mysql> SHOW CREATE VIEW index2_view1 \ G See view information displayed neatly

4 , the views See table view details

In MySQL , all defined views are present information_schema the database views table. Query views , you can check the detailed information in the database of all views.

The basic syntax: SELEC * the FROM INFORMATION_SCHEMA.VIEWS;

mysql> SELECT * FROM information_schema.views \G

 

Fourth, modify the view

Modifying means to modify the view definition of an existing database table. When some of the fields of the base table changes, consistency can be maintained between the base table and the view by modifying the view.

. 1 , the CREATE OR the REPLACE the VIEW   statements modify the view

The basic syntax (all arguments are the parameters to create the view is the same) as follows:

CREATE  OR  REPLACE  [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]

VIE   view name   [( property list )]

AS SELECT   statement

[WITH  [CASCADED | LOCAL ]  CHECK  OPTION];

Modify index1_view view, you can use the modified DESC view performance

mysql> CREATE OR REPLACE ALGORITHM=TEMPTABLE

    -> VIEW index1_view(v_name,v_sex)

    -> AS SELECT name,sex FROM index1;

2 , the ALTER statement to modify view

ALTER statements can modify the definition of the table to create the index, it can also be used to modify the view. The basic syntax ( all parameters related to creation parameter view is the same ) :

ALTER  [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]

VIEW   view name   [( property list )]

AS SELECT statement

[WITH  [CASCADED | LOCAL]  CHECK  OPTION];

mysql> ALTER VIEW department_view2(department,name,sex,location)

    -> AS SELECT d_name,worker.name,worker.sex,address

    -> FROM department,worker WHERE department.d_id=work.d_id

    -> WITH CHECK OPTION;

 

Fifth, update view

Update the view refers to the view through the insert (the INSERT) , update (the UPDATE) and delete (DELETE) data in the table. Because the view is a virtual table, where no data, through the view update, are the basic conversion table to be updated. When updating a view, you can only update data within their competence.

mysql> create databases test2; create a database test2

mysql> CREATE TABLE department(d_id INT PRIMARY KEY,

    -> d_name VARCHAR(20),

    -> function VARCHAR(100),

    -> address VARCHAR(50)

    ->); create a department table

mysql> INSERT INTO department(d_id,d_name,function,address) VALUES(1001,"ministry of personnel","Management personnel change","3 floor, tower "); 在表department中插入一行

mysql> INSERT INTO department (d_id, d_name, function, address) VALUES (1002, "ministry of personnel" ,, "In charge of production", "1 floor, tower 5"); in Table department reinserted row

mysql> SELECT * FROM department; View from the department of information

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

| d_id | d_name                | function                    | address          |

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

| 1001 | ministry of personnel | Management personnel change | 3 floor, tower   |

| 1002 | ministry of personnel | In charge of production     | 1 floor, tower 5 |

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

2 rows in set (0.02 sec)

mysql> CREATE VIEW department_view(name,function,address)

      -> AS SELECT d_name, function, address FROM department WHERE d_id = 1001; table department create views department_view

mysql> UPDATE department_view SET name='Scientific research',function='New product development',address='5 floor, tower 3'; 更新视图department_view

mysql> SELECT * FROM department;

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

| d_id | d_name                | function                | address          |

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

| 1001 | Scientific research   | New product development | 5 floor, tower 3 |

| 1002 | ministry of personnel | In charge of production | 1 floor, tower 5 |

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

2 rows in set (0.00 sec)

As can be seen from the above, the last update of the view are implemented in the base table. Update view the update is actually recorded on the base tables. However, not all views can be updated, the following situation can not update the view:

 View contains the SUM () , COUNT () , MAX () , and MIN () like function;

 View contains the UNION , the UNION ALL , the DISTINCT , the GROUP BY and HAVIG like keywords;

 Constants view;

 View SELECT contains a subquery;

 Derived from the non-updated view of the view;

 When you create a view, the ALGORITHM is TEMPTABLE type;

 No default value is present on the column corresponding to the view of the table, and it is not contained in the view;

 WITH [CASCADED | LOCAL] CHECK OPTION will also determine whether view updates. LOCAL parameter indicates when to update the view to meet the conditions may define the view itself; CASCADED parameter indicates the condition of all views and tables to update the view satisfied. When not specified, the default is CASCADED .

 

Sixth, Delete View

Delete to delete the view refers to a view that already exists in the database. When you delete a view, the view can only delete the definition, does not delete data. MySQL , use the DROP VIEW statement to remove the view. But the user must have DROP permission. The basic syntax is as follows:

DROP VIEW [IF EXISTS]   view name list   [RESTRICT | CASCADE]

Wherein, the IF EXISTS   parameter determination means present view, if there is performed, there is not performed; name list view parameter indicates the name list view to be deleted, between the respective view names separated by commas.

mysql> DROP VIEW IF EXISTS worker_view1, worker_view2; delete view worker_view1 and worker_view2

mysql> SELECT Drop_priv FROM mysql.user WHERE user = 'root'; see root user Drop_priv permissions

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

| Drop_priv |

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

| Y |

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

1 row in set (0.00 sec)

 

Guess you like

Origin www.cnblogs.com/uphold/p/11221985.html