User Defined Record

User defined records તમને જુદા જુદા datatypes ના variables ને એક જ custom ડબ્બામાં બાંધવા દે છે.

9 min read · 10 cards · 2 checks

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


Theory

ન મળતી વિગતોને એક જ Variable માં બાંધવી

કલ્પો કે TicketDesk પર તમારા manager ને એક આપોઆપ ચાલતો report જોઈએ છે જે એક ticket નું લખાણનું title, એનું આંકડાકીય priority નું સ્તર, અને સોંપાયેલા agent નું લખાણનું નામ પકડે. જો તમે દરેક ટુકડા માટે અલગ scalar variables બનાવો, તમારો જાહેરાતનો વિભાગ જલદી ગૂંચવાઈ જાય છે. જ્યારે તમે આ અલગ variables એક procedural function માં આપો, તમારા parameters કાબૂ બહાર વધી જાય છે. તો તમે આ જુદા datatypes ને એક જ custom પેકેજમાં કેવી રીતે જૂથબદ્ધ કરો જે તમારી application ની logic માંથી સાથે ફરે?

Theory

ખાસ બનાવેલું Combo Meal

Scalar variables ને વસ્તુઓ અલગ અલગ ખરીદવા જેવા વિચારો: એક burger, થોડી fries, અને એક ઠંડું પીણું. તમારે દરેક વસ્તુ હાથમાં અલગ ઊંચકવી પડે, એક પડી જવાની શક્યતા વધારતાં. એક user defined record એટલે એક જ ખાસ બનાવેલી પૂંઠાની ટ્રેમાં પેક કરેલું combo meal મંગાવવું. તમે ટ્રેનો આકાર બિલકુલ એક burger, એક fry નું પડીકું, અને એક પીણું બેસે એવો બનાવો છો. હવે, તમે આખી ટ્રે એક જ હલનચલનમાં counter પર આપી શકો છો.

Theory

User-Defined Records ની ઔપચારિક વ્યાખ્યા

Oracle ના dialect માં, એક user defined record એ એક composite data નું માળખું છે જે જુદા datatypes નાં અનેક સંબંધિત fields ને એક જ નામ હેઠળ જૂથબદ્ધ કરે છે. %ROWTYPE થી વિપરીત, જે સખ્તાઈથી એક આખા table ના પહેલેથી વ્યાખ્યાયિત હરોળના માળખા સાથે બંધાય છે, એક user-defined record programmer દ્વારા સંપૂર્ણપણે ખાસ બનાવાય છે. તમે TYPE ... IS RECORD syntax વાપરીને નકશો જાહેર કરો છો, અને પછી database ની memory માં મિશ્ર data types સંઘરવા એ નકશામાંથી record ના variables બનાવો છો.

At a glance

Oracle %ROWTYPE records અને custom user defined records વચ્ચેની સરખામણી

લક્ષણ%ROWTYPE RecordUser Defined Record
માળખાનો સ્રોતએક હયાત table કે view માંથી આપોઆપ નકલ થયેલોCode માં programmer દ્વારા સ્પષ્ટપણે રચાયેલો
Field ની લવચીકતાTable ના દરેક column ને બિલકુલ ક્રમમાં સામેલ કરવો જ પડેચોક્કસ columns પસંદ કરી શકે કે અનેક tables નાં fields ભેળવી શકે
જાહેરાતનું પગલુંએક પગલું: variable નું નામ table_name%ROWTYPE વાપરોબે પગલાં: TYPE નો નકશો વ્યાખ્યાયિત કરો, પછી variable જાહેર કરો
Custom FieldsTable ના ન હોય એવા variables કે ગણેલાં fields સામેલ કરી શકતું નથીTable ના data સાથે સ્વતંત્ર counters કે વધારાના flags સામેલ કરી શકે

Practical

Declaring and Using a Custom Record Block

-- 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 ને બદલવા સાચી 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 ની અંદરનાં અલગ fields સુધી પહોંચવા dot notation વાપરે છે. તમે variable નું નામ લખો છો, પછી એક બિંદુ, અને પછી field નું નામ. Option 0 C++ જેવી ભાષાઓની pointer notation વાપરે છે જે ખોટી છે. Option 2 ચોક્કસ variable ના દાખલાને બદલે નકશાના type ના નામનો સંદર્ભ લે છે.

Think first

માનસિક પડકાર: Tables જોડવાં

શું તમે એક જ user defined record ના નકશાની અંદર tickets table માંથી એક field અને agents table માંથી એક field સામેલ કરી શકો? Tap કરીને ઉજાગર કરતાં પહેલાં મનમાં field ની જાહેરાતો વિશે વિચારો.

Show the answer

હા! એ user defined records ના મુખ્ય ફાયદાઓમાંનો એક છે. તમે એક જ record નો type વ્યાખ્યાયિત કરી શકો જ્યાં field એક tickets.title%TYPE હોય અને field બે agents.name%TYPE હોય. આ તમને જોડાયેલો data એક જ એકીકૃત record ના variable માં સરળતાથી લાવવા દે છે.

Watch out

બે પગલાંની જાહેરાતની ભૂલ

University ની lab exams માં એક classic ભૂલ છે પહેલાં એક variable જાહેર કર્યા વગર સીધો એક user-defined record વાપરવાનો પ્રયાસ કરવો. t_ticket_summary.ticket_title := 'Crash'; લખવાથી તરત એક compilation error ઊભી થશે. યાદ રાખો કે TYPE એ ખાલી એક અમૂર્ત નકશો છે, તમારા મનમાંનો એક આકાર. એ memory ની જગ્યા રોકતું નથી. Data ની values વાંચતાં કે લખતાં પહેલાં તમારે હંમેશા એ type માંથી એક નક્કર variable નો દાખલો બનાવવો જ પડશે.

Theory

Production API Pipelines અને ભવિષ્યનાં Semesters

Enterprise software માં, records સાફ programming ના interfaces બનાવવા વપરાય છે. 20 અલગ input variables વાળાં database નાં functions લખવાને બદલે, તમે એક જ record variable આપો છો, code ને modular અને વાંચી શકાય એવો રાખતાં. SQLite વાપરતા તમારા Sem 3 ના mobile databases ના અભ્યાસક્રમમાં, તમે હરોળનો data સંરચનાત્મક model classes માં જૂથબદ્ધ કરશો જે બિલકુલ આ PL/SQL custom records જેવું જ કામ કરે છે.

Summary

Key takeaways

  • એક user defined record જુદા datatypes નાં અનેક fields ને એક જ સંયુક્ત એકમમાં જૂથબદ્ધ કરે છે.
  • એ table સાથે જકડાયેલા rowtype variables ની સરખામણીએ સંપૂર્ણ માળખાકીય લવચીકતા આપે છે.
  • એક record બનાવવા બે પગલાંની પ્રક્રિયા જોઈએ: TYPE નું માળખું વ્યાખ્યાયિત કરવું, પછી એક variable નો દાખલો જાહેર કરવો.
  • Dot notation variable ની અંદરનાં ચોક્કસ fields વાંચવા અને બદલવા સીધો કામગીરીનો પ્રવેશ આપે છે.
  • Record ના દાખલા raw values ને ઝૂમખાબદ્ધ કરીને જટિલ database ની routines માં argument આપવાનું સરળ બનાવે છે.
  • Memory hook: Custom ટ્રેનો નકશો ડિઝાઇન કરો, ખોખું બનાવો, અને એક બિંદુથી પહોંચો!

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