1 / 40

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.

iskaggs
Download Presentation

Algjebra Relacionale & SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ligjerata 2 Algjebra Relacionale & SQL

  2. 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).

  3. 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

  4. OperacionetSelekcionimiProjektimi

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

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

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

  8. Operacionet BinareCartesian ProductTheta JoinInner JoinNatural JoinOuter JoinsSemi Joins

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

  10. Produkti Cartesian - Shembull StudentsCourses stud# namecoursecourse# name 100Faton PH PH Pharmacy 200 Agim CM CM Computing 300 Bujar CM Students XCourses = 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

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

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

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

  14. Inner Join - Shembull StudentsCourses stud# namecoursecourse# 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

  15. 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

  16. Natural Join - Shembull StudentsCourses stud# namecoursecourse# name 100Faton 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

  17. 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

  18. Left Outer Join - Shembull StudentsCourses stud# namecoursecourse# name 100Faton 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

  19. Right Outer Join - Shembull StudentsCourses stud# namecoursecourse# name 100Faton 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

  20. Kombinimi i operacioneve dhe JOIN StudentsCourses stud# name address course course# name 100Faton 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

  21. Disa Operacione te tjera Unioni Intersekcioni Diferenca

  22. 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

  23. 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

  24. 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

  25. 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

  26. Algjebra Relacionale Te shkruarit e oeracioneve ne SQL

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

  28. 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;

  29. 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#;

  30. 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#

  31. 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”;

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

  33. SQL Operators Between, In, Like, Not

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

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

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

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

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

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

More Related