Algjebra Relacionale & SQL

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

Data Bits Many to Many Subkeys JoinsQueries Attributes $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 Final DataBit.
Binary Operations in Relational Algebra & SQL
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra & SQL
Revision for Mid 1 ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Functional Dependencies FDs defined over two sets of attributes: X,
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
CM036: Advanced Database Lecture 3 Relational Algebra and SQL.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.
RELATIONAL ALGEBRA Objectives
Chapter 3 Section 3.4 Relational Database Operators
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra - Chapter (7th ed )
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
Relational Algebra – Part 2
From Relational Algebra to SQL CS 157B Enrique Tang.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Relational Algebra Relational Calculus
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
(SQL - Structured Query Language)
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Teknologji Informative
Leksioni nr 2 Data Base.
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
More Relational Algebra
SQL Fundamentals in Three Hours
Joins CSC 240 (Blum).
Degëzimet.
Lecture 3 Relational Algebra and SQL
Syllabus Introduction Website Management Systems
Chapter 4 Relational Algebra
04 SQL & Relational Algebra
Presentation transcript:

Algjebra Relacionale & SQL Ligjerata 2 Algjebra Relacionale & SQL

Algjebra Relacionale Algjebra Relacionale perdoret per te definuar menyren se si relacionet (tabelat) mund te operojne dhe dhe manipulojne me te dhena. Ajo perdor bazat e SQL me bazen e te dhenave relacionale, dhe ilustron operacionet bazike duke perdore DML. Kjo algjeber i paraqet operacionet themelore (duke involvuar tabelat) dhe operatcionet binare (duke perdore disa tabela).

SQL Structured Query Language (SQL) Eshte e Standardizuar nga ANSI Perkrahet nga RDBMS-te moderne Kommandat jane te perfshira ne tri grupe Data Definition Language (DLL) Krijojne tabelat , etj Data Manipulation Language (DML) Modifikoojne te dhenat Data Control Language Kontrollojne se qka duan te perdorin – pranimet dhe nderprerjen e privilegjet

Operacionet Selekcionimi Projektimi

Selection Seleksionimi ose operacioni  selekton rreshtat prej tabeles qe paraqitet me kushtin:    < kusht > < emri i tabeles> Shembull:  course = ‘CM’ Students Students stud# name course 100 Faton PH stud# name course 200 Agim CM 200 Agim CM 300 Bujar CM 300 Bujar CM

Projeksioni Projekcioni ose  eshte operacion i cili selekton listen e kolonave nga tabela.  < lista e kolones > < emriitabeles > shembulli:  stud#, name Students Students stud# name course stud# name 100 Faton PH 100 Faton 200 Agim CM 200 Agim 300 Bujar CM 300 Bujar

Selekcionimi / Projektimi Selekcionimi dhe projektimi zakonisht kombinohen:  stud#, name ( course = ‘CM’ Students) Students stud# name course 100 Faton PH stud# name 200 Agim CM 200 Agim 300 Bujar CM 300 Bujar

Operacionet Binare Cartesian Product Theta Join Inner Join Natural Join Outer Joins Semi Joins

Produkti Cartesian Çdo rresht në relacionin e parë (R) me çdo rresht në lidhje të dytë (S): R X S

Produkti Cartesian - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM Students X Courses = stud# Students.name course course# Courses.name 100 Faton PH PH Pharmacy 100 Faton PH CM Computing 200 Agim CM PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM PH Pharmacy 300 Bujar CM CM Computing

Theta Join Produkti Kartezian me kusht aplikohet: R ⋈ <kushti> S

Theta Join - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM Students ⋈ stud# = 200 Courses stud# Students.name course course# Courses.name 200 Agim CM PH Pharmacy 200 Agim CM CM Computing

Inner Join (Equijoin) brendshem Theta join ku <kushti> eshte (=) per qelesin primar dhe qelesin e jashtem R ⋈ <R.primary_key = S.foreign_key> S

Inner Join - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM Students ⋈ course = course# Courses stud# Students.name course course# Courses.name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM CM Computing

