0% found this document useful (0 votes)
162 views12 pages

Dbms Assignment 4

This document contains an assignment on database management systems for a class. It includes 8 questions with multiple parts testing students' knowledge of SQL concepts like triggers, data definition language, data manipulation commands, joins, and authorization mechanisms. Students are asked to fill in blanks, choose correct options, state if statements are true/false, name/define terms, answer questions in brief or at length involving example databases and SQL queries.

Uploaded by

Akxar V
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)
162 views12 pages

Dbms Assignment 4

This document contains an assignment on database management systems for a class. It includes 8 questions with multiple parts testing students' knowledge of SQL concepts like triggers, data definition language, data manipulation commands, joins, and authorization mechanisms. Students are asked to fill in blanks, choose correct options, state if statements are true/false, name/define terms, answer questions in brief or at length involving example databases and SQL queries.

Uploaded by

Akxar V
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/ 12

MGM’s College of engineering and Technology, Kamothe, Navi Mumbai

Department of Information Technology


Course Code: ITC304 Course Name: Database Management System
Assignment IV
Class: SE AY: 2019-2020
Date of Issue:16/9/19 Date of Submission: 4/10/19 Date of Return: 7/10/19
Q. Question Module Bloom’s Program CO
No Taxanomy Indicato
level r (PI)
Q1. Fill in the blanks.
(a) A __________ is a special kind of a store procedure that executes 4 1 1.7.1 CO4
in response to certain action on the table like insertion, deletion or
updation of data.

(b) A database language used for defining the whole database structure 4 1 1.7.1 CO3
and schema is called ______________.

(c) 4 1 1.7.1 CO3


____________ command in SQL allows us to change the definition
of a table.
(d) If you don’t specify ASC or DESC after a SQL ORDER BY 4 1 1.7.1 CO3
clause, _____________(ASC/ DESC) is used by default.
(e) ________SQL function is used to find the maximum value. 4 1 1.7.1 CO3

Q2. Choose Correct Options.

(a) In SQL, which command is used select only one copy of each set 4 1 1.7.1 CO3
of duplicate rows.
i) Select Distinct ii) Select Unique
iii) Select Different iv)All of the Above
(b) Which of the following SQL commands is used to retrieve the 4 1 1.7.1 CO3
data?
i) Delete ii) insert iii) select iv) join
(c) ___________removes all rows from a table without logging the 4 1 1.7.1 CO3
individual row deletions.
i) DELETE ii) REMOVE iii) DROP iv) TRUNCATE
(d) The command to remove rows from a table 'CUSTOMER' is: 4 1 1.7.1 CO3
i)Remove From Customer
ii)Drop From Customer
iii) Delete From Customer Where
iv) Update From Customer

(e) The command used to delete a particular column in a relation 4 1 1.7.1 CO3
is_______________
i)Update table ii)Truncate Column
iii)Alter, Drop iv) Delete Column
Q3. State whether the following statements are true or false (Give Reasons)

(a) The JOIN operation combines relational tables. 4 1 1.7.1 CO3

(b) Delete is a DDL command. 4 1 1.7.1 CO3

(c) The drop command deletes all the data from the table, but the 4 1 1.7.1 CO3
structure remains as it is.
Q4. Name or define or design the following.
(a) Name the following. 4 1 1.7.1 CO3
The database language that grants the privileges.
(b) Define Data Manipulation Language. 4 1 1.7.1 CO3

(c) Name the following. 4 1 1.7.1 CO3


The command which deletes one or more tuples from the table at a
time.
Q5. Answer the following questions in brief. (20 to 30 words)

(a) Explain different data manipulation commands. 4 2 1.7.1 CO3

(b) Explain different data definition commands. 4 2 1.7.1 CO3

(c) Why the joins are used? What are different types of outer joins in 4 3 1.7.1 CO3
SQL. Write a note on views in SQL.
Q6. Answer the following questions in brief. (50 to 70 words)

(a) What is a referential integrity in SQL? 4 2 1.7.1 CO4

bbbb What is the need of triggers? Write a note on triggers in SQL. 4 2 1.7.1 CO4

(c ) What are the different mechanisms for authorization in SQL. 4 2 1.7.1 CO4

