Variables, Constants and Data Type, Assigning Values

PL/SQL variables और constants database memory में नामित बाल्टियों की तरह काम करते हैं जो सुरक्षित processing के लिए बदलते या निश्चित data values रखते हैं।

8 min read · 10 cards · 2 checks

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


Theory

एक अप्रत्याशित ticket identifier track करना

मान लीजिए आप एक incoming customer complaint track करने के लिए TicketDesk के लिए एक automated management script लिख रहे हैं। ticket ID number हर एक बार बदलता है जब एक customer एक नया issue submit करता है। urgency level अभी Critical हो सकता है, पर एक administrator कुछ मिनट बाद इसे Low में बदल सकता है। जब आपका server नियम evaluate करता है आप इन तरल information के टुकड़ों को कैसे पकड़ते और थामे रखते हैं? आपको database engine memory के अंदर एक अलग storage container चाहिए जो तुरंत values रख, पढ़, और manipulate कर सके।

Theory

लेबल किए Storage Bins

अपने Oracle database server को एक विशाल, व्यस्त warehouse floor की तरह सोचिए। अगर आप एक box बिना फ़र्श पर raw text फेंकते हैं, यह खो जाता है। एक variable एक ख़ाली plastic bin जैसा है जिस पर 'Numbers Only' दर्शाता एक sticky note है। एक constant एक भारी लकड़ी के crate जैसा है जो औद्योगिक गोंद से sealed है: एक बार आप अंदर एक item रखते और इसकी category declare करते हैं, कोई इसे बदल नहीं सकता। एक value assign करना बस उस ख़ास bin में एक item गिराना है।

Theory

PL/SQL Operations में Variables और Constants

Oracle PL/SQL में, एक variable एक नामित storage address है जिसकी internal सामग्री program execution के दौरान उतार-चढ़ाव कर सकती है। एक constant एक निश्चित data entity है जो initialization के बाद बदल नहीं सकती और इसे CONSTANT keyword चाहिए। हर container को consistency लागू करने के लिए एक परिभाषित datatype (जैसे NUMBER या VARCHAR2) चाहिए। आप इन placeholders को या तो assignment operator := इस्तेमाल करके manual computation के ज़रिए या एक SELECT INTO database query इस्तेमाल करके column records लाकर भरते हैं।

At a glance

Oracle PL/SQL engine में मुख्य declaration और assignment structures

Syntax Componentमुख्य Operational उद्देश्यExample Statement
Assignment Operatorएक declared variable को सीधे एक ख़ास value से overwrite करता हैv_status := 'Open';
CONSTANT Keywordएक memory slot lock करता है ताकि इसकी शुरुआती परिभाषा अपरिवर्तनीय होc_tax CONSTANT NUMBER := 0.18;
%TYPE Attributeएक column का मेल खाता datatype dynamically copy करता हैv_title tickets.title%TYPE;
SELECT INTO Clausedatabase fields को memory variables में खींचता हैSELECT status INTO v_status FROM tickets;

Practical

एक Block के भीतर Ticket Fields Declare और आवंटित करना

-- Enable console text printing in Oracle terminal
SET SERVEROUTPUT ON;

DECLARE
  -- Anchor variable data properties directly to our schema layout
  v_ticket_title tickets.title%TYPE;
  v_ticket_status VARCHAR2(20) := 'Unassigned';
  
  -- Establish a strict baseline tracking limit constant
  c_max_days CONSTANT NUMBER := 7;
BEGIN
  -- Update the tracking state using the assignment mechanism
  v_ticket_status := 'In Progress';
  
  -- Extract a live row directly into our localized variable placeholder
  SELECT title INTO v_ticket_title
  FROM tickets
  WHERE id = 101;
  
  DBMS_OUTPUT.PUT_LINE('Processing Title: ' || v_ticket_title);
  DBMS_OUTPUT.PUT_LINE('Current Status: ' || v_ticket_status);
  DBMS_OUTPUT.PUT_LINE('SLA Day Limit: ' || c_max_days);
