Q#1. What is concurrency control? Ans · For referential integrity to hold in a relational...

14
1 | Page Data base old paper by wasim Q#1. What is concurrency control? Ans: Concurrency control (CC) is a process to ensure that data is updated correctly andappropriately when multiple transactions are concurrently executed in DBMS. Concurrencycontrol protocols can be broadly divided into two categories − Lock based protocols Time stamp based protocols Locks are of two kinds Binary Locks Shared/exclusive Q#2. Shortly explain Referential integrity constraints. Ans: For referential integrity to hold in a relational database, any column in a base table that isdeclared a foreign key can contain either a null value, or only values from a parent table'sprimary key or a candidate key. Q#3. What is anomaly in database? Ans: 1. An insertion anomaly is the inability to add data to the database due to absence of otherdata. For example, assume Student_Group is defined so that null values are not allowed. Update, deletion, and insertion anomalies are very undesirable in anydatabase.Anomalies are avoided by the process of normalization. 2. These problems arise from relations that are generated directly from user views are calledanomalies. There are three types of anomalies: update, deletion and insertion anomalies.An update anomaly is a data inconsistency that results from dataredundancy and a partialupdate. 3. Without normalizationmany problems can occur when trying to load an integrated conceptualmodel into the DBMS. Q#4. What is meant by transitive dependency? Ans: In Database Management System, a transitive dependency is a functional dependencywhich holds by virtue of transitivity. A transitive dependency can occur only in a relation thathas three or more attributes. Let A, B, and C designate three distinct attributes (or distinctcollections of attributes) in the relation. Suppose all three of the following conditions hold: 1. A B 2. It is not the case that B A 3. B C Then the functional dependency AC (which follows from 1 and 3 by the axiom of transitivity)is a transitive dependency. Q#5. What is three tier architecture? Ans: A three-tier architecture is a client-server architecture in which the functional processlogic, data access, computer data storage and user interface are developed and maintained asindependent modules on separate platforms.

Transcript of Q#1. What is concurrency control? Ans · For referential integrity to hold in a relational...

1 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Q#1. What is concurrency control?

Ans: Concurrency control (CC) is a process to ensure that data is updated correctly andappropriately when multiple transactions are concurrently executed in DBMS. Concurrencycontrol protocols can be broadly divided into two categories −

Lock based protocols

Time stamp based protocols

Locks are of two kinds –

Binary Locks

Shared/exclusive

Q#2. Shortly explain Referential integrity constraints. Ans: For referential integrity to hold in a relational database, any column in a base table that isdeclared a foreign key can contain either a null value, or only values from a parent table'sprimary key or a candidate key. Q#3. What is anomaly in database? Ans: 1. An insertion anomaly is the inability to add data to the database due to absence of otherdata. For example, assume Student_Group is defined so that null values are not allowed. Update, deletion, and insertion anomalies are very undesirable in anydatabase.Anomalies are avoided by the process of normalization. 2. These problems arise from relations that are generated directly from user views are calledanomalies. There are three types of anomalies: update, deletion and insertion anomalies.An update anomaly is a data inconsistency that results from dataredundancy and a partialupdate. 3. Without normalizationmany problems can occur when trying to load an integrated conceptualmodel into the DBMS. Q#4. What is meant by transitive dependency? Ans: In Database Management System, a transitive dependency is a functional dependencywhich holds by virtue of transitivity. A transitive dependency can occur only in a relation thathas three or more attributes. Let A, B, and C designate three distinct attributes (or distinctcollections of attributes) in the relation. Suppose all three of the following conditions hold:

1. A →B

2. It is not the case that B →A

3. B →C

Then the functional dependency A→C (which follows from 1 and 3 by the axiom of transitivity)is a transitive dependency. Q#5. What is three tier architecture? Ans: A three-tier architecture is a client-server architecture in which the functional processlogic, data access, computer data storage and user interface are developed and maintained asindependent modules on separate platforms.