Q7. Think and Answer. (200 words)

(a) Consider the insurance database, where the primary keys are 4 4 2.5.1 CO3
underlined.
person (driver-id, name, address)
car (license, model, year)
accident (report-number, date, location)
owns (driver-id, license)
participated (driver-id, car, report-number, damage-amount)
Figure Insurance database

Construct the following SQL queries for this relational database.


a. Find the total number of people who owned cars that were
involved in accidents in 1989.
b. Find the number of accidents in which the cars belonging to
“John Smith” were involved.
c. Add a new accident to the database; assume any values for
required attributes.
d. Delete the Mazda belonging to “John Smith”.
e. Update the damage amount for the car with license number
“AABB2000” in the accident with report number “AR2197” to
$3000.
(b) Consider the employee database. Give an expression in SQL for 4 4 2.5.1 CO3
each of the following queries.

employee (employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure Employee database

a. Find the names of all employees who work for First Bank
Corporation.
b. Find the names and cities of residence of all employees who
work for First Bank Corporation.
c. Find the company that has the most employees.
d. Find the company that has the smallest payroll.
e. Find the names, street addresses, and cities of residence for all
employees who work for First Bank Corporation and earn more
than 10,000Rs.
Q8. My Ideas. (200 words)

(a) Consider the employee database. Give an expression in SQL for 4 4 2.5.1 CO3
each of the following queries.

employee (employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure Employee database

a. Modify the database so that Jones now lives in Newtown.


b. Give all employees of First Bank Corporation a 10 percent
raise.
c. Give all managers of First Bank Corporation a 10 percent raise.
d. Give all managers of First Bank Corporation a 10 percent raise
unless the salary becomes greater than $100,000; in such cases,
give only a 3 percent raise.
(b) Where the triggers can be used in real life? 4 4 1.7.1 CO3

*As per Blooms Taxonomy


MGM’s College of engineering and Technology, Kamothe, Navi Mumbai
Department of Information Technology
Course Code: ITC304 Course Name: Database Management System
Assignment IV Solution
Class: SE AY: 2019-2020

Q. Question
No
Q1. Fill in the blanks.
(a) A trigger is a special kind of a stored procedure that executes in response to certain action on the table like
insertion, deletion or updation of data.

(b) A database language used for defining the whole database structure and schema is called Data Definition
Language.

(c) Alter command in SQL allows us to change the definition of a table.


(d) If you don’t specify ASC or DESC after a SQL ORDER BY clause, ASC (ASC/ DESC) is used by default.

(e) Max SQL function is used to find the maximum value.

Q2. Choose Correct Options.


(a) In SQL, which command is used to select only one copy of each set of duplicate rows.
i) Select Distinct ii) Select Unique
iii) Select Different iv)All of the Above
(b) Which of the following SQL commands is used to retrieve the data?
i) Delete ii) insert iii) select iv) join
(c) ___________removes all rows from a table without logging the individual row deletions.
i) DELETE ii) REMOVE iii) DROP iv) TRUNCATE
(d) The command to remove rows from a table 'CUSTOMER' is:
i)Remove From Customer
ii)Drop From Customer
iii) Delete From Customer Where
iv) Update From Customer

(e) The command used to delete a particular column in a relation is_______________


i)Update table ii)Truncate Column
iii)Alter, Drop iv) Delete Column
Q3. State whether the following statements are true or false (Give Reasons)
(a) The JOIN operation combines relational tables. True

(b) Delete is a DDL command. false

(c) The drop command deletes all the data from the table, but the structure remains as it is. false
Q4. Name or define or design the following.
(a) Name the following.
The database language that grants the privileges. Data Control Language
(b) Define Data Manipulation Language.
A data manipulation language (DML) is a database language used for manipulation that is inserting,
deleting, and modifying updating data in a database.
(c) Name the following.
The command which deletes one or more tuples from the table at a time. Delete
Q5. Answer the following questions in brief. (20 to 30 words)
(a) Explain different data manipulation commands.
A data manipulation language (DML) is a database language used for manipulation that is inserting,
deleting, and modifying updating data in a database.

1. Insert- Insert statement is used to insert data into database tables.


