MySQL CREATE VIEW PHP

MySQL CREATE VIEW  

MySQL CREATE VIEW

MySQL CREATE VIEW

In SQL, a view is a virtual table based on the result-set of an SQL statement.

A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table.

CREATE VIEW Syntax

CREATE VIEW view_name AS
SELECT column1column2, ...
FROM table_name
WHERE condition;

Note: A view always shows up-to-date data! The database engine recreates the data, using the view's SQL statement, every time a user queries a view.

 

SQL CREATE VIEW Examples

Syntax of CREATE VIEW

CREATE VIEW view_name AS
SELECT column1column2, ...
FROM table_name
WHERE condition;

Syntax of Create View and with JOIN CONDITION

Syntax of CREATE VIEW

CREATE VIEW view_name AS
SELECT column1column2, ...
FROM table_name
JOIN table_name ON condition
WHERE condition;

The following SQL creates a view that shows all customers from the USA:

Example

CREATE VIEW customer_of_usa AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "USA";

We can query the view above as follows:

Example

SELECT * FROM customer_of_usa;

 

SQL CREATE OR REPLACE VIEW

SQL CREATE OR REPLACE VIEW Syntax:

CREATE OR REPLACE VIEW view_name AS
SELECT column1column2, ...
FROM table_name
WHERE condition;

The following SQL adds the "City" column to the "customers_of_usa" view:

Example

CREATE OR REPLACE VIEW customers_of_usa AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = "USA";

SQL DROP VIEW

 SQL DROP VIEW Syntax

DROP VIEW view_name;

 

The following SQL drops the "customers_of_usa" view:

Example

DROP VIEW customers_of_usa;

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries