mysql set跟enum记录的详解
mysql set和enum记录
Definition of a ENUM or SET column does act as a constraint on values entered into the column.?An error occurs for values that do not satisfy these conditions:
An ENUM value must be one of those listed in the column definition, or the internal numeric equivalent thereof.?The value cannot be the error value (that is, 0 or the empty string).?For a column defined as ENUM('a','b','c'), values such as '', 'd', or 'ax' are illegal and are rejected.
A SET value must be the empty string or a value consisting only of the values listed in the column definition separated by commas.?For a column defined as SET('a','b','c'), values such as 'd' or 'a,b,c,d' are illegal and are rejected.
以上为手册中说明的详细信息;
SET是一个字符串对象,可以有零或多个值,其值来自表创建时规定的允许的一列值。指定包括多个SET成员的SET列值时各成员之间用逗号(‘,’)间隔开。这样SET成员值本身不能包含逗号。 例如,指定为SET('one', 'two') NOT NULL的列可以有下面的任何值: '' 'one' 'two' 'one,two' SET最多可以有64个不同的成员。 当创建表时,SET成员值的尾部空格将自动被删除。
当检索时,保存在SET列的值使用列定义中所使用的大小写来显示。请注意可以为SET列分配字符集和 校对规则。对于二进制或大小写敏感的校对规则,当为列分配值时应考虑大小写。 MySQL用数字保存SET值,所保存值的低阶位对应第1个SET成员。
如果在数值上下文中检索一个SET值,检索的值的位设置对应组成列值的SET成员。
例如,你可以这样从一个SET列检索数值值:
mysql> SELECT set_col+0 FROM tbl_name;
如果将一个数字保存到SET列中,数字中二进制表示中的位确定了列值中的SET成员。对于指定为SET('a','b','c','d')的列,
成员有下面的十进制和二进制值: SET成员 十进制值 二进制值 'a' 1 0001 'b' 2 0010 'c' 4 0100 'd' 8 1000 ? 如果你为该列分配一个值9,其二进制形式为1001,因此第1个和第4个SET值成员'a'和'd'被选择,结果值为 'a,d'。
对于包含多个SET元素的值,当插入值时元素所列的顺序并不重要。
在值中一个给定的元素列了多少次也不重要。
当以后检索该值时,值中的每个元素出现一次,根据表创建时指定的顺序列出元素。
例如,
假定某个列指定为SET('a','b','c','d'):
mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); 插入值'a,d'、'd,a'、'a,d,d'、'a,d,a'和'd,a,d': mysql> INSERT INTO myset (col) VALUES -> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d'); Query OK, 5 rows affected (0.01 sec) Records: 5 ?Duplicates: 0 ?Warnings: 0
当检索时所有这些值显示为 'a,d':
mysql> SELECT col FROM myset; +------+ | col ?| +------+ | a,d ?| | a,d ?| | a,d ?| | a,d ?| | a,d ?| +------+ 5 rows in set (0.04 sec)
如果将SET列设置为一个不支持的值,则该值被忽略并发出警告:
mysql> INSERT INTO myset (col) VALUES ('a,d,d,s'); Query OK, 1 row affected, 1 warning (0.03 sec) ? mysql> SHOW WARNINGS; +---------+------+------------------------------------------+ | Level ? | Code | Message ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| +---------+------+------------------------------------------+ | Warning | 1265 | Data truncated for column 'col' at row 1 | +---------+------+------------------------------------------+ 1 row in set (0.04 sec) ? mysql> SELECT col FROM myset; +------+ | col ?| +------+ | a,d ?| | a,d ?| | a,d ?| | a,d ?| | a,d ?| | a,d ?| +------+ 6 rows in set (0.01 sec)
SET值按数字顺序排序。NULL值排在非NULL SET值的前面。 通常情况,可以使用FIND_IN_SET()函数或LIKE操作符搜索SET值:
mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0; mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%';
第1个语句找出SET_col包含value set成员的行。第2个类似,但有所不同:它在其它地方找出set_col包含value的行,甚至是在另一个SET成员的子字符串中。 下面的语句也是合法的:
mysql> SELECT * FROM tbl_name WHERE set_col & 1; mysql> SELECT * FROM tbl_name WHERE set_col = 'val1,val2';
第1个语句寻找包含第1个set成员的值。第2个语句寻找一个确切匹配的值。
应注意第2类的比较。
将set值与'val1,val2'比较返回的结果与同'val2,val1'比较返回的结果不同。
指定值时的顺序应与在列定义中所列的顺序相同。 如果想要为SET列确定所有可能的值,使用SHOW COLUMNS FROM tbl_name LIKE set_col并解析输出中第2列的SET定义。
刚才研究MySQL文档,发现SET类型的真正含义: ? 实际上,SET可以包含最多64个成员,其值为一个整数。这个整数 的二进制码表示该SET的值的哪些成员为真。例如有
SET('a','b','c','d'), 那么当它们的值为: ? SET member ?Decimal value ?Binary value ----------------------------- a 1 0001 b ?2 0010 c ?4 ?0100 d ?8 ?1000
如果你将9存入某个SET域,那么其二进制值为1001,也就是说这 个值中'a'和'd'为真。 ? 可以想到,如果这样的话,大家可以用LIKE命令和FIND_IN_SET() 函数来检索SET值:
? mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%'; mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0; ?
当然,以下SQL语句也是合法的,他们显得更加简洁:
? mysql> SELECT * FROM tbl_name WHERE set_col = 'val1,val2'; mysql> SELECT * FROM tbl_name WHERE set_col & 1; ? ? enum mysql> show create table b; +-------+-------------------------------------------------------------------------------------------------+ | Table | Create Table ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| +-------+-------------------------------------------------------------------------------------------------+ | b ? ? | CREATE TABLE `b` ( `f1` enum('a','b','c') DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |? +-------+-------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) ? ? mysql> select * from b; Empty set (0.00 sec) ? mysql> insert into b values(1); Query OK, 1 row affected (0.00 sec) ? mysql> insert into b values(2);? Query OK, 1 row affected (0.00 sec) ? mysql> insert into b values(3);? Query OK, 1 row affected (0.00 sec) ? mysql> insert into b values(4);? Query OK, 1 row affected, 1 warning (0.00 sec) ? mysql> select * from b;? +------+ | f1 ? | +------+ | a ? ?|? | b ? ?|? | c ? ?|? | ? ? ?|? +------+ 4 rows in set (0.00 sec) ?
对于enum类型一次只能是列举的某一个数值,大约个数或是0时,插入的值为空。
?
set类型适合用于复合值(类似多项选择),enum适合于单值选择(类似单项选择)。
以上就是mysql set跟enum记录的详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
