[Original] mysql view_mysql

Source: Internet
Author: User
[Original] mysql view bitsCN.com
The format is not correct because it has not been modified.
A view is a table imported from one or more tables. it is a virtual table. a view is like a window, through which you can see the data provided by the system. The view allows you to perform operations conveniently and ensures database system security. Content: Role of a view Create View View Modify view Update view Delete ViewView overview view can be exported from an existing table or an existing View. the database only saves the View definition but does not store view data. View data is stored in the original table. The view data depends on the original table. once the original table changes, the view data also changes accordingly. If you need to frequently query the data of fields in multiple tables, you can create a view on these tables. MySQL views do not support the input parameter function. View function: it can act as a filter. information that is useless to users or that users do not have the permission to understand can be directly blocked. this can be summarized as: simplifying operations and increasing data security, improve the logical independence of tables. create a view. mysql creates a view through create view. syntax: Create[Algorithm = {undifined | merge | temptable}] ViewView_name [(properties)] As Select*[ With[ CASCADED| LOCAL] Check Option]; Resolution: algorithm is an optional parameter, indicating the algorithm selected for the view, undefined indicates that mysql automatically selects the algorithm, and merge indicates that view statements and view definitions are merged, make the definition part of the view replace the corresponding part of the statement. temptable indicates that the view structure is saved to the temporary table, and then execute the statement using the temporary table. View_name indicates the name of the view to be created. Properties is an optional parameter that specifies the nouns of each attribute in the view. by default, properties are the same as those queried in the select statement. Select is a complete query statement that queries certain records that meet the conditions from a table and imports them into the view. With check option is an optional parameter, indicating that the update view must be within the permission range of the view. It is best to use the with cascaded check option parameter for permission query: Permission information is stored in the user table of the mysql database and queried using the following statement: SelectSelect_priv, create_view_priv FromMysql. UserWhrer User= 'Username'; 'username' indicates the username of the database user. Query result: mysql> SelectSelect_priv, create_view_priv FromMysql. User Where User= 'Root ';
+ ------------- + ------------------ +
| Select_priv | create_view_priv |
+ ------------- + ------------------ +
| Y |
| Y |
+ ------------- + ------------------ +
2 Rows In Set(0.09 sec) Create a view in a single table:Mysql> use login;
DatabaseChanged
Mysql> Select* FromLogin;
+ ----- + ---------- + ---------------------------------- +
| Uid | username | Password|
+ ----- + ---------- + ---------------------------------- +
| 1 | test | 098f6bcd4621d373cade4e832627b4f6 |
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | User| Ee11cbb1_52e40b07aac0ca060c23ee |
| 5 | testk | f2fc2720249d97db37e2a5a3330baa4e |
+ ----- + ---------- + ---------------------------------- +
5 Rows In Set(0.05 sec)

Mysql> Create ViewLogin_view As Select* FromLogin WhereUid> = 2 AndUid <= 4;
Query OK, 0 RowsAffected( 0.12 sec)

Mysql> DescLogin_view;
+ ---------- + ------------- + ------ + ----- + --------- + ------- +
| Field | Type| Null| Key| Default| Extra |
+ ---------- + ------------- + ------ + ----- + --------- + ------- +
| Uid | Int(11) | NO| 0 |
| Username | Varchar(32) | NO| NULL|
| Password| Varchar(32) | NO| NULL|
+ ---------- + ------------- + ------ + ----- + --------- + ------- +
3 Rows In Set(0.07 sec)

