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.

34 min read33 خولەک خوێندنەوە · 32 stepsهەنگاو

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ـی ئەمڕۆ هەردوو شێوازەکە تێکەڵ دەکات.

Recipe or description?ڕێچکە یان وەسف؟

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.

چێشتلێنەر لەوانەیە وەسفێک بە هەڵە تێبگات؛ هەرگیز وا ناکات. هەردوو زمانەکە وردن و ڕێک دەتوانن هەمان پرسیارەکان بکەن.

Quick checkپشکنینی خێراWhich language describes HOW to get the answer, step by step?کام زمان وەسفی دەکات چۆن وەڵامەکە وەربگرین، هەنگاو بە هەنگاو؟

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 و بۆیە جەبر دەتوانێت کاری لەسەر بکات.

Three ways to say the same thingسێ ڕێگا بۆ گوتنی هەمان شت
Everyday database wordوشەی ڕۆژانەی Relational model (mathematics)مۆدێلی پەیوەندیدار (بیرکاری)Other nameناوی تر
tablerelationrelation—
rowrecord
columnattributefieldfield
  • 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یش لەخۆ دەگرێت، کە واتە «نەزانراو».
Quick checkپشکنینی خێراD = {tea, coffee, juice} and K = {kleicha, baklava}. How many pairs does D × K have?D = {tea, coffee, juice} و K = {kleicha, baklava}. D × K چەند جووتی هەیە؟

What makes an algebra 'relational'چی جەبرێک دەکاتە «پەیوەندیدار»

Definitionپێناسە

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 example ORDER 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ـەکان وەک سەیر دەکات، بۆیە ئەنجامێک هەرگیز هەمان دوو جار تێدا نییە.
The operators of relational algebra
SymbolهێماOperationکارWhat it doesچی دەکاتKindجۆر
σSelectionkeeps the rows that meet a conditionئەو دەهێڵێتەوە کە جێبەجێ دەکەنbasicبنەڕەتی
πProjectionkeeps the listed columns دیاریکراوەکان دەهێڵێتەوەbasicبنەڕەتی
×Cartesian productpairs every row of one table with every row of anotherهەر لەگەڵ هەموو تر جووت دەکاتbasicبنەڕەتی
∪Unionrows in the first table OR the second OR both یەکەم یان دووەم یان هەردووکیانbasicبنەڕەتی
−Set differencerows in the first table but not in the second یەکەم کە لە دووەمدا نینbasicبنەڕەتی
∩Intersectionrows in both tablesئەو لە هەردوو هەنderivedداتاشراو
⋈Joina product followed by a selection و پاشان derivedداتاشراو
÷Divisionanswers 'for all' questionsوەڵامی پرسیارەکانی «بۆ هەموو» دەداتەوەderivedداتاشراو
ρRenamegives 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 پڕ دەکرێنەوە).

Quick checkپشکنینی خێراWhy can relational-algebra operators be nested inside each other?بۆچی دەتوانرێن یەک لەناو یەک دابنرێن؟

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 Student
student_nonamedeptcity
101AramCSSulaimani
102ShilanITErbil
103KarwanCSDuhok
104DilanSESulaimani
105HanaITKirkuk
Course Course
course_codetitlecredits
DB101Database Fundamentals3
WP101Web Programming3
AI301Artificial Intelligence4
NW201Computer Networks3
Enrolled Enrolled
student_nocourse_codegrade
101DB101A
101WP101B
102DB101B
103DB101C
103WP101A
103AI301B
104WP101A

Selection σ: choose rowsهەڵبژاردن σ: هەڵبژاردنی ڕیز

Definitionپێناسە

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ـی هەیە.

σ city = 'Sulaimani' (Student)ئەنجامی σ ⁦city = 'Sulaimani' (Student)⁩
student_nonamedeptcity
101AramCSSulaimani
104DilanSESulaimani

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) شیلان، کاروان و هانا دەدات.