2 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Q#7. Explain the difference b/w logical and physical data independence. Ans: Physical data independence is the ability to modify the physical scheme withoutmaking itnecessary to rewrite application programs. Such modifications include changing fromunblocked to blocked record storage, or from sequential to random access files. Logical data independence is the ability to modify the conceptual scheme without makingit necessary to rewrite application programs. Such a modification might be adding a field to arecord; an application program’s view hides this change from the program. Q#8. Define integrity constraints. Ans: Integrity Constraints are of four types: 1. Domain integrity Constraint Domain integrity means the definition of a valid set of values for an attribute. You define-data type, - length or size - is null value allowed - is the value unique or not for anattribute. You may also define the default value, the range (values in between) and/orspecific values for the attribute. 2. Entity integrity Constraint The entity integrity constraint states that primary keys can't be null. There must be aproper value in the primary key field. 3. Referential integrity Constraint The referential integrity constraint is specified between two tables and it is used tomaintain the consistency among rows between the two tables.The rules are 1. You can't delete a record from a primary table if matching records exist in a relatedtable. 2. You can't change a primary key value in the primary table if that record has relatedrecords. 3. You can't enter a value in the foreign key field of the related table that doesn't exist inthe primary key of the primary table. 4. However, you can enter a Null value in the foreign key, specifying that the records areunrelated. 4. Foreign key integrity Constraint There are two foreign key integrity constraints: 1. Cascade update related fields 2. Cascade deletes related rows. These constraints affect the referential integrity constraint. Q#9. What is a transaction? Ans: A transaction is a sequence of operations performed as a single logical unit ofwork. A logicalunit of work must exhibit four properties, called the atomicity, consistency, isolation, anddurability (ACID) properties, to qualify as a transaction.Atomicity. Q#10. What`s the purpose of data model? Ans: A database model is a type of data model that determines the logical structure of a databaseand fundamentally determines in which manner data can be stored, organized, andmanipulated. The most popular example of a database model is the relational model, whichuses a table-based format. Q#11. Define Aggregation.

3 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Ans: Data aggregation is any process in which information is gathered and expressed in asummary form, for purposes such as statistical analysis. A common aggregation purpose is toget more information about particular groups based on specific variables such as age,profession, or income. Q#12. What is the difference b/w degree and Cardinality in relation? Ans: 1) Cardinality is the min and max no. of occurrences of one entity that may be related to asingle occurrences of another entity. Degree is the no. of entities that participates in a relationship. 2) Degree: This is the number of entities involved in the relationship and is usually two (a binaryrelationship). Unary relationships also exist, where only 1 entity is involved - a person is married toanother person, or an employee manages other employees.

Cardinality: This specifies the number of each entity that is involved in the relationship. There are 3 types of cardinality for binary and unary relationships: One to one(1:1). e.g. 1 man is married to 1 woman. One to many(1:m). e.g. 1 manager manages many employees, each employee is managed by 1manager. Many to many(m:n). e.g. Each students take many modules, each module is taken by manystudents. Q#13. Differentiate b/w subtype and super type. Ans: Super and sub types come always together like parent and child relationship where theparent/super type (e.g. WORKER) can have different type of child/subtype (e.g.EMPLOYEE,CONTRACTOR etc.) Q#14. Define determinant. Ans: Definition: A determinant in a database table is any attribute that you can use todeterminethe values assigned to other attribute(s) in the same row. Examples: Consider a table with the attributes employee_id, first_name, last_name anddate_of_birth. In this case, the field employee_id determines the remaining three fields. Thename fields do not determine the employee_id because the firm may have more than oneemployee with the same first and/or last name. Similarly, the DOB field does not determine theemployee_id or the name fields because more than one employee may share the samebirthday. Q#15. Define entity cluster. Ans: An entity cluster is a “virtual” entity type used to represent multiple entities andrelationshipsin the ERD. An entity cluster is formed by combining multiple interrelatedentities into a singleabstract entity object. An entity cluster is considered “virtual” or “abstract” in the sense that itis not actually an entity in the final ERD. Instead, it is a temporary entity used to representmultiple entities and relationships, with the purposeof simplifying the ERD and thusenhancing its readability. Examples: 1) OFFERING, which groups the COURSE and CLASS entities and relationships. 2) LOCATION, which groups the ROOM and BUILDING entities and relationships. Q#16. Differentiate b/w Entity integrity VS referential integrity. Ans: The basis of Referential Integrity is foreign keys. A foreign key in one tablereferences aprimary key in another table. The primary key for a table uniquely identifies entities (rows) inthe table. Primary keys are maintained with Entity Integrity, foreign keys with ReferentialIntegrity.

