Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 98

PL/SQL vs MySQL Stored Programs

PL/SQL और MySQL Stored Programs एक Language नहीं हैं

PL/SQL Oracle Database का procedural SQL extension है। MySQL procedures, functions, triggers और scheduled events के लिए अलग stored-program language देता है। Variables, conditions, loops, cursors और error handling जैसे concepts मिलते हैं, लेकिन grammar, objects और runtime behavior अलग हैं।

सही migration mindset: text नहीं, business rule translate करें। Inputs, outputs, SQL effects, errors, transaction boundaries, security context और performance पहचानकर target database के supported features में implement करें।

यह तुलना Oracle PL/SQL और MySQL 8.4 के लिए है। PL/SQL हर database की procedural SQL का generic नाम नहीं है।

PL/SQL vs MySQL Difference Matrix

AreaOracle PL/SQLMySQL
Anonymous blockDECLARE...BEGIN...EXCEPTION...ENDGeneral stored anonymous PL/SQL block नहीं; routine create या client SQL
Assignmentv_total := 10SET v_total = 10
Parameterp_id IN NUMBERIN p_id INT
StringVARCHAR2VARCHAR
ErrorEXCEPTION WHENHANDLER, SIGNAL, RESIGNAL
ModulePackage spec/bodyDirect package equivalent नहीं
OutputDBMS_OUTPUTSELECT result या structured log
RowsFETCH FIRST/ROWNUMLIMIT
NumberingIdentity/sequenceAUTO_INCREMENT

Types को similar name से नहीं; range, precision, character semantics और nullability से map करें।

Blocks, Declarations और Variables

Oracle block में optional declaration, required executable और optional exception part होता है। MySQL में declarations BEGIN...END के start पर आते हैं। Order important है: variables/conditions, फिर cursors, फिर handlers।

DELIMITER //
CREATE PROCEDURE show_customer_total(IN p_customer_id INT)
BEGIN
  DECLARE v_total DECIMAL(12,2) DEFAULT 0;
  SELECT COALESCE(SUM(total_amount), 0)
    INTO v_total
  FROM orders
  WHERE customer_id = p_customer_id;
  SELECT v_total AS paid_total;
END //
DELIMITER ;
Delimiter client instruction है: mysql client को semicolons वाला compound definition भेजने में मदद करता है। यह stored procedure का part नहीं और हर connector API में नहीं भेजा जाता।

Procedures, Functions और Parameters

दोनों named procedures/functions देते हैं, details अलग हैं। MySQL procedure में mode name से पहले: IN p_id INT, OUT p_total DECIMAL(12,2)। Function inputs लेती, RETURNS declare करती और RETURN चलाती है।

CALL customer_summary(101, @paid_total);
SELECT @paid_total;
SELECT service_fee(1500.00);

Oracle packages richer overloading और package state देते हैं। हर overload को same name में रखने का अनुमान न लगाएँ। Explicit names/signatures चुनें और session state को suitable table/application में redesign करें। MySQL में SQL SECURITY DEFINER/INVOKER; Oracle में AUTHID behavior है। Owners blindly copy न करें; least privilege rebuild करें।

Exceptions, Cursors और Diagnostics

Oracle error को EXCEPTION section में route करता है। MySQL block में handlers declare करता है:

DECLARE duplicate_key CONDITION FOR 1062;
DECLARE EXIT HANDLER FOR duplicate_key
BEGIN
  ROLLBACK;
  SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = 'Order already exists';
END;

CONTINUE execution जारी रखता; EXIT declaring block छोड़ता है। UNDO supported नहीं। Structured detail के लिए GET DIAGNOSTICS और current error preserve/augment करने के लिए RESIGNAL use करें।

MySQL cursors asensitive, read-only और nonscrollable हैं। Typical loop में CONTINUE HANDLER FOR NOT FOUND, OPEN, FETCH और CLOSE होते हैं। दोनों systems में set-based SQL first choice रखें।