Quick checkپشکنینی خێراHow many rows does σ dept = 'CS' ∨ dept = 'SE' (Student) return?σ dept = 'CS' ∨ dept = 'SE' (Student) چەند دەگەڕێنێتەوە؟

Projection π: choose columnsدەرهێنانی ستوون π: هەڵبژاردنی ستوون

Definitionپێناسە

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 — هەرچەندە پێنج قوتابی بەشیان هەیە.

Quick checkپشکنینی خێراHow many rows does π city (Student) return?π city (Student) چەند دەگەڕێنێتەوە؟

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:

چونکە هەموو ئەنجامێک ، دەتوانرێن یەک لەناو یەک دابنرێن. «ناوی ئەو قوتابیانەی خەڵکی سلێمانین» و پاشان : سەرەتا هەڵبژێرە، پاشان . دەربڕینەکە لە ناوەوە بۆ دەرەوە بخوێنەوە:

The Student table with five rows; the rows of Aram (101) and Dilan (104), whose city is Sulaimani, are highlighted. An arrow labelled σ (selection picks rows, city = 'Sulaimani') leads to a two-row table with the same four columns, in which the name column is highlighted. An arrow labelled π (projection picks columns, name) leads to a one-column table: Aram, Dilan. Below: the expression π name (σ city = 'Sulaimani' (Student)) and the SQL SELECT name FROM student WHERE city = 'Sulaimani';خشتەی Student بە پێنج ڕیزەوە؛ ڕیزەکانی ئارام (101) و دیلان (104)، کە شارەکەیان سلێمانییە، دیاری کراون. تیرێک بە σ (هەڵبژاردن ڕیز هەڵدەبژێرێت، city = 'Sulaimani') دەچێت بۆ خشتەیەکی دوو ڕیزی بە هەمان چوار ستوون، کە ستوونی name تێیدا دیاری کراوە. تیرێک بە π (دەرهێنانی ستوون ستوون هەڵدەبژێرێت، name) دەچێت بۆ خشتەیەکی یەک ستوونی: Aram، Dilan. لە خوارەوە: دەربڕینی π name (σ city = 'Sulaimani' (Student)) و SQLـی SELECT name FROM student WHERE city = 'Sulaimani';
σ removes rows, π removes columns. Together they answer 'names of the students from Sulaimani'.σ لادەبات، π . پێکەوە وەڵامی «ناوی قوتابیانی سلێمانی» دەدەنەوە.CêIT
Quick checkپشکنینی خێراWhich expression gives the names of the IT students?کام دەربڕین ناوی قوتابیانی IT دەدات؟

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ن ()، سێ بیرکاری (یەکگرتن، یەکتربڕین و جیاوازیی کۆمەڵە) لەسەریان کار دەکەن:

Two overlapping circles. Robotics holds 101 Aram and 104 Dilan on its own side; Debate holds 102 Shilan on its own side; 103 Karwan is in the overlap. Results: Robotics ∪ Debate = 101 Aram, 102 Shilan, 103 Karwan, 104 Dilan (in Robotics or Debate or both). Robotics ∩ Debate = 103 Karwan (in both tables). Robotics − Debate = 101 Aram, 104 Dilan (in Robotics but not in Debate). Debate − Robotics = 102 Shilan (the order matters).دوو بازنەی یەکتربڕ. Robotics لە لای خۆیەوە 101 Aram و 104 Dilanـی تێدایە؛ Debate لە لای خۆیەوە 102 Shilanـی تێدایە؛ 103 Karwan لە بەشە هاوبەشەکەدایە. ئەنجامەکان: Robotics ∪ Debate = 101 Aram، 102 Shilan، 103 Karwan، 104 Dilan (لە Robotics یان Debate یان هەردووکیان). Robotics ∩ Debate = 103 Karwan (لە هەردوو خشتەکەدا). Robotics − Debate = 101 Aram، 104 Dilan (لە Robotics بەڵام نەک لە Debate). Debate − Robotics = 102 Shilan (ڕیزبەندی گرنگە).
∪ = OR, ∩ = AND, − = 'but not'. Karwan appears only once in the union: a relation has no duplicate rows.∪ = «یان»، ∩ = «و»، − = «بەڵام نەک». کاروان تەنها یەک جار لە دێت: relation دووبارەی نییە.CêIT

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 نییە.

