0% found this document useful (0 votes)
17 views15 pages

Finals

Part of finals work form database management book of it series.

Uploaded by

Honey James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

Finals

Part of finals work form database management book of it series.

Uploaded by

Honey James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
5.8 Database Languages —— A data sublanguage consists of two parts: * Data Definition Language (DDL) ) Suv +. Data Manipulation Language (DML) wf 9°< eek © oOo ~~ DDL is used to specify database schema. DML is used to read and update the database. These languages are called data sublanguages becase they do-not provide constructs for all computing needs like conditional or iterative statements. Many DBMSs provide the facility to embed the sublanguage in a high-level. programming language like COBOL, Fortran, C++, Java and Visual Basic etc. In this situation, the high-level language is called host language. 5.8.1 Data Definition Language A language that is used to describe and name the entities, attributes, relationships, associated integrity and security constraints is called data definition language. DDL is used to express a set of definitions for specifying database schema. It is used to define or modify a schema. It is not used to manipulate data. alied system catalog, data dictionary or data directory. Data dictionary isa file that contains Data Manipulation Language ‘A language that supports the basic data manipulation operations on data in databases 's called data manipulation language. Data manipulation operations include the following: * Insertion of new data in database * Modification of data in database * Retrieval of data from database ‘+ Deletion of data from database © Procedural DML: It requires a user to specify the reqiiited data and how to get the required data. * Nonprocedural DML: It requires a user\lo specify the required without specifying how to get the data. Nonprocedural DML is usually easier to learn and use than procedural DML. The user «does not specify how to get data. These languages may generate code that is not as efficient as produced by procedural languages. 59 Relational Algebra Relational algebra is a procedural query language that processes one or more relations {o define another relation without changing original relations. The operands as well as the result are relations. The output of one operation can become the input of another operation to ‘reate nested expression in relational algebra. This property is known as closure. 5.9.1 Basic Operations of Relational Algebra There are two categories of operations in relational algebra: 1. Unary Operations ‘The ‘operations which involve only one relation are called) unary operations. The following operations are the unary operations: * Selection * Projection 2. Binary Operations The operations which involve pairs of relations are called binary operations. A binary Sheraton uses two relations as input and produces a new relation as output. The following operations are the binary operations: * Union © Set Difference * Cartesian Product 5. election Operation The Selection operation is a unary operation. The selection operator is sigma O It acts like a filter on a relation. It returns only a certain number of tuples. It selects the tuples using a condition. The condition appears as subscript toO . The resulting relation has the same degree as the original relation. However, the resulting relation may have fewer tuples than the original relation. © © (R) returns only those tuples in R that satisfy condition C A condition C may consist of any combination of comparison or logical operators that operate on the attributes of R. v ” * ‘e Comparison operators: es ste ical operators: a =m se the Truth tables for logical expressions: Examples [Saleen [400 } CS Assceant | [Junaid [220 Econ_[ Lecturer __| [Saleem __[500__[ Fin _[ Associate __] Question Select only those Employees who are in CS department: © ve= cs (EMP) Result [Name] Office [Dept [Rank ___| [Saleem [400 [cs | Assistant | Question Select only those Employees with last name Saleem who are assistant professors: sane seeenr “pai s:rastane (EMP) Result Question Select only those Employees who are Assistant Professors oF in Economics department: Geemarc (EMP) Result [ Name [Office [Dept [Rank] | Saleem [400 [CST Assistant _| Question Select only those Employees who are not in the CS department or Lecturer: OF ere tect pepe cs) (EMP) Result 5.9.3 Projection Operator Projection is also a unary operator. The Projection operator is pi 7U. It limits the attributes returned from the original relation. The resulting relation has the same number of tuples as the original relation. The degree of the resulting relation may be equal to or less than the original relation. The general syntax is: TUstiioues R Where attributes is the list of attributes to be displayed and R is the relation. Examples Assume the same EMP relation used in previous examples. Question Display only the names and departments of the employees: Te same, sep (EMP) Result Combining Selection and Projection The selection and projection operators can be combined to perform both operations, Question Show the names of all employces working in the CS department: Tame ( OF reps -'cs (EMP) ) Result [Name] Question Show name and rank of those Employees who are not in CS department or Lecturers: Mame, Faas tecuser pepe «cs)(EMP) ) Result 5.9.4 Set Operations The Selection and Projection operations extract information from one relation. The set operations are used to extract information from several relations. The relational algebra has set operations of Union, Set difference, Intersection and Cartesian product. These operations are also called binary operations. 5.9.4.1 Union The union operation of two relations combines the tuples of both relations to produce a third relation. If two relations contain identical tuples, the duplicate tuples are eliminated The notation for the union of two relations A and Bis A UNION B. It is donated by U. If A is defined as A = { a, b, c } and set B is defined as B = ( a, ¢, 1, 2 }, then C= A U B will return C = { a,b,c, 1,2). The relations used in the union operation must have same number of attributes. The corresponding attributes must also come from same domain. Such relations are also called union compatible relations. Union are commutative operations: AUB=3UaA Example: A UB Following is an example of union operation. TWO felations A and B are combined together by using union operator. fh Tale Table io 4 [pfs 50) Figure: Union operation — 5.9.4.2 Difference ‘The difference operation works on two relations. It prodiices”a third relation that Contains the tuples that occur in the first relation but nol insecond. The difference operation can be performed on union compatible relations. The order of subtraction is significant. The difference operator is not commutative. [t means: A-B#FB-A Example Following is an example of difference operation. There are two relations A and B. The result of A ~ B and B - A are as follows: Table A Table B B-A Figure: The Difference operation 5.9.4.3 Intersection ‘The intersection operation works on two relations. It producésa third relation that only common tuples. Both relations must be union compatible: It is denoted by N. Example Following is an example of intersection operation. There are two relations A and B, The result of A INTERSECTION B is as follows: Table B ANB Figure: The Intersection operation 5.9.4.4 Product The product works on two relations. It concatenates every tuple in one relation with every tuple in second relation. It is also called cartesian product or cross product. The Product of relation A with m tuples and relation B with n tuples is relation C with ra x n tuples. The product is denoted as A X B. The Product needs not to be union compatible: It means that they can be of different degree. It is commutative and associative. Table A Table B Figure: The Product operation 5.9.4.5 Division The division operator results in columns values in one table for which there are other matching column values corresponding to every row in another table. Figure: Division operator 5.10 Join A join operation combines the product, selection and possibly projection. The join operator combines data from one tuple of a relation with tuples from another or same relation when certain criteria are met. The criteria involve a relationship among the attributes in the join relations. Different types of joins are as follows: + Thetajoin 4 © Equi join wh Natural Join | +) Outer Join | 5.10.1 Theta Join / Theta join is the result of performing a SELECT operation using a comparison operator theta on the product. Theta is denoted by 8. In normal corss product, all rows of one relation are merged with all rows of second relation, In Theta join, only selected rows of first relation are merged with all rows of second relation. It is denoted as follows: RIXs Example Suppose there are two relations FACULTY and COURSE as follows: FACULTY Relation, (O Abdullah Abdullah i Associate Abdullah i [ Associate Aysha Ashfaq _[ Eci 4 Associate Aysha Ashfag Associate Aysha Ashfaq Associate 5.10.2 Equi Join Equi join is a type of join in which tuples are joined on the basis of values of a common attribute between two relations. It is the most commonly used type of join. The common attributes of both relations appear twice in the output. Example Apply the following operation on FACULTY and COURSE relations: FACULTY MM pactrry io ~ course np COURSE [FactD [Name] 1001 a Khalil regret Resignation CS-520, DBMS hor 1003" [Aysha Ashiag re Associate ](CS-430__ [FM 5.10. : Natural Join A eA. at same ds equi join. The dilierence is that the common attributes appear only once, A join removes the duplicate attributes. In most systems, a natural join patares that the attributes have same name to identity attributes to be used in the join. It Example FACULTY DX cao, ry COURSE [fact [Name | Department | Designation | CourseID | Title] 1001 | Usman Khalil_[CS Assistant | CS-520__| DBM 1003 | Aysha Ashfag/| Eco Associate __| CS-430__| FM 5.10.4 Outer Join In outer join, all tuples of left and right relations are part of output. All tuples of left relation which are not matched with right relation are left as Null. Similarly, all tuples of right relation which are not matched with left relation are left as Null There are three forms of outer join depending on the data to be kept: * Left Outer Join: It is denoted by It includes all tuples of left hand relation and includes only the matching tuples from right hand relation. The unmatched rows are represented ie Null. * Right Outef Join: It is denoted byDC. It includes all tuples of right hand relation and includes only the matching tuples from left hand relation. The unmatched rows are represented as/Null. *, Full Outer Join: It is denoted by. It includes all tuples of left and right relations. Example ‘Suppose there are two relations PEOPLE and MENU as follows: PEOPLE MENU age Left Outer Join PEOPLE J] MENU will display the following output: [Name [Age] Food [Day] [Pizza _| [Bear [NUT _| Right Outer Join PEOPLE MCMENU will display the following output: Pizza Burger Chicken Full Outer Join PEOPLE }€MENU will display the following output: [Daud [19] NULL | NULL NULL [NULL |Fish | Thursday NULL [ Rice Friday 5.10.5 Semi Join Semi join takes the natural join of two relations then projects the attributes of firs relation only. After join and matching the common attributes of both relations, only attribute: of first relation are projected. Example There are two relations FACULTY and COURSE as follows: FACULTY Relation Usman Khalil -Tanveer Hussain Fin DBMs | 1 [cs-430_ TF FACULTY P< COURSE will display the following output: [1001 [Usman Khai [CST Assistant _ | [1003 [Aysha Ashfaq [Eco __| Associate Fle First Normal Form A relation is in first normal form (INF) if it does not contain a repeating group. A repeating group is a set of one or more data items that may occur a variable number of times in a tuple. The value in each attribute value should be atomic and every tuple should be unique. Each cell in a relation should contain only one value- An example of un-normalized relation is as follows: Ghafoor Babar Figure: Un-normalized Relation The above relation is un-normalized because it contains repeating groups of three attributes Skill Number, Skill Category and Proficiency Number. All three fields contain more than one value. In order to convert this relation in first normal form, these repeating groups should be removed. The following relation is in first normal form: Primary Key ountant) Skill | Skill, [Number Category Figure: First Normal Form (INF) 3.18 Second Normal Form A relation is in Second Normal Form (2NF) if it is in INF and if all of its nonkey attributes are fully functionally dependent on the whole key. It means that none of non-key altribufes are related to a part of key. The above relation in INF has some attributes which are not depending on the whole primary key. For example, Accountant Name, Accountant Age and group information is determined by Accountant Number and is not dependent on Skill. The following relation can be created in which all attributes are fully dependent on primary key Account Number. Primary Key Group Supervisor Figure: Accountant Table in 2NF Similarly, another relation Skill can be created in which all fields are fully dependent on the primary key as follows: Primary Key wee | omens: | Category [eee Feecng | Figure: Skill Table in 2NF The attribute Proficiency in INF relation was fully dependent on the whole primary key. The Proficiency requires to know the accountant number and skill number. The third relation will be created as follows: Primary Key Figure: Proficiency Table in 2NF ‘There are three relations in second normal form (2NF). The attributes of all relations are fully dependent on the primary keys. 5.20 Third Normal Form A relation is in third normal form if it is in 2NF and if no non-key attribute is dependent on another non-key attribute. It means that all non-key attributes are functionally dependent only on primary key. There should be no transitive dependency in a relation. In order to convert a relation to 3NF: f + Remove all attributes from the 2NF record that depend on another non-key field * Place them into a new relation with the other attribute as the primary key. The Accountant table in 2NF contains some attributes which are depending on non-key attributes. For example, Group City and Group Supervisor are depending on a non-key field Group Number, A new relation can be created as follows: Figure: Accountant Table in 3NF The second table created form the Accountant table in INF is as follows: Primary Key ‘Group Group ium [Supervisor| [se [sD | eater] [TERE Giatoor | Figure: Group table in 3NF Both Accountant table and Group table contain the attribute Group Number. This ‘ribute is used to join both tables. The Skill table in 2NF contains no attribute, whichis depending on a non-key attribute. ‘s already in third normal form and will be used without any further change. Primary Key v Figure: Skill Table in 3NF The Proficiency table in 2NF also contains no attribute which is depending on non-key ribute. It is already in third normal form and will be used without any further change. Primary Key Figure: Proficiency Table in 3NF

You might also like