ให้นักศึกษาแต่ละกัน Import ข้อมูล company.sql จาก Course Material เข้า DB ของแต่ละคน และทำแบบฝึกหัดดังต่อไปนี้

1) สร้าง Stored Function ชื่อ discount() ตามรายละเอียดดังต่อไปนี้

  • Stored Function สำหรับคำนวณส่วนลด ตามเงื่อนไขดังนี้
    • ตั้งแต่ 100 ขึ้นไป ลด 10% (purch_amt >= 100)
    • ตั้งแต่ 1,000 ขึ้นไป ลด 20% (purch_amt >= 1000)
    • ตั้งแต่ 3,000 ขึ้นไป ลด 30% (purch_amt >= 3000)
  • โดยมี Syntax ดังนี้
    • discount(purch_amt)
  • ทดสอบการทำงานโดยใช้ SQL Command (SELECT) เพื่อแสดงข้อมูล ord_no, ord_date, purch_amt, discount(purch_amt) AS discount, purch_amt-discount(purch_amt) AS total

2) สร้าง Trigger ชื่อ orders_insert ที่ทำงานทุกครั้ง หลังการเพิ่มข้อมูล ในตาราง orders โดยเพิ่มข้อมูลใหม่ลงในตาราง orders_audit ดังนี้

  • log_date (ord_date)
  • ord_no
  • customer_id
  • salesman_id
  • action = ‘INSERT’

ทั้งสองข้อให้ทำบนฐานข้อมูลของตนเอง (u6613xxx)

Deadline: 31 October 2025

Categories:

Tags:

Comments are closed