Variables, Constants and Data Type

Variables named memory containers हैं जो बदलता data store करते हैं, constants locked boxes हैं जो कभी नहीं बदलते, और data types परिभाषित करते हैं कि उनके अंदर किस तरह का data fit होता है।

10 min read · 10 cards · 2 checks

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


Theory

Dynamic College Registry

कल्पना कीजिए peak college admissions के दौरान CampusLib application manage करना। एक student का residential address या phone number semesters में कई बार बदल सकता है, पर उनका University Roll Number पहले दिन से स्थायी रूप से जमा रहता है। database programming में, आप values hardcode नहीं कर सकते क्योंकि कुछ data पानी की तरह बहता है, जबकि दूसरा data पूरी तरह पत्थर में जड़ा है। PL/SQL में इसे सुंदर ढंग से सँभालने के लिए, हमें variables, constants, और data types पर महारत हासिल करनी होगी।

Theory

Labelled Storage Boxes

एक variable को 'Lunch' labeled एक plastic tiffin box की तरह सोचिए। आज इसमें maggi है; कल आप इसे धोकर fried rice से भर सकते हैं। box label वही रहता है, पर भीतरी content बदलता है। एक constant एक sealed, stamped university exam envelope की तरह है, एक बार central office में pack होने पर, कोई regulations तोड़े बिना अंदर जो है उसे बदल नहीं सकता। एक Data Type बस box का physical आकार है: एक round thermos केवल liquid items रख सकता है (जैसे DATE या NUMBER), जबकि एक flat folder text की sheets रखता है (जैसे VARCHAR2)।

Theory

Smart Memory Allocate करना

औपचारिक रूप से, एक Variable एक named memory location है जिसका value program execution के दौरान उतार-चढ़ाव कर सकता है, जबकि एक Constant CONSTANT keyword इस्तेमाल करता है ताकि इसका initialized value पूरे transaction block में read-only रहे। basic scalar types से आगे, Oracle एक शानदार shorthand tool पेश करता है: %TYPE attribute। v_name members.name%TYPE; लिखकर, आप Oracle को उस table column से बिल्कुल data type और length configuration अपने-आप copy करने को कहते हैं, code crashes रोकते हुए अगर एक system administrator बाद में database field size बढ़ाता है।

At a glance

PL/SQL में data storage elements के मूल संरचनात्मक गुण।

Element TypeSyntax Layout RuleValue MutabilityBest Used For
Variablevariable_name data_type [:= value];BEGIN block के भीतर कभी भी overwrite किया जा सकता हैuser inputs, loop counters, या single query results store करना
Constantconstant_name CONSTANT data_type := value;सख़्ती से read-only; reassign होने पर एक compile error फेंकता हैFixed tax rates, अधिकतम book check-out limits, या mathematical constants
%TYPE Anchorvariable_name table.column%TYPE;base column के type rules पूरी तरह inherit करता हैअधिकतम data lengths hardcode किए बिना database row values सुरक्षित रूप से fetch करना

Practical

Memory Anchors Declare और Test करना

-- A sample block demonstrating safe variable and constant declarations
DECLARE
  -- Constant: Max books allowed must be initialized immediately
  MAX_BOOKS_ALLOWED CONSTANT NUMBER := 5;
  
  -- Smart Variable: Automatically inherits data type from CampusLib schema
  v_book_title      books.title%TYPE;
  
  -- Standard Variable with a default initialization value
  v_current_fine    NUMBER(5,2) := 0.00;
BEGIN
  -- Assigning a value to our smart variable using the proper operator
  v_book_title := 'Mastering Oracle';
  v_current_fine := 45.50;
  
  DBMS_OUTPUT.PUT_LINE('Book Title: ' || v_book_title);
  DBMS_OUTPUT.PUT_LINE('Fine Amount: Rs. ' || v_current_fine);
  DBMS_OUTPUT.PUT_LINE('Max Limit: ' || MAX_BOOKS_ALLOWED);
END;
/

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

Follow along