Quick checkپشکنینی خێراWhich expression gives the students who are in Debate but not in Robotics?کام دەربڕین ئەو قوتابیانە دەدات کە لە Debateدان بەڵام لە Roboticsدا نین؟

Cartesian product ×لێکدانی دیکارتی ×

Definitionپێناسە

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⁩ هەیە:

Debate × Courseئەنجامی ⁦Debate × Course⁩
student_nonamecourse_codetitlecredits
102ShilanDB101Database Fundamentals3
102ShilanWP101Web Programming3
102ShilanAI301Artificial Intelligence4
102ShilanNW201Computer Networks3
103KarwanDB101Database Fundamentals3
103KarwanWP101Web Programming3
103KarwanAI301Artificial Intelligence4
103KarwanNW201Computer Networks3

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 هەیە. ئەنجامەکە تا بتوانێت ناوی دەهێڵێتەوە و پێویستی بە ڕێگایەک هەیە بۆ جیاکردنەوەی ئەو دووانە — ، لە پەڕەی داهاتوودا.

Quick checkپشکنینی خێراHow many rows does Student × Course have?Student × Course چەند هەیە؟

Rename ρناونانەوە ρ

Definitionپێناسە

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) ناوی و دەگۆڕێت. کاتێک پێویستە کە لەگەڵ خۆیدا تێکەڵ بکرێت. کام جووتە قوتابی خەڵکی هەمان شارن؟

Pairs of students from the same cityجووتە قوتابیی هەمان شارTEXT
π S1.name, S2.name (
    σ S1.city = S2.city ∧ S1.student_no < S2.student_no (
        ρ S1 (Student) × ρ S2 (Student) ) )
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.

هەمان شار؛ < ڕێگە دەگرێت لەوەی قوتابییەک لەگەڵ خۆیدا جووت بێت و هەر جووتێک دوو جار دەربکەوێت.

Quick checkپشکنینی خێراWhy is ρ needed in the query above?بۆچی لە سەرەوەدا ρ پێویستە؟

Joins ⋈لێکبەستنەکان ⋈

Definitionپێناسە

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)⁩

Definitionپێناسە

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):

Pairs where the first course has fewer creditsئەو جووتانەی وانەی یەکەم یەکەی کەمترە
C1.course_codeC1.creditsC2.course_codeC2.credits
DB1013AI3014
WP1013AI3014
NW2013AI3014

Joins ⋈لێکبەستنەکان ⋈

Equijoinلێکبەستنی یەکسانی

Definitionپێناسە

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 دەهێڵێتەوە — تەنها نرخی یەکسانیان تێدایە.

Quick checkپشکنینی خێراStudent has 4 columns and Enrolled has 3. How many columns does the equijoin Student ⋈ Student.student_no = Enrolled.student_no Enrolled have?Student ٤ و Enrolled ٣ هەیە. Student ⋈ Student.student_no = Enrolled.student_no Enrolled چەند هەیە؟

Joins ⋈لێکبەستنەکان ⋈

Natural joinلێکبەستنی سروشتی

Definitionپێناسە

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 ⋈ Enrolled — Hana (105) has no enrolment, so she is not hereStudent ⋈ Enrolled — هانا ⁦(105)⁩ هیچ تۆمارکردنێکی نییە، بۆیە لێرە نییە
student_nonamedeptcitycourse_codegrade
101AramCSSulaimaniDB101A
101AramCSSulaimaniWP101B
102ShilanITErbilDB101B
103KarwanCSDuhokDB101C
103KarwanCSDuhokWP101A
103KarwanCSDuhokAI301B
104DilanSESulaimaniWP101A
Quick checkپشکنینی خێراHow many student_no columns does Student ⋈ Enrolled (natural join) have?Student ⋈ Enrolled () چەند student_noـی هەیە؟

Joins ⋈لێکبەستنەکان ⋈

Outer joins: keep the rows without a partnerلێکبەستنە دەرەکییەکان: ڕیزە بێهاوتاکان بهێڵەوە

Definitionپێناسە

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 ⟕ Enrolled — the last row is Hana, kept with NULLsStudent ⟕ Enrolled — دوایین هانایە کە بە NULL هێڵراوەتەوە
student_nonamedeptcitycourse_codegrade
101AramCSSulaimaniDB101A
101AramCSSulaimaniWP101B
102ShilanITErbilDB101B
103KarwanCSDuhokDB101C
103KarwanCSDuhokWP101A
103KarwanCSDuhokAI301B
104DilanSESulaimaniWP101A
105HanaITKirkukNULLNULL

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⁩ ڕێک ئەم بۆ تۆ دەنووسێت (دوو یاریدەدەر کە کێشە دەدۆزنەوە).

Quick checkپشکنینی خێراYou need a list of ALL students with their courses, including students who have no course yet. Which operation?پێویستت بە لیستی هەموو قوتابییەکانە لەگەڵ وانەکانیان، تەنانەت ئەوانەی هێشتا هیچ وانەیەکیان نییە. کام کار؟

Relational Algebra (Outer Join Operation)Neso Academy · Englishئینگلیزی · 69K viewsبینین · 1.2K likesلایک

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دابەشکردن ÷: پرسیارەکانی «بۆ هەموو»

Definitionپێناسە

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دا دەردەکەون.

Left: π student_no, course_code (Enrolled) with seven pairs: 101 DB101, 101 WP101, 102 DB101, 103 DB101, 103 WP101, 103 AI301, 104 WP101. Divided by Required: DB101, WP101. A check grid asks whether each student has every required course: 101 yes for both; 102 DB101 yes, WP101 no; 103 yes for both; 104 DB101 no, WP101 yes. Rows 101 and 103 are highlighted. Result: student_no 101 and 103. Note: A ÷ B keeps the values of A that are paired with every row of B; 102 lacks WP101 and 104 lacks DB101, so only 101 and 103 remain; the extra AI301 of 103 does not matter.لای چەپ: π student_no, course_code (Enrolled) بە حەوت جووت: 101 DB101، 101 WP101، 102 DB101، 103 DB101، 103 WP101، 103 AI301، 104 WP101. دابەش لەسەر Required: DB101، WP101. خشتەیەکی پشکنین دەپرسێت ئایا هەر قوتابییەک هەموو وانە پێویستەکانی هەیە: 101 بۆ هەردووکیان بەڵێ؛ 102 DB101 بەڵێ، WP101 نەخێر؛ 103 بۆ هەردووکیان بەڵێ؛ 104 DB101 نەخێر، WP101 بەڵێ. ڕیزەکانی 101 و 103 دیاری کراون. ئەنجام: student_no 101 و 103. تێبینی: دابەشکردن ئەو نرخانەی A دەهێڵێتەوە کە لەگەڵ هەموو ڕیزێکی Bدا جووت بوون؛ قوتابیی 102 وانەی WP101ی نییە و قوتابیی 104 وانەی DB101ی نییە، بۆیە تەنها 101 و 103 دەمێننەوە؛ وانەی زیادەی AI301 گرنگ نییە.
Enrolled ÷ Required = the students who have every required course.⁦Enrolled ÷ Required⁩ = ئەو قوتابیانەی هەموو وانە پێویستەکانیان هەیە.CêIT

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:

