User Defined Record

User-defined records आपको custom storage pouches बनाने देते हैं जो अलग तरह के data को, जैसे एक student का नाम, उनका total fine, और membership status, एक अकेले, संगठित variable में bundle करते हैं।

10 min read · 11 cards · 2 checks

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


Theory

बिखरी Student Profile

कल्पना कीजिए आपकी university library को gate पर एक custom verification receipt print चाहिए। security guard को तीन अलग चीज़ें देखनी हैं: members table से student का नाम, books table से book title, और calculated late fee। अगर आप standard scalar variables इस्तेमाल करते हैं, आपको तीन अलग, ढीले variables declare करने, उन्हें अलग-अलग track करने, और तीनों को अपने program loops से pass करने होंगे। जब आप उन्हें एक अकेली साफ़ report में staple कर सकते हैं तो कागज़ की तीन ढीली sheets क्यों ले जाएँ?

Theory

Custom Travel Organizer Pouch

basic scalar variables को अपने ढीले coins, paper cash, और passport को अपनी jeans की अलग pockets में डालने की तरह सोचिए। किसी एक का track खोना आसान है। अब, एक %ROWTYPE variable को एक pre-made box ख़रीदने की तरह सोचिए जो स्पष्ट रूप से एक ख़ास table structure से मेल खाता है। एक User-Defined Record शुरू से अपना ख़ुद का custom travel organizer pouch design करने जैसा है! आप बिल्कुल तय करते हैं कि इसमें कितनी pockets हैं: एक numeric fine के लिए एक छोटा slot, एक text name के लिए एक लंबी transparent pocket, और एक date stamp के लिए एक square slot। आप blueprint बनाते हैं, pouch को नाम देते हैं, और इसे data के एक custom bundle को रखने के लिए इस्तेमाल करते हैं।

Theory

एक User-Defined Record क्या है?

एक User-Defined Record PL/SQL में एक composite data type है जो आपको कई संबंधित fields को, हर एक के अपने नाम और data type के साथ, एक अकेली structured unit में group करने देता है। %ROWTYPE के उलट जो अपने-आप एक पूरी मौजूदा table या view row को mirror करता है, एक user-defined record आपको पूर्ण नियंत्रण देता है। आप custom blueprint परिभाषित करने के लिए DECLARE block में TYPE ... IS RECORD statement इस्तेमाल करते हैं, और फिर उस नए type के आधार पर variables instantiate करते हैं।

At a glance

table row anchors और custom records के बीच संरचनात्मक व्यवहारिक अंतर।

Feature ComparisonTable %ROWTYPE AnchorUser-Defined Record Type
Structural Definitionअंतर्निहित रूप से एक पूरी table के संरचनात्मक columns mirror करता है।field declarations इस्तेमाल करके programmer द्वारा स्पष्ट रूप से custom-built।
Field Freedomtarget database table से हर column शामिल करने पर मजबूर।कई tables, custom types, या subsets से fields जोड़ सकता है।
Declaration Phaseसीधे इसका उपयोग करके declared: v_row table_name%ROWTYPE;एक two-step process चाहिए: पहले TYPE परिभाषित करें, फिर variable declare करें।

Practical

एक Custom Library Record परिभाषित और Populate करना

-- 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;
/

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

Follow along

एक Record का Lifecycle

  1. 1. Compile Blueprint Oracle TYPE definition block पढ़ता है और आपके custom composite layout को local session memory में register करता है।
  2. 2. Memory Allocation variable declaration statement हर परिभाषित internal field element के लिए अलग continuous memory slots reserve करता है।
  3. 3. Component Targeting dot operator (variable.field) इस्तेमाल करके, आप local assignments के लिए structure के भीतर स्पष्ट compartments target करते हैं।
  4. 4. Monitored Selection एक SELECT INTO query बिल्कुल एक row return करती है, केवल mapped fields भरते हुए बिना adjacent data nodes तोड़े।

Quiz

निम्न में से कौन सा syntax pattern एक PL/SQL DECLARE block के अंदर एक user-defined record type को सही परिभाषित करता है?

  1. CREATE TYPE record_name AS RECORD (field1 NUMBER);
  2. TYPE type_name IS RECORD (field1 data_type, field2 data_type);
  3. DECLARE RECORD type_name (field1 NUMBER, field2 VARCHAR2);
  4. 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 layout चाहिए। एक बार यह custom type register हो जाता है, आप फिर उस नए बने type handle इस्तेमाल करके असल memory variables declare कर सकते हैं।

Watch out

Complete Equality Comparison का अंधबिंदु

यहाँ एक परम जाल है जो external university examiners marks काटने के लिए इस्तेमाल करना पसंद करते हैं! PL/SQL में, आप standard equality operators इस्तेमाल करके दो record variables की सीधे तुलना नहीं कर सकते। IF v_pass1 = v_pass2 THEN लिखना एक तुरंत compilation failure trigger करेगा। Oracle नहीं जानता कि composite data structures में एक global comparison कैसे evaluate करे। आपको उन्हें field-दर-field evaluate करना होगा, इस तरह: IF v_pass1.student_name = v_pass2.student_name THEN।

Think first

Record-to-Record Assignment Test

Mental Challenge: क्या आप सारे dot notations field-दर-field लिखे बिना एक record variable को सीधे दूसरे record variable को assign कर सकते हैं (जैसे v_pass1 := v_pass2;)? tap करने से पहले type mapping मन में सोचिए।

Show the answer

हाँ, आप कर सकते हैं! बशर्ते दोनों record variables बिल्कुल एक ही User-Defined Record TYPE definition block इस्तेमाल करके declared हों, आप एक ही बार में एक पूर्ण block copy assignment statement execute कर सकते हैं। Oracle सारी internal field contents को containers में तुरंत copy कर देगा।

Theory

Semester 4 Lab Project Architecture

अपने end-of-semester lab presentations के लिए जटिल library management या banking applications design करते समय, अपने code को साफ़ करने के लिए records इस्तेमाल कीजिए। अगर एक PL/SQL Function को कई values return करने हों (जैसे एक student का eligibility status और उनकी total books checked out), आप scalar types इस्तेमाल नहीं कर सकते। अपनी modular data pipeline को साफ़ और सुंदर रखने के लिए इसके बजाय एक अकेला custom record type return कीजिए!

Summary

Key takeaways

  • User-defined records developers को अनोखे, multi-field composite data structures बनाने देते हैं।
  • records बनाने को एक two-step नृत्य चाहिए: पहले structure type परिभाषित करें, फिर variable declare करें।
  • individual internal attributes dot notation syntax (variable_name.field_name) इस्तेमाल करके पढ़े और update किए जाते हैं।
  • Records localized architectural लचीलापन देते हैं जिससे table %ROWTYPE anchors मेल नहीं खा सकते।
  • पूरे record blocks पर सीधे boolean comparisons (=, !=) PL/SQL में पूरी तरह अवैध हैं।
  • Memory hook: पहले type layout map करो, फिर अपने custom box को नाम दो, और एक dot इस्तेमाल करके fields चुनो!

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 and Iterative Statements

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

User Defined Record · Concepts of Relational Database Management Systems · Gri-Learn