Natural Join Inner join jep te dhena te teperta (ne shembullin e meparshem: course and course#). Për të hequr këtë dyfishim:  < stud#, Students.name, course, Courses.name > (Students ⋈ <course = course#> Courses) Ose R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 Rezultati quhet natural join i Students dhe Courses

Natural Join - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing Bujar CM R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 stud# Students.name course Courses.name 100 Faton PH Pharmacy 200 Agim CM Computing 300 Bujar CM Computing

Outer Joins Inner join + rreshat e tabeles qe nuk e plotesojne <kushtin> Left Outer Join: R <R.primary_key = S.foreign_key> S Të gjitha rreshtave nga R ruhen dhe rreshtat tjere e S janë te mbushur me NULL Right Outer Join: R <R.primary_key = S.foreign_key> S Të gjitha rreshtave nga S ruhen dhe rreshtave qe nuk e plotesojne kushtin në R janë te mbushura me NULL

Left Outer Join - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 400 Petrit EN CH Chemistry Students <course = course#> Courses stud# Students.name course course# Courses.name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing Petrit EN NULL NULL

Right Outer Join - Shembull Students Courses stud# name course course# name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing 400 Petrit EN CH Chemistry Students <course = course#> Courses stud# Students.name course course# Courses.name 100 Faton PH PH Pharmacy 200 Agim CM CM Computing NULL NULL NULL CH Chemistry

Kombinimi i operacioneve dhe JOIN Students Courses stud# name address course course# name 100 Faton Aberdeen PH PH Pharmacy 200 Agim Dundee CM CM Computing 300 Bujar Aberdeen CM Si te paraqiten emrat e studenteve (nga Aberdeen) dhe emrat e kursev ete tyre R1= Students ⋈ <course=course#> Courses R2=  <address=“Aberdeen”> R1 R3=  <Students.name, Course.name> R2 Students.name Courses.name Faton Pharmacy Bujar Computing

Disa Operacione te tjera Unioni Intersekcioni Diferenca

Unioni Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types R S R  S A B a2 b2 a3 b3 A B a1 b1 a2 b2 a3 b3 A B a1 b1 a2 b2

Intersection Takes the set of rows that are common to each relation Participating relations must be compatible R S R  S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a2 b2

Difference Takes the set of rows in the first relation but not the second Participating relations must be compatible R S R - S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a1 b1

Ushtrimi - Employee WorkLoad Project empid name empid* projid* duration projid name E100 Faton E100 P001 17 P001 DB E200 Agim E200 P001 12 P002 Access E300 Bujar E300 P002 15 P003 SQL E400 Petrit Përcaktoni rezultatin e operacioneve për: Natural join ndermjet Employee dhe WorkLoad Left outer join ndermjet Employee dhe WorkLoad Right outer join ndermjet WorkLoad dhe Project

Algjebra Relacionale Te shkruarit e oeracioneve ne SQL

Unary Operations Selection Projection  course = ‘Computing’ Students In SQL: Select * From Students Where course = ‘Computing’; Projection  stud#, name Students In SQL: Select stud#, name From Students; Selection & Projection  stud#, name ( course = ‘Computing’ Students) In SQL: Select stud#, name From students Where course = ‘Computing’;

Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; Theta Join: Students ⋈ <stud# =200> Courses In SQL: Select * From Students, Courses Where stud# = 200;

Binary Operations/Joins Inner Join (Equijoin): Students ⋈ <course=course#> Courses In SQL: Select * From Students, Courses Where course=course#; Natural Join: R1= Students ⋈ <course = course#> Courses R2=  < stud#, Students.name, course, Courses.name > R1 In SQL: Select stud#, Students.name, course, Courses.name From Students, Courses Where course=course#;

Outer Joins Left Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course = course#(+) Right Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course(+) = course#

Combination of Unary and Join Operations R1= Students ⋈ <course=course#> Courses R2=  <address=“Aberdeen”> R1 R3=  <Students.name, Course.name> R2 In SQL: Select Students.name, Courses.name From Students, Courses Where course=course# AND address=“Aberdeen”;

Set Operations Union: R  S Intersection: R  S In SQL: In SQL: Select * From R Union Select * From S; Intersection: R  S In SQL: Select * From R Intersect Select * From S; Difference: R - S In SQL: Select * From R Minus Select * From S;

SQL Operators Between, In, Like, Not

SQL Operators SELECT * FROM Book WHERE catno BETWEEN 200 AND 400; FROM Product WHERE prod_desc BETWEEN ‘C’ AND ‘S’; WHERE catno NOT BETWEEN 200 AND 400;

SQL Operators SELECT Catno FROM Loan WHERE Date-Returned IS NULL; WHERE Date-Returned IS NOT NULL;

SQL Operators SELECT Name FROM Member WHERE memno IN (100, 200, 300, 400); WHERE memno NOT IN (100, 200, 300, 400);

SQL Operators SELECT Name FROM Member WHERE address NOT LIKE ‘%Aberdeen%’; WHERE Name LIKE ‘_ES%’; Note: In MS Access, use * and # instead of % and _

Selecting Distinct Values Student stud# name address 100 Faton Aberdeen 200 Agim Dundee 300 Bujar Aberdeen SELECT Distinct address FROM Student; address Aberdeen Dundee

Exercise Employee(empid, name) Project(projid, name) WorkLoad(empid*, projid*, duration) List the names of employees working on project name ‘Databases’.