Vous êtes sur la page 1sur 12

HOMEWORK NO:2

CAP301: Database
Management System

SUBMITTED TO: - SUBMITTED BY:-


RITU RAI SURENDRA
MCA 3nd SEM
ROLL NO- D3804A15
REGD NO- 10806601
Q1:-> Consider the insurance database where the primary keys are underlined.
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) add new accident to the database ; assume any values for required attributes
(c) Delete the mazla belonging to “john smith”

Insurance database

Person ( driverid, name, address)

Car (license, model, year)

Accident (reportnumber, date, location)

Owns (driverid, license)

Participated (driverid, car, reportno, damageamount)

ANSWER:

a. This is not the same as the total number of accidents in 1989. We must count
people with several accidents only once.

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.
We assume the driver was “Jones,” although it could be someone else. Also, we
assume “Jones” owns one Toyota. First we must find the license of the given car.
Then the participated and accident relations must be updated in order to both record
the accident and tie it to the given car. We assume values “Berkeley” for location,
’2001-09-01’ for date and date, 4007 for reportnumber and 3000 for damage amount.

insert into accident


values (4007, ’2001-09-01’, ’Berkeley’)

insert into participated

select o.driver-id, c.license, 4007, 3000

from person p, owns o, car c

where p.name = ’Jones’ and p.driver-id = o.driver-id and

o.license = c.license and c.model = ’Toyota’

c:
Since model is not a key of the car relation, we can either assume that only one of
John Smith’s cars is a Mazda, or delete all of John Smith’s Mazdas (the query is the
same). Again assume name is a key for person.

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)

The owns, accident and participated records associated with the Mazda

still exist.

Q2:-> Explain the various formats for inserting the records into the database table?

ANSWER:

METHOD 1: Insert into table_name(col1,col2,col3) values(value1,value2,value3);

Example: insert into emp2(emp_name,emp_salary,emp_dept)


values(‘surendra’,20000,’technical’);
METHOD 2: Insert into table_name(value1,value2,value3);

Example: insert into emp2(‘surendra’,20000,’technical’);

METHOD 3: Insert into table_name(‘col1’,&col2,’col3’);

Example: insert into emp2 (‘&emp_name’,&emp_salary,’&emp_dept’);

Q3:-> Give an SQL schema definition for the employee database. Choose an
appropriate domain for each attribute and an appropriate primary key for each
relation schema.

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 names, street addresses, and cities of residence of all employees who
work for First Bank Corporation and earn more than $10,000.

d. Find all employees in the database who live in the same cities as the
companies for which they work.

e. Find the company that has the smallest payroll.

Employee Database

Employee (employeename, street, city)

Works ( employeename, companyname,salary)

Company (companyname, city)

Manages (employeename, managertime)

ANSWER:

a.

the names of all employees who work for First Bank Corporation.

select employee-name

from works
where company-name = ’First Bank Corporation’

b.

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.

If people may work for several companies, the following solution will

only list those who earn more than $10,000 per annum from “First Bank

Corporation” alone.

select *

from employee

where employee-name in

(select employee-name

from works

where company-name = ’First Bank Corporation’ and salary > 10000)

As in the solution to the previous query, we can use a join to solve this one also.

d.

select e.employee-name

from employee e, works w, company c

where e.employee-name = w.employee-name and e.city = c.city and

w.company -name = c.company –name


e.

select company-name

from works

group by company-name

having sum (salary) <= all (select sum (salary)

from works

group by company-name)

Q4:-> describe the following terms by giving its importance with example.

(a) Null value (b) group by clause (c) joins

(d) Views (e) complex queries (f) where and order by clause

ANSWER:-

(a)

Many argue that it should be allowed to have NULL values in a database. The reality is;
all database systems I know of, allow NULL values for columns. But is it right; or even
better; are NULL values at all allowed at all according to the relational model?

Thus, the relational model states that every attribute has a value for a given occurence
(row/tuple). (Remember, NULL is not a value, it is something unknown).

Let us look at an example: I have a table ACCOUNTS: By the way, since it allows a
column to have a NULL value, it is definetely not a relational table; ref the above
definition. More on that later:
Notice the column AVAILABLE: for account_no 30 it holds a NULL value. How could
that be?

