To create database views, use the Construct VIEW statement. Views can be generated from a single table, several tables, or another view.
A user must have the appropriate system privileges to build a view, depending on the implementation.
The following is the fundamental syntax for CREATE VIEW:
ADD A VIEW WITH THE NAME view name to your project. AS SELECT column1, column2,..... FROM table name WHERE [condition]; AS SELECT column1, column2,..... FROM table name WHERE [condition]; AS SELECT column1, column2,..... FROM table name WHERE [condition]; AS SELECT column1, column2,..... FROM table name WHERE [condition]; AS SELECT column1, column2,..... FROM table name WHERE [condition]; AS
In the same way that many tables can be included in a typical SQL SELECT query, you can consist of multiple tables in your SELECT statement.
Consider the following CUSTOMERS table records:
The following example explains how to make a view from the CUSTOMERS table. The client name and age from the CUSTOMERS table would be pulled into this view.
CREATE VIEW CUSTOMERS VIEW IN SQL AS SELECT name, age FROM CUSTOMERS; Now you may query CUSTOMERS VIEW as a table. Here's an illustration of what I'm referring to.
SQL > SELECT * FROM CUSTOMERS VIEW yields the following result.
mysql> select cus_name, salary from customers;
A checkbox option is WITH CHECK OPTION.
WITH CHECK OPTION is a CREATE VIEW statement option. The WITH CHECK OPTION assures that all UPDATE and INSERT actions meet the condition set forth in the view definition (s).
If the UPDATE or INSERT commands do not satisfy the requirement, they will fail (s).
The following code block demonstrates how to create the same view CUSTOMERS VIEW using the WITH CHECK OPTION.
TO CREATE A VIEW, SELECT THE NAME AND AGE OF CUSTOMERS. THE OPINION OF THE CUSTOMERS
WHERE age IS NOT NULL WITH CHECK OPTION; Because the view is defined by data with no NULL values in the AGE column, the WITH CHECK OPTION should prevent any NULL values from being inserted in the AGE column in this case.
You can edit a view if it fits all of the aforementioned conditions. The code block below demonstrates how to update Ramesh's age.
UPDATE CUSTOMERS IN SQL This will update both the underlying table CUSTOMERS and the view itself. set salary =salary+100; Now try querying the basic table with the SELECT statement, and you'll receive the following result.
mysql> update customers set salary =salary+100;
A View's Rows Rows in a View Inserting Rows into a View
Rows of data can be inserted into a display. The INSERT command follows the same set of rules as the UPDATE command.
Because the CUSTOMERS VIEW lacks all of the NOT NULL attributes, we can't insert rows; otherwise, you can insert rows in a view just like you can in a table.
The rows of data in a view can be deleted. The UPDATE and INSERT commands use the same logic as the DELETE command.
The code below shows how to delete a record with the AGE value of 22.
mysql> delete from customers where cus_id=321; This will remove a record from the base table CUSTOMERS, and the view will reflect this. Now try querying the basic table with the SELECT statement, and you'll receive the following result.
Wherever there is a view, there must be a way to remove it if it is no longer needed. DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP VIEW view name, DROP
How to delete the CUSTOMERS VIEW from the CUSTOMERS database is demonstrated in the example below.
|