General Syntax:
INSERT INTO <TABLE NAME> (<COLUMNS TO INSERT>) VALUES (<VALUES TO INSERT>)
Example: insert into Employee (name, dept_id) values (‘ABC’, 3);
2.Update- The update command updates existing data within a table.
General syntax:
UPDATE <TABLE NAME>
SET <COLUMN NAME> = <UPDATED COLUMN VALUE>,
<COLUMN NAME> = <UPDATED COLUMN VALUE>,
<COLUMN NAME> = <UPDATED COLUMN VALUE>,…
WHERE <CONDITION>
Example: update Employee set Name=’AMIT’ where E_id=5;
3.Delete- Deletes records from the database table according to the given constraints.
General Syntax:
DELETE FROM <TABLE NAME>
WHERE <CONDITION>
Example: delete from Employee where e_id=5;
To delete all records from the table:
Delete * from <TABLE NAME>;

(b) Explain different data definition commands.

DDL stands for Data Definition Language. It is a language used for defining and modifying the data and its
structure. It is used to build and modify the structure of your tables and other objects in the database.

DDL commands are as follows,


1. CREATE
2. DROP
3. ALTER
4. RENAME
5. TRUNCATE

1. CREATE COMMAND
CREATE command is used for creating objects in the database. It creates a new table.
Syntax:
CREATE TABLE <table_name>
( column_name1 datatype,
column_name2 datatype,
.
.
.
column_name_n datatype
);

Example : CREATE command


CREATE TABLE employee
(
empid INT,
ename CHAR,
age INT,
city CHAR(25),
phone_no VARCHAR(20)
);

2. DROP COMMAND
DROP command allows to remove entire database objects from the database. It removes entire data
structure from the database. It deletes a table, index or view.

Syntax:
DROP TABLE <table_name>;
OR
DROP DATABASE <database_name>;
Example : DROP Command
DROP TABLE employee;
OR
DROP DATABASE employees;

If you want to remove individual records, then use DELETE command of the DML statement.

3. ALTER COMMAND
An ALTER command allows to alter or modify the structure of the database. It modifies an existing
database object. Using this command, you can add additional column, drop existing column and even
change the data type of columns.
Syntax:
ALTER TABLE <table_name>
ADD <column_name datatype>;

OR

ALTER TABLE <table_name>


CHANGE <old_column_name> <new_column_name>;

OR

ALTER TABLE <table_name>


DROP COLUMN <column_name>;

Example : ALTER Command

ALTER TABLE employee


ADD (address varchar2(50));
OR
ALTER TABLE employee
CHANGE (phone_no) (contact_no);
OR
ALTER TABLE employee
DROP COLUMN age;

4. RENAME COMMAND
RENAME command is used to rename an object. It renames a database table.
Syntax:
RENAME TABLE <old_name> TO <new_name>;

Example:
RENAME TABLE emp TO employee;
5. TRUNCATE COMMAND
TRUNCATE command is used to delete all the rows from the table permanently. It removes all the
records from a table, including all spaces allocated for the records.This command is same as DELETE
command, but TRUNCATE command does not generate any rollback data.
Syntax:
TRUNCATE TABLE <table_name>;

Example:
TRUNCATE TABLE employee;

(c) Why the joins are used? What are different types of outer joins in SQL. Write a note on views in SQL.

A SQL join is a Structured Query Language (SQL) instruction to combine data from two sets of data (i.e.
two tables).
Types of outer joins in SQL
1.SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the
right table (table2). The result is NULL from the right side, if there is no match.
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

2.SQL RIGHT JOIN Keyword


The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from
the left table (table1). The result is NULL from the left side, when there is no match.
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

3.SQL FULL OUTER JOIN Keyword


The FULL OUTER JOIN keyword return all records when there is a match in left (table1) or right (table2)
table records.
FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

SQL VIEW
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.

CREATE VIEW Syntax


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

Q6. Answer the following questions in brief. (50 to 70 words)


(a) What is a referential integrity in SQL?
The concept of REFERENTIAL INTEGRITY is that it does not allow to add any record in a table that
contains the foreign key unless the reference table containing a corresponding primary key.
If any record in referenced table (i.e. the table who contain primary key) is deleted, all the corresponding
records in the referencing table will be deleted for the referential integrity.

