Sunday, August 26, 2012

Views


Views
- a VIEW is a virtual relation based on the result-set of a SELECT statement.
- it has a column and rows that comes from the real tables.

Create Table
Command:
            CREATE VIEW view_name AS SELECT column_name FROM table_name;
Example:
            CREATE VIEW sample AS SELECT first_name FROM student;

            You can select a multiple column in view and from different table. All the select query can use in view. Also, you can use the WHERE clause.

Example:
            CREATE VIEW sample AS SELECT first_name, section_name
            FROM student, section
            WHERE student.id=section.stud_id;

            You can use the concatenation to place a value of multiple columns to one column in views.

Example:
            CREATE VIEW sample AS SELECT concat(first_name,min_name,last_name)
            FROM student;

Insert value into Views
            You can insert a value/data into a view table. Just like the insertion in the typical table in mysql.

Command:
            INSERT INTO sample
VALUES (‘sheryll’, ‘BSIT’);

Drop View
You can also drop a view table. If you drop the view table, it does not affect the original table, but if you drop the original table that the column in the view comes from, the view table can be deleted.

Command:
            DROP VIEW student;

No comments:

Post a Comment