Relational operations Algebra (select, project, union, intersection, rename)

Relational algebra database ના filtering અને જોડવાની કામગીરી માટે ગાણિતિક નકશા આપે છે, tables પર તાર્કિક ગળણીઓની એક શ્રેણી તરીકે કામ કરતાં.

12 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Digital સોયની શોધ

કલ્પો કે તમારું university નું database દરેક semester, પ્રવાહ, અને campus શહેરના દસ હજારથી વધુ સક્રિય student records વાળી એક જ મુખ્ય spreadsheet સંઘરે છે. જો એક faculty advisor ને ઝડપથી માત્ર એ Semester 2 BCA students ના email સરનામાં જોવાં હોય જેમણે programming labs માં એંસી ટકાથી વધુ મેળવ્યા, તો system કોઈ માણસને દરેક હરોળ અને column વંચાવ્યા વગર એ બિલકુલ ટુકડો કેવી રીતે લાવે? તમે લખો છો એ દરેક SQL query પાછળ, એક અદૃશ્ય ગાણિતિક engine tables ને કાપવા, વહેંચવા, અને ભેળવવા સમીકરણો ચલાવે છે. આ operators શુદ્ધ ગાણિતિક સ્તરે કેવી રીતે કામ કરે છે?

Theory

રસોડાની ચાળણી સામે ઊભો શાકનો સમારનાર

Relational algebra ના operators ને એક ધમધમતા college ના hostel ના રસોડાનાં ઓજારો જેવા વિચારો. એક Selection ચલાવવું એટલે મિશ્ર દાળને એક બારીક તારની ચાળણીમાંથી રેડવી: એ માત્ર તમારા બિલકુલ આડા કદના માપદંડ સાથે મળતી વસ્તુઓ રાખે છે, બાકીની rows ફેંકી દેતાં. એક Projection ચલાવવું એટલે એક મોટો છરો લઈને ઊભું સમારવું, માત્ર તમને જોઈતી શાકની હરોળો અલગ પાડતાં (જેમ કે માત્ર બટાટાનો column રાખવો અને ડુંગળી અને ગાજર ફેંકી દેવાં). જ્યારે તમે એ બંને જોડો, ત્યારે તમને memory બગાડ્યા વગર બિલકુલ જોઈતા ટુકડા મળે છે!

Theory

પાંચ મુખ્ય ગાણિતિક નકશા

Relational database ના સિદ્ધાંતમાં, Relational Algebra એ એક procedural query ભાષા છે જે એક કે વધુ relations ને input તરીકે લે છે અને output તરીકે એક તદ્દન નવો relation બનાવે છે. પાંચ પાયાની કામગીરી આ ગાણિતિક system નો આધાર બનાવે છે: Selection (σ) એક condition ના આધારે rows filter કરે છે; Projection (π) ચોક્કસ ઊભા columns અલગ પાડે છે; Union (∪) બે સુસંગત tables ના records ભેળવે છે; Intersection (∩) માત્ર બંને tables માં હાજર rows કાઢે છે; અને Rename (ρ) સંદેહ ટાળવા table કે column નાં label બદલે છે.

At a glance

Table 1: Relational Algebra ના પાયાના operators અને એમની ગાણિતિક કેન્દ્રની ધરીઓ.

Algebraic Operatorવપરાયેલો સંકેતમાળખાકીય ધરીનું કેન્દ્રExam ના સંકેતનું ઉદાહરણ
Selectionσ (Sigma)આડું (Rows ના ભાગ filter કરે છે)σ marks > 80 (STUDENTS)
Projectionπ (Pi)ઊભું (Column નાં લક્ષણો filter કરે છે)π roll_no, email (STUDENTS)
Union∪ (Union)મળતી rows ને ઊભી રીતે જોડે છેCLASS_A ∪ CLASS_B
Intersection∩ (Intersection)સહિયારી બેવડી rows કાઢે છેLAB_ATTEND ∩ LECTURE_ATTEND
Renameρ (Rho)Metadata ના ઉપનામનો ફેરફારρ NEW_LIST (STUDENTS)

Theory

Worked Example: Lab ની હાજરી Filter કરવી

ચાલો એક exam-શૈલીનો પડકાર પગલું-દર-પગલું ઉકેલીએ. આપણી પાસે MARKS નામનો એક input relation છે જેમાં columns છે: Roll, Sub, અને Score. આપણું કામ છે માત્ર એ Roll ઓળખકર્તાઓ કાઢવા જ્યાં Sub બિલકુલ 'BCA204' હોય અને Score 75 થી મોટો કે બરાબર હોય. ચાલો કામગીરીના execution નો ક્રમ ગોઠવીએ.

Follow along

Query Synthesis ની Pipeline

  1. Step 1: પહેલાં Rows Filter કરો લક્ષ્ય tuples અલગ પાડવા Sigma વાપરીને આડી હરોળની પસંદગીના માપદંડ લાગુ કરો: σ Sub = 'BCA204' ∧ Score ≥ 75 (MARKS).
  2. Step 2: લક્ષ્ય Column અલગ પાડો માત્ર roll નો column પકડવા Pi વાપરીને selection ના statement ને એક ઊભા projection ની અંદર વીંટાળો: π Roll ( σ Sub = 'BCA204' ∧ Score ≥ 75 (MARKS) ).
  3. Step 3: Output ના અચળનું મૂલ્યાંકન Database processing ની memory બચાવવા પહેલાં અંદરનું filter આંકે છે, માત્ર મળતા student IDs ધરાવતો એક સાફ tabular ક્રમ પાછો આપતાં.

Think first

માનસિક તપાસ: Set Union ના નિયમો