Packages और Oracle-Specific Features का Redesign

Oracle package public declarations, private implementation, variables और overloads group करता है। MySQL 8.4 में direct spec/body object नहीं। Migration में:

  • billing_create_invoice जैसे routine prefixes;
  • ownership/deployment fit होने पर suitable schema;
  • orchestration और external services के लिए application modules;
  • durable state के लिए keyed, locked, retained tables;
  • narrow permission-controlled interfaces के लिए views/routines।
Package state को uncontrolled session variables से emulate न करें। Pooling, retries और concurrent requests में hidden state unsafe है।

Sequences, collections, records, %TYPE, %ROWTYPE, bulk collect, FORALL, autonomous transactions, ref cursors, database links, synonyms और Oracle built-ins के one-line equivalents assume न करें।

Worked Migration: Customer Spend

DELIMITER //
CREATE PROCEDURE customer_spend(
  IN  p_customer_id INT,
  OUT p_order_count INT,
  OUT p_paid_total DECIMAL(12,2)
)
SQL SECURITY INVOKER
READS SQL DATA
BEGIN
  IF p_customer_id IS NULL THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'customer_id is required';
  END IF;
  SELECT COUNT(*), COALESCE(SUM(total_amount), 0)
    INTO p_order_count, p_paid_total
  FROM orders
  WHERE customer_id = p_customer_id
    AND status = 'PAID';
END //
DELIMITER ;

CALL customer_spend(101, @orders, @total);
SELECT @orders AS paid_orders, @total AS paid_total;
paid_orders | paid_total 3 | 3350.00

Routine business contract preserve करती है, Oracle punctuation नहीं। Query set-based, NULL input explicit, output precision defined और security context intentional है।

Production Migration Checklist

  1. हर routine, package, trigger, job, dependency और caller inventory करें।
  2. Inputs, outputs, effects, errors, transaction owner लिखें।
  3. Range, precision, timezone, charset/collation से types map करें।
  4. Oracle-only SQL/built-ins के tested equivalents बनाएँ।
  5. Packages, state, autonomous work और bulk operations redesign करें।
  6. Exceptions को conditions, handlers और SIGNAL में convert करें।
  7. Set-based SQL prefer; cursors/per-row functions benchmark करें।
  8. Least-privileged definer/invoker privileges rebuild करें।
  9. Boundary, NULL, concurrency, rollback और retries test करें।
  10. Controlled Oracle baseline से results/row changes compare करें।

stored procedures, functions, cursors और transactions पढ़ें।

Official संदर्भ

Language structure और migration cautions official Oracle PL/SQL और MySQL 8.4 manuals से verify किए गए हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या MySQL में PL/SQL उपलब्ध है?
नहीं। PL/SQL Oracle Database की procedural SQL है। MySQL procedures, functions, triggers और events के लिए अपनी stored-program language देता है; concepts समान होने पर भी syntax interchangeable नहीं है।
MySQL में Oracle PL/SQL package का replacement क्या है?
MySQL में package specification/body का direct equivalent नहीं। Naming conventions, suitable schemas, routines और application modules use करें; package state को mechanically translate नहीं, redesign करें।
DBMS_OUTPUT.PUT_LINE को MySQL में कैसे बदलें?
Learning और diagnostics में procedure SELECT result set दे सकती है। Production logging के लिए authorized log table या application observability use करें, ad-hoc output नहीं।
PL/SQL exceptions को MySQL में कैसे migrate करें?
Named exceptions और SQLCODE logic को DECLARE CONDITION, DECLARE HANDLER, SIGNAL, RESIGNAL और GET DIAGNOSTICS से map करें। हर error और transaction path test करें।
क्या Oracle PL/SQL code सीधे MySQL में copy होगा?
आमतौर पर नहीं। Data types, packages, parameters, exceptions, sequences, autonomous transactions, dynamic SQL और built-in functions review या redesign चाहिए।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.