Theory
વેરાયેલી Student Profile
કલ્પો કે તમારી university library ને દરવાજે એક ખાસ ચકાસણીની પહોંચ છાપવી છે. Security guard ને ત્રણ અલગ વસ્તુઓ જોવી છે: members table માંથી student નું નામ, books table માંથી પુસ્તકનું નામ, અને ગણેલું મોડું fee. જો તમે standard scalar variables વાપરો, તો તમારે ત્રણ અલગ, છૂટા variables જાહેર કરવા પડે, એમને અલગ અલગ track કરવા પડે, અને ત્રણેયને તમારા program ના loops માંથી પસાર કરવા પડે. જ્યારે તમે એમને એક સુઘડ report માં ટાંકી શકો ત્યારે ત્રણ છૂટાં કાગળ શા માટે ઊંચકવાં?
Theory
ખાસ બનાવેલી Travel Organizer થેલી
પાયાના scalar variables ને તમારા છૂટા સિક્કા, કાગળના પૈસા, અને passport ને jeans ના જુદા જુદા ખિસ્સામાં મૂકવા જેવા વિચારો. એકનો હિસાબ ખોવો સહેલો છે. હવે, એક %ROWTYPE variable ને એક પહેલેથી બનેલું ખોખું ખરીદવા જેવો વિચારો જે સ્પષ્ટપણે એક ચોક્કસ table ના માળખા સાથે મળે છે. એક User-Defined Record એટલે શરૂઆતથી તમારી પોતાની ખાસ travel organizer થેલી ડિઝાઇન કરવી! તમે નક્કી કરો છો કે એમાં કેટલાં ખિસ્સાં હશે: એક આંકડાકીય fine માટે એક નાનું ખાનું, એક text નામ માટે એક લાંબું પારદર્શક ખિસ્સું, અને એક date ના થપ્પા માટે એક ચોરસ ખાનું. તમે નકશો બનાવો છો, થેલીને નામ આપો છો, અને એને data ના એક ખાસ ઝૂમખાને રાખવા વાપરો છો.
Theory
User-Defined Record શું છે?
એક User-Defined Record એ PL/SQL માં એક composite data type છે જે તમને અનેક સંબંધિત fields, દરેકને પોતાના નામ અને data type સાથે, એક જ સંરચિત એકમમાં જૂથબદ્ધ કરવા દે છે. %ROWTYPE થી વિપરીત જે આપોઆપ એક આખા હયાત table કે view ની હરોળનું પ્રતિબિંબ પાડે છે, એક user-defined record તમને સંપૂર્ણ નિયંત્રણ આપે છે. તમે custom નકશો વ્યાખ્યાયિત કરવા DECLARE block માં TYPE ... IS RECORD statement વાપરો છો, અને પછી એ નવા type ના આધારે variables બનાવો છો.
At a glance
Table row anchors અને custom records વચ્ચેના સંરચનાત્મક વર્તનના ભેદ
| લક્ષણની સરખામણી | Table %ROWTYPE Anchor | User-Defined Record Type |
|---|---|---|
| સંરચનાત્મક વ્યાખ્યા | એક આખા table ના સંરચનાત્મક columns નું આપોઆપ પ્રતિબિંબ પાડે છે. | Field ની જાહેરાતો વાપરીને programmer દ્વારા સ્પષ્ટપણે ખાસ બનાવાય છે. |
| Field ની છૂટ | લક્ષ્ય database table માંથી દરેક column સામેલ કરવા મજબૂર. | અનેક tables, custom types, કે ભાગોમાંથી fields જોડી શકે છે. |
| જાહેરાતનો તબક્કો | સીધું આ રીતે જાહેર થાય છે: v_row table_name%ROWTYPE; | બે પગલાંની પ્રક્રિયા માંગે છે: પહેલાં TYPE વ્યાખ્યાયિત કરો, પછી variable જાહેર કરો. |
Practical
Defining and Populating a Custom Library Record
-- Defining a custom type and loading data using dot notation
DECLARE
-- Step 1: Create the blueprint/type structure
TYPE GatePassRecord IS RECORD (
student_name members.name%TYPE,
borrowed_isbn books.book_id%TYPE,
fine_balance NUMBER(7,2) := 0.00
);
-- Step 2: Declare an actual variable of our custom type
v_pass GatePassRecord;
BEGIN
-- Accessing fields and assigning data dynamically via dot notation
v_pass.borrowed_isbn := 9005;
v_pass.fine_balance := 45.50;
-- Streaming direct query outputs straight into individual record components
SELECT name
INTO v_pass.student_name
FROM members
WHERE member_id = 101;
DBMS_OUTPUT.PUT_LINE('Gate Verification Successful for: ' || v_pass.student_name);
DBMS_OUTPUT.PUT_LINE('Outstanding Clearances: Rs. ' || v_pass.fine_balance);
END;
/Follow along
એક Record ના જીવનચક્રનું જીવનચક્ર
- 1. નકશો Compile કરવો Oracle TYPE ની વ્યાખ્યાનો block વાંચે છે અને તમારું custom composite માળખું સ્થાનિક session ની memory માં નોંધે છે.
- 2. Memory ની ફાળવણી Variable ની જાહેરાતનું statement વ્યાખ્યાયિત દરેક આંતરિક field element માટે અલગ સળંગ memory ખાનાં અનામત રાખે છે.
- 3. ઘટકને નિશાન બનાવવું Dot operator (variable.field) વાપરીને, તમે સ્થાનિક સોંપણીઓ માટે માળખાની અંદરના ચોક્કસ ખાનાંને નિશાન બનાવો છો.
- 4. દેખરેખ હેઠળની પસંદગી એક SELECT INTO query બિલકુલ એક હરોળ પાછી આપે છે, પડખેના data ના ગાંઠાં તોડ્યા વગર માત્ર map કરેલાં fields ભરતાં.
Quiz
નીચેનામાંથી કઈ syntax ની ભાત એક PL/SQL DECLARE block ની અંદર એક user-defined record type ને સાચી રીતે વ્યાખ્યાયિત કરે છે?
- CREATE TYPE record_name AS RECORD (field1 NUMBER);
- TYPE type_name IS RECORD (field1 data_type, field2 data_type);
- DECLARE RECORD type_name (field1 NUMBER, field2 VARCHAR2);
- RECORD type_name IS TYPE (field1 data_type);
Show the answer
TYPE type_name IS RECORD (field1 data_type, field2 data_type);
એક record type બનાવવા માટેની યોગ્ય PL/SQL syntax 'TYPE type_name IS RECORD (...);' keyword ના માળખાની જરૂર પાડે છે. એક વાર આ custom type નોંધાઈ જાય, પછી તમે એ નવા બનેલા type handle વાપરીને ખરેખરા memory variables જાહેર કરી શકો છો.
Watch out
સંપૂર્ણ સમાનતાની સરખામણીનો આંધળો ખૂણો
અહીં એક પાકો ફાંદો છે જે બહારના university examiners marks કાપવા વાપરવાનું પસંદ કરે છે! PL/SQL માં, તમે standard equality operators વાપરીને બે record variables ને સીધા સરખાવી શકતા નથી. IF v_pass1 = v_pass2 THEN લખવાથી તરત compilation નિષ્ફળતા ઊભી થશે. Oracle ને ખબર નથી કે composite data structures પર વૈશ્વિક સરખામણી કેવી રીતે આંકવી. તમારે એમને field-દર-field આંકવા પડશે, આ રીતે: IF v_pass1.student_name = v_pass2.student_name THEN.
Think first
Record-થી-Record Assignment ની કસોટી
માનસિક પડકાર: શું તમે બધી dot notations field-દર-field લખ્યા વગર એક record variable સીધો બીજા record variable ને સોંપી શકો (દા.ત. v_pass1 := v_pass2;)? Tap કરતાં પહેલાં મનમાં type mapping વિશે વિચારો.
Show the answer
હા, તમે કરી શકો! શરત એ કે બંને record variables બિલકુલ એક જ User-Defined Record TYPE ની વ્યાખ્યાના block વાપરીને જાહેર થયા હોય, તો તમે એક જ વારમાં આખા block ની નકલનું assignment statement ચલાવી શકો છો. Oracle બધી આંતરિક field ની સામગ્રી તરત ડબ્બાઓ વચ્ચે નકલ કરી દેશે.
Theory
Semester 4 Lab Project નું માળખું
તમારા semester ના અંતના lab presentations માટે જટિલ library management કે banking applications ડિઝાઇન કરતી વખતે, તમારો code સાફ કરવા records વાપરો. જો એક PL/SQL Function ને અનેક values પાછી આપવી હોય (જેમ કે એક student ની પાત્રતાનું status AND એમણે લીધેલાં કુલ પુસ્તકો), તો તમે scalar types વાપરી શકતા નથી. તમારી modular data pipeline સાફ અને સુઘડ રાખવા એને બદલે એક જ custom record type પાછો આપો!
Summary
Key takeaways
- User-defined records developers ને અનન્ય, અનેક-field વાળાં composite data માળખાં બનાવવા દે છે.
- Records બનાવવા બે પગલાંનું નૃત્ય જોઈએ: પહેલાં માળખાનો type વ્યાખ્યાયિત કરો, પછી variable જાહેર કરો.
- અલગ આંતરિક attributes dot notation ની syntax (variable_name.field_name) વાપરીને વંચાય અને update થાય છે.
- Records એવી સ્થાનિક માળખાકીય લવચીકતા આપે છે જે table %ROWTYPE anchors આપી શકતાં નથી.
- આખા record blocks પર સીધી boolean સરખામણીઓ (=, !=) PL/SQL માં સંપૂર્ણપણે ગેરકાયદે છે.
- Memory hook: પહેલાં type નું માળખું ગોઠવો, પછી તમારા custom ખોખાને નામ આપો, અને એક dot વાપરીને fields પસંદ કરો!