Home  >  Article  >  Database  >  mysql中的enum和set类型_MySQL

mysql中的enum和set类型_MySQL

WBOY
WBOYOriginal
2016-06-01 13:38:56921browse

bitsCN.com


mysql中的enum和set类型

 

mysql中的enum和set其实都是string类型的而且只能在指定的集合里取值, 

不同的是set可以取多个值,enum只能取一个 

Sql代码  

CREATE TABLE `20121101_t` (  

  `id` int(11) NOT NULL AUTO_INCREMENT,  

  `name` varchar(20) NOT NULL,  

  `cl` set('x','w','r') NOT NULL,  

  `c2` enum('f','d') NOT NULL,  

  PRIMARY KEY (`id`)  

) ENGINE=InnoDB   

 

insert into 20121101_t  

values(null,'a.txt','r,w','d');  

  

insert into 20121101_t  

values(null,'b.txt','r,w','f');  

 

比如给b.txt文件加上执行权限 

Sql代码  

update 20121101_t set cl = cl|1 where id =2  

 

1是因为x权限出现在了第一个 

 

再比如给b.txt文件去掉写权限 

Sql代码  

update 20121101_t set cl = cl&~2 where id =2  

 

这时再看 

Sql代码  

select * from 20121101_t  

 

1 a.txt w,r d 

2 b.txt x,r f 

 

可以仿照linux下chmod的用法,直接用数字表示权限 

比如把b.txt变成只读 

Sql代码  

update 20121101_t set cl = 4 where id =2  

 

比如要找到所有包含了读权限的文件 

Sql代码  

select * from 20121101_t where cl&4  

或者 

Sql代码  

select * from 20121101_t where FIND_IN_SET('r',cl)>0  

 

enum就相对简单了,比如把a.txt从文件夹变成文件 

Sql代码  

update 20121101_t set c2 = 'f' where id =1  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn