You are on page 1of 4

SQL Views

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.

A view is created with the CREATE VIEW statement.

CREATE VIEW SYNTAX


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

Creating View from a single table:


In this example we will create a View named DetailsView from the table Stu-
dentDetails.

Query:
CREATE VIEW bookV (ID,TITLE,PUBLISHER,YEAR, PRICE)
AS SELECT ID, TITLE, PUBLISHER, YEAR, PRICE
FROM BOOK
WHERE YEAR > 1995;

To see the data in the View :

SELECT * FROM DetailsView;

Creating View from multiple tables:

To create a View from multiple tables we can simply include multiple tables in
the SELECT statement. Query:

CREATE VIEW MarksView AS


SELECT StudentDetails.NAME, StudentDetails.ADDRESS, Student-
Marks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
To display data of View Marks View:

SELECT * FROM MarksView;

DATA QUERY AND MANIPULATION OPERATIONS


WITH VIEWS
CREATE VIEW bookV (ID,TITLE,PUBLISHER,YEAR, PRICE)
AS SELECT ID, TITLE, PUBLISHER, YEAR, PRICE
FROM BOOK
WHERE YEAR > 1995;

1. Issuing the following SQL statements

Query:
SELECT *
fROM bookV
WHERE PUBLISHER = “McGraw-hill";

2. Update Operation

Query:
UPDATE bookV
SET PRICE = PRICE * 1.12
WHERE PUBLISHER = “McGraw-hill”;

3. Insert Operation

Query:
INSERT INTO bookV(ID,TITLE,PUBLISHER,YEAR,PRICE)
VALUES ("Co2", "Countdown 2000", "TMH", 1997, 295);

4. Delete statement

Query:
DELETE FROM bookV
WHERE PUBLISHER = “McGraw-Hill";

5. The Check Option


Consider the following UPDATE operation on bookV :
Query:
UPDATE bookV
SET YEAR = 1984
WHERE TITLE = "Abduction";

You might also like