1. When you modify, query, or delete a record, the system will prompt how many records are affected, but no table creation prompt will be displayed. When the table data is modified, it is reflected in the view. The command for modifying, querying, and deleting a view is the same as that for creating a table. A view is a table exported from one or more tables (or views. View is the point of view for database users. You can define the data structure required by the physical database based on their different needs. The data structure defined by the user is the view. The view and Table (sometimes different from the view, also known as the basic Table-Base Table) are different. The view is a virtual Table, that is, the data corresponding to the view is not actually stored, the database only stores view definitions. When operating on View data, the system operates the basic table associated with the view according to the view definition. Once a view is defined, it can be queried, modified, deleted, and updated like a table. 2. view has the following advantages: (1) centralized data for users, simplifying data query and processing. Sometimes the data you need is scattered in multiple tables, and the definition view can combine them to facilitate data query and processing. Www.2cto.com (2) shields the complexity of the database. You do not have to understand the table structure in a complex database, and changes to the database tables do not affect your use of the database. (3) simplify the management of user permissions. You only need to grant the user the permission to use the view, without having to specify that the user can only use specific columns of the table, which also increases the security. (4) Facilitate data sharing. Each user does not have to define and store the data they need, and can share the data in the database. In this way, the same data only needs to be stored once. (5) data can be reorganized to output to other applications. 3. use the create view statement to CREATE a view. Syntax: create [re replace] [algorithm = {undefined | merge | temptable}] view name [(column_list)] as select_statement [with [cascaded | local] check option] example: create view v_xs as select * from xs; (created under xsdb) | xsdb. xs (view of the table in xsdb created under the flying xsdb database) Description: ● column_list: to define a clear name for the column in the view, you can use the optional column_list clause, list the names of columns separated by commas. The number of names in column_list must be equal to the number of columns retrieved by the SELECT statement. Column_list can be omitted if the column name is the same as that in the source table or view. Www.2cto.com ● or replace: Given the or replace clause, the statement can replace the existing view with the same name. ● Algorithm clause: the Optional ALGORITHM clause is an extension of standard SQL MySQL. It specifies the MySQL algorithm and the ALGORITHM will affect the MySQL processing view. ALGORITHM can have three values: MERGE, TEMPTABLE, or UNDEFINED. Without the ALGORITHM clause, the default ALGORITHM is UNDEFINED (UNDEFINED ). If the MERGE option is specified, the text of the statements that reference the view is merged with the view definition, so that a part of the view definition replaces the corresponding part of the statement. The MERGE algorithm requires that the rows in the view and the rows in the base table have a one-to-one relationship. If the relationship does not exist, use a temporary table instead. If the TEMPTABLE option is specified, the view result is placed in a temporary table and then executed using it. ● Select_statement: SELECT statement used to create a view. Multiple tables or views can be queried in a SELECT statement. However, the SELECT statement has the following restrictions: (1) users who define a view must have the query permission on the referenced table or view (the SELECT statement can be executed); (2) subqueries in the from clause cannot be included; (3) system or user variables cannot be referenced; (4) preprocessing statement parameters cannot be referenced; (5) the table or view referenced in the definition must exist. (6) If the reference is not a table or view of the current database, add the database name before the table or view. (7) you can use order by in the view definition. However, if you select a specific view and the view uses a statement with its own order, order by in the view definition is ignored. (8) For other options or clauses in the SELECT statement, if the view also contains these options, the effect is not defined. For example, if the view definition contains the LIMIT clause and the SELECT statement uses its own LIMIT clause, MySQL does not define which LIMIT to use. ● With check option: indicates that all modifications made to the updatable view must comply WITH the constraints specified by select_statement. This ensures that after the data is modified, you can still view the modified data in the view. When a view is defined based on another view, with check option provides two parameters: LOCAL and CASCADED. They determine the scope of the inspection test. The Local keyword enables check option to only CHECK the defined view. cascaded checks all views. If no keyword is specified, the default value is CASCADED. 4. Note the following when using a view: (1) by default, a new view will be created in the current database. To explicitly create a view in a given database, you must specify the name db_name.view_name when creating the view. The name of the view www.2cto.com (2) must follow the naming rules of the flag. It cannot have the same name as the table and must be unique for each user view name, that is, for different users, different names are required for defining the same view. (3) Rules, default values, or triggers cannot be associated with views. (4) You cannot create any indexes on the view, including full-text indexes. 5. Example 1: assume that the current database is TEST, and create the CS_KC view on the XSCJ database, including the student ID of each computer Major, the course number and the score it chooses. Make sure that all modifications to this view meet the condition that the professional name is computer. Createor replace view xscj. CS_KC as select xs. student ID, course number, score FROMXSCJ. XS, XSCJ. XS_KC where xs. student ID = XS_KC. student id and xs. professional name = 'compute' with check option; Example 2: Find the student ID and average score of the students WITH an average score of over 80. In this example, the average score view XS_KC_AVG is created, including the student ID (num in the view) and average score (score_avg in the view ). Create an average score view (XS_KC_AVG) at www.2cto.com: CREATEVIEW XS_KC_AVG (num, score_avg) as select student ID, AVG (score) FROMXS_KC group by student ID, and query the view XS_KC_AVG. SELECT * FROM XS_KC_AVG WHEREscore_avg> = 80; FROM the two examples above, we can see that creating a view can hide complex table connections to the end user, simplifying the user's SQL program design. Note: If a new field is added to the basic table associated with the view, the view does not contain the new field. For example, the columns in the view CS_XS are associated with all columns in the XS table. If the XS table adds the "nationality" field, the data of the "nationality" field cannot be queried in the CS_XS view. If the table or view associated with the view is deleted, the view cannot be used any more. 6. To UPDATE basic table data through a updatable view, you must ensure that the view is updatable, that is, you can use them in INSET, UPDATE, or DELETE statements. For updatable views, the rows in the view and the rows in the base table must have a one-to-one relationship. There are some other specific structures that make the views unupdatable. If a view contains any of the following structures, it cannot be updated: www.2cto.com (1) aggregate function; (2) DISTINCT keyword; (3) group by clause; (4) order by clause; (5) HAVING clause; (6) UNION operator; (7) subquery in the selected list; (8) FROM clause contains multiple tables; (9) the SELECT statement references non-updatable views. (10) subqueries in the where clause reference the tables in the FROM clause. (11) the ALGORITHM option is set to TEMPTABLE (using a temporary table will always make the view unupdatable ). 7. INSERT data using the INSERT statement INSERT data into the base table through the View Example: Create the view CS_XS, the view contains information about computer students, and INSERT a record to the CS_XS view: ('123456 ', 'limu ', 'computer', 1, '2017-10-21 ', 50, NULL, NULL ). First, create a VIEW CS_XS: (this table is used for the deletion and modification below) createor replace view CS_XS www.2cto.com as select * from xs where professional name = 'compute' with check option; note: when creating a view, add the with check option clause because the with check option clause checks whether the new data meets the WHERE clause conditions in the view definition when updating data. The with checkoption clause can only be used WITH updatable views. Next, insert the record: INSERTINTO CS_XS VALUES ('201312', 'limum', 'put', 1, '2017-10-14 ', 50, NULL, NULL). Note: here, when inserting records, the professional name can only be "computer ". In this case, you can use the SELECT statement to query the CS_XS view and basic table XS. You can find that this record has been added to the XS table. When a view depends on multiple basic tables, data cannot be inserted into the view, because this will affect multiple basic tables. For example, you cannot insert data to the view CS_KC because CS_KC depends on two basic tables: XS and XS_KC. There is also a restriction on the INSERT statement: the SELECT statement must contain all columns that cannot be empty in the specified table in the FROM clause. For example, if the "name" field is not added when the CS_XS view is defined, an error occurs during data insertion. Www.2cto.com 8. Modify the data using the UPDATE statement. You can modify the data examples of the basic table through the view: increase the total credits of all students in the CS_XS view by 8. UPDATECS_XS SET total credits = total credits + 8; this statement actually adds 8 to the original total credits field value of all records in the basic table XS on which the CS_XS view depends. If a view depends on multiple basic tables, only the data of one basic table can be changed at a time. For example, change the 081101 course score of students whose CS_KC view is 101 to 90. UPDATECS_KC SET score = 90 WHERE student ID = '000000' AND course number = '000000'; in this example, the view CS_KC depends on two basic tables: xs and XS_KC, A modification to the CS_KC view can only change the student ID (derived from the XS table) or course number and score (derived from the XS_KC table ). The following changes are incorrect: UPDATECS_KC SET student ID = '000000', course number = '000000' www.2cto.com WHERE score = 90; 9. you can use the DELETE statement to DELETE data from a basic table through the view: DELETE records of female students in CS_XS. DELETEFROM CS_XS WHERE gender = 0; Note: you cannot use the DELETE statement for views that depend on multiple basic tables. For example, you cannot execute the delete statement on the CS_KC view to DELETE data from the related basic tables XS and XS_KC. 10. Use the ALTER statement to modify the definition of an existing view. Syntax format: ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] check option] syntax of the ALTERVIEW statement is similar to create view. For example, modify the CS_XS VIEW to only contain the student ID, name, and total credits of a Computer Major. USEXSCJ; ALTERVIEW CS_XS as select student ID, name, total credits from xs where professional name = 'compute'; 11. use an SQL statement to delete the view www.2cto.com syntax format: dropVIEW [IF EXISTS] view name 1 [, view name 2]... [RESTRICT | CASCADE] declares if exists. IF the view does not exist, no error message is displayed. You can also declare restrict and cascade, but they have no impact. You can use drop view to delete multiple views at a time. For example, drop view CS_KC and CS_XS will delete the views CS_KC and CS_XS. Author tianyazaiheruan