Professional Documents
Culture Documents
DBMS Lab Assignments-8 PDF
DBMS Lab Assignments-8 PDF
SUB-QUERY
A subquery is a SELECT query that is nested within another SELECT, INSERT, UPDATE,
or DELETE statement.
A subquery can also be nested inside another subquery.
Subqueries can often be re-written into regular JOINs.
Sometimes an existence subquery can perform better than equivalent non-subquery methods.
A correlated subquery is a subquery whose results depend on the values of the outer query.
SYNTAX:-
7) Select ID, Frist name from employee where ID belongs to title table.
8) Show all details of employee(s) whose job title is Developer.
9) Show ID, Frist name for those employee(s) whose ID belongs to title table and start date
is greater than '3-1-2003’.
VIEWS
Views are virtual tables that are compiled at run time. The data associated with views are not
physically stored in the view, but it is stored in the base tables of the view. A view can be made
over one or more database tables. Generally we put those columns in view that we need to
retrieve/query again and again. Once you have created the view, you can query view like as table.
We can make index, trigger on view.
In Sql Server we make views for security purpose since it restricts the user to view some
columns/fields of the table(s). Views show only those columns that are present in the query which
is used to make view. One more advantage of Views is data abstraction since the end user is not
aware of all the data present in database table.
i) Simple View
When we create a view on a single table, it is called simple view.
In simple view we can insert, update, delete data. We can only insert data in simple view
if we have primary key and all not null fields in the view.
Syntax for inserting data into a view is same as insert into statement.
Syntax for updating data into a view is same as update statement.
Syntax for deleting data from a view is same as delete statement.
To delete a view syntax is drop view view_name.
To display all the data from a view Query view like as table.
Example: - Select * from view_name;
You can modify the definition of a VIEW in SQL Server without dropping it by using the
ALTER VIEW Statement.
SYNTAX:-
ALTER VIEW view_name AS
SELECT expressions
FROM tables
WHERE conditions;
10) Create a view called emp_data from the table employee and title having column id,
11) first_name, last_name, salary and job_title.
12) Create a view called emp_data2 from the table employee and title having column id,
13) first_name, last_name, region and job_title where region in west and North.
14) Retrieve all the records from emp_data and emp_data2 view.
15) Try to insert a record in emp_data view.
16) Update the region to SOUTH in the view called emp_data2 where region is North.
17) Delete the record form emp_data where between 2 and 5.
18) Add a new column in emp_data view called city.
19) Delete the view called emp_data2.