数据库-列的类型-Enum类型和set类型

Enum类型

create database EnumTypeStudy;

use EnumTypeStudy;

#创建一个表  enum 单选项 只能选择一个 enum 最多有65535个组合
create table EnumTypes(etype enum('男','女','保密'));

insert into Enumtypes values('男');
select *from enumtypes;

# 如果插入的是数字?
insert into Enumtypes values(1);
insert into Enumtypes values(2);
insert into Enumtypes values(3);
#男 女 保密 对应着 数值 1 2 3 显示我显示的是男 女 保密 就像 c++ 键值对一样 
select *from enumtypes;

# 若是想看看到底是不是存的是数字
select etype + 0 from EnumTypes; 

#当 enum 加入非空 默认取第一个值为准
# 没有加入非空
create table EnumTypes1(etype enum('男','女','保密'), name varchar(32));
insert into EnumTypes1(name) values("张三");

select *from EnumTypes1;
/*
select *from EnumTypes1;
+-------+------+
| etype | name |
+-------+------+
| NULL  | 张三 |
+-------+------+
1 row in set (0.00 sec)
*/
# 若指定not null,加上只插入姓名 那么默认取第一个值 为不为空的值 
create table EnumTypes2(etype enum('男','女','保密')not null, name varchar(32));

insert into EnumTypes2(name) values("张三");
select *from EnumTypes2;
/*
+-------+------+
| etype | name |
+-------+------+
| 男    | 张三 |
+-------+------+
1 row in set (0.00 sec)
*/
insert into EnumTypes2 values('女',"李四");
select *from EnumTypes2;
/*
+-------+------+
| etype | name |
+-------+------+
| 男    | 张三 |
| 女    | 李四 |
+-------+------+
*/

set类型

#列的类型set类型
create database setTypeStudy;

use setTypeStudy;
#创建一个表  set 多选项 只能选择 多个 只要值在范围  最多有64个组合 set( 值1,.....值64)
create table setTypes(stypes set('唱','跳','rap','篮球'));

insert into settypes values("唱,跳");
select* from settypes;

insert into settypes values("rap,跳");
select* from settypes;

insert into settypes values("篮球,rap");
select* from settypes;

select stypes+0 from settypes;
select bin(stypes+1) from settypes;
insert into settypes values("唱,跳,rap");
select bin(stypes+1) from settypes;
insert into settypes values("唱,跳,rap,篮球");
select bin(stypes+1) from settypes;



mysql> insert into settypes values("唱,跳");
Query OK, 1 row affected (0.01 sec)

mysql> select* from settypes;
+--------+
| stypes |
+--------+
| 唱,跳  |
+--------+
1 row in set (0.00 sec)

mysql> insert into settypes values("rap,跳");
Query OK, 1 row affected (0.01 sec)

mysql> select* from settypes;
+--------+
| stypes |
+--------+
| 唱,跳  |
| 跳,rap |
+--------+
2 rows in set (0.00 sec)

mysql>
mysql> select stypes+0 from settypes;
+----------+
| stypes+0 |
+----------+
|        3 |
|        6 |
|       12 |
+----------+
3 rows in set (0.00 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
+---------------+
3 rows in set (0.00 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
| 1000          |
+---------------+
4 rows in set (0.00 sec)

mysql> select stypes+0 from settypes;      
+----------+
| stypes+0 |
+----------+
|        3 |
|        6 |
|       12 |
|        7 |
+----------+
4 rows in set (0.00 sec)

mysql> insert into settypes values("唱,跳,rap,篮球");
Query OK, 1 row affected (0.01 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
| 1000          |
| 10000         |
+---------------+
5 rows in set (0.00 sec)

mysql> select stypes+0 from settypes;      
+----------+
| stypes+0 |
+----------+
|        3 |
|        6 |
|       12 |
|        7 |
|       15 |
+----------+
5 rows in set (0.00 sec)

mysql> delete from settypes;
Query OK, 5 rows affected (0.01 sec)

mysql> insert into settypes values("唱,跳");
Query OK, 1 row affected (0.01 sec)

mysql> select* from settypes;
+--------+
| stypes |
+--------+
| 唱,跳  |
+--------+
1 row in set (0.00 sec)

mysql> insert into settypes values("rap,跳");
Query OK, 1 row affected (0.01 sec)

mysql> select* from settypes;
+--------+
| stypes |
+--------+
| 唱,跳  |
| 跳,rap |
+--------+
2 rows in set (0.00 sec)

mysql>
mysql> insert into settypes values("篮球,rap");
Query OK, 1 row affected (0.01 sec)

mysql> select* from settypes;
+----------+
| stypes   |
+----------+
| 唱,跳    |
| 跳,rap   |
| rap,篮球 |
+----------+
3 rows in set (0.00 sec)

mysql>
mysql> select stypes+0 from settypes;
+----------+
| stypes+0 |
+----------+
|        3 |
|        6 |
|       12 |
+----------+
3 rows in set (0.00 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
+---------------+
3 rows in set (0.00 sec)

mysql> insert into settypes values("唱,跳,rap");
Query OK, 1 row affected (0.01 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
| 1000          |
+---------------+
4 rows in set (0.00 sec)

mysql> insert into settypes values("唱,跳,rap,篮球");
Query OK, 1 row affected (0.01 sec)

mysql> select bin(stypes+1) from settypes;
+---------------+
| bin(stypes+1) |
+---------------+
| 100           |
| 111           |
| 1101          |
| 1000          |
| 10000         |
+---------------+
5 rows in set (0.00 sec)

你可能感兴趣的:(小森数据库技术总结,数据库)