(b) The basic syntax of the Group By statement is:

Group By TableName.Field1, TableName.Field2

The Access group by clause comes after the where clause of a query and before the
optional having clause in the query:

Select TableName.FieldName1, Count(TableName.Field2),Avg(TableName.Field3) From


TableName Where your criteria Group By TableName.FieldName1 Having some criteria

The ending semicolon is a required component of all SQL statements.

Now see the Access group by clause with real column and table names:

Select M_Employees.Emp_Name, Sum(iif(Daily_Absence=true,1,0)), Avg(Daily_Hours)


From M_Employees Where M_Employees.Work_Date>=#01/01/05# and
M_Employees.Work_Date<=#03/31/05# Group By M_Employees.Emp_Name Having
Sum(iif(Daily_Absence=true,1,0))>3;

The above query will select distinct employee names whose daily absences total more
than 3 during the 1st quarter of 2005 and will display the average of the hours worked per
day during the same time period grouped by emp_name.

C:

The JOIN keyword is used in an SQL statement to query data from two or more tables,
based on a relationship between certain columns in these tables.

Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) with a unique value for each
row. Each primary key value must be unique within the table. The purpose is to bind data
together, across tables, without repeating all of the data in every table.
Look at the "Persons" table:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Note that the "P_Id" column is the primary key in the "Persons" table. This means that no
two rows can have the same P_Id. The P_Id distinguishes two persons even if they have
the same name.

d)

A view is simply the representation of a SQL statement that is stored in memory so that it
can easily be re-used. For example, if we frequently issue the following query

SELECT empid FROM emp;

I might well want to make this a view (the reality is that we would probably never create a
view for a statement this simple but we wanted to use an easy example).

To create a view use the create view command as seen in this example

CREATE VIEW view_emp


AS
SELECT empid FROM emp;

This command creates a new view called VIEW_EMP. Note that this command does not
result in anything being actually stored in the database at all except for a data dictionary
entry that defines this view. This means that every time you query this view, Oracle has to
go out and execute the view and query the database data. We can query the view like this:

SELECT * FROM view_emp WHERE empid BETWEEN 500 AND 1000;

And Oracle will transform the query into this:

SELECT * FROM (select empid from emp) WHERE empid BETWEEN


500 AND 1000;

Benefits of Oracle Views

Oracle views offer some compelling benefits. These include:

* Commonality of code being used. Since a view is based on one common set of SQL, this
means that when it is called it’s less likely to require parsing. This is because the basic
underlying SQL that is called is always the same. However, since you can add additional
where clauses when calling a view, you still need to use bind variables. Additional where
clauses without a bind variable can still cause a hard parse!

* Security. Views have long been used to hide the tables that actually contain the data you
are querying. Also, views can be used to restrict the columns that a given user has access
to. Using views for security on less complex databases is probably not a bad thing. As
databases become more complex, this solution becomes harder to scale and other solutions
will be needed.

* Predicate pushing. Oracle supports pushing of predicates into a given view.

f)

The ORDER BY keyword is used to sort the result-set by a specified column.

The ORDER BY keyword sort the records in ascending order by default.

If you want to sort the records in a descending order, you can use the DESC keyword.

ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

Example #1

SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;

This would return all records sorted by the supplier_city field in ascending order.

Part B
Q2 :-> discuss insertion, deletion and modification anomalies. Why are they
considered bad? Illustrate with the help of example?

ANSWER:

Database anomalies are the problems in relations that occur due to redundancy in the
relations. These anomalies affect the process of inserting, deleting and modifying data in
the relations. Some important data may be lost if a elations is updated that contains
database anomalies. It is important to remove these anomalies in order to perform
different processing on the relations without any problem.

Different types of database anomalies are as follows:

Insertion Anomaly: The insertion anomaly occurs when a new record is inserted in the
relation. In this anomaly, the user cannot insert a fact about an entity until he has an
additional fact about another entity.

