User Defined Record

User defined records आपको अलग datatypes के variables को एक अकेले custom container में bundle करने देते हैं।

9 min read · 10 cards · 2 checks

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


Theory

बेमेल details को एक अकेले variable में bundle करना

कल्पना कीजिए TicketDesk पर आपके manager एक automated report चाहते हैं जो एक ticket का text title, इसका numerical priority level, और assigned agent का text name पकड़े। अगर आप हर टुकड़े के लिए individual scalar variables बनाते हैं, आपका declaration section जल्दी अव्यवस्थित हो जाता है। जब आप इन अलग variables को एक procedural function में पास करते हैं, आपके parameters क़ाबू से बाहर गुणा हो जाते हैं। आप इन अलग datatypes को एक अकेले custom package में कैसे समूहित करें जो आपके application logic के आर-पार साथ चले?

Theory

अनुकूलित Combo Meal

scalar variables को items अलग-अलग ख़रीदने की तरह सोचिए: एक burger, कुछ fries, और एक cold drink। आपको हर item अलग-अलग अपने हाथों में ढोना होगा, एक गिराने की संभावना बढ़ाते हुए। एक user defined record एक अकेले अनुकूलित cardboard tray के अंदर पैक एक combo meal order करने जैसा है। आप tray का आकार बिल्कुल एक burger, एक fry pack, और एक drink फ़िट करने के लिए बनाते हैं। अब, आप पूरी tray को एक अकेली हरकत में counter के आर-पार पास कर सकते हैं।

Theory

User-Defined Records को औपचारिक रूप से परिभाषित करना

Oracle dialect में, एक user defined record एक composite data structure है जो अलग datatypes के कई संबंधित fields को एक अकेले नाम के तहत समूहित करता है। %ROWTYPE के उलट, जो सख़्ती से एक पूरी table की पूर्वनिर्धारित row structure से bind होता है, एक user-defined record programmer द्वारा पूरी तरह अनुकूलित होता है। आप TYPE ... IS RECORD syntax इस्तेमाल करके blueprint declare करते हैं, और फिर उस blueprint से record variables instantiate करते हैं ताकि database memory में मिश्रित data types store करें।

At a glance

Oracle %ROWTYPE records और custom user defined records के बीच तुलना

Feature%ROWTYPE RecordUser Defined Record
Structure Sourceएक मौजूदा table या view से अपने-आप copy होता हैcode में programmer द्वारा स्पष्ट रूप से संरचित
Field Flexibilitytable का हर column बिल्कुल क्रम में शामिल करना होगाख़ास columns चुन सकते हैं या कई tables के fields मिला सकते हैं
Declaration Stepएक कदम: variable name table_name%ROWTYPE इस्तेमाल कीजिएदो कदम: TYPE blueprint परिभाषित कीजिए, फिर variable declare कीजिए
Custom Fieldsnon table variables या calculated fields शामिल नहीं कर सकतेtable data के साथ स्वतंत्र counters या अतिरिक्त flags शामिल कर सकते हैं

Practical

एक Custom Record Block Declare और इस्तेमाल करना

-- Enable console display in Oracle SQL*Plus
SET SERVEROUTPUT ON;

DECLARE
  -- Step 1: Define the custom record blueprint
  TYPE t_ticket_summary IS RECORD (
    ticket_title tickets.title%TYPE,
    urgency_status VARCHAR2(20),
    days_open NUMBER := 0
  );
  
  -- Step 2: Declare a variable of the new custom type
  v_my_record t_ticket_summary;
BEGIN
  -- Step 3: Populate fields using a query or direct assignment
  SELECT title, status INTO v_my_record.ticket_title, v_my_record.urgency_status
  FROM tickets
  WHERE id = 101;
  
  v_my_record.days_open := 5;
  
  -- Step 4: Access fields using dot notation
  DBMS_OUTPUT.PUT_LINE('Ticket: ' || v_my_record.ticket_title);
  DBMS_OUTPUT.PUT_LINE('SLA Days: ' || v_my_record.days_open);
END;
/

Copy करके Oracle FreeSQL खोलें
Oracle FreeSQL, Oracle SQL का मुफ़्त online editor है। Code पहले copy हो जाता है: उसे वहां paste करके Run करें।

Quiz

'v_info' नामक एक record variable के अंदर 'days_open' field को modify करने के लिए सही dot notation syntax क्या है?

  1. v_info->days_open := 10;
  2. v_info.days_open := 10;
  3. t_ticket_summary.days_open := 10;
  4. v_info(days_open) := 10;
Show the answer

v_info.days_open := 10;

PL/SQL एक record variable के भीतर individual fields access करने के लिए dot notation इस्तेमाल करता है। आप variable name लिखते हैं, फिर एक period, और फिर field name। Option 0 C++ जैसी languages का pointer notation इस्तेमाल करता है जो ग़लत है। Option 2 ख़ास variable instance के बजाय blueprint type name को reference करता है।

Think first

Mental Challenge: Tables जोड़ना

क्या आप एक अकेले user defined record blueprint के अंदर tickets table से एक field और agents table से एक field शामिल कर सकते हैं? reveal करने को tap करने से पहले field declarations मन में सोचिए।

Show the answer

हाँ! यह user defined records के मुख्य फ़ायदों में से एक है। आप एक अकेला record type परिभाषित कर सकते हैं जहाँ field एक tickets.title%TYPE है और field दो agents.name%TYPE है। यह आपको joined data को एक अकेले समेकित record variable में सहजता से fetch करने देता है।

Watch out

दो-कदम Declaration की भूल

university lab exams में एक classic ग़लती पहले एक variable declare किए बिना सीधे एक user-defined record इस्तेमाल करने की कोशिश है। t_ticket_summary.ticket_title := 'Crash'; लिखना एक तुरंत compilation error trigger करेगा। याद रखिए कि TYPE बस एक अमूर्त blueprint है, आपके मन में एक आकार। यह memory space नहीं घेरता। data values पढ़ने या लिखने से पहले आपको हमेशा उस type से एक ठोस variable instance बनाना होगा।

Theory

Production API Pipelines और भविष्य के Semesters

enterprise software में, records साफ़ programming interfaces बनाने के लिए इस्तेमाल होते हैं। 20 अलग input variables के साथ database functions लिखने के बजाय, आप एक अकेला record variable पास करते हैं, code को modular और पढ़ने-लायक़ रखते हुए। SQLite इस्तेमाल करते अपने Sem 3 mobile databases curriculum में, आप row data को structural model classes में समूहित करेंगे जो इन PL/SQL custom records के समान काम करती हैं।

Summary

Key takeaways

  • एक user defined record अलग datatypes के कई fields को एक अकेली संयुक्त unit में समूहित करता है।
  • यह table locked rowtype variables की तुलना में पूर्ण architectural लचीलापन देता है।
  • एक record बनाने को एक दो-कदम process चाहिए: TYPE structure परिभाषित करना, फिर एक variable instance declare करना।
  • Dot notation variable के भीतर ख़ास fields पढ़ने और modify करने की सीधी operational पहुँच देता है।
  • Record instances raw values को गुच्छित करके जटिल database routines में argument पास करना सरल बनाते हैं।
  • Memory hook: custom tray blueprint design करो, box बनाओ, और एक dot से access करो!

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 PL/SQL and Conditional Statements

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

User Defined Record · Mastering SQL - PL/SQL (SEC-02 option A) · Gri-Learn