Remove the SQL language definition view

View and delete SQL language definition 2008-11-05 19:23:42 Read 90 Comments 0 Font Size: medium and small subscription.

         

           Views are derived from one or several base table (or view) table. It is different from the basic table is a virtual table. Database view definition stored only, without storing the data corresponding to the view, which is still stored in the original data base table. So the data base table changes, the query from the view out of the data will change. In this sense, the view is like a window, which can be seen through the data in the database of interest and change.

            View, once defined, it can be queried and the same basic table is deleted. You may then define a new view on the view, but there are certain restrictions on the view change (add, delete, change) operations.

First, the establishment view (create view)

     using the SQL language in the following format:

                        the Create View View name [(column name, column name, ....)]

                         as sub-queries

                        [with check option];

        Note: 1, sub-queries (that is, select ... from ... where ...) can be arbitrarily complex select statements, but are usually not allowed to

                        contain an order by clause and distinct phrases.

                   2, "with check option" expressed a view is update, insert, delete operation to ensure that when the update, insert, or delete

                         rows that satisfy the predicate condition (ie conditional expression sub-query) view definition. If this requirement is not, you can not write,

                        "with the Check the Option" statement.

                    3, attributes or all of the view omitted or column name specified all, it can not be specified portion.

                    4, sometimes you can not write the name of each column, but all must explicitly specify column names in the view of the following three conditions:

                           (1) a target column is not simply attribute name, but the aggregate function or column expression ;

                           (2) multi-table a few selected fields with the same name as the view of the connection;

                           (3) the need to enable new and more appropriate name for a column in the view.

                      5, with a necessary part of the format "[]" of the content, not the format, as long as you can write when needed.

              [Example] establishment information a student's view, and when the required modifications and still ensure that students insert this view only information system.

                 View IS-Student the Create

                 AS

                 SnO SELECT, sname, SAGE

                 from Student

                WHERE = sdept 'the IS'

                whth Check Option; implemented in view of the modification, insertion, still meet a subquery deletion

                                                                           condition expression



concept definition: a subset of the ranks of view: If a is derived from a single table, and just get rid of some of the basic table rows and some columns, but retains the main yard, saying such a view is ~

virtual column: some of the columns in the view of not query directly from the basic table data, but the data base table calculated, the data was not real in the table.

View with an expression: view with columns, also known as virtual ~

[Example two] establish an information system No. 1 elective course students of view.

the IS-View the SI Create (SnO, sname, Grade)

AS

SELECT student.sno, sname, Grade

the From Student, SC

WHERE sdept = 'the IS' and student.sno = cs.sno and sc.cno = '. 1';

Note: Because the two tables are related to the student number sno, so when you want to select it clear that to pick which one sno, so in front of sno to add the table name student.sno, to avoid confusion.

In the where clause condition, this condition should add student.Sno = cs.Sno, the two tables together.

Must meet to write the second case view of each column name: multi-table a few selected fields with the same name as the view of the connection.

[Example Three] The reaction student birth year view

Create View the BT-S (SnO, sname, sbirth)

AS

SELECT SnO, sname, 2008-SAGE        

from Student;

NOTE: The rows in the view may be data obtained from the basic table through computing come, select statement can be an expression.

  At the same time this case also meets the basic format of the first case in the view definition must be given in view of the column name: the target column (referring to a select statement in the column) is not a simple property name, but the column expression.

[Four] cases the number of students learning and his grade point average is defined as a view.

create view SG (sno, gavg) satisfies a first format where the view must write the column name: target list is a function of aggregate

AS

SELECT SnO, the AVG (Grade) target list is a function of aggregate

from sc

group by sno; obtained according to the results of learning packet number, i.e., the same number of learning together, achieve a student not

                                                             different course grade together, and then write a student performance

[Examples] The five student table all girls record is defined as a view.

View F-student the Create (F-SnO, name, Sex, Age, the dept)

AS

the SELECT * * represents the properties view F-student to be established properties listed with studet table columns

                                                                                correspond, so do not have to be listed , * on behalf of all.

Student from

WHERE SSEx = 'M';

 

two, delete a view (drop view)

format is as follows: drop view cascade view name] [

Note: cascade (concatenation), represents together with the view to be deleted and all views derived from it. If you do not add cascade, it means that if the view is related to the other view, refused to perform the deletion.

Plus the "[]" means: not a necessary part of the format, when you need to write.

Third, view and update and query operations on the same basic table

Reproduced in: https: //my.oschina.net/dminter/blog/205030

Guess you like

Origin blog.csdn.net/weixin_34179968/article/details/91888810