4 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Subjective Part:

2. List the nine major component in a database system environment. Ans:

The nine major components in a database system environment are as follows

:1.Computer aided software engineering tools

2.Repository

3.DMBS

4.Database

5.Application Programs

6.User Interface

7.Data and database administrator

8.System developers

9.End Users

B) what are the different option to avoid locking? Briefly explain all options.

Ans:

3. Explain the difference between user views s conceptual schema, and an internal

schema as different perspective of same database.

5 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Ans: A database schema defines its entities and the relationship among them.

It’s the database designers who design the schema to help programmers

understand the database and make it useful.

Databases are characterized by three-schema architecture

because there are three different ways to look at them. Each schema

is important to different groups in an organization.

1) User view,

2) physical schema.

1) User views conceptual schema User views specify which users are permitted access to what data in a database.

For example, an employee database might contain employee names, addresses, and phone numbers.

3) Physical Schema (internal) The physical schema of a database refers to how data is stored on the computer on which it resides.

6 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Firstly we need to create tables:

CREATE TABLE Room

(

RoomNO INTEGER,

HotelNo VARCHAR,

Type VARCHAR,

Price DECIMAL,

PRIMARY KEY (RoomNO, HotelNo),

FOREIGN KEY(HotelNO) REFERENCES Hotel ON DELETE CASECADE

);

CREATE TABLE Hotel(

HotelNO INTEGER,

Address VARCHAR,

Name VARCHAR,

PRIMARY KEY (HotelNo),

);

7 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Note: Thora detail main hain queries isi main question ki bhi queries hain.

Simple Queries

6.7 List full details of all hotels.

SELECT * FROM Hotel;

6.8 List full details of all hotels in London.

SELECT * FROM Hotel WHERE city = ‘London’;

6.9 List the names and addresses of all guests in London, alphabetically ordered by name.

SELECT guestName, guestAddress FROM Guest WHERE address LIKE ‘%London%’

ORDER BY guestName;

Strictly speaking, this would also find rows with an address like: ‘10 London Avenue, New York’.

6.10 List all double or family rooms with a price below £40.00 per night, in ascending order of price.

SELECT * FROM Room WHERE price < 40 AND type IN (‘D’, ‘F’)

ORDER BY price;

(Note, ASC is the default setting).

8 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

6.11 List the bookings for which no dateTo has been specified.

SELECT * FROM Booking WHERE dateTo IS NULL;

Aggregate Functions

6.12 How many hotels are there?

SELECT COUNT(*) FROM Hotel;

6.13 What is the average price of a room?

SELECT AVG(price) FROM Room;

6.14 What is the total revenue per night from all double rooms?

SELECT SUM(price) FROM Room WHERE type = ‘D’;

6.15 How many different guests have made bookings for August?

SELECT COUNT(DISTINCT guestNo) FROM Booking

WHERE (dateFrom <= DATE’2004-08-01’ AND dateTo >= DATE’2004-08-01’) OR

(dateFrom >= DATE’2004-08-01’ AND dateFrom <= DATE’2004-08-31’);

9 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Subqueries and Joins

6.16 List the price and type of all rooms at the Grosvenor Hotel.

SELECT price, type FROM Room

WHERE hotelNo =

(SELECT hotelNo FROM Hotel

WHERE hotelName = ‘Grosvenor Hotel’);

6.17 List all guests currently staying at the Grosvenor Hotel.

SELECT * FROM Guest

WHERE guestNo =

(SELECT guestNo FROM Booking

WHERE dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE AND

hotelNo =

(SELECT hotelNo FROM Hotel

WHERE hotelName = ‘Grosvenor Hotel’));

6.18 List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the

room, if the room is occupied.

SELECT r.* FROM Room r LEFT JOIN

