// MySQL Developer
MySQL Developer
คู่มือสำหรับนักพัฒนา — Stored Procedures, Triggers, Functions, Cursors และ Transactions
// Topics
หัวข้อสำหรับ MySQL Developer
Stored Procedures
เขียน Business Logic ไว้ใน Database ลด Network Round-trips และเพิ่ม Security ด้วย Parameterized Procedures
Triggers
กำหนด Action ที่จะเกิดขึ้นโดยอัตโนมัติเมื่อมี INSERT, UPDATE หรือ DELETE บน Table
Cursors
ประมวลผลข้อมูลทีละแถวด้วย Cursor — เหมาะสำหรับงานที่ต้องการ Row-by-Row Processing
Transactions
รักษา Data Integrity ด้วย BEGIN, COMMIT, ROLLBACK — ACID Compliance ด้วย InnoDB
Functions
สร้าง Custom MySQL Functions ที่สามารถใช้ใน SQL Statement เช่นเดียวกับ Built-in Functions
Date & Time Functions
จัดการวันที่และเวลาด้วย NOW(), DATE_FORMAT(), DATEDIFF() และ Function อื่นๆ
// Code Examples
ตัวอย่าง Code
Stored Procedure
DELIMITER //
CREATE PROCEDURE GetUserOrders(
IN p_user_id INT,
IN p_status VARCHAR(20)
)
BEGIN
SELECT o.id, o.total, o.created_at
FROM orders o
WHERE o.user_id = p_user_id
AND o.status = p_status
ORDER BY o.created_at DESC;
END//
DELIMITER ;
-- Call it:
CALL GetUserOrders(123, 'pending');
Trigger Example
DELIMITER //
CREATE TRIGGER before_order_insert
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
IF NEW.total < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT =
'Total cannot be negative';
END IF;
SET NEW.created_at = NOW();
END//
DELIMITER ;
Transaction
START TRANSACTION;
INSERT INTO orders(user_id, total)
VALUES(1, 1500.00);
UPDATE inventory
SET qty = qty - 1
WHERE product_id = 42;
-- If everything OK:
COMMIT;
-- If error occurs:
-- ROLLBACK;
Custom Function
DELIMITER //
CREATE FUNCTION CalcTax(
amount DECIMAL(10,2)
) RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN amount * 0.07;
END//
DELIMITER ;
-- Use in SELECT:
SELECT total,
CalcTax(total) AS vat
FROM orders;
สนใจ MySQL Training?
หลักสูตรอบรม MySQL Developer สำหรับผู้ที่ต้องการเรียนรู้อย่างเป็นระบบ