Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views10 pages

Dbmsimpunit 3

The document provides an overview of important concepts related to Database Management Systems, specifically focusing on SQL, its types, and various commands such as CREATE, ALTER, SELECT, INSERT, UPDATE, and DELETE. It discusses views, query processing steps, optimization techniques, and the evaluation of queries, including selection, sorting, and join operations. Additionally, it outlines the costs associated with query execution and the methods for optimizing query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Dbmsimpunit 3

The document provides an overview of important concepts related to Database Management Systems, specifically focusing on SQL, its types, and various commands such as CREATE, ALTER, SELECT, INSERT, UPDATE, and DELETE. It discusses views, query processing steps, optimization techniques, and the evaluation of queries, including selection, sorting, and join operations. Additionally, it outlines the costs associated with query execution and the methods for optimizing query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

BANSAL INSTITUTE OF SCIENCE AND TECHNOLOGY

DEPARTMENT OF INFORMATION TECHNOLOGY

DATA BASE MANAGEMENT SYSTEM

Important Questions and Answers

UNIT –III

1.What is SQL.Discuss its types


Ans: Structure Query Language(SQL) is a programming language used for storing and managing data
in RDBMS. SQL was the first commercial language introduced for E.F Codd's Relational model. Today
almost all RDBMS (MySQL, Oracle, Infomax, Sybase, MS Access) uses SQL as the standard database
language. SQL is used to perform all type of data operations in RDBMS.

Types of SQL command:

1.DDL-Data definition Language

2.DML-Data manipulation Langauge

3. TCL: Transaction Control Language

4. DCL: Data Control Language

5DQL: Data Query Language

Structured Query Language (SQL) is a data sub language that has constructs for defining and

processing a database.

It can be

H Used stand-alone within a DBMS command

H Embedded in triggers and stored procedures

H Used in scripting or programming languages

History of SQL-92

 SQL was developed by IBM in late 1970s.

 SQL-92 was endorsed as a national standard by ANSI in 1992.

 SQL3 incorporates some object-oriented concepts but has not gained acceptance in industry.

. Create Table

CREATE TABLE statement is used for creating relations.


Each column is described with three parts: column name, data type, and optional constraints.

CREATE TABLE PROJECT (ProjectID Integer Primary Key, Name Char(25) Unique Not Null,
Department VarChar (100) Null, MaxHours Numeric(6,1) Default 100);

Constraints

Constraints can be defined within the CREATE TABLE statement, or they can be added to the table
after it is created using the ALTER table statement.

PRIMARY KEY may not have null values

UNIQUE may have null values

NULL/NOT NULL

FOREIGN KEY

CHECK

ALTER Statement

ALTER statement changes table structure, properties, or constraints after it has been created. Example
ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY
(EmployeeNum) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON
DELETE NO ACTION;

DROP Statements

DROP TABLE statement removes tables and their data from the database A table cannot be dropped
if it contains foreign key values needed by other tables. H Use ALTER TABLE DROP
CONSTRAINT to remove integrity constraints in the other table first Example:

DROP TABLE CUSTOMER

2.4.5 SELECT Statement

SELECT can be used to obtain values of specific columns, specific rows, or both.

SELECT (column names or *) FROM (table name(s)) [WHERE (conditions)];

WHERE Clause Conditions

Sorting the Results

ORDER BY phrase can be used to sort rows from SELECT statement. SELECT Name, Department
FROM EMPLOYEE ORDER BY Department;

SELECT Name, Department FROM EMPLOYEE ORDER BY Department DESC, Name ASC;

INSERT INTO Statement

The order of the column names must match the order of the values. Values for all NOT NULL
columns must be provided
– INSERT INTO PROJECT VALUES (1600, ‗Q4 Tax Prep‘, ‗Accounting‘, 100);

UPDATE Statement

UPDATE EMPLOYEE SET Phone = ‘287-1435’ WHERE Name = ‘James’;

DELETE FROM PROJECT WHERE Department = ‗Accounting‘; ON DELETE CASCADE


removes any related referential integrity constraint

2.What is a view .Discuss its types with examples


Ans: 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 column1, column2, ...
FROM table_name
WHERE condition;

3.Answer the following queries


Relational schema for SQL queries

