Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Lebanese French University

College of Engineering and Computer Science


Department of Information technology
Second Semester
2023 - 2024

Asst. Lect. Mateen Naser Ridha


Mateen.naser@lfu.edu.krd

Advanced Database II
Lecture 1
Create Views
▪ Database views are created using the CREATE VIEW statement.

▪ Views can be created from a single table, multiple tables or another view.

▪ To create a view, a user must have the appropriate system privilege according to the specific
implementation.

▪ The basic CREATE VIEW syntax is as follows

CREATE VIEW view_name As < query expression >

View name Any legal SQL expression

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

Advanced Database II | IT Dept.| Stage 3 5/17/2024 2


1.Creating View from a single table(cont.)

Example #2
Based on the COMPACT_DISC_ INVENTORY table, which includes six
columns. Suppose you want to view only the CD_TITLE, COPYRIGHT,
and IN_STOCK columns.

CREATE VIEW COMPACT_DISCS_IN_STOCK


( COMPACT_DISC, COPYRIGHT, IN_STOCK ) AS
SELECT CD_TITLE, COPYRIGHT, IN_STOCK
FROM COMPACT_DISC_INVENTORY;

Advanced Database II | IT Dept.| Stage 3 5/17/2024 3


Updating a View
▪ What does it mean to insert a row of data into a view, delete a row from a view, or
update a row of a view?

▪ For some views these operations can obviously be translated into equivalent
operations against the source table(s) of the view.

Advanced Database II | IT Dept.| Stage 3 5/17/2024 4


Updating a View(cont.)
▪ There are certain conditions needed to be satisfied to update a view. If any one of these
conditions is not met, then we will not be allowed to update the view.

1. The SELECT statement which is used to create the view should not include GROUP BY clause
or ORDER BY clause.

2. The SELECT statement should not have the DISTINCT keyword.

3. The view should have all NOT NULL values.

4. The view should not be created using nested queries or complex queries.

5. The view should be created from a single table. If the view is created using multiple tables then
we will not be allowed to update the view.

Advanced Database II | IT Dept.| Stage 3 5/17/2024 5


Updating a View(cont.)
• If a view satisfies all the above-mentioned rules then view can be updated.

The following code block has an example to update the age of Ramesh.

UPDATE CUSTOMERS_VIEW
SET AGE = 35
WHERE name = 'Ramesh';

• We can use the CREATE OR REPLACE VIEW statement to add or remove fields from a view.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1,column2,.. FROM table_name
WHERE condition;

Advanced Database II | IT Dept.| Stage 3 5/17/2024 6


Updating a View(cont.)
➢ Inserting a row in a view
• We can insert a row in a view in a same way as we do in a table.
• We can use the INSERT INTO statement of SQL to insert a row in a View.

INSERT INTO DetailsView(NAME, ADDRESS)


VALUES("Suresh","Gurgaon");

➢ Deleting a row from a View


• Deleting rows from a view is also as simple as deleting rows from a table.
• We can use the DELETE statement of SQL to delete rows from a view.

DELETE FROM DetailsView


WHERE NAME="Suresh";

Advanced Database II | IT Dept.| Stage 3 5/17/2024 7


Using the WITH CHECK OPTION Clause
• The WITH CHECK OPTION clause in SQL is a very useful clause for views. It is applicable
to a updatable view.
• If the view is not updatable, then there is no meaning of including this clause in the
CREATE VIEW statement.

✓ The WITH CHECK OPTION clause is used to prevent the insertion of rows in
the view where the condition in the WHERE clause in CREATE VIEW
statement is not satisfied.

✓ If we have used the WITH CHECK OPTION clause in the CREATE VIEW
statement, and if the UPDATE or INSERT clause does not satisfy the
conditions then they will return an error.

Advanced Database II | IT Dept.| Stage 3 5/17/2024 8


Using the WITH CHECK OPTION Clause(cont.)
To work around this problem, you can add the WITH CHECK OPTION clause to your
view definition, as in the following example:

CREATE VIEW EMP_COMM AS


SELECT EMPLOYEE_ID, YEAR_1999, YEAR_2000
FROM EMPLOYEE_COMMISSIONS
WHERE YEAR_1999 > 100
WITH CHECK OPTION;

Now if you tried to update a YEAR_1999 value to an amount less than or equal
to 100, you would receive an error message telling you that the change could
not be made.

Advanced Database II | IT Dept.| Stage 3 5/17/2024 9


Thanks
Any Question

Advanced Database II | IT Dept.| Stage 3


?
5/17/2024 10

You might also like