Download presentation
Presentation is loading. Please wait.
Published byRuby Moore Modified over 9 years ago
1
RELATIONAL ALGEBRA Relational Database Handout - 3
2
Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful Query Languages. Query use relational algebra. A query is applied to relation instances, and the result of a query is also a relation instance.
3
Relational Database: Definition It is a set of relations. Each relation is created and data are stored in the relations in the form of tables, this is in rows and columns. – Relation is called table in relational database. – Row of the relation is also called as tuple. – Coulmn of the relation is also called as attribute. Ex : Employee relation/table EnoEnamesalDesigDno E1 ali500tutor10 E2ahmed1000head10 E3saleh700tutor20 E4sami400clerk30
4
Relation rollnosnameaddrGSM 22056salehRuwi9524554 24562aliseeb9786999 23456ahmedmuscat9987667 Relation is collection of rows and columns. Eg: Student relation Note : all rows should be distinct & each cell has only one value records/ rows/ tuples fields Field heading (Attributes / columns)
5
Relation can be shown in the form of schema as given below : Schema specifies name of relation, plus name and type of each column. Eg: Students (sid: number, name: string, age: number, gp: number) Student Relation
6
Domain for field/column Definition: The range of values that can be assigned to a field/column. Examples : Regno - domain is all students numbers in the college. Salary - domain is 400 to 2000 Gender - domain is M or F Cardinality = no. of rows in a relation Degree = no. of columns in a relation
7
Constraint types for field / column values Unique : S ingle different value for each row. Eg: Regno, Idno, Empno, Custno Null : Value is not compulsory. Eg: Office phone no Not Null : Compulsory some value. Eg: Name, course
8
Primary key It is an attribute or a column in a relation which takes unique value for each row to identify the unique row/record in relation. Examples : Regno, Empno, Itemcode, Accno Note : Primary key takes Unique and Not null characters. Some times two or more columns/fields will be combined to make a primary key. Then it is called composite key.
9
Relational Algebra: 5 Basic Operations Selection ( ) Selects a subset of rows from relation (horizontal). Projection ( ) Returns only wanted columns from relation (vertical). Set-difference (–) Returns rows in r1, but not in r2. Union ( ) Returns rows from both r1 and r2. Cross-product / Cartesian Product (x) Allows us to combine two relations. Note : Each operation returns a relation.
10
Selection ( ) ( employee) - it will select rows having salary > 500 sal > 500 enoenamesaldesig IT1ALI500TUTOR BUS2AHMED1000HEAD IT2SABA400CLERK IT3SALEH500TUTOR BUS1BADER650TUTOR enoenamesaldesig BUS2AHMED1000HEAD BUS1BADER650TUTOR
11
Projection ( ) enoename BUS2AHMED BUS1BADER eno, ename (employee) eno, sal (employee) enosal BUS21000 BUS1 650
12
Union ( ) Union compatible rule :- 1.Two relations should have same degree 2. Two relation corresponding same type of column domain. IT-Student (Degree = 4 Cardinality = 3) EnoEnameaddrgsm i1aliruwi9456799 i25bushranizwa9545456 I34salehseeb9967883 Bus-Student (Degree = 4 Cardinality = 3 EnoEnameaddrgsm B45bilal wadi9456839 B2salehseeb9655456 B45aliyajebroo9236883
13
IT-Student U Bus-Student (Degree = 4 Cardinality =6) Union ( ) Union returns those tuples that are either or both of two relations. EnoEnameaddrgsm i1aliruwi9456799 i25bushranizwa9545456 I34aliyaseeb9967883 B45bilal wadi9456839 B2salehseeb9655456 B45aliyajebroo9236883
14
DnoDnameLoc 10BusinessB2 20Information Tech. B1 30FoundationC1 EnoEnamesalDesigDno E1 ali500tutor10 E2ahmed1000head10 E3saleh700tutor20 E4sami400clerk30 CROSS PRODUCT (X) or CARTESIAN Product It takes 2 relations and it returns new relation where every record of first relation is combined with every record of second relation. EMP (Degree = 5 Cardinality =4) DEPT (Degree = 3 Cardinality = 3)
15
EMPDEPT Degree = 5+3 = 8 Cardinality = 4*3 = 12 ) EnoEnamesalDesigDno DnameLoc E1 ali500tutor10 BusinessB2 E1 ali500tutor1020 Information Tech. B1 E1 ali500tutor1030 FoundationC1 E2ahmed1000head10 BusinessB2 E2ahmed1000head1020 Information Tech. B1 E2ahmed1000head1030 FoundationC1 E3saleh700tutor2010 BusinessB2 E3saleh700tutor20 Information Tech. B1 E3saleh700tutor2030 FoundationC1 E4sami400clerk3010 BusinessB2 E4sami400clerk3020 Information Tech. B1 E4sami400clerk30 FoundationC1
16
Set-difference (–) The minus operator returns only records which are only in the first relation. rollnoname 22045saleh 22056arif 22345ahmed 23356majid 23456anwar rollnoname 22045saleh 22056arif 23456anwar rollnoname 22345ahmed 23356majid students Sports students students-sports students
17
Join is Refinement of Cross product. In Join two Relations should have a common attribute and the result relation will contain only record whose values in the common attribute are the same or equal. Eg Emp Dept JOIN (Natural Join) Degree = 5+3 = 8 Cardinality = matching rows EnoEnamesalDesigDno DnameLoc E1 ali500tutor10 BusinessB2 E2ahmed1000head10 BusinessB2 E3saleh700tutor20 Information Tech. B1 E4sami400clerk30 FoundationC1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.