Consider a bank database, which contains two tables:

 CUSTOMER_MASTER Table: This holds basic customer/account holder data such as name,
social security number, address and date of birth.
 ACCOUNTS_MASTER Table: This stores basic bank account data such as account type, account
creation date, account holder and withdrawal limits.

To uniquely identify each customer/account holder in the CUSTOMER_MASTER table, a primary key
column named CUSTOMER_ID is created.
To identify a customer and bank account relationship in the ACCOUNTS_MASTER table, an existing
customer in the CUSTOMER_MASTER table must be referenced. Thus, the CUSTOMER_ID column –
also created in the ACCOUNTS_MASTER table – is a foreign key. This column is special because its
values are not newly created. Rather, these values must reference existing and identical values in the
primary key column of another table, which is the CUSTOMER_ID column of the
CUSTOMER_MASTER table.
Referential integrity is a standard that means any CUSTOMER_ID value in the CUSTOMER_MASTER
table may not be edited without editing the corresponding value in the ACCOUNTS_MASTER table. For
example, if Andrew Smith’s customer ID is changed in the CUSTOMER_MASTER table, this change also
must be applied to the ACCOUNTS_MASTER table, thus allowing Andrew Smith’s account information
to link to his customer ID.
bbbb What is the need of triggers? Write a note on triggers in SQL.
Triggers can be written for the following purposes −
 Generating some derived column values automatically
 Enforcing referential integrity
 Event logging and storing information on table access
 Auditing
 Synchronous replication of tables
 Imposing security authorizations
 Preventing invalid transactions

SQL Trigger | Student Database

Trigger: A trigger is a stored procedure in database which automatically invokes whenever a special event
in the database occurs. For example, a trigger can be invoked when a row is inserted into a specified table
or when certain table columns are being updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax:
1. create trigger [trigger_name]: Creates or replaces an existing trigger with the trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. on [table_name]: This specifies the name of the table associated with the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for each row
being affected.
6. [trigger_body]: This provides the operation to be performed as trigger is fired
BEFORE and AFTER of Trigger:
BEFORE triggers run the trigger action before the triggering statement is run.
AFTER triggers run the trigger action after the triggering statement is run.
Example:
Given Student Report Database, in which student marks assessment is recorded. In such schema, create a
trigger so that the total and average of specified marks is automatically inserted whenever a record is insert.
(c ) What are the different mechanisms for authorization in SQL.
Authorization

 Authorization is finding out if the person,once identified,is permitted to have the resource.
 Authorization explains that what you can do and is handled through the DBMS unless external
security procedures are available.
 Database management system allows DBA to give different access rights to the users as per their
requirements.
 Basic Authorization we can use any one form or combination of the following basic forms of
authorizations
o Resource authorization:-Authorization to access any system resource. e.g. sharing of
database, printer etc.
o Alternation Authorization:- Authorization to add attributes or delete attributes from
relations
o Drop Authorization:-Authorization to drop a relation.
 Granting of privileges:
o A system privilege is the right to perform a particular action,or to perform an action on any
schema objects of a particular type.
o An authorized user may pass on this authorization to other users.This process is called as
granting of privileges.
o Syntax:

o GRANT <privilege list>


o ON<relation name or view name>
o TO<user/role list>
o Example:
The following grant statement grants user U1,U2 and U3 the select privilege on
Emp_Salary relation:

GRANT select
ON Emp_Salary
TO U1,U2 and U3.
 Revoking of privileges:
o We can reject the privileges given to particular user with help of revoke statement.
o To revoke an authorization,we use the revoke statement.
o Syntax:

o REVOKE <privilege list>


o ON<relation name or view name>
o FROM <user/role list>[restrict/cascade]
 Example:
The revocation of privileges from user or role may cause other user or roles also have to loose that
privileges.This behavior is called cascading of the revoke.

Revoke select
ON Emp_Salary
FROM U1,U2,U3.

Q7. Think and Answer. (200 words)


(a) Consider the insurance database, where the primary keys are underlined.
person (driver-id, name, address)
car (license, model, year)
accident (report-number, date, location)
owns (driver-id, license)
participated (driver-id, car, report-number, damage-amount)
Figure Insurance database

Construct the following SQL queries for this relational database.

a. Find the total number of people who owned cars that were involved in accidents in 1989.
select count (distinct name)
from accident, participated, person
where accident.report-number = participated.report-number
and participated.driver-id = person.driver-id
and date between date ’1989-00-00’ and date ’1989-12-31’

b. Find the number of accidents in which the cars belonging to “John Smith” were involved.
select count (distinct *)
from accident
where exists
(select *
from participated, person
where participated.driver-id = person.driver-id
and person.name = ’John Smith’
and accident.report-number = participated.report-number)

c. Add a new accident to the database; assume any values for required attributes.
insert into accident
values (4007, ’2001-09-01’, ’Berkeley’)

d. Delete the Mazda belonging to “John Smith”.


delete car
where model = ’Mazda’ and license in
(select license
from person p, owns o
where p.name = ’John Smith’ and p.driver-id = o.driver-id)

e. Update the damage amount for the car with license number “AABB2000” in the accident with report
number “AR2197” to $3000.
update participated
set damage-amount = 3000
where report-number = “AR2197” and driver-id in
(select driver-id
from owns
where license = “AABB2000”)

(b) Consider the employee database. Give an expression in SQL for each of the following queries.

employee (employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure Employee database

a. Find the names of all employees who work for First Bank Corporation.
select employee-name
from works
where company-name = ’First Bank Corporation’

b. Find the names and cities of residence of all employees who work for First Bank Corporation.
select e.employee-name, city
from employee e, works w
where w.company-name = ’First Bank Corporation’ and
w.employee-name = e.employee-name

c. Find the company that has the most employees


select company-name
from works
group by company-name
having count (distinct employee-name) >= all
(select count (distinct employee-name)
from works
group by company-name)

d. Find the company that has the smallest payroll.


select company-name
from works
group by company-name
having sum (salary) <= all (select sum (salary)
from works
group by company-name)

e. Find the names, street addresses, and cities of residence for all employees who work for First Bank
Corporation and earn more than 10,000Rs.

select *
from employee
where employee-name in
(select employee-name
from works
where company-name = ’First Bank Corporation’ and salary > 10000)
Q8. My Ideas. (200 words)
(a) Consider the employee database. Give an expression in SQL for each of the following queries.

employee (employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure Employee database

a. Modify the database so that Jones now lives in Newtown.

Update employee
set city = ’Newton’
where person-name = ’Jones

b. Give all employees of First Bank Corporation a 10 percent raise.


update works
set salary = salary * 1.1
where company-name = ’First Bank Corporation’

c. Give all managers of First Bank Corporation a 10 percent raise.


update works
set salary = salary * 1.1
where employee-name in (select manager-name
from manages)
and company-name = ’First Bank Corporation’

d. Give all managers of First Bank Corporation a 10 percent raise unless the salary becomes greater than
$100,000; in such cases, give only a 3 percent raise.

update works T
set T.salary = T.salary * 1.03
where T.employee-name in (select manager-name
from manages)
and T.salary * 1.1 > 100000
and T.company-name = ’First Bank Corporation’
update works T
set T.salary = T.salary * 1.1
where T.employee-name in (select manager-name
from manages)
and T.salary * 1.1 <= 100000
and T.company-name = ’First Bank Corporation
(b) Where the triggers can be used in real life?
A trigger is a special kind of stored procedure that automatically executes when an event occurs in the
database server. DML triggers execute when a user tries to modify data through a data manipulation
language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.
These triggers fire when any valid event is fired, regardless of whether or not any table rows are affected.

e.g. To raise error

CREATE TRIGGER Purchasing.LowCredit ON Purchasing.PurchaseOrderHeader


AFTER INSERT
AS
IF EXISTS (SELECT *
FROM Purchasing.PurchaseOrderHeader p
JOIN inserted AS i
ON p.PurchaseOrderID = i.PurchaseOrderID
JOIN Purchasing.Vendor AS v
ON v.BusinessEntityID = p.VendorID
WHERE v.CreditRating = 5
)
BEGIN
RAISERROR ('A vendor''s credit rating is too low to accept new
purchase orders.', 16, 1);
ROLLBACK TRANSACTION;
RETURN
END;
GO

You might also like