首页 > 其他分享 >修改set类型,确保将插入值与定义值是一致

修改set类型,确保将插入值与定义值是一致

时间:2023-09-23 23:32:40浏览次数:40  
标签:set 定义 football 插入 basketball mysql hobby YES NULL

mysql> insert into tb_demo(hobby)values('basketball'),('volleyball,football'),('football,football,basketball');
ERROR 1265 (01000): Data truncated for column 'hobby' at row 2

mysql> desc tb_demo;
+---------+------------------------------------------+------+-----+---------+-------+
| Field   | Type                                     | Null | Key | Default | Extra |
+---------+------------------------------------------+------+-----+---------+-------+
| id      | int(11)                                  | YES  |     | NULL    |       |
| name    | varchar(30)                              | YES  |     | NULL    |       |
| age     | tinyint(4)                               | YES  |     | NULL    |       |
| score   | float(4,1)                               | YES  |     | NULL    |       |
| sex     | enum('w','m')                            | YES  |     | NULL    |       |
| hobby   | set('football','basketball','volleyall') | YES  |     | NULL    |       |
| photo   | varbinary(255)                           | YES  |     | NULL    |       |
| spend   | decimal(5,1)                             | YES  |     | NULL    |       |
| address | json                                     | YES  |     | NULL    |       |
| intro   | text                                     | YES  |     | NULL    |       |
+---------+------------------------------------------+------+-----+---------+-------+
10 rows in set (0.00 sec)


mysql> alter table tb_demo modify hobby set('football','basketball','volleyball');
Query OK, 18 rows affected (0.02 sec)
Records: 18  Duplicates: 0  Warnings: 0

mysql> desc tb_demo;
+---------+-------------------------------------------+------+-----+---------+-------+
| Field   | Type                                      | Null | Key | Default | Extra |
+---------+-------------------------------------------+------+-----+---------+-------+
| id      | int(11)                                   | YES  |     | NULL    |       |
| name    | varchar(30)                               | YES  |     | NULL    |       |
| age     | tinyint(4)                                | YES  |     | NULL    |       |
| score   | float(4,1)                                | YES  |     | NULL    |       |
| sex     | enum('w','m')                             | YES  |     | NULL    |       |
| hobby   | set('football','basketball','volleyball') | YES  |     | NULL    |       |
| photo   | varbinary(255)                            | YES  |     | NULL    |       |
| spend   | decimal(5,1)                              | YES  |     | NULL    |       |
| address | json                                      | YES  |     | NULL    |       |
| intro   | text                                      | YES  |     | NULL    |       |
+---------+-------------------------------------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

mysql> insert into tb_demo(hobby)values('basketball'),('volleyball,football'),('football,football,basketball');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select hobby from tb_demo;
+---------------------+
| hobby               |
+---------------------+
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| NULL                |
| basketball          |
| football,volleyball |
| football,basketball |
+---------------------+
21 rows in set (0.00 sec)

mysql> delete  from tb_demo where hobby=' ' or hobby  IS NULL ;

mysql> select hobby from tb_demo;
+---------------------+
| hobby               |
+---------------------+
| basketball          |
| football,volleyball |
| football,basketball |
+---------------------+

操作技巧:

1、在使用SET类型时,一定要按照预先定义的选项进行数据插入。如果插入了不存在的选项,将导致插入失败。

2、如何删除某个字段为空值时的记录。

标签:set,定义,football,插入,basketball,mysql,hobby,YES,NULL
From: https://blog.51cto.com/sky9896/7581552

相关文章

  • 微信小程序自定义tabbar遮挡scroll-view问题
    在使用小程序开发时,底部为自定义导航栏,在使用scroll-view滚动页面时,滚动到底部时最后一条或多条数据被导航栏遮挡,如下:解决方案:1.获取用户手机宽度和高度letdeviceWidth=wx.getSystemInfoSync().windowWidth;//获取屏幕宽度letdeviceHeight=wx.getSystemInfoSync().win......
  • 自定义属性编辑器
    1,编写实体类Address.javapackagecom.mashibing.selfEditor;publicclassAddress{privateStringprovice;privateStringcity;privateStringtown;publicStringgetProvice(){returnprovice;}publicvoidsetProvice(Stringprovice)......
  • HTTP安全响应头配置之Set-Cookie
    Cooke请求头对应Cookie字段、响应头对应Set-Cookie字段建议安全设置的cookie值如下Set-Cookie:<key>=<value>;Expires=<expriesDate>[;domain=domain][;path=path];Secure;HttpOnly;SameSite=strictvalue:一般是键值对expires:表示会在xxx时间之后失效(浏览器不会再发送给服务器......
  • 自定义标签完成解析工作
    1、创建实体类User.javapackagecom.mashibing.selftag;publicclassUser{privateStringusername;privateStringemail;privateStringpassword;publicUser(){}publicUser(Stringusername,Stringemail,Stringpassword){this.use......
  • 1.单列集合(接口 Collection,List,Set)
    单列集合(接口Collection,List,Set)单列集合体系结构:特点:1.List系列集合: 添加的元素是有序、可重复、有索引;2.Set系列集合: 添加的元素是无序、不重复、无索引;3.有序为存入和取出都是一样的顺序,非内部里的顺序;Collection概念:Collection是单列集合的祖宗接口,它的功能......
  • 题解 CF1257G【Divisor Set】
    problem我们说一个集合\(D\)是一个好的集合,当不存在集合中的两个不同元素\(a,b\)使得\(a\)是\(b\)的约数。给定一个超大整数的素数表示形式\(N=\prod_{i=1}^n{p_i}\),要求从它的所有因子中选择尽可能多的元素组成一个好的集合。问这个最大的size是多少,输出模99824......
  • elasticsearch 自定义字典
    ......
  • Vue-插槽及自定义事件分发
    一.插槽slot在某些场景中,我们可能想要为子组件传递一些模板片段,让子组件在它们的组件中渲染这些片段。<slot> 元素是一个插槽出口 (slotoutlet),标示了父元素提供的插槽内容 (slotcontent)将在哪里被渲染。 插槽就好比一个占位符,它不是解决页面必须元素的位置,而是解决未......
  • Java:JSR 310日期时间体系LocalDateTime、OffsetDateTime、ZonedDateTime
    JSR310日期时间体系:LocalDateTime:本地日期时间OffsetDateTime:带偏移量的日期时间ZonedDateTime:带时区的日期时间(目录)日期时间包importjava.time.LocalDateTime;importjava.time.OffsetDateTime;importjava.time.ZonedDateTime;importjava.time.format.DateTimeF......
  • 算法题——定义一个方法自己实现 toBinaryString 方法的效果,将一个十进制整数转成字符
    用除基取余法,不断地除以基数(几进制,基数就是几)得到余数,直到商为0,再将余数倒着拼起来即可。privatestaticStringtoBinaryString(intnumber){StringBuildersb=newStringBuilder();while(true){if(number==0)break;intyushu=num......