Deletion Anomaly: The deletion anomaly occurs when a record is deleted from the
relation. In this anomaly, the deletion of facts about an entity automatically deleted the
fact of another entity.

Modification Anomaly: The modification anomaly occurs when the record is updated in
the relation. In this anomaly, the modification in the value of specific attribute requires
modification in all records in which that value occurs.

Q3:-> what is functional dependency? What are the possible sources of information
that defines the functional dependencies that hold among the attribute of relation
schema?

ANSWER:

A functional dependency occurs when one attribute in a relation uniquely determines


another attribute. This can be written A -> B which would be the same as stating "B is
functionally dependent upon A."

Examples:

In a table listing employee characteristics including Social Security Number (SSN) and
name, it can be said that name is functionally dependent upon SSN (or SSN -> name)
because an employee's name can be uniquely determined from their SSN. However, the
reverse statement (name -> SSN) is not true because more than one employee can have the
same name but different SSNs.
Q4:-> What is multivalued dependency? What type of constraint does it specify?
When does it arise?

Multivalued dependency denoted by X Y specified on relation schema R, where X and Y


are both subsets of R, specifies the following constraint on any relation r of R: if two
tuples t1 and t2 exist in r such that t1[X] = t2[X] then t3 and t4 should also exist in r with
the following properties
t3[x] = t4[X] = t1[X] = t2[X]
t3[Y] = t1[Y] and t4[Y] = t2[Y]
t3[Z] = t2[Z] and t4[Z] = t1[Z]
where [Z = (R-(X U Y)) ]

ANSWER:

Q5:-> Define join dependencies and 5th normal form? Why is 5NF also known as
project-join normal form (PJNF)?

ANSWER:

A join dependency is a constraint on the set of legal relations over a database scheme. A
table T is subject to a join dependency if T can always be recreated by joining multiple
tables each having a subset of the attributes of T. If one of the tables in the join has all the
attributes of the table T, the join dependency is called trivial.

The join dependency plays an important role in the Fifth normal form, also known as
project-join normal form, because it can be proven that if you decompose a scheme R in
tables R1 to Rn, the decomposition will be a lossless-join decomposition if you restrict the
legal relations on R to a join dependency on R called * (R1,R2,...Rn).

Another way to describe a join dependency is to say that the set of relationships in the join
dependency is independent of each other.

Given a pizza-chain that models purchases in table Customer = { order-number, customer-


name, pizza-name, delivery-boy }. It is obvious that you can derive the following
relations:

• customer-name depends on order-number

• pizza-name depends on order-number

• delivery-boy depends on order-number

Fifth normal form (5NF), also known as Project-join normal form (PJ/NF) is a level of
database normalization, designed to reduce redundancy in relational databases recording
multi-valued facts by isolating semantically related multiple relationships. A table is said
to be in the 5NF if and only if every join dependency in it is implied by the candidate
keys.

A join dependency *{A, B, … Z} on R is implied by the candidate key(s) of R if and only


if each of A, B, …, Z is a superkey for R

Q6:-> Define boyce-codd normal form. How does it differ from 3NF? Why is it
considered as stronger form of 3NF?

ANSWER:

Boyce-Codd normal form (or BCNF) is a normal form used in database normalization. It
is a slightly stronger version of the third normal form (3NF). A table is in Boyce-Codd
normal form if and only if, for every one of its non-trivial functional dependencies X →
Y, X is a superkey—that is, X is either a candidate key or a superset thereof.

Only in rare cases does a 3NF table not meet the requirements of BCNF. A 3NF table
which does not have multiple overlapping candidate keys is guaranteed to be in BCNF.[4]
Depending on what its functional dependencies are, a 3NF table with two or more
overlapping candidate keys may or may not be in BCNF.

Boyce-codd Normal Form (BCNF) A Table is in BCNF if and only if every determinant
is a candidate key. BCNF is a stronger form of 3NF.

The difference between 3NF and BCNF is that for a Functional dependency A--->B, 3NF
allows this dependency in a table if attribute B is a primary key attribute and attribute A is
not a candidate key, where as BCNF insists that for this dependency to remain in a table,
attribute A must be a candidate key.

Vous aimerez peut-être aussi