END;
/

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

Quiz

कौन सा code configuration एक Oracle declaration zone के अंदर एक अधिकतम priority limit रखता एक अपरिवर्तनीय constant सही तरह बनाता है?

  1. c_limit NUMBER CONSTANT = 5;
  2. c_limit CONSTANT NUMBER := 5;
  3. c_limit NUMBER := 5 CONSTANT;
  4. c_limit CONSTANT NUMBER = 5;
Show the answer

c_limit CONSTANT NUMBER := 5;

Oracle PL/SQL में, एक constant को एक ख़ास structural pattern चाहिए: पहले identifier, फिर explicit CONSTANT keyword, फिर structural datatype, और अंतिम initialization := assignment operator के ज़रिए। एक अकेला equal sign = SQL queries में conditional matching के लिए आरक्षित है, allocation के लिए नहीं।

Think first

Mental Challenge: %TYPE के साथ Structural अनुकूलनशीलता

अगर एक system administrator database schema को tickets.title column की चौड़ाई VARCHAR2(100) से VARCHAR2(300) में बदलने के लिए modify करता है, buffer crashes से बचने के लिए %TYPE इस्तेमाल करते एक script में आपको क्या बदलाव करने होंगे? tap करने से पहले इसे आंतरिक रूप से विश्लेषित कीजिए।

Show the answer

आपको कोई code updates करने की ज़रूरत नहीं! यह anchoring %TYPE mechanism का मुख्य engineering फ़ायदा दर्शाता है। यह execution runtime पर अंतर्निहित table blueprint को dynamically reference करता है, अपने-आप विस्तारित scale से मेल खाते हुए और code compilation errors बिना block stability सुरक्षित रखते हुए।

Watch out

Laboratory Mismatch और Assignment जाल

University examiners अक्सर student scripts को दो classic allocation ग़लतियों पर पकड़ते हैं। पहली, execution expressions के अंदर := के बजाय अनजाने में एक सरल = type करना, जो एक तुरंत compilation failure trigger करता है। दूसरी, ऐसी queries पर SELECT INTO इस्तेमाल करना जो multi-row streams या zero records देती हैं। एक basic SELECT INTO बिल्कुल 1 match की उम्मीद करता है: कुछ भी और TOO_MANY_ROWS या NO_DATA_FOUND विसंगतियों के ज़रिए एक runtime crash मजबूर करता है।

Theory

Enterprise इस्तेमाल और Frontend Transitions

anchored variable allocation इस्तेमाल करना production analytics pipelines में ग़ैर-ज़रूरी text re-parsing रोकता है, transaction processing speeds बढ़ाते हुए। जैसे आप SQLite data frameworks इस्तेमाल करते अपने Sem 3 mobile architecture courses में transition करते हैं, आप parameterized lookup queries बनाएँगे जो इस बिल्कुल binding behavior की नक़ल करती हैं, frontend inputs को SQL injection कमज़ोरियों को जोखिम में डाले बिना सुरक्षित रूप से सीधे memory locations से जोड़ते हुए।

Summary

Key takeaways

  • Variables memory में निर्दिष्ट data bins दर्शाते हैं जिनके values block routines के दौरान लचीले रहते हैं।
  • Constants declaration के दौरान explicit CONSTANT keyword modifier इस्तेमाल करके initialization माँगते हैं।
  • विशेष operator combo := active block logic के भीतर internal value mapping सँभालता है।
  • %TYPE anchoring औज़ार अपने-आप live column properties विरासत में लेता है ताकि data mismatch errors ख़त्म करे।
  • SELECT INTO इस्तेमाल करती queries को exceptions से बचने के लिए बिल्कुल 1 मेल खाता record खोजना होगा।
  • Memory hook: layout परिभाषित करो, percent-type से anchor करो, colon-equals इस्तेमाल करके assign करो, और अकेली rows पकड़ो!

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

Variables, Constants and Data Type, Assigning Values · Mastering SQL - PL/SQL (SEC-02 option A) · Gri-Learn