کارێکی بنەڕەتی نییە: دەتوانرێت بە π، × و − دروست بکرێت. بیرۆکەکە ئەوەیە ئەو قوتابیانە بدۆزینەوە کە شتێکیان کەمە، پاشان لایان ببەین:

  1. All candidates: π student_no (A) = {101, 102, 103, 104}.

    هەموو پاڵێوراوەکان: π student_no (A) = {⁦101, 102, 103, 104⁩}.

  2. Every pair each candidate should have: π student_no (A) × B = 8 pairs.

    هەموو ئەو جووتانەی هەر پاڵێوراوێک دەبێت هەیبێت: π student_no (A) × B = 8 جووت.

  3. The pairs that are missing: (π student_no (A) × B) − A = {(102, WP101), (104, DB101)}.

    ئەو جووتانەی کەمن: (π student_no (A) × B) − A = {⁦(102, WP101), (104, DB101)⁩}.

  4. The students who miss something: π student_no ((π student_no (A) × B) − A) = {102, 104}.

    ئەو قوتابیانەی شتێکیان کەمە: π student_no ((π student_no (A) × B) − A) = {⁦102, 104⁩}.

  5. 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⁩}.

Quick checkپشکنینی خێراIf Required were {DB101, WP101, AI301}, what would A ÷ Required be?ئەگەر Required بریتی بوایە لە {⁦DB101, WP101, AI301⁩}، A ÷ Required چی دەبوو؟

Relational Algebra (Division Operation)Neso Academy · Englishئینگلیزی · 96K viewsبینین · 1.7K likesلایک

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). کەوانەکان مەبەستەکەت ڕوون دەکەنەوە — بەکاریان بهێنە.

The schema (columns) of each result هەر ئەنجامێک، واتە
OperationکارColumns of the result ئەنجام
σ (R)the same as Rهەمان هی R
π list (R)exactly the listڕێک لیستەکە
R ∪ S, R ∩ S, R − Sthe shared schema of R and S (taken from R) هاوبەشەکەی R و S (لە Rـەوە وەرگیراوە)
R × S, R ⋈θ Sall 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 ÷ Bthe columns of A that are not in Bئەو A کە لە Bدا نین
Quick checkپشکنینی خێراIn π name (Student) ∪ π name (Robotics) − π name (Debate) without brackets, which operators are applied first?لە π name (Student) ∪ π name (Robotics) − π name (Debate)دا بەبێ کەوانە، کام سەرەتا جێبەجێ دەکرێن؟

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:

ڕێکارییە: دەڵێت کام هەنگاوەکان جێبەجێ بکرێن. بۆیە لە ناوەوە بەکاری دەهێنێت بۆ نیشاندانی پلانی جێبەجێکردنی . یەک پرسیار زۆرجار دەتوانرێت بە چەند دەربڕینێکی هاوتا بنووسرێت و لە ئەوەیان هەڵدەبژێرێت کە هەرزانترین جێبەجێکردنی هەیە. بە هۆی یاسا وردەکانی جەبرەکەوە، دەتوانێت بە سەلامەتی دووبارە بنووسێتەوە. بۆ نموونە، ئەم دووانە هەمان دەدەن:

TEXT
σ 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 join
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 ، بە کاری کەمتر.

Quick checkپشکنینی خێراWhy does the optimizer like to push σ down, below a join?بۆچی لە کاتی حەز دەکات σ بهێنێتە خوارەوە، بۆ ژێر ؟

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 لە .

Two plans for the same join (SQLite)دوو پلان بۆ هەمان لێکبەستن (SQLite)SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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';
Quick checkپشکنینی خێراIn a SQLite plan, what is the difference between SCAN and SEARCH?لە پلانێکی ، جیاوازیی نێوان SCAN و SEARCH چییە؟

Relational calculus: describe what you wantژمێرکاریی پەیوەندیدار: ئەوەی دەتەوێت وەسف بکە