Mysql> Select* FromLogin_view;
+ ----- + ---------- + ---------------------------------- +
| Uid | username | Password|
+ ----- + ---------- + ---------------------------------- +
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | User| Ee11cbb1_52e40b07aac0ca060c23ee |
+ ----- + ---------- + ---------------------------------- +
3 Rows In Set(0.04 sec) Create a view on multiple tables Creating a view in multiple tables is similar to a single table operation. you only need to use a multi-table query in the select statement.View: use desc Statement 2: show table status to query basic view information. Show table status like 'View _ name '; Mysql> Show TableStatus Like'Login _ view '/ G;
* *************************** 1. Row***************************
Name: Login_view
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW This statement is used to view table information: Mysql> Show TableStatus Like'Login '/ G;
* *************************** 1. Row***************************
Name: Login
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 5
Avg_row_length: 3276
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 7340032
Auto_increment: 6
Create_time: 18:28:57
Update_time: NULL
Check_time: NULL
Collation: Utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 Row In Set(0.01 sec) From the two query structures above, we can intuitively see the differences between views and tables. Although show table status allows you to view the basic information of a view, it is not recommended because it provides too little information. 3. view information using the show create view statement. Mysql> Show Create ViewLogin_view/ G;
* *************************** 1. Row***************************
View: Login_view
Create View: CREATEALGORITHM = UNDEFINED DEFINER= 'Root' @ 'localhost' SQL SECURITY DEFINERVIE
W'login _ view' AS Select'Login'. 'uid' AS'Uid', 'login'. 'username' AS'Username', 'login '.' Password`
AS' Password' From'Login' Where('Login'. 'uid'> = 2) And('Login'. 'uid' <= 4 ))
Character_set_client: gbk
Collation_connection: gbk_chinese_ci
1 Row In Set(0.00 sec) 4: view details in the view table. Select* FromInformation_schema.views/ GInformation_schema is a mysql database. The query result is as follows: mysql> Select* FromInformation_schema.views/ G;
* *************************** 1. Row***************************
TABLE_CATALOG: def
TABLE_SCHEMA: login
TABLE_NAME: Login_view
VIEW_DEFINITION: Select'Login'. 'login'. 'uid' AS'Uid', 'login'. 'login'. 'username' AS'Username'
, 'Login'. 'login '.' Password' AS' Password' From'Login'. 'login' Where('Login'. 'login'. 'uid'> = 2)
Nd ('login'. 'login'. 'uid' <= 4 ))
CHECK_OPTION: NONE
IS_UPDATABLE: YES
DEFINER: Root @ localhost
SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: gbk
COLLATION_CONNECTION: gbk_chinese_ci
1 Row In Set(0.10 sec) modify the create or replace view statement and alter Statement. 1. create or replace view syntax: Create OrReplace [algorithm = {undefined | merge | temptable}] ViewView_name [(propertise)] AsSelecte *[ With[ Cascaded| Local] Check Option]; A bit like create view. create or replace creates a view if the view does not exist. if the view already exists, modify the view. 2. alter Statement Alter[Algorithm = {undefined | merge | temptable}] ViewView_name [(propertise)] AsSelecte *[ With[ Cascaded| Local] Check Option]; The parameter is the same as create view. Update view updates. do not write them as updata. Syntax: UpdateView_name SetProperty_1 = a [, property_2 = B ..]; note: The update will modify the source table, not just the view. because the view is not stored, the actual operation is for the source table. Primary key fields cannot be updated. Update modifies all data in the view. View update unavailable
  1. The View contains functions such as sum (), count (), max (), and min.
  2. A view contains keywords such as union, union all, distinct, group by, and having.
  3. Constant view
  4. The select statement for creating a view contains the select clause.
  5. Views exported from non-updatable views. // View can be exported.
  6. Create a view with the algorithm parameter temptable.
  7. The table corresponding to the view does not have a column with the default value, and the column is no longer in the view.
  8. In addition, with check option may determine whether the view sequence is updated.
When deleting a view, only the view definition is deleted, but the data in the View Source table is not deleted. Syntax: Drop View[If exits] view_name_list [ Restrict| Cascad]; resolution: if exits determines whether a view exists. if a view exits exists, the view is deleted. if it does not exist, the view is not executed. Multiple views can be deleted at a time. Mysql> Drop ViewIf exits login_view;
ERROR 1064 (42000): You have an error InYour SQLSyntax; CheckThe manual that corresponds ToYour
MySQL server Version ForThe RightSyntax ToUse near 'exits login_view' AtLine 1
Mysql> Drop ViewIf ExistsLogin_view;
Query OK, 0 RowsAffected( 0.00 sec)

Mysql> DescLogin_view;
ERROR 1146 (42S02 ): Table'Login. login_view 'doesn' t exist permission is required to delete a view. Command to check whether the deletion permission is available: SelectDrop_priv FromMysql. User Where User= 'Username'; the result is: mysql> SelectDrop_priv FromMysql. User Where User= 'Root ';
+ ----------- +
| Drop_priv |
+ ----------- +
| Y |
| Y |
+ ----------- +
2 Rows In Set(0.00 sec) <完>BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.