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

ตั้งค่าและแสดงตัวเลือก SET ในคอลัมน์ MySQL

ใช้ SET('val1','val2',...) กำหนดตัวเลือก แล้วดูด้วย DESC หรือ INFORMATION_SCHEMA.COLUMNS

สร้างตารางคอลัมน์ SET

mysql> create table DemoTable ( 
    Color SET('RED','GREEN','BLUE','YELLOW') ); 
Query OK, 0 rows affected (0.47 sec)

วิธี 1: ดูด้วย DESC

mysql> desc DemoTable;
+-------+------------------------------------+------+-----+---------+-------+
| Field | Type                               | Null | Key | Default | Extra |
+-------+------------------------------------+------+-----+---------+-------+
| Color | set('RED','GREEN','BLUE','YELLOW') | YES  |     | NULL    |       |
+-------+------------------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

วิธี 2: ดูด้วย INFORMATION_SCHEMA

mysql> select column_type from information_schema.columns 
where table_name = 'DemoTable' and column_name = 'Color';
+------------------------------------+
| COLUMN_TYPE                        |
+------------------------------------+
| set('RED','GREEN','BLUE','YELLOW') |
+------------------------------------+
1 row in set (0.01 sec)

ข้อดีของ SET: จำกัดค่าที่รับได้ รับหลายค่าได้ (คั่นด้วยคอมม่า) จัดเก็บแบบบิตแมป ประหยัดพื้นที่