EMPLOYEE (FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, #SUPERSSN, #DNO)
DEPARTMENT (DNAME, DNUMBER, #MGRSSN, MGRSTARTDATE)

DEPT_LOCATIONS (#DNUMBER, DLOCATION)

PROJECT (PNAME, PNUMBER, PLOCATION, #DNUM)

WORKS_ON (#ESSN, #PNO, HOURS)

DEPENDENT (#ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)

SQL queries

Query 0

Retrieve the birthdate and address of the employee(s) whose name is ‘John B Smith’

SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME = ‘John’ AND MINIT = ‘B’ AND LNAME =
‘Smith’;

Query 1
Retrieve the name and address of all employees who work for the ‘Research’ department

SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME = ‘Research’
AND DNUMBER = DNO;

Query2

Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works
in for every employee of the ‘Research’ department .

SELECT * FROM EMPLOYEE, DEPARTMENT WHERE DNAME = ‘Research’ AND DNO = DNUMBER;

4. You are given the following relational schema:

1. Find the names of all the students enrolled in computer course;


Select name from student ,course where student.courseid=course.courseid;

2. Add a column city in student table


Alter table student add(city varchar(20));
3. List the name of all course with their duration
Select course-nm,duration from course.
4. list name of all students starting with a
select name from student where name like ‘a%’;

5 List emailID and cellno of all mechanical engg students

Select EmailID ,CellNo from student where course.coursenm=’Mechanical engineering’ and


Course.CourseID=Student.CourseID.

5.What are the various steps in query processing and optimization.

Ans:
Basic Query Processing Steps ...

Query parsing and translation (query compiler)

 check the syntax (e.g. SQL for relational DBMS)

 verify that the mentioned relations do exist and replace views

 transform the SQL query to a query plan represented by a relational algebra expression (for
relational DBMS) - different possible relational algebra expressions for a single query

Query optimisation (query optimiser

 transform the initial query plan into the best possible query plan based on the given data set -
specify the execution of single query plan operations (evaluation primitives)

• e.g. which algorithms and indices to be used - the query execution plan is defined by a sequence of
evaluation primitives

 Query evaluation (command processor)

 execute the query execution plan and return the result

6.What is Query Expression and Execution

SELECT name, street FROM Customer, Order WHERE Order.customerID = Customer.customerID


AND status = 'open';

 Transform the SQL query to the following query plan


7.What is query cost and what are its measures.

Ans: The query costs are defined by the time to answer a query (process the query execution plan)
Different factors contribute to the query costs

disk access time, CPU time or even network communication time The costs are often dominated by
the disk access time seek time (tS ) (~4 ms) transfer time (tT ) (e.g. 0.1 ms per disk block) - write
operations are normally slower than read operations For simplicity, we will use the number of block
transfers and the number of seeks as cost measure

real systems may also take CPU costs into account

8.Discuss selection ,sorting and join operation

Selection Operation

The lowest-level query processing operator for accessing data is the file scan search and retrieve
records for a given selection condition

 Linear search

given a file with n blocks, we scan each block and check if any records satisfy the condition  a
selection on a candidate key attribute (unique) can be terminated after a record has been found -
average costs: tS + n/2 * tT , worst case costs: tS + n * tT  applicable to any file regardless of
ordering, the availability of indices or the type of selection operation

 Binary search

an equality selection condition on a file that is ordered on the selection attribute (n blocks) can be
realised via a binary search  note that this only works if we assume that the blocks of the file are
stored continously!  worst case costs: log2 (n) * (tS + tT )

Sorting

Sorting in database systems is important for two reasons  a query may specify that the output
should be sorted  the processing of some relational query operations can be implemented more
efficiently based on sorted relations - e.g. join operation.

For relations that fit into memory, techniques like quicksort can be used  For relations that do not
fit into memory an external merge sort algorithm can be used.
External Merge Sort Example

Join Operation

Different algorithms for implementing join operations

 nested-loop join

 block nested-loop join

 index nested-loop join

 merge join  hash join

The query optimiser may choose an algorithm based on cost estimates

Nested-Loop Join

Block Nested-Loop Join

Indexed Nested-Loop Join

Other Join Implementations

Merge join

Hybrid merge join

Hash join

9.How query evaluation is done.


Ans: The individual relational operations that have been discussed so far normally form part of more
complex expressions

There are two approaches how a query execution tree can be evaluated

materialisation - compute the result of an evaluation primitive and materialise (store) the new
relation on the disk

pipelining - pass on tuples to parent operations even while an operation is still being executed

Materialisation

Evaluate one operation after another starting at the leave nodes of the query expression tree.

materialise intermediate results in temporary relations and use those for evaluating operations at
the next level.

A materialised evaluation is always possible

costs of reading and writing temporary relations can be quite high

 double buffering with two output buffers for each operation

Pipelining

Pipelining evaluates multiple operations simultaneously by passing results of one operation to the
next one without storing the tuples on the disk

 Much cheaper than materialisation since no I/O operations for temporary relations

 Pipelining is not always possible

 e.g. does not work for input for sorting algorithms

 Pipelines can be executed in a demand driven or in a producer driven manner

What is Query Optimization.

There are alternative ways for evaluating a given query  different equivalent expressions (query
expression trees)  different potential algorithms for each operation of the expression
Types of Query optimization

1.Cost based

2.Heuristic based

Cost based

generate logically equivalent expressions by using a set of equivalence rules (2) annotate the
expressions to get alternative query evaluation plans (e.g. which algorithms to be used) (3) select the
cheapest plan based on the estimated costs.

Estimation of query evaluation costs based on

 statistical information from the catalogue manager in combination with the expected
performance of the algorithms

Equivalence Rules

Conjunctive selection operations can be deconstructed into a sequence of individual selections

 Selection operations are commutative

 Cascade of projection operations (only final one)

Selections can be combined with cartesian products and theta joins


Heuristic Optimisation

Cost-based optimisation can be expensive

 a DBMS may use some heuristics to reduce the number of cost-based choices

A heuristic optimisation transforms the query expression tree by using a set of rules that typically
improve the execution performance

 perform selection as early as possible - reduces the number of tuples

 perform projection as early as possible - reduces the number of attributes

 perform most restrictive selection and join operations (smallest result size) before other
operations

You might also like