Topic 3 Msbte Questions and Answers1
Topic 3 Msbte Questions and Answers1
SUMMER – 2017
Q. Define super key. 2 Marks
Definition: Super key of an entity set is a set of one or more attributes whose values uniquely
determine each entity.
The SQL GROUP BYclause is used along with the SQL aggregate functions and specifies the
groups where selected rows are placed.
When one or more aggregate functions are presented in the SQL SELECT column list, the SQL
GROUP BY clause calculates a summary value for each group.
SQL is an acronym for Structured Query Language an is a standard relational query language
used for creating and manipulating databases.
Features of SQL are:
SQL allows the user to create,update, delete, and retrieve data from a database.
SQL is very simple and easy to learn.
SQL works with database programs like DB2, Oracle, MS Access, Sybase, MS SQL Sever etc.
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join
condition along with rows which do not satisfy the join condition.
The SQL OUTER JOIN operator (+) is used only on one side of the join condition only.
Outer join are of three types:
1.Left outer join (also known as left join): this join returns all the rows from left table combine
with the matching rows of the right table. If we get no matching in the right table it returns
NULL values.
Left Outer Join syntax :
SELECT column-name-list from table-name LEFT OUTER JOIN table-name2 on table-
name1.column-name = table-name2.column-name;
2.Right outer join(also known as right join): this join returns all the rows from right table are
combined with the matching rows of left table .If we get no columnmatching in the left table, it
returns null value.
Right Outer Join Syntax:
SELECT column-name-list from table-name1 RIGHT OUTER JOIN table-name2 on table-
name1.column-name = table-name2.column-name;
3.The full outer join keyword returns all records when there is a match in either left (table1) or
right (table2) table records.
FULL OUTER JOIN Syntax:
SELECT column_name(s)from table1full outer join table2 on table1.column_name
=table2.column_name;
Syntax:
Create table tablename (column datatype size references parenttablename (primary key
attribute)....)
Example:
Create table product (EmpId number (4) references Emp (EmpId), PName varchar2(10));
After table creation the foreign key is added as:Alter table product add constraint fk_prod foreign
key (EmpId) references Emp (EmpId);
The syntax:
SQL> COMMIT;
Or
COMMIT WORK;
Rollback:
The ROLLBACK command is used to undo transactions that have not already been saved to the
database.
The ROLLBACK command can only be used to undo transactions since the last COMMIT or
ROLLBACK command was issued.
We can either rollback the entire transaction or till a particular save point transaction can be
rolled back.
The syntax for ROLLBACK is:
ROLLBACK TO SAVEPOINT_NAME;
OR
ROLLBACK;
OR
ROLLBACK WORK;
SUMMER 2016
Entity: An entity is a thing or object in the real world with an independent existence. An entity
may be an object with a physical existence.
Attribute: Describing properties of an entity is called attributes. For example, a student entity
may have name, class, and age as attributes.
Q. Explain the steps used in query processing with suitable diagram. (Diagram - 2 marks;
Explanation - 2 marks) 4 Marks
Query processing: It is a three step process that transforms a high-level query (of relational
calculus/SQL) into an equivalent and more efficient lower-level query (of relational algebra).
1. Parsing and translation – Check syntax and verify relations. It translates the query into an
equivalent relational algebra expression.
2. Optimization – Generate an optimal evaluation plan (with lowest cost) for the query plan.
3. Evaluation – The query-execution engine takes an (optimal) evaluation plan, executes that
plan, and returns the answers to the query.
1. Not Null: By default all columns in tables allows null values. When a NOT NULL constraint
is enforced on column or set of columns it will not allow null values.
2. CHECK: The constraint defines a condition that each row must satisfy. A single column can
have multiple check condition.
3. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of
specified column in table. It restricts null values too.
Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME PRIMARY KEY);
Example: SQL> CREATE TABLE EMP (ID NUMBER (5) CONSTRAINT ID_PK PRIMARY
KEY, NAME VARCHAR2 (10), SAL NUMBER (10));
4. Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database
table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns. It allows null value.
3. Isolation: It is necessary to maintain isolation for the transactions. This means one transaction
should not be aware of another transaction getting executed. Also their intermediate result should
be kept hidden.
4. Durability: When a transaction gets completed successfully, it is important that the changes
made by the transaction should be preserved in database in spite of system failures.
Ans:
Commit
The COMMIT command is used to save changes invoked by a transaction to the database.
The COMMIT command saves all transactions to the database since the last COMMIT or
ROLLBACK command.
The syntax for COMMIT command is as follows:
SQL> COMMIT;
Rollback:
The ROLLBACK command is used to undo transactions that have not already been saved to the
database.
The ROLLBACK command can only be used to undo transactions since the last COMMIT or
ROLLBACK command was issued.
We can either rollback the entire transaction or till a particular save point transaction can be
rolled back.
The syntax for ROLLBACK is:
ROLLBACK TO SAVEPOINT_NAME; OR
ROLLBACK;
e.g. ROLLBACK TO sv1;
OR ROLLBACK
Ans:
i) select upper(emp_name) from Employee;
ii) select emp_id from Employee where emp_city = ‗Pune‗ or emp_city = ‗Mumbai‗;
Revoke: This command is used to withdraw the privileges that has been granted to a user.
Q. Describe string function, date and time function. (Any two String Function - 2 marks;
any two Date and Time Function - 2 marks) 4 marks
Sr. Function Description
No
Initcap(str) Converts first letter of string to capital letter. Example: Select
1. initcap(‗rdbms‗) from dual;
Length(char) Find outs the length of given string. Example: Select length(‗RDBMS‗) from
4. dual;
5. Ltrim(char,set) It trim from the left of character string. Example: Select Ltrim(‗manas khan‗,
‗manas‗) from dual;
6. Rtrim(char,set) It trim from the Right of character string. Example: Select Rtrim(‗manas
khan‗, ‗khan‗) from dual;
7. Lpad(char1,len It returns char1, left-padded to given length with the sequence of characters in
gth, char2) char2. Example: Select Lpad(‗SKY‗, 8, ‗*‗) from dual;
8. Rpad(char1,len It returns char1, right-padded to given length with the sequence of characters
gth ,char2) in char2. Example: Select Lpad(‗SKY‗, 8, ‗*‗) from dual;
9 Translate(char, It returns expr with all occurrences of each character in from string replaced
fromstring,to by its corresponding character in to_string. Example: Select
string) translate(Hickory,‗H‗,‗D‗) from dual
10 Replace(char,s It returns character string with each occurrences of search string replaced with
earchstring,[re [repstring] Example: Select replace(‗Tick and Tock‗,‗T‗,‗Cl‗) from dual;
pstring])
11 Substr(char,m, It returns substring of character string that stack at m character and is of length
n) n Example: Select substr(Triangle‗4,5) from dual;
12 Concatenation( It merges two or more strings or a string and a data value together Example:
) Select (‗Branch is‗|| branch_name) from table_name;
1. The group by clause can be used in a select statement to collect data across multiple records
and group by one or more columns.
2. The group by field contains the repetitive records or values like department number in
employee.
3. The syntax is:
Select col1,col2,….,coln aggregate_function (expression) From table_name Where predicates
Group by column_name;
4. Example: Display average salary of each department
Q. List and explain any 4 arithmetic operators in SQL with example. 4 Marks
(For each - 1 mark) Ans: Arithmetic operators are used to perform mathematical functions in
SQL—the same as in most other languages. There are four conventional operators for
mathematical functions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
Addition
Addition is performed through the use of the Meaning
plus (+) symbol. Example
SELECT SALARY + BONUS FROM The SALARY column is added with the
EMPLOYEE_PAY_TBL; BONUS column for a total for each row of
data
Subtraction
Subtraction is performed using the minus (-) Meaning
symbol. Example
SELECT SALARY - BONUS FROM The BONUS column is subtracted from the
EMPLOYEE_PAY_TBL; SALARY column for the difference
Multiplication
Multiplication is performed by using the Meaning
asterisk (*) symbol. Example
SELECT SALARY * 10 FROM The SALARY column is multiplied by 10
EMPLOYEE_PAY_TBL;
Division
Division is performed through the use of Meaning
the ―/‖ symbol. Example
SELECT SALARY / 10 FROM The SALARY column is divided by 10
EMPLOYEE_PAY_TBL;
Q. Consider the structure for book table as Book_master {book_id, book_name, subcode,
author, no_of_copies, price}.
Write SQL queries for the following:
i) Display total no. of book for subject „DBM‟.
ii) Get authorwise list of all books.
iii) Display all books whose prices are between Rs. 200 and Rs.500 4 Marks
Ans:
i. Select sum(no_of_copies) from Book_master where subcode=‗DBM‗; (1 mark)
OR Select book_id From Book_master Where price >= 200 and price <= 500;
WINTER 2016
Q. List four DDL commands. 2 Marks
DDL commands:
1. Create 2. Alter 3. Rename 4. Drop 5. Truncate
Q. List DCL commands any four. 2 Marks
DCL commands:
1. Grant 2. Revoke 3. Commit 4. Rollback 5. Savepoint
Q. Explain DELETE and DROP Command with syntax and example 4 Marks
DELETE Command: The SQL DELETE Query is used to delete the existing records
from a table. You can use WHERE clause with DELETE query to delete selected rows,
otherwise all the records would be deleted.
Syntax:
DELETE FROM table_name WHERE [condition];
Example:
1) To Delete record from customer table with Name as JACK
DELETE FROM CUSTOMER WHERE NAME = 'JACK';
DROP Command: The SQL DROP Command is use to delete all records and schema of
the table.
Syntax:
DROP Table <table name>;
Example:
Drop table emp;
The SQL ALTER TABLE command is used to modify the definition (structure) of a
table by modifying the definition of its columns
It can be used for
1. To add any new column to a table
2. To change data type or size of already existing data column of a table.
3. To delete a column from a table
4. To add / drop constrains from column of a table.
The three options with ALTER command are:
1. Add column:-We can add any number of columns in a table using ALTER table
command with add clause. Added column becomes last column by default.
Syntax to add a column:-
ALTER TABLE <table_name>
ADD (<column_name1>< datatype>(size),
..<column_nameN>< datatype>(size) );
2. Drop Column: - We can delete the existing column with help of drop clause in the
ALTER table command. We can drop one column at a time. After dropping any
column from the table, there must be at least one column left in the table.
Syntax to drop a column:-
ALTER TABLE <table_name >
DROP column <column_name>;
3. Modify column:-We can change the data type and/or size of a column in a table by
using modify clause in ALTER table. The size of the column can be increased or
decreased if the column contains only null values or if the table has no rows.
Syntax to modify a column
ALTER TABLE <table_name >
MODIFY( <column_name1>< datatype>(size),
<column_name2>< datatype>(size),
..
<column_name N>< datatype>(size)) ;
Ans
i) select * from Emp where salary >= 5000;
ii) select * from Emp where job=‘Manager‘ or job=‘Analyst‘;
OR
ii) select * from Emp where job in(‘Manager‘ , ‘Analyst‘);
iii) update Emp set location=‘Pune‘ where deptno=40; {consider location
attribute in Emp table}
iv) select Ename,Salary from Emp where salary >50000 and(deptno = 10 or
deptno=30);
Q. Explain Domain integrity constraint with syntax and example. 4 Marks
1. Not Null constraint: This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not
allowed.
syntax: create table <table name>( Column_name Datatype (Size) [CONSTRAINT
constraint name] NOT NULL );
Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee
( id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10)
);
OR
For Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee
( id number(5),
name char(20) NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10)
);
1) Check constraint: it defines a condition that each row must satisfy. A single
column can have multiple check constraints that reference the column in its
definition.
Syntax at table creation:
Create table <table_name>
(column_name1 datatype(size) constraint <constraint_name> check <condition or
logical expression>,
---
column_name n datatype(size)
);
Example:
create table emp( empno number(5), ename varchar2(25), salary number(7,2)
constraint emp_sal_ck check(salary > 5000), job varchar2(15) );
After table creation
Syntax:
Alter table <table_name> add constraint<constraint_name> check <condition>;
Example:
alter table emp add constraint emp_deptno_ck check(deptno>5);
Q. Give the use of grant and revoke command with syntax and example.
1. GRANT command is used to provide access or privileges on the database objects to
the users.
The Syntax for the GRANT command is:
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
The Example for the GRANT command is:
GRANT insert,update
ON emp
TO staff ;
2. REVOKE command removes user access rights or privileges to the database objects.
The Syntax for the REVOKE command is:
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name};
The Example for the REVOKE command is:
REVOKE insert,update
ON emp
FROM staff ;
SUMMER 2015
Q. What is Primary Key? Give example 2 Marks
Ans:
Primary key: Within a relation there is always one attribute which has values that are
unique in a relation and thus can be used to identify tuple of that relation. Such a unique
identifier is called the primary key.
E.g. In the Student(Rollno,name,class,address) relation Rollno is the primary key.
Q. List any four DML commands. 2 Marks
(For each command – ½ Mark)
Ans: List of DML commands:
1. Select
2. Insert
3. Update
4. Delete
Q. What are the atomic values? 2 Marks
(Correct Definition-2 Marks)
Ans:
A piece of data in a database table that cannot be broken down any further called atomic
value.
OR
The value of each attribute containing only a single value from that attribute domain is
called atomic value.
Ans :
i) select FIRST_NAME,LAST,NAME from employee;
ii) select distinct DEPARTMENT from employee;
iii) select FIRST_NAME ―Employee Name‖ from employee;
iv) select ltrim(FIRST_NAME) from employee;
Ans:
Foreign key represents relationship between two tables.
A foreign is a column ( or group of columns) whose values are derived from the „primary
key‟ of same or some other table.
For eg : consider two tables :
Emp={empid, empname, deptno, salary}
Dept= { deptno, deptname}
In table Emp, empid is a primary key which contains unique values to identify each
record.
In table Dept, deptno is a primary key containing unique values for deptnos.
To set the relationship between these two tables, we can define Emp.deptno as a foreign
key as:
1. At creation time (Assuming Dept is already created with deptno as primary key)
Create table Emp ( empid number(4), empname varchar(20), deptno number(3)
constraint fk1 references Dept(deptno) , salary number(10,2));
2. After creation :
Alter table Emp add constraint fk1 foreign key (deptno) references dept(deptno);
On delete cascade :
When a relation is created between two tables, it is permissible to delete records in child
table , but a vice–versa operation is not allowed. i.e. deleting records from parent table
when it is referenced from some other table is not allowed.
On delete cascade clause allows automatic deletion of child records when parent record is
deleted.
Eg :
Alter table Emp add constraint foreign key emp(deptno) references dept(deptno) on delete
cascade;
2) SELF JOIN:
The SQL SELF JOIN is used to join a table to itself, as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
Syntax:
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
Select x.stud_name, y.stud_name from stud_infox,stud_info y
Where x.leader= y.stud_id;
3) LEFT OUTER JOIN:
A left outer join retains all of the rows of the ―left‖ table, regardless of whether
there is a row that matches on the ―right‖ table.
Syntax:
Select column1name,column2name
from
table1name any_alias1 ,table2name any_alias2
on
any_alias1.columnname(+) = any_alias2.columnname;
OR
Select column1name,column2name
from
table1name left outer join table2name on
table1name.columnname= table2name.columnname;
Example:
select
last_name,department_name
from
employeese,departments d
on
e.department_id(+) = d.department_id;
OR
select
last_name,department_name
from
employees left outer join departments
on
employees.department_id = departments.department_id;
4) RIGHT OUTER JOIN
A right outer join retains all of the rows of the ―right‖ table, regardless of whether
there is a row that matches on the ―left‖ table.
Syntax:
Select column1name,column2name
from table1name any_alias1 ,table2name any_alias2
on
any_alias1.columnname =any_alias2.columnname (+);
OR
Select column1name,column2name
from table1name any_alias1 right outer join table2name any_alias2
on
any_alias1.columnname =any_alias2.columnname;
Example:
Select last_name,department_name
from
employeese,departments d
on
e.department_id = d.department_id(+);
OR
Select last_name,department_name
from
employees e right outer join departments d
on
e.department_id = d.department_id;
database.
or ROLLBACK command.
d is as follows:
SQL> COMMIT;
Savepoint:
SAVEPOINT SAVEPOINT_NAME;
e.g. SAVEPOINT SV1;
Rollback:
ROLLBACK TO SAVEPOINT_NAME;
e.g. ROLLBACK TO sv1;
Group by clause
group by clause can be used in a select statement to collect data across
multiple records and group by one or more columns.
in employee.
Select avg(sal)
From emp
group by deptno;
Order by clause:
result-set by one or more columns.
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Q. Describe serializability.
In concurrent execution of transaction, if the consistency level of the concurrent schedule is same
as the consistency level after serial schedule of the same schedule, then that concurrent schedule
is called as serializable concurrent schedule and this property of schedule is called as
serializability.
Serializability ensures consistency of database.
Example :
Transaction T1: Rs. 50 of A‘s account are transferred to B‘s Account.
Transaction T2 : 10% of A‘s balance is transferred to B‘s Account Consider initial amount as
A=100, B=150 so initially A+B=250 Serial Schedule appears as
Here also A+B=250 at the end of schedule, so this concurrent schedule preserves consistency
and hence it is a serializable schedule and shows serializability property.