Unit 2 SQL
Unit 2 SQL
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.
Views in SQL are kind of virtual tables. A view also has rows and columns as they
are in a real table in the database.
We can create a view by selecting fields from one or more tables present in the
database. A View can either have all the rows of a table or specific rows based on
certain condition.
Views are used for security purpose in databases, views restricts the user
from viewing certain column and rows means by using view we can apply the
restriction on accessing the particular rows and columns for specific user. Views
display only those data which are mentioned in the query, so it shows only data
which is returned by the query that is defined at the time of creation of the View.
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created
from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS SELECT column1, column2..... FROM
table_name WHERE condition;
view_name: Name for the View
table_name: Name of the table
condition: Condition to select rows
Examples:
Creating View from a single table:
In this example we will create a View named DetailsView from the table
StudentDetails.
Query:
CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM
StudentDetails WHERE S_ID < 5;
To see the data in the View, we can query the view in the same manner as we
query a table.
SELECT * FROM DetailsView;
Output:
In this example, we will create a view named StudentNames from the table
StudentDetails.
Query:
CREATE VIEW StudentNames AS SELECT S_ID, NAME FROM
StudentDetails ORDER BY NAME;
If we now query the view as,
SELECT * FROM StudentNames;
Output:
Creating View from multiple tables: In this example we will create a View
named MarksView from two tables StudentDetails and StudentMarks. 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, StudentMarks.MARKS FROM StudentDetails,
StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME;
To display data of View MarksView:
SELECT * FROM MarksView;
Output:
DELETING VIEWS
we will want to delete it. SQL allows us to delete an existing View. We can delete
or drop a View using the DROP statement.
Syntax:
DROP VIEW view_name;
view_name: Name of the View which we want to delete.
For example, if we want to delete the View MarksView, we can do this as:
DROP VIEW MarksView;
UPDATING VIEWS
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.
6. 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;
For example, if we want to update the view MarksView and add the field AGE
to this View from StudentMarks Table, we can do this as:
CREATE OR REPLACE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
If we fetch all the data from MarksView now as:
SELECT * FROM MarksView;
Output:
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. Also deleting a row from a view first delete the row from the
actual table and the change is then reflected in the view.
Syntax:
DELETE FROM view_name
WHERE condition;
view_name:Name of view from where we want to delete rows
condition: Condition to select rows
Example: In this example, we will delete the last row from the view DetailsView
which we just added in the above example of inserting rows.
DELETE FROM DetailsView
WHERE NAME="Suresh";
If we fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output:
Declare
end;
Output:
before
num1 = 1000 num2 = 2000
after
num1 = 2000 num2 = 1000
Output:
Greatest number is 67
Syntax
LOOP
Sequence of statements;
END LOOP;
Here, the sequence of statement(s) may be a single statement or a block of
statements. An EXIT statement or an EXIT WHEN statement is required to break
the loop.
Example
DECLARE
x number := 10;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
IF x > 50 THEN
exit;
END IF;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: ' || x);
END;
/
When the above code is executed at the SQL prompt, it produces the following result
−
10
20
30
40
50
After Exit x is: 60
PL/SQL procedure successfully completed.
declare
-- it gives the final answer after computation
fac number :=1;
-- given number n
-- taking input from user
n number := &1;
-- start block
begin
Block Structure
PL/SQL blocks have a pre-defined structure in which the code is to be grouped.
Below are different sections of PL/SQL blocks.
1. Declaration section
2. Execution section
3. Exception-Handling section
The below picture illustrates the different PL/SQL block and their section order.
Declaration Section
This is the first section of the PL/SQL blocks. This section is an optional part. This
is the section in which the declaration of variables, cursors, exceptions,
subprograms, pragma instructions and collections that are needed in the block will
be declared. Below are few more characteristics of this part.
Exception-Handling Section:
The exception is unavoidable in the program which occurs at run-time and to handle
this Oracle has provided an Exception-handling section in blocks. This section can
also contain PL/SQL statements. This is an optional section of the PL/SQL blocks.
This is the section where the exception raised in the execution block is
handled.
This section is the last part of the PL/SQL block.
Control from this section can never return to the execution block.
This section starts with the keyword ‘EXCEPTION’.
This section should always be followed by the keyword ‘END’.
A view can be "materialized" by storing the tuples of the view in the database. Index
structures can be built on the materialized view. Consequently, database accesses to
the materialized view can be much faster than recomputing the view. A materialized
view is like a cache --- a copy of the data that can be accessed quickly.
If a regular view is a saved query, a materialized view is a saved query plus its results
stored as a table.
Advantages of views
Security
Each user can be given permission to access the database only through a small
set of views that contain the specific data the user is authorized to see, thus
restricting the user's access to stored data
Query Simplicity
A view can draw data from several different tables and present it as a single table,
turning multi-table queries into single-table queries against the view.
Structural simplicity
Views can give a user a "personalized" view of the database structure, presenting
the database as a set of virtual tables that make sense for that user.
Consistency
A view can present a consistent, unchanged image of the structure of the
database, even if the underlying source tables are split, restructured, or renamed.
Data Integrity
If data is accessed and entered through a view, the DBMS can automatically
check the data to ensure that it meets the specified integrity constraints.
The CASE expression goes through conditions and returns a value when the first
condition is met (like an if-then-else statement). So, once a condition is true, it
will stop reading and return the result. If no conditions are true, it returns the
value in the ELSE clause. If there is no ELSE part and no conditions are true, it
returns NULL.
Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Below is a selection from the "OrderDetails" table.
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40