Definitionپێناسە

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, and t.city is its city.
  • Domain relational calculus (DRC): a variable stands for one value from a column's domain, for example n for a name.
  • ⁦(TRC)⁩: جێگەی تەواو، واتە ، دەگرێتەوە، بۆ نموونە t و t.city شارەکەیەتی.
  • ⁦(DRC)⁩: جێگەی یەک نرخ لە دەگرێتەوە، بۆ نموونە n بۆ ناوێک.
Quick checkپشکنینی خێراIn which calculus does a variable stand for a whole row?لە کام ژمێرکارییەدا جێگەی تەواو دەگرێتەوە؟

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⟩ ∈ Student in 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 یەکەییەکان شتێک دەڵێت و بۆ ئەوانی تر خۆبەخۆ ڕاستە. هەمان لە لۆجیکی گوزارەییی کۆرسی فێر دەکرێت.

Truth table of implication
ABA ⇒ B¬A ∨ B
trueڕاستtrueڕاستtrueڕاستtrueڕاست
trueڕاستfalseهەڵەfalseهەڵەfalseهەڵە
falseهەڵەtrueڕاستtrueڕاستtrueڕاست
falseهەڵەfalseهەڵەtrueڕاستtrueڕاست
Quick checkپشکنینی خێراWhen is A ⇒ B false?A ⇒ B کەی هەڵەیە؟

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)⁩یان بۆ ڕاستە». ئەمە چوار پرسیارە لەسەر ، هەریەکە لەگەڵ وەڵامەکەی:

TEXT
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⁩ — پەڕەکانی پێشوو.

Quick checkپشکنینی خێراIn query 2, which variable is bound by a quantifier?لە ٢دا، کام بە بەستراوە؟

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

TEXT
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 i in both tables connects them, like a join: Aram, Shilan, Karwan.

هەمان i لە هەردوو پێکەوەیان دەبەستێتەوە، وەک : Aram، Shilan، Karwan.

Quick checkپشکنینی خێراIn DRC query 4, what makes the connection between Student and Enrolled?لە ٤ی DRCدا، چی Student و Enrolled پێکەوە دەبەستێتەوە؟

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)⁩ هەبوو، کە تێیدا بەتاڵ بە نموونەیەک لەو دەتەوێت پڕ دەکەیتەوە.
Quick checkپشکنینی خێراWhy is { t | ¬ (t ∈ Student) } not allowed?بۆچی { t | ¬ (t ∈ Student) } ڕێگەپێدراو نییە؟

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 بەکار بهێنیت، کە بە شێوەی بنەڕەتی دووبارەکان لادەبەن).

Each operator and its SQLهەر و SQLـەکەی
AlgebraجەبرSQLExampleنموونە
σ condition (R)WHERE conditionSELECT * 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)ASFROM student AS s1
R × SCROSS JOINSELECT * FROM debate CROSS JOIN course;
R ⋈θ SJOIN … 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 ⟗ SLEFT / RIGHT / FULL OUTER JOIN… FROM student AS s LEFT JOIN enrolled AS e ON s.student_no = e.student_no
R ∪ SUNIONSELECT * FROM robotics UNION SELECT * FROM debate;
R ∩ SINTERSECTSELECT * FROM robotics INTERSECT SELECT * FROM debate;
R − SEXCEPTSELECT * FROM robotics EXCEPT SELECT * FROM debate;
A ÷ Bno operator: NOT EXISTS twice, or GROUP BY … HAVING COUNT(…)هیچ نییە: دوو جار NOT EXISTS، یان GROUP BY … HAVING COUNT(…)see the next screenلە پەڕەی داهاتوودا
Quick checkپشکنینی خێراWhich SQL keyword plays the role of selection σ?کام وشەی SQL ڕۆڵی σ دەگێڕێت؟

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

JoinsلێکبەستنەکانSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- 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;
Union, intersection, differenceیەکگرتن، یەکتربڕین، جیاوازیی کۆمەڵەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;
Division: students who took every required courseدابەشکردن: ئەو قوتابیانەی هەموو وانە پێویستەکانیان خوێندووەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- '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;
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 BY and HAVING are taught in GROUP BY and HAVING.

وانە پێویستەکانی هەر قوتابییەک بژمێرە و ئەوانە بهێڵەوە کە ژمارە تەواوەکەیان هەیە. GROUP BY و HAVING لە ⁦GROUP BY⁩ و HAVING فێر دەکرێن.

A full outer join in MySQL (which has no FULL OUTER JOIN)لێکبەستنی دەرەکیی تەواو لە MySQL (کە ⁦FULL OUTER JOIN⁩ـی نییە)SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The aliases name the result columns, so the ORDER BY at the end can use them. (An ORDER BY after UNION cannot use e. or c. 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 دەدات:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- 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;

A join of N tables needs N−1N-1 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 پێویستی بە N−1N-1 لێکبەستن هەیە: بۆ هەر ئەڵقەیەکی زنجیرەکە یەکێک. زنجیرەیەکی سێ و لە ناوەڕاستدایە – enrolled student و course دەبەستێتەوە، بۆیە دوو پێویستن:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;
Quick checkپشکنینی خێراA query joins employee, works and company. How many join conditions does it need? employee، works و company بەیەکەوە دەبەستێتەوە. چەند لێکبەستنی پێویستە؟

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دا) دوو ناوی جیاواز دەداتە دوو کۆپییەکە: ئەمە .

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;

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⁩یان هاوبەشە:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- 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;
Quick checkپشکنینی خێراWhy does a self-join need two aliases, such as gp and p?بۆچی پێویستی بە دوو ناوی جیاواز هەیە، وەک gp و p؟

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 − RDifference 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 (5×7=355 \times 7 = 35 rows). N tables need N−1N-1 conditions.هەموو هەموو دەبینێت: (5×7=355 \times 7 = 35 ). N پێویستی بە N−1N-1 هەیە.
Accepting each pair twice from a self-joinوەرگرتنی هەر جووتێک دوو جار لە Use < instead of <>, and DISTINCT if needed.< لە جیاتی <> بەکار بهێنە و ئەگەر پێویست بوو DISTINCT.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانWrite it in relational algebraبە جەبری پەیوەندیدار بینووسە

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وەڵامەکە پیشان بدە
TEXT
(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)
ExerciseڕاهێنانNow in SQLئێستا بە SQL

Translate expressions (1) to (4) of the previous exercise into SQL.

دەربڕینەکانی ⁦(1)⁩ تا ⁦(4)⁩ی ڕاهێنانی پێشوو بکە بە SQL.

Show answerوەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- (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;
ExerciseڕاهێنانA calculus queryپرسێکی ژمێرکاری

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وەڵامەکە پیشان بدە
TEXT
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 ∃.

لە هەردووکیاندا تەنها ناونیشانەکە ئازادە (ئەنجامەکە)؛ تر بە ∃ بەستراونەتەوە.

ExerciseڕاهێنانChoose the operatorکارپێکەر هەڵبژێرە

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 (یان چەپ بە گۆڕینی شوێنی ).
ExerciseڕاهێنانEmployees and companiesفەرمانبەران و کۆمپانیاکان

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 نابێت لەوێ بێت؟

The tables (salaries in IQD)خشتەکان (مووچە بە دینار)SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- 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';

Q3 is the trap. Many answers write company_name <> 'First Bank':

⁦Q3⁩ داوەکەیە. زۆر وەڵام company_name <> 'First Bank' دەنووسن:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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';

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 خاوەنی و بە یاساکانی جەبر باشتر دەکات.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

05-01-relational-algebra-1.mp4Stanford Dbclass · Englishئینگلیزی · 415K viewsبینین · 7.8K likesلایک

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

The Database Development Life Cycleسووڕی ژیانی گەشەپێدانی بنکەدراوە

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.