PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore 560100 ) Solution Set - Test-II Sub: Database Management Systems 16MCA23 Date: 04/04/2017 Sem & Section:II Duration: 90 min Name of the Faculty: Manjulaprasad Max marks: 40 Note: 1. Answer any Five questions. 2. Draw neat diagram for all necessary questions. 1. Discuss about different type of Attributes in E-R diagram with example (8marks) Single Each entity has a single atomic value for the attribute. For example, SSN or Gender. Composite The attribute may be composed of several components. For example, Address (Apt#, House#, Street, City, State, ZipCode, Country) or Name (FirstName, MiddleName, LastName). Composition may form a hierarchy where some components are themselves composite. Multi-valued An entity may have multiple values for that attribute. For example, Color of a CAR or PreviousDegrees of a STUDENT. Denoted as {Color} or {PreviousDegrees}. Simple- Attributes that are indivisible. Ex: book_title, Year, Page count Stored attribute- Which are already stored in the database. Ex: Age of a person. Derived attribute- Value is derived from the stored databases. Ex: Day, month and year of dob. Complex attribute- The attributes that can be formed by nesting the composite and multivalued. Ex: {Address_phone({phone, address(no,street, city,pin)})} Notations for every attribute should be drawn. a. Entities are specific objects or things in the mini-world that are represented in the database. For example the EMPLOYEE John Smith, the Research DEPARTMENT, the ProductX PROJECT b. Attributes are properties used to describe an entity. For example an EMPLOYEE entity may have a Name, SSN, Address, Gender,BirthDate Strong Entity- Which has a complete identifier whose values may be used to identify its instances uniquely and which has a key attribute, Primary key. Independent entity Ex: Employee -ssn, Company, Book, etc. Relationship line is single and total participation may or may not exist Weak Entity- Which may not have any key attributes of their own. Does not have primary key.
It will depend on other strong entity- dependent entity. Ex: Emp_dependent, Edition. Relationship line is double and total participation will exist Rrelationship types and degrees in ER diagram. Relationship degree Unary relationship relationship betn entities in a single entity type. Binary relationship- relationship betn two entity type. Ternary relationship- relationship betn three entity type. N-ary relationship- relationship betn n entity type Relationship types are basically classified into Mapping cardinality &Participation constraint. ( Also known as ratio constraints ) Maximum Cardinality One-to-one (1:1) One-to-many (1:N) or Many-to-one (N:1) Many-to-many Minimum Cardinality (also called participation constraint or existence dependency constraints) zero/more (optional participation, partial/ not existence-dependent) one or more (mandatory, total,existence-dependent)
2. A. Update operations on the relations are INSERT a tuple, DELETE a tuple, MODIFY a tuple. Insert is used to add new attribute values to the relation R. Domain constraint can be violated if the attribute value is given that does not appear in he corresponding domain. Key constraint can be violated if a key value in the tuple t already exists. Entity integrity can be violated if the PK of the new tuple is null. Ref integrity can be violated if the value of any FK in refers to tuple that does not exist in the reference reln Employee(empid, empname, empadd, empph, dno) Insert<01, suresh, electronic city, 4> Delete and Update operations Cascading should be taken care. Deleting the data in the reference table without deleting the main table is not allowed Delete the works_on tuple with essn=97878 and pno=10 It just deletes only one tuple. Delete the employee tuple with ssn=97878. can t b cos works_on refers to this table, refrential integrity violations Updating the old value should satisfy the integrity constraints. Changing the reference table value is difficult. Update the salary of the employee tuple with ssn=97878 #accepted Update the dno of employee tuple with ssn=97878 to 98788. # not accepted B. Explain all types of JOINS with example (04 marks) Binary Relational Operations JOIN Operation The sequence of cartesian product followed by select is used quite commonly to identify and select related tuples from two relations, a special operation, called JOIN. It is denoted by a This operation is very important for any relational database with more than a single relation, because it allows us to process relationships among relations. The general form of a join operation on two relations R(A 1, A 2,..., A n ) and S(B 1, B 2,..., B m ) is: R <join condition>s where R and S can be any relations that result from general relational algebra expressions. DEPT_MGR DEPARTMENT MGRSSN=SSN EMPLOYEE EQUIJOIN Operation The most common use of join involves join conditions with equality comparisons only. Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple. The JOIN seen in the previous example was EQUIJOIN. NATURAL JOIN Operation Because one of each pair of attributes with identical values is superfluous, a new operation called natural join denoted by * was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition.
The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations. If this is not the case, a renaming operation is applied first. To apply a natural join on the DNUMBER attributes of DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write: DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS 3. What are the various Relational Model Constraints. Explain all with example. (08 marks) Constraints are conditions that must hold on all valid relation instances. There are three main types of constraints: 1. Key constraints 2. Entity integrity constraints 3. Referential integrity constraints Key Constraints Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance r(r) will have the same value for SK. That is, for any distinct tuples t1 and t2 in r(r), t1[sk] t2[sk]. Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey. Example: The CAR relation schema: CAR(State, Reg#, SerialNo, Make, Model, Year) has two keys Key1 = {State, Reg#}, Key2 = {SerialNo}, which are also superkeys. {SerialNo, Make} is a superkey but not a key. If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined. Entity Integrity Constraint Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(r). This is because primary key values are used to identify the individual tuples. t[pk] null for any tuple t in r(r) Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key. Referential Integrity constraint A constraint involving two relations (the previous constraints involve a single relation). Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation. Tuples in the referencing relation R 1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R 2. A tuple t 1 in R 1 is said to reference a tuple t 2 in R 2 if t 1 [FK] = t 2 [PK]. A referential integrity constraint can be displayed in a relational database schema as a directed arc from R 1.FK to R 2. Semantic Integrity Constraints: - based on application semantics and cannot be expressed by the model per se - E.g., the max. no. of hours per employee for all projects he or she works on is 56 hrs per week - A constraint specification language may have to be used to express these - SQL-99 allows triggers and ASSERTIONS to allow for some of these
4. Explain the steps of ER- to - Relational Mapping in Relational database design with example. (10 marks) ER-to-Relational Mapping Algorithm Step 1: Mapping of Regular Entity Types Step 2: Mapping of Weak Entity Types Step 3: Mapping of Binary 1:1 Relation Types Step 4: Mapping of Binary 1:N Relationship Types. Step 5: Mapping of Binary M:N Relationship Types. Step 6: Mapping of Multivalued attributes. Step 7: Mapping of N-ary Relationship Types. Step 1: Mapping of Regular Entity Types. For each regular (strong) entity type E in the ER schema, create a relation R that includes all the simple attributes of E. Choose one of the key attributes of E as the primary key for R. If the chosen key of E is composite, the set of simple attributes that form it will together form the primary key of R. Example: We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in the relational schema corresponding to the regular entities in the ER diagram. SSN, DNUMBER, and
PNUMBER are the primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT as shown. Step 2: Mapping of Weak Entity Types For each weak entity type W in the ER schema with owner entity type E, create a relation R and include all simple attributes (or simple components of composite attributes) of W as attributes of R. In addition, include as foreign key attributes of R the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s). The primary key of R is the combination of the primary key(s) of the owner(s) and the partial key of the weak entity type W, if any. Example: Create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT. Include the primary key SSN of the EMPLOYEE relation as a foreign key attribute of DEPENDENT (renamed to ESSN). The primary key of the DEPENDENT relation is the combination {ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the partial key of DEPENDENT. Step 3: Mapping of Binary 1:1 Relation Types For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the entity types participating in R. There are three possible approaches: (1) Foreign Key approach: Choose one of the relations-s, say-and include a foreign key in S the primary key of T. It is better to choose an entity type with total participation in R in the role of S. Example: 1:1 relation MANAGES is mapped by choosing the participating entity type DEPARTMENT to serve in the role of S, because its participation in the MANAGES relationship type is total. (2) Merged relation option: An alternate mapping of a 1:1 relationship type is possible by merging the two entity types and the relationship into a single relation. This may be appropriate when both participations are total. (3) Cross-reference or relationship relation option: The third alternative is to set up a third relation R for the purpose of cross-referencing the primary keys of the two relations S and T representing the entity types. Step 4: Mapping of Binary 1:N Relationship Types. For each regular binary 1:N relationship type R, identify the relation S that represent the participating entity type at the N-side of the relationship type. Include as foreign key in S the primary key of the relation T that represents the other entity type participating in R. Include any simple attributes of the 1:N relation type as attributes of S. Example: 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION in the figure. For WORKS_FOR we include the primary key DNUMBER of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it DNO. Step 5: Mapping of Binary M:N Relationship Types. For each regular binary M:N relationship type R, create a new relation S to represent R. Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types; their combination will form the primary key of S. Also include any simple attributes of the M:N relationship type (or simple components of composite attributes) as attributes of S. Example: The M:N relationship type WORKS_ON from the ER diagram is mapped by creating a relation WORKS_ON in the relational database schema. The primary keys of the PROJECT and
EMPLOYEE relations are included as foreign keys in WORKS_ON and renamed PNO and ESSN, respectively. Attribute HOURS in WORKS_ON represents the HOURS attribute of the relation type. The primary key of the WORKS_ON relation is the combination of the foreign key attributes {ESSN, PNO}. Step 6: Mapping of Multivalued attributes. For each multivalued attribute A, create a new relation R. This relation R will include an attribute corresponding to A, plus the primary key attribute K-as a foreign key in R-of the relation that represents the entity type of relationship type that has A as an attribute. The primary key of R is the combination of A and K. If the multivalued attribute is composite, we include its simple components. Example: The relation DEPT_LOCATIONS is created. The attribute DLOCATION represents the multivalued attribute LOCATIONS of DEPARTMENT, while DNUMBER-as foreign key-represents the primary key of the DEPARTMENT relation. The primary key of R is the combination of {DNUMBER, DLOCATION}. Step 7: Mapping of N-ary Relationship Types. For each n-ary relationship type R, where n>2, create a new relationship S to represent R. Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types. Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S. Example: The relationship type SUPPY in the ER below. This can be mapped to the relation SUPPLY shown in the relational schema, whose primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME}
5. Explain Unary and Set operations with example (8 marks) SELECT Operation SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. It is a filter that keeps only those tuples that satisfy a qualifying condition those satisfying the condition are selected while others are discarded. Example: To select the EMPLOYEE tuples whose department number is four or those whose salary is greater than $30,000 the following notation is used: σdno = 4 (EMPLOYEE) σsalary > 30,000 (EMPLOYEE) PROJECT Operation This operation selects certain columns from the table and discards the other columns. The PROJECT creates a vertical partitioning one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns. Example: To list each employee s first and last name and salary, the following is used: π LNAME, FNAME,SALARY (EMPLOYEE) UNION Operation The result of this operation, denoted by R È S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Example: To retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5, we can use the union operation as follows: DEP5_EMPS s DNO=5 (EMPLOYEE) RESULT1 p SSN (DEP5_EMPS) RESULT2(SSN) p SUPERSSN (DEP5_EMPS) RESULT RESULT1 È RESULT2
INTERSECTION OPERATION The result of this operation, denoted by R Ç S, is a relation that includes all tuples that are in both R and S. The two operands must be "type compatible" Example: The result of the intersection operation (figure below) includes only those who are both students and instructors. Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible. Example: The figure shows the names of students who are not instructors, and the names of instructors who are not students. 6) Design an E-R diagram for University atabase system 6 marks
8
7) Design an E-R diagram for Movie database system. Similar to above. 8Consider the Company database. EMPLOYEE( FNAME, LNAME,SSN,BDATE,ADDRESS, GENDER, SALARY,SSN,DNO) DEPARTMENT(DNAME, DNUMBER,MGRSSN,MGRSTARTDATE) DEPT-LOCATIONS(DNUMBER,DLOCATION) PROJECT(PNAME,PNUMBER,PLOCATION,DNUM) WORKS-ON(ESSN,PNO,HOURS) DEPENDENT(ESSN,DEPENDENT-NAME,GENDER,BDATE,RELATIONSHIP) P.T.O Write Relational Algebra queries for the following. 1. A query to retrieve the first name, last name and salary of all employees who work in department number 5. 2. A query to retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5. (use intermediate results) 3. A query to retrieve the names of employees who work on all the projects that smith works_on. 4. A query to retrieve each department number, the number of employees in the department, and their average salary. 5. List the names of all employees with tow or more dependents. Query 1: Πfname, lname, salary (σdno=5 (EMPLOYEE) ) Query 2: Dept_emps σdno=5 (EMPLOYEE) R1 πssn (dept_emps) R2 (ssn) πsuperssn (dept_emps) R R1 U R2 Query 3: smith σfname=smith (EMPLOYEE) smith_pnos πpno(works_on smith)ssn=ssn ssn_pos πpno,essn(works_on) SSNS(ssn) ssn_pos smith_pnos Result Πfname, lname(ssns EMPLOYEE) Query 4: ρr(dno,no.of emp,avg_sal)(dno З count ssn, avg salary (EMPLOYEE)) Query 5: T1(ssn,no.of_dependents) Essn З count dependent_name(dependent) T2 σno.of_dependents 2(T1)
Result Πfname, lname(t2 EMPLOYEE)