Oracle Runtime पर %TYPE कैसे Process करता है

  1. 1. Schema Check Oracle compiler declaration section पढ़ता है और database dictionary में target table (जैसे books) खोजता है।
  2. 2. Structure Mirroring यह निर्दिष्ट column (जैसे VARCHAR2(100)) का data type और सटीक byte size तुरंत निकालता है।
  3. 3. Memory Allocation compiler आपके local PL/SQL variable configuration के लिए एक समान internal storage buffer allocate करता है।
  4. 4. Decoupled Execution variable सुरक्षित रूप से चलता है भले database administrators बाद में column sizes बदलें, मैनुअल code updates ख़त्म करते हुए।

Quiz

क्या होता है अगर आप DECLARE section में CONSTANT keyword इस्तेमाल करके एक constant declare करते हैं पर वहीं इसे एक initial value assign करना भूल जाते हैं?

  1. Oracle अपने-आप 0 या एक empty string का एक default value assign करता है।
  2. Oracle आपको इसे blank declare करने और बाद में BEGIN block के अंदर इसका value assign करने देता है।
  3. Oracle एक compilation error फेंकता है क्योंकि constants को declaration के बिल्कुल पल पर initialize किया जाना होगा।
  4. block सफलतापूर्वक compile होता है पर constant को अपने-आप एक standard variable में बदल देता है।
Show the answer

Oracle एक compilation error फेंकता है क्योंकि constants को declaration के बिल्कुल पल पर initialize किया जाना होगा।

क्योंकि constants पूरी तरह read-only हैं, उन्हें बाद में executable block में modify नहीं किया जा सकता। इसलिए, Oracle सख़्ती से लागू करता है कि आपको एक constant को इसके declaration के दौरान सीधे assignment operator (:=) या DEFAULT keyword इस्तेमाल करके एक value से initialize करना होगा।

Watch out

Silent NULL Arithmetic जाल

अगर आप v_total_fine NUMBER; जैसा एक standard numeric variable declare करते हैं और इसे := 0 से स्पष्ट रूप से initialize नहीं करते, Oracle इसकी default संरचनात्मक अवस्था NULL पर set करता है। SQL और PL/SQL में, एक NULL value पर की गई कोई भी mathematical operation तुरंत NULL में परिणत होती है (जैसे 50 + NULL = NULL)। अगर आप एक uninitialized variable में एक late fee जोड़ने की कोशिश करते हैं, आपकी कुल calculation एक blank space में ग़ायब हो जाती है! numeric counters को हमेशा 0 पर initialize कीजिए।

Think first

Dynamic Columns Schema Test

Mental Challenge: अगर आप अपने database schema में एक table की column definition VARCHAR2(50) से VARCHAR2(100) में बदलते हैं, क्या आपको एक PL/SQL block को हाथ से खोजना, फिर से लिखना, और recompile करना होगा जो उस column को %TYPE के ज़रिए reference करता है? tap करने से पहले इसे सोचिए।

Show the answer

नहीं, नहीं होगा! यह %TYPE का शुद्ध maintenance जादू है। अगली बार जब PL/SQL block execute होता है, Oracle dynamically updated data dictionary जाँचता है, नई VARCHAR2(100) allocation size का पता लगाता है, और इसे आपके variable पर एक भी line मैनुअल code edits की ज़रूरत के बिना अपने-आप लागू करता है।

Summary

Key takeaways

  • Variables उतार-चढ़ाव करते data buffers रखते हैं जिन्हें executable block के भीतर कभी भी overwrite किया जा सकता है।
  • Constants CONSTANT keyword इस्तेमाल करते हैं और declaration पर तुरंत एक अपरिवर्तनीय value assign होनी होगी।
  • Scalar data types text strings, numbers, dates, और truth conditions जैसे basic individual values cover करते हैं।
  • %TYPE attribute local variables को असल database schema columns से dynamically bind करता है।
  • Uninitialized variables NULL पर default होते हैं, जो अनजाँचे छोड़ने पर mathematical calculations को चुपचाप मिटा सकता है।
  • Memory hook: Variables ढीले pages हैं, constants hardcovers हैं, और %TYPE एक live संरचनात्मक mirror है!

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

Variables, Constants and Data Type · Concepts of Relational Database Management Systems · Gri-Learn