(SELECT g.guestName, h.hotelNo, b.roomNo FROM Guest g, Booking b, Hotel h

WHERE g.guestNo = b.guestNo AND b.hotelNo = h.hotelNo AND

10 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

hotelName= ‘Grosvenor Hotel’ AND

dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE) AS XXX

ON r.hotelNo = XXX.hotelNo AND r.roomNo = XXX.roomNo;

6.19 What is the total income from bookings for the Grosvenor Hotel today?

SELECT SUM(price) FROM Booking b, Room r, Hotel h

WHERE (dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE) AND

r.hotelNo = h.hotelNo AND r.roomNo = b.roomNo AND

hotelName = ‘Grosvenor Hotel’;

6.20 List the rooms that are currently unoccupied at the Grosvenor Hotel.

SELECT * FROM Room r

WHERE roomNo NOT IN

(SELECT roomNo FROM Booking b, Hotel h

WHERE (dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE) AND

b.hotelNo = h.hotelNo AND hotelName = ‘Grosvenor Hotel’);

6.21 What is the lost income from unoccupied rooms at the Grosvenor Hotel?

SELECT SUM(price) FROM Room r

WHERE roomNo NOT IN

11 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

(SELECT roomNo FROM Booking b, Hotel h

WHERE (dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE) AND

b.hotelNo = h.hotelNo AND hotelName = ‘Grosvenor Hotel’);

Grouping

6.22 List the number of rooms in each hotel.

SELECT hotelNo, COUNT(roomNo) AS count FROM Room

GROUP BY hotelNo;

6.23 List the number of rooms in each hotel in London.

SELECT hotelNo, COUNT(roomNo) AS count FROM Room r, Hotel h

WHERE r.hotelNo = h.hotelNo AND city = ‘London’

GROUP BY hotelNo;

6.24 What is the average number of bookings for each hotel in August?

SELECT AVG(X)

FROM ( SELECT hotelNo, COUNT(hotelNo) AS X

FROM Booking b

WHERE (dateFrom <= DATE’2004-08-01’ AND

dateTo >= DATE’2004-08-01’) OR

12 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

(dateFrom >= DATE’2004-08-01’ AND

dateFrom <= DATE’2004-08-31’)

GROUP BY hotelNo);

Yes - this is legal in SQL-92!

6.25 What is the most commonly booked room type for each hotel in London?

SELECT MAX(X)

FROM ( SELECT type, COUNT(type) AS X

FROM Booking b, Hotel h, Room r

WHERE r.roomNo = b.roomNo AND b.hotelNo = h.hotelNo AND

city = ‘London’

GROUP BY type);

6.26 What is the lost income from unoccupied rooms at each hotel today?

SELECT hotelNo, SUM(price) FROM Room r

WHERE roomNo NOT IN

(SELECT roomNo FROM Booking b, Hotel h

WHERE (dateFrom <= CURRENT_DATE AND

dateTo >= CURRENT_DATE) AND

b.hotelNo = h.hotelNo)

GROUP BY hotelNo;

13 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Populating Tables

6.27 Insert records into each of these tables.

INSERT INTO Hotel

VALUES (‘H111’, ‘Grosvenor Hotel’, ‘London’);

INSERT INTO Room

VALUES (‘1’, ‘H111’, ‘S’, 72.00);

INSERT INTO Guest

VALUES (‘G111’, ‘John Smith’, ‘London’);

INSERT INTO Booking

VALUES (‘H111’, ‘G111’, DATE’2005-01-01’, DATE’2005-01-02’, ‘1’);

6.28 Update the price of all rooms by 5%.

UPDATE Room SET price = price*1.05;

14 | P a g e D a t a b a s e o l d p a p e r b y w a s i m

Q.No:05

• Create an ERD based on the Crow’s Foot model, using the following requirements.

• · An INVOICE is written by a SALESREP. Each sales representative can write many invoices,

but each invoice is written by a single sales representative.

• · The INVOICE is written for a single CUSTOMER. However, each customer may have many

invoices.

• · An INVOICE may include many detail lines (LINE), which describe the products bought by

the customer.

• · The product information is stored in a PRODUCT entity.

• · The product's vendor information is found in a VENDOR entity.