Chapterبەش 5 · Lectureوانە 5.1
Relational Algebra and Relational Calculusجەبری پەیوەندیدار و ژمێرکاریی پەیوەندیدار
The two formal languages under SQL. Relational algebra: selection, projection, union, intersection, difference, Cartesian product, rename, joins (theta, equi, natural, outer) and division. Relational calculus: tuple and domain calculus, quantifiers and safe queries. And how every operator is written in SQL.
ئەو دوو زمانە فەرمییەی لە ژێر SQLـەوەن. : ، ، ، ، ، ، ، (theta، یەکسانی، سروشتی، دەرەکی) و . : و ، و سەلامەتەکان. هەروەها چۆنیەتی نووسینی هەموو بە SQL.
What you will learnچی فێر دەبیت
- Explain what a query language is, and how procedural relational algebra differs from declarative relational calculus.
- Apply σ, π, ∪, ∩, −, ×, ρ, ⋈ and ÷ to small tables and work out the result and its schema.
- Choose the right join — theta, equijoin, natural, left/right/full outer — for a question.
- Read and write simple tuple and domain relational calculus queries, and recognise unsafe ones.
- Translate relational-algebra expressions into SQL and back.
- ڕوونی بکەیتەوە چییە و ڕێکاری چۆن لە ڕوونکەرەوە جیاوازە.
- σ، π، ∪، ∩، −، ×، ρ، ⋈ و ÷ لەسەر بچووک جێبەجێ بکەیت و ئەنجامەکە و دەربهێنیت.
- گونجاو بۆ پرسیارێک هەڵبژێریت — theta، ، ، چەپ/ڕاست/تەواو.
- سادەی و بخوێنیتەوە و بنووسیت و ناسەلامەتەکان بناسیتەوە.
- دەربڕینەکانی بکەیت بە SQL و بە پێچەوانەوە.
Asking a database questionsپرسیارکردن لە بنکەدراوە
A query language is a language for asking a database for data and for changing it. SQL is the one you use every day. A query language is not a general programming language. It rests on formal logic, which lets the DBMS rewrite a query into a faster one that gives the same answer. It is made for reaching large amounts of data easily and quickly, not for heavy calculations. The two formal languages of this lesson are deliberately not 'Turing complete': they cannot express every possible program. (Modern SQL has extra features, such as recursive queries written with WITH RECURSIVE, that go beyond them.)
زمانێکە بۆ داواکردنی لە و گۆڕینی. SQL ئەو زمانەیە کە هەموو ڕۆژێک بەکاری دەهێنیت. گشتی نییە. لەسەر لۆجیکی فەرمی دامەزراوە، بۆیە دەتوانێت بگۆڕێت بۆ یەکێکی خێراتر کە هەمان وەڵام دەدات. بۆ گەیشتن بە بڕێکی زۆری بە ئاسانی و خێرایی دروست کراوە، نەک بۆ ژمێرکاریی قورس. دوو زمانە فەرمییەکەی ئەم وانەیە بە ئەنقەست Turing complete نین: ناتوانن هەموو بەرنامەیەکی شیاو دەرببڕن. (SQLـی ئەمڕۆ تایبەتمەندیی زیاتری هەیە، وەک WITH RECURSIVE، کە لەو دوو زمانە تێدەپەڕن.)
Under SQL lie two formal languages. Relational algebra is procedural (operational): a query is a list of steps that says how to get the answer, and the DBMS uses it to describe its execution plans. Relational calculus is declarative: a query describes what the answer must look like and leaves the 'how' to the system. History reflects both: SEQUEL (IBM, 1974), the first version of SQL, borrowed many ideas from the algebra, while QUEL, the language of the Ingres system, was built on tuple relational calculus. Today's SQL mixes both styles.
لە ژێر SQLـەوە دوو زمانی فەرمی هەن. ڕێکارییە (): لیستێکی هەنگاوەکانە کە دەڵێت چۆن وەڵامەکە وەربگرین و بۆ وەسفکردنی پلانەکانی جێبەجێکردنی بەکاری دەهێنێت. ڕوونکەرەوەیە (): وەسفی دەکات وەڵامەکە دەبێت چی بێت و «چۆن»ـەکە بۆ سیستەمەکە جێ دەهێڵێت. مێژوو هەردووکیانی تێدایە: SEQUEL (IBM، 1974)، یەکەم وەشانی SQL، زۆر بیرۆکەی لە جەبرەکەوە وەرگرت، بەڵام QUEL، زمانی سیستەمی Ingres، لەسەر دروست کرا. SQLـی ئەمڕۆ هەردوو شێوازەکە تێکەڵ دەکات.
At a restaurant in Erbil you can talk to the cook in two ways. You can give a recipe: 'fry the onions, add the meat, then the rice' — that is relational algebra, a list of steps. Or you can describe the dish: 'I want rice with meat and fried onions on top' — that is relational calculus, and the cook decides the steps.
لە چێشتخانەیەک لە هەولێر دەتوانیت بە دوو شێوە قسە لەگەڵ چێشتلێنەرەکە بکەیت. دەتوانیت ڕێچکەیەکی پێ بدەیت: «پیازەکە سوور بکەرەوە، گۆشتەکە زیاد بکە، پاشان برنجەکە» — ئەمە ، لیستێک لە هەنگاوەکان. یان دەتوانیت وەسفی خواردنەکە بکەیت: «برنجم دەوێت لەگەڵ گۆشت و پیازی سوورکراوە لەسەری» — ئەمە و چێشتلێنەرەکە هەنگاوەکان دیاری دەکات.
Where the analogy stops workingسنووری ئەم نموونەیە
A cook may misunderstand a description; a DBMS never does. Both languages are exact, and they can ask exactly the same questions.
چێشتلێنەر لەوانەیە وەسفێک بە هەڵە تێبگات؛ هەرگیز وا ناکات. هەردوو زمانەکە وردن و ڕێک دەتوانن هەمان پرسیارەکان بکەن.
A table is a mathematical relationخشتە پەیوەندییەکی بیرکارییە
The Cartesian product of two sets is the set of all ordered pairs that take one element from each set. A tea house sells drinks D = {tea, coffee} and sweets K = {kleicha, baklava}. Then D × K = {(tea, kleicha), (tea, baklava), (coffee, kleicha), (coffee, baklava)}: every drink with every sweet. The order counts: K × D = {(kleicha, tea), (kleicha, coffee), (baklava, tea), (baklava, coffee)} has the same items in each pair, but the other way round.
ـی دوو ، هەموو ئەو جووتە ڕیزکراوانەیە کە لە هەر یەک توخم وەردەگرن. چایخانەیەک خواردنەوەی D = {tea, coffee} و شیرینیی K = {kleicha, baklava} دەفرۆشێت. ئەوکات D × K = {(tea, kleicha), (tea, baklava), (coffee, kleicha), (coffee, baklava)}: هەموو خواردنەوەیەک لەگەڵ هەموو شیرینییەک. ڕیزبەندی گرنگە: K × D = {(kleicha, tea), (kleicha, coffee), (baklava, tea), (baklava, coffee)} لە هەر جووتێکدا هەمان شتەکانی تێدایە، بەڵام بە پێچەوانەوە.
The domain of an attribute is the set of values it may hold: for credits, the whole numbers 1 to 6; for city, the city names. A relation is a subset of the Cartesian product of its attributes' domains: out of all possible combinations, the table keeps the ones that are true. That is why a table is called a relation, and why algebra can work on it.
ی ئەو نرخانەیە کە دەتوانێت هەڵیانبگرێت: بۆ credits، ژمارە تەواوەکانی 1 تا 6؛ بۆ city، ناوی شارەکان. relation ژێرکۆمەڵەیەکە لە : لە نێوان هەموو تێکەڵە شیاوەکاندا، ئەوانە دەهێڵێتەوە کە ڕاستن. بۆیە بە دەوترێت relation و بۆیە جەبر دەتوانێت کاری لەسەر بکات.
| Everyday database wordوشەی ڕۆژانەی | Relational model (mathematics)مۆدێلی پەیوەندیدار (بیرکاری) | Other nameناوی تر |
|---|---|---|
| table | relationrelation | — |
| row | record | |
| column | attribute | fieldfield |
- The order of the rows does not matter: a relation is a set (all its rules: three properties of a relation).
- The order of the columns matters only when we refer to them by position (column 1, column 2 …), not when we use their names.
- An attribute's domain usually includes NULL, which means 'unknown'.
- ڕیزبەندیی گرنگ نییە: relation (هەموو ڕێساکانی: سێ تایبەتمەندییەکەی relation).
- ڕیزبەندیی تەنها کاتێک گرنگە کە بە شوێن ئاماژەیان پێ بکەین ( 1، 2 …)، نەک کاتێک ناوەکانیان بەکار دەهێنین.
- زۆرجار NULLیش لەخۆ دەگرێت، کە واتە «نەزانراو».
What makes an algebra 'relational'چی جەبرێک دەکاتە «پەیوەندیدار»
A formal query language whose operations, such as , , and , take (relations) as input and give a new table as output. It describes step by step how to get a result, and is built on it.
زمانێکی فەرمیی پرسە کە کارەکانی، وەک ، ، و ، (relation) وەک نێوخستە وەردەگرن و خشتەیەکی نوێ وەک دەرخستە دەدەن. هەنگاو بە هەنگاو ڕوون دەکاتەوە چۆن ئەنجامێک بەدەست بهێنرێت و لەسەر ئەم بنەمایە دامەزراوە.
Every algebra has two ingredients: the things it works on, called operands, and the ways of combining them, called operators. In school algebra the operands are numbers and + is an operator: 3 + 4 combines 3 and 4 into a new number, 7. In relational algebra the operands are relations (tables), and every operator gives back a relation. Because every result is again a table, operators can be nested and chained into bigger queries. This is called the closure property.
هەموو جەبرێک دوو پێکهاتەی هەیە: ، واتە ئەو شتانەی کاریان لەسەر دەکرێت، لەگەڵ ، واتە ڕێگاکانی تێکەڵکردنیان. لە جەبری قوتابخانەدا ژمارەن و + : 3 + 4 ژمارەکانی 3 و 4 دەکاتە ژمارەیەکی نوێ، کە 7ـە. لە relationـن () و هەموو relationێک دەگەڕێنێتەوە. چونکە هەموو ئەنجامێک دیسان ، دەتوانرێن یەک لەناو یەک دابنرێن و پێکەوە ببەسترێن بۆ گەورەتر. ئەمە پێی دەوترێت .
- A query works on any instance of its input tables: today's rows or next year's.
- The schema of the result is fixed by the query itself, not by the data.
- Columns can be named (
name,city) or numbered by position ($2,$4). Numbers keep formal definitions short; names make queries easy to read. Many SQL systems still accept positions, for exampleORDER BY 2, but the SQL standard has dropped them and MySQL has deprecated them, so use names. - Relational algebra treats relations as sets, so a result never contains the same row twice.
- لەسەر هەر تێچووەکانی کار دەکات: ئەمڕۆ یان ساڵی داهاتوو.
- ئەنجامەکە خودی دیاری دەکات، نەک .
- دەکرێت بە ناو (
name،city) یان بە ژمارەی شوێن ($2،$4) دیاری بکرێن. ژمارە پێناسە فەرمییەکان کورت دەکاتەوە؛ ناو ئاسان دەکات بۆ خوێندنەوە. زۆر سیستەمی SQL هێشتا ژمارەی شوێن وەردەگرن، بۆ نموونەORDER BY 2، بەڵام ستانداردی SQL ئەمەی لابردووە و MySQL ڕایگەیاندووە کە لە داهاتوودا لای دەبات، بۆیە ناو بەکاربهێنە. - relationـەکان وەک سەیر دەکات، بۆیە ئەنجامێک هەرگیز هەمان دوو جار تێدا نییە.
| Symbolهێما | Operationکار | What it doesچی دەکات | Kindجۆر |
|---|---|---|---|
σ | Selection | keeps the rows that meet a conditionئەو دەهێڵێتەوە کە جێبەجێ دەکەن | basicبنەڕەتی |
π | Projection | keeps the listed columns دیاریکراوەکان دەهێڵێتەوە | basicبنەڕەتی |
× | Cartesian product | pairs every row of one table with every row of anotherهەر لەگەڵ هەموو تر جووت دەکات | basicبنەڕەتی |
∪ | Union | rows in the first table OR the second OR both یەکەم یان دووەم یان هەردووکیان | basicبنەڕەتی |
− | Set difference | rows in the first table but not in the second یەکەم کە لە دووەمدا نین | basicبنەڕەتی |
∩ | Intersection | rows in both tablesئەو لە هەردوو هەن | derivedداتاشراو |
⋈ | Join | a product followed by a selection و پاشان | derivedداتاشراو |
÷ | Division | answers 'for all' questionsوەڵامی پرسیارەکانی «بۆ هەموو» دەداتەوە | derivedداتاشراو |
ρ | Rename | gives a table or its columns new namesناوی نوێ دەداتە یان | helperیاریدەدەر |
Codd took the set operators of mathematics (product, union, intersection, difference) and added select, project, join and divide. Later authors extended the algebra with the outer join and the outer union (a union of two tables with different columns, the gaps filled with NULL).
کۆد بیرکاری (، ، ، ) وەرگرت و ، ، و بۆ زیاد کرد. نووسەرانی دواتر جەبرەکەیان بە و یەکگرتنی دەرەکی (outer union) فراوان کرد (یەکگرتنی دوو بە جیاوازەوە، کە بۆشاییەکان بە NULL پڕ دەکرێنەوە).
The example tablesخشتە نموونەییەکان
All the examples use this small copy of the university database. Hana (105) has not enrolled in anything yet, and nobody takes NW201 — you will see why that matters for outer joins.
هەموو نموونەکان ئەم کۆپییە بچووکەی زانکۆ بەکار دەهێنن. هانا (105) هێشتا لە هیچ وانەیەکدا تۆمار نەکراوە و کەس NW201 ناخوێنێت — دەبینیت بۆچی ئەمە بۆ گرنگە.
student_no | name | dept | city |
|---|---|---|---|
| 101 | Aram | CS | Sulaimani |
| 102 | Shilan | IT | Erbil |
| 103 | Karwan | CS | Duhok |
| 104 | Dilan | SE | Sulaimani |
| 105 | Hana | IT | Kirkuk |
course_code | title | credits |
|---|---|---|
| DB101 | Database Fundamentals | 3 |
| WP101 | Web Programming | 3 |
| AI301 | Artificial Intelligence | 4 |
| NW201 | Computer Networks | 3 |
student_no | course_code | grade |
|---|---|---|
| 101 | DB101 | A |
| 101 | WP101 | B |
| 102 | DB101 | B |
| 103 | DB101 | C |
| 103 | WP101 | A |
| 103 | AI301 | B |
| 104 | WP101 | A |
Selection σ: choose rowsهەڵبژاردن σ: هەڵبژاردنی ڕیز
The relational-algebra operation σ that keeps only the rows of a table that meet a . The result has the same columns. In SQL it is the .
کاری σ لە کە تەنها ئەو ڕیزانەی خشتەیەک دەهێڵێتەوە کە جێبەجێ دەکەن. ئەنجامەکە هەمان ستوونەکانی هەیە. لە SQLـدا ئەم کارە دەکات.
Selection σ condition (R) keeps only the rows of R for which the condition is true. The condition is built from comparisons (=, ≠, <, >, ≤, ≥) joined with AND (∧), OR (∨) and NOT (¬). The result has the same columns as R.
σ condition (R) تەنها ئەو R دەهێڵێتەوە کە بۆیان ڕاستە. لە بەراوردەکان (=، ≠، <، >، ≤، ≥) دروست دەبێت کە بە AND (∧)، OR (∨) و NOT (¬) پێکەوە دەبەسترێن. ئەنجامەکە هەمان Rـی هەیە.
student_no | name | dept | city |
|---|---|---|---|
| 101 | Aram | CS | Sulaimani |
| 104 | Dilan | SE | Sulaimani |
With ∧ both parts must be true: σ dept = 'CS' ∧ city = 'Duhok' (Student) gives only Karwan (103). With ∨ one part is enough: σ dept = 'IT' ∨ city = 'Duhok' (Student) gives Shilan, Karwan and Hana.
بە ∧ دەبێت هەردوو بەشەکە ڕاست بن: σ dept = 'CS' ∧ city = 'Duhok' (Student) تەنها کاروان (103) دەدات. بە ∨ یەک بەش بەسە: σ dept = 'IT' ∨ city = 'Duhok' (Student) شیلان، کاروان و هانا دەدات.
Projection π: choose columnsدەرهێنانی ستوون π: هەڵبژاردنی ستوون
The relational-algebra operation π that keeps only the chosen columns of a table and removes duplicate rows from the result. In SQL it is the list of columns after SELECT.
کاری π لە کە تەنها ستوونە هەڵبژێردراوەکانی خشتەیەک دەهێڵێتەوە و ڕیزە دووبارەکان لە ئەنجامەکە لادەبات. لە SQLـدا لیستی ستوونەکانی دوای SELECTـە.
Projection π column-list (R) keeps only the listed columns, in that order. The schema of the result is exactly that list. Because relational algebra works with sets, duplicate rows are removed: π dept (Student) gives three rows — CS, IT, SE — although five students have a department.
π column-list (R) تەنها دیاریکراوەکان بەو ڕیزبەندییە دەهێڵێتەوە. ئەنجامەکە ڕێک ئەو لیستەیە. چونکە لەگەڵ کار دەکات، دووبارەکان لادەبرێن: π dept (Student) سێ دەدات — CS، IT، SE — هەرچەندە پێنج قوتابی بەشیان هەیە.
Combining operatorsپێکەوەبەستنی کارپێکەرەکان
Because every result is a table, operators can be put inside each other. 'The names of the students from Sulaimani' is a selection followed by a projection: first choose the rows, then the columns. Read the expression from the inside out:
چونکە هەموو ئەنجامێک ، دەتوانرێن یەک لەناو یەک دابنرێن. «ناوی ئەو قوتابیانەی خەڵکی سلێمانین» و پاشان : سەرەتا هەڵبژێرە، پاشان . دەربڕینەکە لە ناوەوە بۆ دەرەوە بخوێنەوە:
Union, intersection and differenceیەکگرتن، یەکتربڕین و جیاوازیی کۆمەڵە
Two student clubs keep member lists with the same columns: Robotics(student_no, name) = {101 Aram, 103 Karwan, 104 Dilan} and Debate(student_no, name) = {102 Shilan, 103 Karwan}. Because the tables are relations (sets), the three set operations of mathematics (union, intersection and difference) work on them:
دوو یانەی قوتابیان لیستی ئەندامەکانیان بە هەمان هەڵدەگرن: Robotics(student_no, name) = {101 Aram, 103 Karwan, 104 Dilan} و Debate(student_no, name) = {102 Shilan, 103 Karwan}. چونکە relationن ()، سێ بیرکاری (یەکگرتن، یەکتربڕین و جیاوازیی کۆمەڵە) لەسەریان کار دەکەن:
These operations need union-compatible tables: the same number of columns, and matching columns with compatible domains (types). Robotics ∪ Course makes no sense — a student number cannot be compared with a course code. And difference is not symmetric: R − S is usually not S − R.
ئەم کردانە پێویستیان بە هەیە: هەمان ژمارەی و بەرامبەر بە (جۆری) گونجاو. Robotics ∪ Course هیچ واتایەکی نییە — ژمارەی قوتابی بە کۆدی وانە بەراورد ناکرێت. هەروەها هاوسەنگ نییە: R − S زۆرجار وەک S − R نییە.
Cartesian product ×لێکدانی دیکارتی ×
The relational-algebra operation × that pairs every row of one table with every row of another. If the tables have 4 and 5 rows, the result has 20.
کاری × لە کە هەر ڕیزێکی خشتەیەک لەگەڵ هەموو ڕیزەکانی خشتەیەکی تر جووت دەکات. ئەگەر خشتەکان 4 و 5 ڕیزیان هەبێت، ئەنجامەکە 20 ڕیزی دەبێت.
The Cartesian (cross) product R × S pairs every row of R with every row of S. The result has all the columns of both tables, and its number of rows is rows(R) × rows(S). Debate × Course has 2 × 4 = 8 rows:
R × S هەموو R لەگەڵ هەموو S جووت دەکات. ئەنجامەکە هەموو هەردوو هەیە و ژمارەی دەکاتە ژمارەی R × ژمارەی S. Debate × Course 2 × 4 = 8 هەیە:
student_no | name | course_code | title | credits |
|---|---|---|---|---|
| 102 | Shilan | DB101 | Database Fundamentals | 3 |
| 102 | Shilan | WP101 | Web Programming | 3 |
| 102 | Shilan | AI301 | Artificial Intelligence | 4 |
| 102 | Shilan | NW201 | Computer Networks | 3 |
| 103 | Karwan | DB101 | Database Fundamentals | 3 |
| 103 | Karwan | WP101 | Web Programming | 3 |
| 103 | Karwan | AI301 | Artificial Intelligence | 4 |
| 103 | Karwan | NW201 | Computer Networks | 3 |
On its own the product is rarely useful: most of these pairs mean nothing. Its value is as a building block for joins. It also causes name clashes: Student × Enrolled (5 × 7 = 35 rows) has two columns called student_no. The result keeps the attribute names where it can and needs a way to tell the two apart — the rename operator, next.
بە تەنها کەم بەسوودە: زۆربەی ئەم جووتانە هیچ واتایەکیان نییە. بەهاکەی وەک بەردی بناغەیە بۆ . هەروەها دەبێتە هۆی لێکچوونی ناو: Student × Enrolled (5 × 7 = 35 ) دوو بە ناوی student_no هەیە. ئەنجامەکە تا بتوانێت ناوی دەهێڵێتەوە و پێویستی بە ڕێگایەک هەیە بۆ جیاکردنەوەی ئەو دووانە — ، لە پەڕەی داهاتوودا.
Rename ρناونانەوە ρ
The relational-algebra operation ρ that gives a new name to a result table or to its columns, for example when two columns would otherwise have the same name.
کاری ρ لە کە ناوێکی نوێ دەداتە خشتەیەکی ئەنجام یان ستوونەکانی، بۆ نموونە کاتێک دوو ستوون ئەگینا هەمان ناویان دەبێت.
Rename gives a result, or its columns, new names. ρ S1 (Student) is the Student table called S1. ρ Club(no, member) (Robotics) renames both the table and its columns. Rename is needed when a table is combined with itself. Which pairs of students come from the same city?
ناوی نوێ دەداتە ئەنجامێک یان . ρ S1 (Student) Studentـە بە ناوی S1. ρ Club(no, member) (Robotics) ناوی و دەگۆڕێت. کاتێک پێویستە کە لەگەڵ خۆیدا تێکەڵ بکرێت. کام جووتە قوتابی خەڵکی هەمان شارن؟
π S1.name, S2.name (
σ S1.city = S2.city ∧ S1.student_no < S2.student_no (
ρ S1 (Student) × ρ S2 (Student) ) )S1.name | S2.name Aram | Dilan
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Two copies of Student with different names, so their columns can be told apart.
دوو کۆپیی Student بە دوو ناوی جیاواز، بۆ ئەوەی لێک جیا بکرێنەوە.
- Lineدێڕی 2
Same city;
<stops a student being paired with himself and stops each pair appearing twice.هەمان شار؛
<ڕێگە دەگرێت لەوەی قوتابییەک لەگەڵ خۆیدا جووت بێت و هەر جووتێک دوو جار دەربکەوێت.
Joins ⋈لێکبەستنەکان ⋈
The relational-algebra operation ⋈ that combines the rows of two tables that belong together, for example a student with his or her courses. It is a followed by a .
کاری ⋈ لە کە ئەو ڕیزانەی دوو خشتە کە پێکەوە پەیوەندیدارن تێکەڵ دەکات، بۆ نموونە قوتابییەک لەگەڵ وانەکانی. بریتییە لە و پاشان .
A join combines rows of two tables that belong together. Every join is a Cartesian product followed by a selection, written as one operation: the product makes all the pairs, the selection keeps the meaningful ones. There are three kinds, from the most general to the most automatic: the condition (theta) join, the equijoin and the natural join. Outer joins add the rows that found no partner.
دوو کە پێکەوە پەیوەندییان هەیە تێکەڵ دەکات. هەموو و پاشان ، کە وەک یەک کار دەنووسرێت: هەموو جووتەکان دروست دەکات، ئەوانە دەهێڵێتەوە کە واتایان هەیە. سێ جۆر هەن، لە گشتیترینەوە بۆ خۆکارترین: (theta)، و . ئەو زیاد دەکەن کە هاوتایان نەدۆزیوەتەوە.
Joins ⋈لێکبەستنەکان ⋈
Condition (theta) joinلێکبەستنی مەرجدار (theta)
A with any , such as gpa > 3.0, written ⋈c. It equals σc(A × B).
جۆرێکە لە بە هەر ، وەک gpa > 3.0، کە بە ⋈c دەنووسرێت. یەکسانە بە σc(A × B).
R ⋈θ S = σθ (R × S), where θ (theta) is any condition. The condition need not be an equality. Which course has more credits than another? ρ C1 (Course) ⋈ C1.credits < C2.credits ρ C2 (Course):
R ⋈θ S = σθ (R × S)، کە θ (theta) هەر . پێویست ناکات یەکسانی بێت. کام وانە یەکەی زیاترە لە وانەیەکی تر؟ ρ C1 (Course) ⋈ C1.credits < C2.credits ρ C2 (Course):
C1.course_code | C1.credits | C2.course_code | C2.credits |
|---|---|---|---|
| DB101 | 3 | AI301 | 4 |
| WP101 | 3 | AI301 | 4 |
| NW201 | 3 | AI301 | 4 |
Joins ⋈لێکبەستنەکان ⋈
Equijoinلێکبەستنی یەکسانی
A whose condition only checks that columns are equal, for example S1.sid = C1.sid.
جۆرێکە لە کە مەرجەکەی تەنها یەکسانیی ستوونەکان دەپشکنێت، بۆ نموونە S1.sid = C1.sid.
An equijoin is a condition join whose condition only compares columns with =. Student ⋈ Student.student_no = Enrolled.student_no Enrolled gives 7 rows: each enrolment with its student. The result keeps both columns Student.student_no and Enrolled.student_no — they simply hold equal values.
کە تەنها بە = بەراورد دەکات. Student ⋈ Student.student_no = Enrolled.student_no Enrolled 7 دەدات: هەر تۆمارکردنێک لەگەڵ قوتابییەکەی. ئەنجامەکە هەردوو Student.student_no و Enrolled.student_no دەهێڵێتەوە — تەنها نرخی یەکسانیان تێدایە.
Joins ⋈لێکبەستنەکان ⋈
Natural joinلێکبەستنی سروشتی
A that matches rows on all the columns that have the same name in both tables and keeps only one copy of each of those columns.
جۆرێکە لە کە ڕیزەکان بەپێی هەموو ئەو ستوونانە جووت دەکات کە لە هەردوو خشتەکەدا هەمان ناویان هەیە و تەنها یەک کۆپی لە هەر یەکێک لەو ستوونانە دەهێڵێتەوە.
The natural join Student ⋈ Enrolled (no condition written) compares all columns that have the same name — here only student_no — and keeps one copy of each shared column. It equals a product, then a selection, then a projection: π student_no, name, dept, city, course_code, grade (σ Student.student_no = Enrolled.student_no (Student × Enrolled)).
Student ⋈ Enrolled (بەبێ نووسینی ) هەموو ئەو بەراورد دەکات کە هەمان ناویان هەیە — لێرەدا تەنها student_no — و یەک کۆپی لە هەر هاوبەش دەهێڵێتەوە. یەکسانە بە ، پاشان ، پاشان : π student_no, name, dept, city, course_code, grade (σ Student.student_no = Enrolled.student_no (Student × Enrolled)).
student_no | name | dept | city | course_code | grade |
|---|---|---|---|---|---|
| 101 | Aram | CS | Sulaimani | DB101 | A |
| 101 | Aram | CS | Sulaimani | WP101 | B |
| 102 | Shilan | IT | Erbil | DB101 | B |
| 103 | Karwan | CS | Duhok | DB101 | C |
| 103 | Karwan | CS | Duhok | WP101 | A |
| 103 | Karwan | CS | Duhok | AI301 | B |
| 104 | Dilan | SE | Sulaimani | WP101 | A |
Joins ⋈لێکبەستنەکان ⋈
Outer joins: keep the rows without a partnerلێکبەستنە دەرەکییەکان: ڕیزە بێهاوتاکان بهێڵەوە
A that also keeps the rows that have no match in the other table, filling the missing columns with NULL. It can be LEFT, RIGHT or FULL.
جۆرێکە لە کە ئەو ڕیزانەش دەهێڵێتەوە کە لە خشتەکەی تردا هاوتایان نییە و ستوونە نەبووەکانیان بە NULL پڕ دەکاتەوە. دەکرێت LEFT، RIGHT یان FULL بێت.
An ordinary (inner) join drops the rows that find no partner: Hana is missing from Student ⋈ Enrolled. An outer join keeps them and fills the missing columns with NULL. A left outer join ⟕ keeps every row of the left table, a right outer join ⟖ every row of the right table, and a full outer join ⟗ both.
ئاسایی (ناوەکی) ئەو لادەبات کە هاوتایان نادۆزنەوە: هانا لە Student ⋈ Enrolledدا نییە. دەیانهێڵێتەوە و نەبووەکان بە NULL پڕ دەکاتەوە. چەپ ⟕ هەموو چەپ دەهێڵێتەوە، ڕاست ⟖ هەموو ڕاست و تەواو ⟗ هەردووکیان.
student_no | name | dept | city | course_code | grade |
|---|---|---|---|---|---|
| 101 | Aram | CS | Sulaimani | DB101 | A |
| 101 | Aram | CS | Sulaimani | WP101 | B |
| 102 | Shilan | IT | Erbil | DB101 | B |
| 103 | Karwan | CS | Duhok | DB101 | C |
| 103 | Karwan | CS | Duhok | WP101 | A |
| 103 | Karwan | CS | Duhok | AI301 | B |
| 104 | Dilan | SE | Sulaimani | WP101 | A |
| 105 | Hana | IT | Kirkuk | NULL | NULL |
In the same way, Enrolled ⟖ Course keeps NW201 (Computer Networks), which nobody takes, with NULL in student_no and grade. In our data every enrolment has a course, so the full outer join Enrolled ⟗ Course gives the same rows as the right outer join.
بە هەمان شێوە، Enrolled ⟖ Course وانەی NW201 (Computer Networks) دەهێڵێتەوە کە کەس نایخوێنێت، بە NULL لە student_no و gradeدا. لە ئێمەدا هەموو تۆمارکردنێک وانەیەکی هەیە، بۆیە تەواو Enrolled ⟗ Course هەمان ڕاست دەدات.
Keep only the rows that found no partner and you answer questions such as 'which students have no course?': in SQL a LEFT JOIN followed by WHERE … IS NULL (the SQL examples). In Microsoft Access the Find Unmatched Query Wizard writes exactly this query for you (two wizards that find problems).
ئەگەر تەنها ئەو بهێڵیتەوە کە هیچ هاوتایەکیان نەدۆزیوەتەوە، وەڵامی پرسیاری وەک «کام قوتابی هیچ وانەیەکی نییە؟» دەدەیتەوە: لە SQLدا LEFT JOIN و پاشان WHERE … IS NULL (نموونەکانی SQL). لە Microsoft Accessدا Find Unmatched Query Wizard ڕێک ئەم بۆ تۆ دەنووسێت (دوو یاریدەدەر کە کێشە دەدۆزنەوە).
Shows why an inner join loses rows without a partner and how left, right and full outer joins keep them, filling the gaps with NULL.
پیشان دەدات بۆچی ناوەکی هەر کە هاوتای نەبێت لەدەست دەدات و چۆن چەپ، ڕاست و تەواو دەیهێڵنەوە و بۆشاییەکان بە NULL پڕ دەکەنەوە.
Whole video: inner join recap, then left, right and full outer join.
هەموو ڤیدیۆکە: پێداچوونەوەیەک بە ناوەکیدا، پاشان چەپ، ڕاست و تەواو.
Division ÷: 'for all' questionsدابەشکردن ÷: پرسیارەکانی «بۆ هەموو»
The relational-algebra operation A ÷ B (also written A/B) that finds the values in A that are paired with every value in B, for example the students who have taken all the listed courses.
کاری A ÷ B (یان A/B) لە کە ئەو نرخانەی A دەدۆزێتەوە کە لەگەڵ هەموو نرخەکانی Bـدا جووت بوون، بۆ نموونە ئەو قوتابیانەی هەموو وانە دیاریکراوەکانیان خوێندووە.
The web track requires two courses: Required(course_code) = {DB101, WP101}. Which students have taken every required course? Division answers exactly this kind of 'for all' question. Let A(student_no, course_code) be π student_no, course_code (Enrolled) and B(course_code) be Required. Then A ÷ B is the set of student_no values that appear in A together with every course_code of B.
ڕێڕەوی وێب دوو وانەی پێویستە: Required(course_code) = {DB101, WP101}. کام قوتابییانە هەموو وانە پێویستەکانیان خوێندووە؟ ڕێک وەڵامی ئەم جۆرە پرسیارەی «بۆ هەموو» دەداتەوە. با A(student_no, course_code) بێت بە π student_no, course_code (Enrolled) و B(course_code) بێت بە Required. ئەوکات A ÷ B کۆمەڵەی ئەو نرخانەی student_noـە کە لە Aدا لەگەڵ هەموو course_codeێکی Bدا دەردەکەون.
Division is not a basic operation: it can be built from π, × and −. The idea is to find the students who are missing something, and subtract them:
کارێکی بنەڕەتی نییە: دەتوانرێت بە π، × و − دروست بکرێت. بیرۆکەکە ئەوەیە ئەو قوتابیانە بدۆزینەوە کە شتێکیان کەمە، پاشان لایان ببەین:
All candidates:
π student_no (A)= {101, 102, 103, 104}.هەموو پاڵێوراوەکان:
π student_no (A)= {101, 102, 103, 104}.Every pair each candidate should have:
π student_no (A) × B= 8 pairs.هەموو ئەو جووتانەی هەر پاڵێوراوێک دەبێت هەیبێت:
π student_no (A) × B= 8 جووت.The pairs that are missing:
(π student_no (A) × B) − A= {(102, WP101), (104, DB101)}.ئەو جووتانەی کەمن:
(π student_no (A) × B) − A= {(102, WP101), (104, DB101)}.The students who miss something:
π student_no ((π student_no (A) × B) − A)= {102, 104}.ئەو قوتابیانەی شتێکیان کەمە:
π student_no ((π student_no (A) × B) − A)= {102, 104}.Subtract them from all candidates:
A ÷ B = π student_no (A) − π student_no ((π student_no (A) × B) − A)= {101, 103}.لە هەموو پاڵێوراوەکانیان کەم بکەرەوە:
A ÷ B = π student_no (A) − π student_no ((π student_no (A) × B) − A)= {101, 103}.
Explains the division operator: when it can be used and how it answers "for all" questions, with a worked example.
لە ڕوون دەکاتەوە: کەی بەکار دێت و چۆن وەڵامی پرسیارەکانی جۆری «بۆ هەموو» دەداتەوە، لەگەڵ نموونەیەکی هەنگاو بە هەنگاو.
Whole video: the conditions for division and one worked example.
هەموو ڤیدیۆکە: مەرجەکانی و نموونەیەکی هەنگاو بە هەنگاو.
Precedence and the schema of a resultپێشینەیی و سکیمای ئەنجام
When an expression has no brackets, operators are applied in this order (like × before + in school maths): first the unary operators σ, π, ρ; then × and ⋈; then ∩; and last ∪ and −. So R ∪ S ∩ T means R ∪ (S ∩ T). Brackets make your meaning clear — use them.
کاتێک دەربڕینێک کەوانەی نییە، بەم ڕیزبەندییە جێبەجێ دەکرێن (وەک × پێش + لە بیرکاریی قوتابخانەدا): سەرەتا σ، π، ρ؛ پاشان × و ⋈؛ پاشان ∩؛ و لە کۆتاییدا ∪ و −. بۆیە R ∪ S ∩ T واتە R ∪ (S ∩ T). کەوانەکان مەبەستەکەت ڕوون دەکەنەوە — بەکاریان بهێنە.
| Operationکار | Columns of the result ئەنجام |
|---|---|
σ (R) | the same as Rهەمان هی R |
π list (R) | exactly the listڕێک لیستەکە |
R ∪ S, R ∩ S, R − S | the shared schema of R and S (taken from R) هاوبەشەکەی R و S (لە Rـەوە وەرگیراوە) |
R × S, R ⋈θ S | all the columns of R, then all of Sهەموو R، پاشان هەموو هی S |
R ⋈ S (natural) | the columns of R and S, each shared column once R و S، هەر هاوبەش یەک جار |
A ÷ B | the columns of A that are not in Bئەو A کە لە Bدا نین |
Algebra inside the DBMSجەبر لەناو سیستەمی بەڕێوەبردنی بنکەدراوەدا
Relational algebra is operational: it says which steps to run. That is why a DBMS uses it inside, to represent a query's evaluation plan. One question can usually be written as several equivalent expressions, and the query optimizer picks the one that is cheapest to run. Thanks to the algebra's exact rules, the optimizer can rewrite a query safely. For example, these two give the same rows:
ڕێکارییە: دەڵێت کام هەنگاوەکان جێبەجێ بکرێن. بۆیە لە ناوەوە بەکاری دەهێنێت بۆ نیشاندانی پلانی جێبەجێکردنی . یەک پرسیار زۆرجار دەتوانرێت بە چەند دەربڕینێکی هاوتا بنووسرێت و لە ئەوەیان هەڵدەبژێرێت کە هەرزانترین جێبەجێکردنی هەیە. بە هۆی یاسا وردەکانی جەبرەکەوە، دەتوانێت بە سەلامەتی دووبارە بنووسێتەوە. بۆ نموونە، ئەم دووانە هەمان دەدەن:
σ city = 'Sulaimani' (Student ⋈ Enrolled) -- join 5 students with 7 enrolments, then keep 3 rows
(σ city = 'Sulaimani' (Student)) ⋈ Enrolled -- keep the 2 students from Sulaimani first, then joinstudent_no | name | dept | city | course_code | grade 101 | Aram | CS | Sulaimani | DB101 | A 101 | Aram | CS | Sulaimani | WP101 | B 104 | Dilan | SE | Sulaimani | WP101 | A
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Builds the whole 7-row join, then throws most of it away.
هەموو 7 ڕیزییەکە دروست دەکات، پاشان زۆربەی فڕێ دەدات.
- Lineدێڕی 2
Filters first: only 2 student rows take part in the join. Same 3 rows at the end, less work.
سەرەتا پاڵاوتن دەکات: تەنها 2 قوتابی بەشداری دەکەن. لە کۆتاییدا هەمان 3 ، بە کاری کەمتر.
Algebra inside the DBMSجەبر لەناو سیستەمی بەڕێوەبردنی بنکەدراوەدا
How the optimizer chooses a planباشترکەری پرسەکە چۆن پلانێک هەڵدەبژێرێت
How does the optimizer choose? It lists several plans, estimates what each costs from statistics it keeps about the tables (how many rows, how many different values in a column) and runs the cheapest. One operator can also be run in several ways: a join can compare every pair of rows (nested loops), look the partner up in an index, sort both tables and merge them, or build a hash table. You can ask a DBMS which plan it chose: EXPLAIN in MySQL, EXPLAIN QUERY PLAN in SQLite.
چۆن هەڵدەبژێرێت؟ چەند پلانێک ڕیز دەکات، تێچووی هەر یەکێکیان لە ئامارەکانەوە مەزەندە دەکات کە دەربارەی هەڵیان دەگرێت (چەند ، چەند نرخی جیاواز لە ) و هەرزانترینیان جێبەجێ دەکات. یەک دەکرێت بە چەند ڕێگایەک جێبەجێ بکرێت: دەتوانێت هەموو جووتە بەراورد بکات ()، هاوتاکەی لە بدۆزێتەوە، هەردوو ڕیز بکات و تێکەڵیان بکات، یان دروست بکات. دەتوانیت لە بپرسیت کام پلانی هەڵبژاردووە: EXPLAIN لە MySQL، EXPLAIN QUERY PLAN لە .
EXPLAIN QUERY PLAN
SELECT s.name, e.course_code
FROM student AS s JOIN enrolled AS e ON s.student_no = e.student_no
WHERE s.city = 'Sulaimani';
CREATE INDEX idx_enrolled_course ON enrolled (course_code);
EXPLAIN QUERY PLAN
SELECT s.name, e.course_code
FROM student AS s JOIN enrolled AS e ON s.student_no = e.student_no
WHERE e.course_code = 'WP101';QUERY PLAN |--SCAN s `--SEARCH e USING COVERING INDEX sqlite_autoindex_enrolled_1 (student_no=?) QUERY PLAN |--SEARCH e USING INDEX idx_enrolled_course (course_code=?) `--SEARCH s USING INDEX sqlite_autoindex_student_1 (student_no=?)
SCAN = read the whole table; SEARCH = jump to the rows through an index. With a condition on student, SQLite reads student first and looks up each student's enrolments. With a condition on enrolled and an index on course_code, it starts from enrolled instead. Same join, two different plans.
SCAN = خوێندنەوەی هەموو ؛ SEARCH = بازدان بۆ لە ڕێگەی . بە لەسەر student، سەرەتا student دەخوێنێتەوە و تۆمارکردنەکانی هەر قوتابییەک دەدۆزێتەوە. بە لەسەر enrolled و لەسەر course_code، لە جیاتی ئەوە لە enrolledـەوە دەست پێدەکات. هەمان ، دوو پلانی جیاواز.
Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە
A formal query language based on logic, in which you describe the result you want with a , not how to compute it. It has two forms: and .
زمانێکی فەرمیی پرسە کە لەسەر لۆجیک دامەزراوە و تێیدا بە وەسفی ئەو ئەنجامە دەکەیت کە دەتەوێت، نەک چۆنیەتی ژماردنی. دوو شێوەی هەیە: و .
Relational calculus is the declarative partner of the algebra. A query is a logical formula (from first-order predicate logic) that describes the rows we want; it says nothing about the steps. The answer is every assignment of values that makes the formula true. One calculus query can be carried out by many different algebra expressions, and an SQL query without aggregation (no COUNT, SUM, GROUP BY) corresponds to a calculus expression.
هاوبەشە ڕوونکەرەوەکەی جەبرەکەیە. لۆجیکییە (لە لۆجیکی پێشبینیی پلەی یەکەمەوە) کە ئەو وەسف دەکات کە دەمانەوێت؛ هیچ دەربارەی هەنگاوەکان ناڵێت. وەڵامەکە هەموو ئەو نرخانەیە کە دەکەنە ڕاست. یەک ژمێرکاری دەتوانرێت بە چەندین دەربڕینی جیاوازی جەبر جێبەجێ بکرێت و SQL بەبێ کۆکردنەوە (بێ COUNT، SUM، GROUP BY) هاوتای دەربڕینێکی ژمێرکارییە.
- Tuple relational calculus (TRC): a variable stands for a whole row, a , for example
t, andt.cityis its city. - Domain relational calculus (DRC): a variable stands for one value from a column's domain, for example
nfor a name.
- (TRC): جێگەی تەواو، واتە ، دەگرێتەوە، بۆ نموونە
tوt.cityشارەکەیەتی. - (DRC): جێگەی یەک نرخ لە دەگرێتەوە، بۆ نموونە
nبۆ ناوێک.
Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە
Formulas, quantifiers and implicationفۆرمولەکان، quantifier و گوزارەی مەرجی
- Atomic formulas: a row belongs to a relation (
t ∈ Student, or⟨i, n, d, c⟩ ∈ Studentin DRC), or a comparison of variables and constants with<,>,=,≤,≥,≠(t.city = 'Erbil'). - Connectives: NOT
¬, AND∧, OR∨join formulas into bigger ones. - Quantifiers:
∃ x (P)— 'there exists' an x for which P is true;∀ x (P)— P is true 'for all' x. The short forms∃ s ∈ Student (P)and∀ r ∈ Required (P)limit the variable to the rows of one table. - Free and bound variables: a quantifier binds its variable. Only the result variables, written left of the bar
|, may be free in the formula.
- : سەر بە relationێکە (
t ∈ Student، یان لە DRCدا⟨i, n, d, c⟩ ∈ Student)، یان بەراوردی و نەگۆڕەکان بە<،>،=،≤،≥،≠(t.city = 'Erbil'). - پەیوەندکەرەکان: NOT
¬، AND∧، OR∨پێکەوە دەبەستن بۆ فۆرمولەی گەورەتر. - :
∃ x (P)— «هەیە» xێک کە Pـی بۆ ڕاست بێت؛∀ x (P)— P «بۆ هەموو» xێک ڕاستە. شێوە کورتەکانی∃ s ∈ Student (P)و∀ r ∈ Required (P)بە یەک سنووردار دەکەن. - : خۆی دەبەستێتەوە. تەنها ئەنجام، کە لە لای چەپی هێڵی
|دەنووسرێن، دەتوانن لە ئازاد بن.
Implication A ⇒ B ('if A then B') is false only when A is true and B is false. So A ⇒ B means the same as ¬A ∨ B. It is how 'for all' conditions are written: ∀ r ∈ Course (r.credits = 4 ⇒ …) says something only about the 4-credit courses and is automatically true for the others. The same truth table is taught in Propositional logic in the AI course.
A ⇒ B («ئەگەر A ئەوا B») تەنها کاتێک هەڵەیە کە A ڕاست بێت و B هەڵە بێت. بۆیە A ⇒ B هەمان واتای ¬A ∨ Bـی هەیە. بەم شێوەیە «بۆ هەموو» دەنووسرێن: ∀ r ∈ Course (r.credits = 4 ⇒ …) تەنها دەربارەی وانە 4 یەکەییەکان شتێک دەڵێت و بۆ ئەوانی تر خۆبەخۆ ڕاستە. هەمان لە لۆجیکی گوزارەییی کۆرسی فێر دەکرێت.
A | B | A ⇒ B | ¬A ∨ B |
|---|---|---|---|
| trueڕاست | trueڕاست | trueڕاست | trueڕاست |
| trueڕاست | falseهەڵە | falseهەڵە | falseهەڵە |
| falseهەڵە | trueڕاست | trueڕاست | trueڕاست |
| falseهەڵە | falseهەڵە | trueڕاست | trueڕاست |
Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە
Tuple relational calculus by exampleژمێرکاریی پەیوەندیداری تۆمار بە نموونە
A TRC query has the form { t | P(t) }: 'the rows t for which P(t) is true'. Here are four questions on our tables, each with its answer:
TRC ئەم شێوەیەی هەیە: { t | P(t) }: «ئەو t کە P(t)یان بۆ ڕاستە». ئەمە چوار پرسیارە لەسەر ، هەریەکە لەگەڵ وەڵامەکەی:
1 { t | t ∈ Student ∧ t.city = 'Sulaimani' }
2 { t | ∃ s ∈ Student ( t.name = s.name ∧ s.dept = 'CS' ) }
3 { t | ∃ s ∈ Student ∃ e ∈ Enrolled ( s.student_no = e.student_no
∧ e.course_code = 'DB101'
∧ t.name = s.name ) }
4 { t | ∃ s ∈ Student ( t.student_no = s.student_no
∧ ∀ r ∈ Required ( ∃ e ∈ Enrolled ( e.student_no = s.student_no
∧ e.course_code = r.course_code ) ) ) }Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Whole rows of the students from Sulaimani: 101 Aram and 104 Dilan (like σ).
تەواوی قوتابیانی سلێمانی: 101 Aram و 104 Dilan (وەک σ).
- Lineدێڕی 3
Only the name: t has one attribute,
name. Answer: Aram, Karwan (like π after σ).تەنها ناو: t یەک هەیە،
name. وەڵام: Aram، Karwan (وەک π دوای σ).- Lineدێڕی 5-7
Two tables joined through the shared
student_no: the names of DB101 students — Aram, Shilan, Karwan (like a join).دوو لە ڕێگەی
student_noـی هاوبەشەوە بەستراونەتەوە: ناوی قوتابیانی DB101 — Aram، Shilan، Karwan (وەک ).- Lineدێڕی 9-11
'For every required course there is an enrolment': students 101 and 103 — the division of the previous screens.
«بۆ هەموو وانەیەکی پێویست تۆمارکردنێک هەیە»: قوتابیانی 101 و 103 — پەڕەکانی پێشوو.
Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە
Domain relational calculus by exampleژمێرکاریی پەیوەندیداری بوار بە نموونە
A DRC query has the form { ⟨x1, x2, …, xn⟩ | P(x1, …, xn) }. Each variable stands for one column value, written in the column order of the table: for Student, ⟨i, n, d, c⟩ = (student_no, name, dept, city).
DRC ئەم شێوەیەی هەیە: { ⟨x1, x2, …, xn⟩ | P(x1, …, xn) }. هەر جێگەی نرخی یەک دەگرێتەوە و بە ڕیزبەندیی دەنووسرێت: بۆ Student، ⟨i, n, d, c⟩ = (student_no, name, dept, city).
1 { ⟨i, n, d, c⟩ | ⟨i, n, d, c⟩ ∈ Student ∧ c = 'Sulaimani' }
2 { ⟨i, n, d, c⟩ | ⟨i, n, d, c⟩ ∈ Student ∧ c = 'Sulaimani' ∧ d = 'SE' }
3 { ⟨n⟩ | ∃ i, d, c ( ⟨i, n, d, c⟩ ∈ Student ∧ c = 'Sulaimani' ) }
4 { ⟨n⟩ | ∃ i, d, c ( ⟨i, n, d, c⟩ ∈ Student
∧ ∃ g ( ⟨i, 'DB101', g⟩ ∈ Enrolled ) ) }Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Choose rows by a condition: 101 Aram and 104 Dilan.
هەڵبژاردنی بە : 101 Aram و 104 Dilan.
- Lineدێڕی 3
Add a condition with ∧: only 104 Dilan.
زیادکردنی بە ∧: تەنها 104 Dilan.
- Lineدێڕی 5
Return only some attributes: the other variables are bound with ∃. Answer: Aram, Dilan.
تەنها هەندێک بگەڕێنەوە: تر بە ∃ دەبەسترێنەوە. وەڵام: Aram، Dilan.
- Lineدێڕی 7-8
The same variable
iin both tables connects them, like a join: Aram, Shilan, Karwan.هەمان
iلە هەردوو پێکەوەیان دەبەستێتەوە، وەک : Aram، Shilan، Karwan.
Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە
Safe queries and expressive powerپرسە سەلامەتەکان و هێزی دەربڕین
Some formulas have infinitely many answers. { t | ¬ (t ∈ Student) } asks for every row that is not a student — every possible name, number and city in the world. Such a query is unsafe. A safe query only returns values that appear in its tables or in its constants. With this restriction three languages have exactly the same power: relational algebra, safe tuple calculus and safe domain calculus. A language that can express every query of the algebra is called relationally complete; SQL is.
هەندێک وەڵامی بێکۆتاییان هەیە. { t | ¬ (t ∈ Student) } داوای هەموو ئەو دەکات کە قوتابی نین — هەموو ناو، ژمارە و شارێکی شیاو لە جیهاندا. لەم جۆرە ناسەلامەتە. سەلامەت تەنها ئەو نرخانە دەگەڕێنێتەوە کە لە یان لە نەگۆڕەکانیدا هەن. بەم سنوورە سێ زمان ڕێک هەمان هێزیان هەیە: ، سەلامەت و سەلامەت. زمانێک کە بتوانێت هەموو جەبر دەرببڕێت پێی دەوترێت خاوەنی ؛ SQL وایە.
- Calculus is non-operational (declarative): it says what is wanted. Algebra is operational: it says which steps to run.
- Calculus strongly influenced SQL and Query-By-Example (QBE), where you fill a blank table with an example of the rows you want.
- ژمێرکاری ڕێکاری نییە (): دەڵێت چی دەوێت. جەبر ڕێکارییە: دەڵێت کام هەنگاوەکان جێبەجێ بکرێن.
- ژمێرکاری کاریگەرییەکی زۆری لەسەر SQL و Query-By-Example (QBE) هەبوو، کە تێیدا بەتاڵ بە نموونەیەک لەو دەتەوێت پڕ دەکەیتەوە.
From relational algebra to SQLلە جەبری پەیوەندیدارەوە بۆ SQL
Every operator of the algebra has an SQL form. You already know SELECT … FROM … WHERE from The four basic statements; joins and the set operations are new here. One difference matters everywhere: the algebra works with sets, but SQL tables and results are bags — they may hold the same row twice — unless you write DISTINCT (or use UNION, INTERSECT, EXCEPT, which remove duplicates by default).
هەموو جەبرەکە شێوەیەکی SQLـی هەیە. SELECT … FROM … WHERE پێشتر لە چوار ڕستە بنەڕەتییەکە دەزانیت؛ و لێرەدا نوێن. یەک جیاوازی لە هەموو شوێنێکدا گرنگە: جەبرەکە لەگەڵ دا کار دەکات، بەڵام و ئەنجامەکانی SQL کیسەن (بە ئینگلیزی bags) — دەکرێت هەمان دوو جار هەڵبگرن — مەگەر DISTINCT بنووسیت (یان UNION، INTERSECT، EXCEPT بەکار بهێنیت، کە بە شێوەی بنەڕەتی دووبارەکان لادەبەن).
| Algebraجەبر | SQL | Exampleنموونە |
|---|---|---|
σ condition (R) | WHERE condition | SELECT * FROM student WHERE city = 'Sulaimani'; |
π a, b (R) | the column list after SELECT (+ DISTINCT for a set)لیستی دوای SELECT (+ DISTINCT بۆ کۆمەڵە) | SELECT DISTINCT dept FROM student; |
ρ S1 (R) | AS | FROM student AS s1 |
R × S | CROSS JOIN | SELECT * FROM debate CROSS JOIN course; |
R ⋈θ S | JOIN … ON condition | … FROM course AS c1 JOIN course AS c2 ON c1.credits < c2.credits |
R ⋈ S (natural) | NATURAL JOIN / JOIN … USING (col) | SELECT * FROM student NATURAL JOIN enrolled; |
R ⟕ S, R ⟖ S, R ⟗ S | LEFT / RIGHT / FULL OUTER JOIN | … FROM student AS s LEFT JOIN enrolled AS e ON s.student_no = e.student_no |
R ∪ S | UNION | SELECT * FROM robotics UNION SELECT * FROM debate; |
R ∩ S | INTERSECT | SELECT * FROM robotics INTERSECT SELECT * FROM debate; |
R − S | EXCEPT | SELECT * FROM robotics EXCEPT SELECT * FROM debate; |
A ÷ B | no operator: NOT EXISTS twice, or GROUP BY … HAVING COUNT(…)هیچ نییە: دوو جار NOT EXISTS، یان GROUP BY … HAVING COUNT(…) | see the next screenلە پەڕەی داهاتوودا |
From relational algebra to SQLلە جەبری پەیوەندیدارەوە بۆ SQL
The algebra examples in SQLنموونەکانی جەبر بە SQL
Here are some of the lesson's examples written in SQL. All of them were run on the example tables; you can try them in phpMyAdmin or SQLite (SQLite basics).
ئەمانە هەندێک لە نموونەکانی وانەکەن بە SQL نووسراون. هەموویان لەسەر نموونەییەکان جێبەجێ کراون؛ دەتوانیت لە phpMyAdmin یان SQLite تاقییان بکەیتەوە (بنەماکانی SQLite).
-- Student ⋈ Enrolled, only DB101 (natural join + selection)
SELECT * FROM student NATURAL JOIN enrolled WHERE course_code = 'DB101';
-- the students with an A, with an explicit join condition
SELECT s.name, e.course_code, e.grade
FROM student AS s JOIN enrolled AS e ON s.student_no = e.student_no
WHERE e.grade = 'A'
ORDER BY s.name;
-- Student ⟕ Enrolled, keeping only the students with no course
SELECT s.name, e.course_code
FROM student AS s LEFT JOIN enrolled AS e ON s.student_no = e.student_no
WHERE e.course_code IS NULL;student_no | name | dept | city | course_code | grade 101 | Aram | CS | Sulaimani | DB101 | A 102 | Shilan | IT | Erbil | DB101 | B 103 | Karwan | CS | Duhok | DB101 | C name | course_code | grade Aram | DB101 | A Dilan | WP101 | A Karwan | WP101 | A name | course_code Hana | NULL
SELECT student_no, name FROM robotics
UNION
SELECT student_no, name FROM debate
ORDER BY student_no;
SELECT student_no, name FROM robotics
INTERSECT
SELECT student_no, name FROM debate;
SELECT student_no, name FROM robotics
EXCEPT
SELECT student_no, name FROM debate
ORDER BY student_no;101 | Aram 102 | Shilan 103 | Karwan 104 | Dilan 103 | Karwan 101 | Aram 104 | Dilan
-- 'there is no required course that this student has not taken'
SELECT DISTINCT e.student_no
FROM enrolled AS e
WHERE NOT EXISTS (
SELECT * FROM required AS r
WHERE NOT EXISTS (
SELECT * FROM enrolled AS e2
WHERE e2.student_no = e.student_no
AND e2.course_code = r.course_code))
ORDER BY e.student_no;
-- the same answer by counting
SELECT student_no
FROM enrolled
WHERE course_code IN (SELECT course_code FROM required)
GROUP BY student_no
HAVING COUNT(DISTINCT course_code) = (SELECT COUNT(*) FROM required)
ORDER BY student_no;student_no 101 103 student_no 101 103
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-9
Two NOT EXISTS = 'for all': no required course is missing for this student. This is the calculus formula with ∀ written in SQL.
دوو NOT EXISTS = «بۆ هەموو»: هیچ وانەیەکی پێویست بۆ ئەم قوتابییە کەم نییە. ئەمە هەمان ∀ـە کە بە SQL نووسراوە.
- Lineدێڕی 13-17
Count the required courses each student has, and keep those with the full number.
GROUP BYandHAVINGare taught in GROUP BY and HAVING.وانە پێویستەکانی هەر قوتابییەک بژمێرە و ئەوانە بهێڵەوە کە ژمارە تەواوەکەیان هەیە.
GROUP BYوHAVINGلە GROUP BY و HAVING فێر دەکرێن.
SELECT e.student_no AS student_no, c.course_code AS course_code, c.title AS title
FROM enrolled AS e LEFT JOIN course AS c ON e.course_code = c.course_code
UNION
SELECT e.student_no, c.course_code, c.title
FROM enrolled AS e RIGHT JOIN course AS c ON e.course_code = c.course_code
ORDER BY course_code, student_no;student_no | course_code | title 103 | AI301 | Artificial Intelligence 101 | DB101 | Database Fundamentals 102 | DB101 | Database Fundamentals 103 | DB101 | Database Fundamentals NULL | NW201 | Computer Networks 101 | WP101 | Web Programming 103 | WP101 | Web Programming 104 | WP101 | Web Programming
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The aliases name the result columns, so the
ORDER BYat the end can use them. (AnORDER BYafterUNIONcannot usee.orc.names.)ناوە خوازراوەکان (
AS) ناو دەدەنە ئەنجامەکە، بۆ ئەوەیORDER BYـی کۆتایی بتوانێت بەکاریان بهێنێت. (ORDER BYدوایUNIONناتوانێت ناوی وەکe.یانc.بەکاربهێنێت.)- Lineدێڕی 6
Sort by course, then by student. Names are clearer than positions such as
ORDER BY 2, 1.سەرەتا بەپێی وانە و پاشان بەپێی قوتابی ڕیز دەکات. ناو لە ژمارەی شوێن، وەک
ORDER BY 2, 1، ڕوونترە.
From relational algebra to SQLلە جەبری پەیوەندیدارەوە بۆ SQL
Joins written with commas, and joins of three tablesلێکبەستنەکان بە فاریزە و لێکبەستنی سێ خشتە
Older books, exam answers and many web pages write a join without the word JOIN. The tables are listed with commas – that is the Cartesian product – and the join condition goes into WHERE – that is the selection. It is the algebra's σθ(R × S) written directly, and it gives the same rows as JOIN … ON:
کتێبە کۆنەکان، وەڵامی تاقیکردنەوەکان و زۆر بەبێ وشەی JOIN دەنووسن. بە فاریزە ڕیز دەکرێن – ئەمە – و لێکبەستنەکە دەچێتە ناو WHERE – ئەمە . ئەمە σθ(R × S)ـی جەبرەکەیە کە ڕاستەوخۆ نووسراوە و هەمان وەک JOIN … ON دەدات:
-- the older way: a product (commas) + the join condition in WHERE
SELECT s.name, e.grade
FROM student AS s, enrolled AS e
WHERE s.student_no = e.student_no
AND e.course_code = 'DB101'
ORDER BY s.name;
-- the same question with JOIN … ON
SELECT s.name, e.grade
FROM student AS s JOIN enrolled AS e ON s.student_no = e.student_no
WHERE e.course_code = 'DB101'
ORDER BY s.name;name | grade Aram | A Karwan | C Shilan | B name | grade Aram | A Karwan | C Shilan | B
Both give the same three rows. Prefer JOIN … ON: the join condition stands next to its table and cannot be forgotten among the other conditions.
هەردووکیان هەمان سێ دەدەن. JOIN … ON باشترە: لێکبەستنەکە لە تەنیشت دەوەستێت و لە نێوان تردا لە بیر ناچێت.
A join of N tables needs join conditions: one for each link in the chain. A many-to-many relationship is a chain of three tables, with the junction table in the middle – enrolled links student and course, so two conditions are needed:
N پێویستی بە لێکبەستن هەیە: بۆ هەر ئەڵقەیەکی زنجیرەکە یەکێک. زنجیرەیەکی سێ و لە ناوەڕاستدایە – enrolled student و course دەبەستێتەوە، بۆیە دوو پێویستن:
SELECT s.name, c.title, e.grade
FROM student AS s
JOIN enrolled AS e ON s.student_no = e.student_no -- condition 1
JOIN course AS c ON e.course_code = c.course_code -- condition 2
WHERE c.credits = 3
ORDER BY s.name, c.title;name | title | grade Aram | Database Fundamentals | A Aram | Web Programming | B Dilan | Web Programming | A Karwan | Database Fundamentals | C Karwan | Web Programming | A Shilan | Database Fundamentals | B
Three tables, two join conditions. Leave one out and every row of the other tables is paired with every row: a Cartesian product. student, enrolled alone already gives rows.
سێ ، دوو لێکبەستن. ئەگەر یەکێکیان لاببەیت، هەموو تر لەگەڵ هەموو جووت دەبێت: . تەنها student, enrolled بە تەنها دەدات.
From relational algebra to SQLلە جەبری پەیوەندیدارەوە بۆ SQL
A table joined with itselfخشتەیەک کە لەگەڵ خۆیدا دەبەسترێتەوە
Sometimes both sides of a join are the same table. A family table stores parent_child(parent, child). A grandparent is the parent of a parent, so the table must be read twice, once for each generation. Rename (ρ in the algebra, AS in SQL) gives the two copies different names: this is a self-join.
هەندێک جار هەردوو لای هەمان . خێزان parent_child(parent, child) هەڵدەگرێت. باپیر دایک یان باوکی دایک یان باوکە، بۆیە دەبێت دوو جار بخوێنرێتەوە، بۆ هەر نەوەیەک جارێک. (ρ لە جەبرەکەدا، AS لە SQLدا) دوو ناوی جیاواز دەداتە دوو کۆپییەکە: ئەمە .
CREATE TABLE parent_child (parent TEXT, child TEXT);
INSERT INTO parent_child VALUES ('Hana', 'Aram'), ('Rezan', 'Aram'),
('Soran', 'Hana'), ('Lana', 'Hana'), ('Karwan', 'Rezan');
-- Aram's grandparents: the parents of Aram's parents
SELECT gp.parent AS grandparent
FROM parent_child AS gp
JOIN parent_child AS p ON gp.child = p.parent
WHERE p.child = 'Aram'
ORDER BY grandparent;grandparent Karwan Lana Soran
p finds Aram's parents (Hana, Rezan); gp finds their parents.
p دایک و باوکی ئارام دەدۆزێتەوە (هانا، ڕێزان)؛ gp دایک و باوکی ئەوان.
A self-join meets every pair twice: (101, 102) and (102, 101). Write < instead of <> to keep each pair once, and DISTINCT when two students share several courses – 101 and 103 share both DB101 and WP101:
هەموو جووتێک دوو جار دەبینێت: (101, 102) و (102, 101). لە جیاتی <>، < بنووسە بۆ ئەوەی هەر جووتێک یەک جار بمێنێتەوە و DISTINCT کاتێک دوو قوتابی چەند وانەیەکی هاوبەشیان هەیە – 101 و 103 هەم DB101 و هەم WP101یان هاوبەشە:
-- pairs of students who share a course
SELECT DISTINCT a.student_no AS first, b.student_no AS second
FROM enrolled AS a JOIN enrolled AS b
ON a.course_code = b.course_code
AND a.student_no < b.student_no
ORDER BY first, second;first | second 101 | 102 101 | 103 101 | 104 102 | 103 103 | 104
With <> the same query gives 12 rows; with < but no DISTINCT, 6 rows (101–103 twice).
بە <> هەمان 12 دەدات؛ بە < بەبێ DISTINCT، 6 (101–103 دوو جار).
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What is trueئەوەی ڕاستە |
|---|---|
| Union = rows in both tables = هەردوو | That is intersection. Union = in the first OR the second OR both.ئەوە . = لە یەکەم یان دووەم یان هەردووکیان. |
SQL SELECT = selection σSELECTـی SQL = σ | The SELECT list is the projection π; WHERE is σ.لیستی SELECT π؛ WHERE σـە. |
| Projecting before selecting on a removed column پێش لەسەر لابراو | Select first, then project: σ needs the columns it tests.سەرەتا ، پاشان : σ پێویستی بەو کە دەیانپشکنێت. |
| Union of tables with different columns بە جیاواز | Set operations need union-compatible tables. پێویستیان بە گونجاو بۆ یەکگرتن هەیە. |
| R − S = S − R | Difference is not symmetric. هاوسەنگ نییە. |
| Equijoin keeps one copy of the column یەک کۆپیی دەهێڵێتەوە | The natural join does; the equijoin keeps both. وا دەکات؛ هەردووکیان دەهێڵێتەوە. |
| Using a natural join on tables with unrelated same-named columnsبەکارهێنانی لەسەر بە هاوناوی بێپەیوەندی | Rows disappear silently; write JOIN … ON instead. بە بێدەنگی ون دەبن؛ لە جیاتی ئەوە JOIN … ON بنووسە. |
Expecting FULL OUTER JOIN in MySQLچاوەڕوانی FULL OUTER JOIN لە MySQLدا | Combine LEFT and RIGHT JOIN with UNION.LEFT و RIGHT JOIN بە UNION پێکەوە ببەستە. |
Forgetting a join condition (FROM student, enrolled with no WHERE)لەبیرچوونی لێکبەستن (FROM student, enrolled بەبێ WHERE) | Every row meets every row: a Cartesian product ( rows). N tables need conditions.هەموو هەموو دەبینێت: ( ). N پێویستی بە هەیە. |
| Accepting each pair twice from a self-joinوەرگرتنی هەر جووتێک دوو جار لە | Use < instead of <>, and DISTINCT if needed.< لە جیاتی <> بەکار بهێنە و ئەگەر پێویست بوو DISTINCT. |
Exercisesڕاهێنانەکان
Using Student, Course, Enrolled, Robotics and Debate, write an expression for: (1) the titles of the 3-credit courses; (2) the names of the students enrolled in WP101; (3) the numbers of the students who are in no club; (4) the courses nobody takes. Then work out each answer.
بە بەکارهێنانی Student، Course، Enrolled، Robotics و Debate، دەربڕینێک بنووسە بۆ: (1) ناونیشانی وانە 3 یەکەییەکان؛ (2) ناوی ئەو قوتابیانەی لە WP101دا تۆمار کراون؛ (3) ژمارەی ئەو قوتابیانەی لە هیچ یانەیەکدا نین؛ (4) ئەو وانانەی کەس نایانخوێنێت. پاشان وەڵامی هەریەکەیان دەربهێنە.
Hintئاماژە
(3) and (4) are 'all … minus those that …' questions: use −.
(3) و (4) پرسیاری «هەموو … جگە لەوانەی …»ن: − بەکار بهێنە.
Show answerوەڵامەکە پیشان بدە
(1) π title (σ credits = 3 (Course))
(2) π name (σ course_code = 'WP101' (Student ⋈ Enrolled))
(3) π student_no (Student) − (π student_no (Robotics) ∪ π student_no (Debate))
(4) π course_code (Course) − π course_code (Enrolled)(1) Database Fundamentals, Web Programming, Computer Networks (2) Aram, Karwan, Dilan (3) 105 (4) NW201
Translate expressions (1) to (4) of the previous exercise into SQL.
دەربڕینەکانی (1) تا (4)ی ڕاهێنانی پێشوو بکە بە SQL.
Show answerوەڵامەکە پیشان بدە
-- (1)
SELECT title FROM course WHERE credits = 3;
-- (2)
SELECT name FROM student NATURAL JOIN enrolled WHERE course_code = 'WP101';
-- (3)
SELECT student_no FROM student
EXCEPT
SELECT student_no FROM (SELECT student_no FROM robotics
UNION
SELECT student_no FROM debate) AS members;
-- (4)
SELECT course_code FROM course
EXCEPT
SELECT course_code FROM enrolled;Database Fundamentals Web Programming Computer Networks Aram Karwan Dilan 105 NW201
Write question (1) — the titles of the 3-credit courses — in tuple relational calculus and in domain relational calculus. Course has the columns (course_code, title, credits).
پرسیاری (1) — ناونیشانی وانە 3 یەکەییەکان — بە و بە بنووسە. Course (course_code, title, credits)ـی هەیە.
Show answerوەڵامەکە پیشان بدە
TRC: { t | ∃ c ∈ Course ( t.title = c.title ∧ c.credits = 3 ) }
DRC: { ⟨n⟩ | ∃ k, r ( ⟨k, n, r⟩ ∈ Course ∧ r = 3 ) }In both, only the title is free (the result); the other variables are bound by ∃.
لە هەردووکیاندا تەنها ناونیشانەکە ئازادە (ئەنجامەکە)؛ تر بە ∃ بەستراونەتەوە.
Which operator answers each question? (a) Students who are in both clubs. (b) Every possible student–course pair, for a timetable draft. (c) Students who have taken every course of the IT department. (d) All courses, with their students if they have any.
کام وەڵامی هەر پرسیارێک دەداتەوە؟ (أ) ئەو قوتابیانەی لە هەردوو یانەکەدان. (ب) هەموو جووتێکی شیاوی قوتابی–وانە، بۆ ڕەشنووسی وانەکان. (ج) ئەو قوتابیانەی هەموو وانەکانی بەشی ITیان خوێندووە. (د) هەموو وانەکان، لەگەڵ قوتابییەکانیان ئەگەر هەیانبێت.
Show answerوەڵامەکە پیشان بدە
- (a) Intersection ∩.
- (b) Cartesian product ×.
- (c) Division ÷ ('every').
- (d) A right outer join
Enrolled ⟖ Course(or a left one with the tables swapped).
- (أ) ∩.
- (ب) ×.
- (ج) ÷ («هەموو»).
- (د) ڕاست
Enrolled ⟖ Course(یان چەپ بە گۆڕینی شوێنی ).
Use the tables below. Write SQL for: (Q1) the names and cities of everyone who works for First Bank; (Q2) the same, only for those who earn more than 1,000,000 IQD there, with their street; (Q3) everyone who does not work for First Bank; (Q4) the name of employee 100; (Q5) the names of those who work for City Builders. Then (Q6): which column of works should not be there?
خوارەوە بەکار بهێنە. SQL بنووسە بۆ: (Q1) ناو و شاری هەموو ئەوانەی لە First Bank کار دەکەن؛ (Q2) هەمان شت، تەنها بۆ ئەوانەی لەوێ زیاتر لە 1,000,000 دینار وەردەگرن، لەگەڵ شەقامەکەیان؛ (Q3) هەموو ئەوانەی لە First Bank کار ناکەن؛ (Q4) ناوی فەرمانبەری 100؛ (Q5) ناوی ئەوانەی لە City Builders کار دەکەن. پاشان (Q6): کام works نابێت لەوێ بێت؟
CREATE TABLE employee (employee_id INT PRIMARY KEY, employee_name TEXT, street TEXT, city TEXT);
CREATE TABLE company (company_id TEXT PRIMARY KEY, company_name TEXT, city TEXT);
CREATE TABLE works (employee_id INT, employee_name TEXT, company_id TEXT, salary_iqd INT,
PRIMARY KEY (employee_id, company_id));
INSERT INTO employee VALUES (100, 'Aram', 'Salim St', 'Sulaimani'),
(101, 'Shilan', '60 Metre St', 'Erbil'),
(102, 'Karwan', 'Zakho Rd', 'Duhok'),
(103, 'Dilan', 'Bakhtiari', 'Sulaimani');
INSERT INTO company VALUES ('C1', 'First Bank', 'Erbil'), ('C2', 'Small Bank', 'Sulaimani'),
('C3', 'City Builders', 'Duhok');
INSERT INTO works VALUES (100, 'Aram', 'C1', 1500000), (101, 'Shilan', 'C1', 900000),
(101, 'Shilan', 'C3', 400000), (102, 'Karwan', 'C3', 1100000);Hintئاماژە
Q3: who works for First Bank? Then take those people away from all employees.
Q3: کێ لە First Bank کار دەکات؟ پاشان ئەو کەسانە لە هەموو فەرمانبەران دەربکە.
Show answerوەڵامەکە پیشان بدە
-- Q1 names and cities of everyone who works for First Bank
SELECT e.employee_name, e.city
FROM employee AS e
JOIN works AS w ON e.employee_id = w.employee_id
JOIN company AS c ON w.company_id = c.company_id
WHERE c.company_name = 'First Bank';
-- Q2 … who earn more than 1,000,000 IQD, with their street
SELECT e.employee_name, e.street, e.city
FROM employee AS e
JOIN works AS w ON e.employee_id = w.employee_id
JOIN company AS c ON w.company_id = c.company_id
WHERE c.company_name = 'First Bank' AND w.salary_iqd > 1000000;
-- Q3 everyone who does NOT work for First Bank
SELECT employee_name
FROM employee
WHERE employee_id NOT IN (SELECT w.employee_id
FROM works AS w JOIN company AS c ON w.company_id = c.company_id
WHERE c.company_name = 'First Bank');
-- Q4 the name of employee 100
SELECT employee_name FROM employee WHERE employee_id = 100;
-- Q5 the names of those who work for City Builders
SELECT e.employee_name
FROM employee AS e
JOIN works AS w ON e.employee_id = w.employee_id
JOIN company AS c ON w.company_id = c.company_id
WHERE c.company_name = 'City Builders';employee_name | city Aram | Sulaimani Shilan | Erbil employee_name | street | city Aram | Salim St | Sulaimani employee_name Karwan Dilan employee_name Aram employee_name Shilan Karwan
Q1, Q2 and Q5 are three-table joins with two join conditions.
Q1، Q2 و Q5 سێ بە دوو لێکبەستنەوە.
Q3 is the trap. Many answers write company_name <> 'First Bank':
Q3 داوەکەیە. زۆر وەڵام company_name <> 'First Bank' دەنووسن:
SELECT DISTINCT e.employee_name
FROM employee AS e
JOIN works AS w ON e.employee_id = w.employee_id
JOIN company AS c ON w.company_id = c.company_id
WHERE c.company_name <> 'First Bank';employee_name Shilan Karwan
Wrong twice: Shilan works for First Bank (and also for City Builders), and Dilan, who works nowhere, is missing. 'Not working for X' is a difference: all employees − those who work for X (difference), written with NOT IN or EXCEPT.
دوو جار هەڵەیە: شیلان لە First Bank کار دەکات (و لە City Buildersـیش) و دیلان، کە لە هیچ شوێنێک کار ناکات، نییە. «لە X کار ناکات» : هەموو فەرمانبەران − ئەوانەی لە X کار دەکەن (جیاوازیی کۆمەڵە)، کە بە NOT IN یان EXCEPT دەنووسرێت.
Q6: works.employee_name copies a fact about the employee into every job row. It depends on employee_id alone – only part of the key (employee_id, company_id) – so it breaks second normal form. Drop it and join employee when the name is needed.
Q6: works.employee_name ڕاستییەک دەربارەی فەرمانبەرەکە کۆپی دەکاتە ناو هەموو کارێک. تەنها پشت بە employee_id دەبەستێت – تەنها بەشێک لە کلیلی (employee_id, company_id) – بۆیە شێوەی نۆرماڵی دووەم دەشکێنێت. لایببە و کاتێک ناوەکە پێویست بوو employee ببەستەوە.
Summary and reviewپوختە و پێداچوونەوە
- A table is a relation: a set of , a subset of the Cartesian product of its domains.
- Relational algebra is procedural: σ rows, π columns, × all pairs, ∪ OR, ∩ AND, − 'but not', ρ new names; ⋈ and ÷ are built from them.
- Joins: theta (any condition), equijoin (=, keeps both columns), natural (same names, one copy); outer joins keep rows without a partner.
- Relational calculus is declarative: TRC with row variables, DRC with value variables, ∃ and ∀; only safe queries are allowed.
- Algebra, safe TRC and safe DRC have the same power; SQL is relationally complete, and the DBMS optimizes queries with algebra rules.
- relationـە: لە ()، ژێرکۆمەڵەیەک لە .
- ڕێکارییە: σ ، π ، × هەموو جووتەکان، ∪ «یان»، ∩ «و»، − «بەڵام نەک»، ρ ناوی نوێ؛ ⋈ و ÷ لەمانە دروست دەکرێن.
- : theta (هەر )، (=، هەردوو دەهێڵێتەوە)، (هەمان ناو، یەک کۆپی)؛ بێهاوتاکان دەهێڵنەوە.
- ڕوونکەرەوەیە: TRC بە ، DRC بە نرخ، ∃ و ∀؛ تەنها ڕێگەپێدراون.
- جەبر، TRCـی سەلامەت و DRCـی سەلامەت هەمان هێزیان هەیە؛ SQL خاوەنی و بە یاساکانی جەبر باشتر دەکات.
What is the difference between relational algebra and relational calculus?جیاوازیی نێوان جەبری پەیوەندیدار و ژمێرکاریی پەیوەندیدار چییە؟
Algebra is procedural: it lists the operations (how). Calculus is declarative: it describes the result with a logical formula (what). With safe queries both can express exactly the same questions.
جەبر ڕێکارییە: کارەکان ڕیز دەکات (چۆن). ژمێرکاری ڕوونکەرەوەیە: ئەنجامەکە بە لۆجیکی وەسف دەکات (چی). بە سەلامەت هەردووکیان دەتوانن ڕێک هەمان پرسیارەکان دەرببڕن.
What does union-compatible mean?گونجان بۆ یەکگرتن واتای چییە؟
The two tables have the same number of columns, and matching columns have compatible domains — needed for ∪, ∩ and −.
هەردوو هەمان ژمارەی هەیە و بەرامبەرەکان گونجاویان هەیە — بۆ ∪، ∩ و − پێویستە.
Write a natural join as basic operations.لێکبەستنی سروشتی بە کارە بنەڕەتییەکان بنووسە.
A Cartesian product, then a selection that makes the same-named columns equal, then a projection that keeps one copy of each: R ⋈ S = π…(σ R.a = S.a (R × S)).
، پاشان کە هاوناوەکان یەکسان دەکات، پاشان کە یەک کۆپی لە هەریەکەیان دەهێڵێتەوە: R ⋈ S = π…(σ R.a = S.a (R × S)).
Which kind of question needs division?چ جۆرە پرسیارێک پێویستی بە دابەشکردن هەیە؟
'For all' questions, such as 'which students have taken every required course?'.
پرسیارەکانی «بۆ هەموو»، وەک «کام قوتابییانە هەموو وانە پێویستەکانیان خوێندووە؟».
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A Stanford lecture that builds relational algebra queries step by step on a small college database: choosing rows and columns, products and joins.
وانەیەکی زانکۆی Stanford کە پلەبەپلە لەسەر بچووکی کۆلێژێک دەنووسێت: ، ، و .
Whole video: selection, projection, Cartesian product, natural join and theta join on a small college database. Set operations, rename and division come later in this lesson.
هەموو ڤیدیۆکە: ، ، ، و لەسەر بچووکی کۆلێژێک. ، و دواتر لەم وانەیەدا دێن.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
The Database Development Life Cycleسووڕی ژیانی گەشەپێدانی بنکەدراوە
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Silberschatz, Korth and Sudarshan, Database System Concepts, 7th ed. — Chapter 2 slides: Introduction to the Relational Model (relational algebra)
- Silberschatz, Korth and Sudarshan, Database System Concepts, 7th ed. — Chapter 4 slides: Intermediate SQL (natural join, outer joins)
- Silberschatz, Korth and Sudarshan, Database System Concepts, 7th ed. — Chapter 27 slides: Formal Relational Query Languages (tuple and domain relational calculus, safety)
- MySQL 8.4 Reference Manual: Set Operations with UNION, INTERSECT, and EXCEPT
- MySQL 8.4 Reference Manual: INTERSECT Clause
- MySQL 8.4 Reference Manual: JOIN Clause
- Wikipedia: QUEL query languages (history)
- K. Durant, Relational Algebra & Relational Calculus (Lecture 4, Northeastern University) — course resource, used only for its list of topics; all text and examples here are original
- SQLite: EXPLAIN QUERY PLAN (SCAN and SEARCH)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.