ધારો કે Table X માં 3 rows છે અને Table Y માં 3 rows છે. જો તમે relational algebra નું expression X ∪ Y ગણો, તો પરિણામી output table માં શક્ય મહત્તમ અને ન્યૂનતમ rows ની સંખ્યા શું છે?

Show the answer

શક્ય મહત્તમ rows 6 છે (જો બંને tables વચ્ચે શૂન્ય એકસરખી rows હોય). શક્ય ન્યૂનતમ rows 3 છે (જો Table X અને Table Y માં બિલકુલ એ જ બેવડા records હોય). કેમ? કારણ કે relational algebra સખ્તાઈથી શુદ્ધ ગાણિતિક સમૂહો પર કામ કરે છે, એટલે કે બેવડા records હંમેશા એક Union ના output માંથી આપોઆપ દૂર થાય છે!

Quiz

બે જુદાં relational database tables વચ્ચે એક Union (∪) કે Intersection (∩) ની કામગીરી સલામત રીતે કરી શકો એ પહેલાં કઈ માળખાકીય શરત પૂરેપૂરી સંતોષાવી જ જોઈએ?

  1. બંને tables માં આડા row records ની બિલકુલ એકસરખી સંખ્યા હોવી જોઈએ.
  2. બંને tables Union Compatible હોવાં જોઈએ, એટલે કે એ ક્રમમાં મળતા domain data types સાથે એકસરખી સંખ્યાના columns ધરાવે છે.
  3. એક table માં માત્ર string data હોવો જોઈએ, જ્યારે બીજામાં માત્ર integers.
  4. Tables એક જ ભૌતિક SSD સંગ્રહના ભાગના block પર સચવાયેલાં હોવાં જોઈએ.
Show the answer

બંને tables Union Compatible હોવાં જોઈએ, એટલે કે એ ક્રમમાં મળતા domain data types સાથે એકસરખી સંખ્યાના columns ધરાવે છે.

Rows ને સલામત રીતે ભેળવવા કે છેદવા, tables Union Compatible હોવાં જ જોઈએ. એટલે કે એમાં લક્ષણોની બિલકુલ એકસરખી ગણતરી હોવી જોઈએ, અને પડખેના ઊભા columns મળતા સુસંગત data domains (types) ધરાવતા હોવા જોઈએ જેથી rows સરળતાથી ગોઠવાય.

Quiz

જો તમે 50 student records ધરાવતા એક table પર, જ્યાં 5 students 'Amit Sharma' નું બેવડું નામ ધરાવે છે, એક Projection કામગીરી π name (STUDENTS) કરો, તો કેટલી rows પાછી મળશે?

  1. 50 rows, બધાં બેવડાં નામ સહિત.
  2. માત્ર 5 rows.
  3. 46 rows, કારણ કે set theory ની વ્યાખ્યાઓ પ્રમાણે બેવડી values આપોઆપ દૂર થાય છે.
  4. એ એક UnionCompatibilityException error ફેંકે છે.
Show the answer

46 rows, કારણ કે set theory ની વ્યાખ્યાઓ પ્રમાણે બેવડી values આપોઆપ દૂર થાય છે.

ગાણિતિક relational algebra માં, એક relation ને સખ્તાઈથી અનન્ય tuples ના એક સમૂહ તરીકે વ્યાખ્યાયિત કરાય છે. જ્યારે તમે માત્ર 'name' column project કરો, 'Amit Sharma' ના બધા બેવડા દાખલા એક જ અનન્ય value માં ભેગા થઈ જાય છે, કુલ 46 rows પાછી આપતાં.

Watch out

Classic ફાંદો: સંકેત ગૂંચવવાની ચૂક

Semester ની પરીક્ષાઓમાં સૌથી સામાન્ય marks-ગુમાવતી ભૂલ છે selection અને projection ના સંકેતો અદલાબદલી કરવા. Students selection માટે Pi (π) લખે છે કારણ કે એ rows 'Picking' માટેના 'P' જેવો દેખાય છે, અને projection માટે Sigma (σ) લખે છે કારણ કે એ column ના 'Subsets' માટેના 'S' જેવો દેખાય છે. યાદ રાખો: σ એટલે Selection (rows), અને π એટલે Projection (columns). આ માળખાના mapping પર સહેલા marks ગુમાવશો નહીં!

Theory

ગણિતને Semester 3 સાથે જોડવું

Relational algebra નાં સૂત્રો વાસ્તવિક દુનિયાનાં ઓજારોના આંતરિક compilation engine તરીકે કામ કરે છે. Semester 3 Database Systems (BCA301) અને Query Optimization ના modules માં, તમે જોશો કે database ના parsers તમારા raw SQL commands (SELECT અને WHERE) ને production servers પર વીજળીની ઝડપે lookups ચલાવવા સીધા optimized બીજગણિતીય વૃક્ષોમાં કેવી રીતે ફેરવે છે.

Summary

Key takeaways

  • Relational algebra relational database ની query ના execution માટેની અંતર્ગત procedural ભાષા બનાવે છે.
  • Selection એક તાર્કિક condition સાથે મળતા આડા rows ના ભાગ કાઢવા Sigma સંકેત વાપરે છે.
  • Projection અલગ ઊભા columns અલગ પાડવા Pi સંકેત વાપરે છે, બિનજરૂરી fields છોડી દેતાં.
  • Union અને Intersection ની કામગીરી બંને લક્ષ્ય inputs વચ્ચે કડક union compatibility માંગે છે.
  • દરેક operator ગાણિતિક સમૂહો process કરે છે, એટલે કે બેવડી entries outputs માંથી દૂર થાય છે.
  • Memory Hook: Sigma rows પસંદ કરે છે, Pi columns project કરે છે, અને સમૂહો ક્યારેય બેવડાં રાખતા નથી!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction of Relational Model

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati