Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

การใช้ User-Defined Variables ใน MySQL

User variables ใน MySQL และวิธีใช้ พร้อมกติกา:

  • User variables เขียนเป็น @var_name (var_name = alphanumeric, ., _, $)
  • Variable name มี special characters ได้ถ้า quoted as string/identifier
  • User-defined variables เป็น session-specific
  • Variable ที่ client หนึ่ง define ไม่เห็น/ใช้ได้โดย other clients
  • Exception: user ที่เข้าถึง Performance Schema user_variables_by_thread table เห็น all user variables ทุก sessions
  • Variables ทั้งหมดของ client session จะ free อัตโนมัติตอน client exit
  • User variable names ไม่ case-sensitive, max length 64 characters

Syntax

SET statement กำหนด user-defined variable:

SET @var_name = expr [, @var_name = expr] ...

ใช้ = หรือ := เป็น assignment operator

Assigning Values

User variables assign ค่าได้จาก limited data types: integer, decimal, floating-point, binary/nonbinary string, NULL

  • Decimal/real values assignment ไม่ preserve precision/scale
  • Type อื่นที่ไม่ permissible จะ convert เป็น permissible type (same coercibility as table column values)
  • MySQL user variables ใช้ remember results โดยไม่ store ใน temporary variables ใน client
  • ใช้ store data โดยไม่ explicitly assign memory location