无法更新mysql表

时间:2017-05-30 02:05:52

标签: mysql

我有一个包含以下sql的表:

CREATE TABLE `event` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_date` datetime DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL,
  `description` varchar(2000) NOT NULL,
  `enabled` bit(1) NOT NULL,
  `end_date_time` datetime NOT NULL,
  `image_url` varchar(255) NOT NULL,
  `start_date_time` datetime NOT NULL,
  `title` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
)

当我使用有权插入的用户运行以下命令时,我得到以下结果:

mysql> insert into event (description, enabled, end_date_time, image_url, start_date_time, title) values ('asdf', 1, NOW(), 'asdf', NOW(), 'asf');

为什么我无法更新'已启用'标志?

mysql> select * from event;
+----+---------------------+---------------------+-------------+---------+---------------------+-----------+---------------------+-------+
| id | created_date        | modified_date       | description | enabled | end_date_time       | image_url | start_date_time     | title |
+----+---------------------+---------------------+-------------+---------+---------------------+-----------+---------------------+-------+
|  1 | 2017-05-29 17:10:39 | 2017-05-29 17:10:39 | DESCRIPTION |        | 2017-05-29 17:10:39 | URL       | 2017-05-29 17:10:39 | TITLE |
|  2 | NULL                | NULL                | asdf        |        | 2017-05-29 22:00:32 | asdf      | 2017-05-29 22:00:32 | asf   |
+----+---------------------+---------------------+-------------+---------+---------------------+-----------+---------------------+-------+
2 rows in set (0.00 sec)

mysql> 

<小时/> 注意

mysql> select if((select enabled from event where id = 2), 'true', 'false');
+---------------------------------------------------------------+
| if((select enabled from event where id = 2), 'true', 'false') |
+---------------------------------------------------------------+
| true                                                          |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> update event set enabled = 0;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select if((select enabled from event where id = 2), 'true', 'false');
+---------------------------------------------------------------+
| if((select enabled from event where id = 2), 'true', 'false') |
+---------------------------------------------------------------+
| false                                                         |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

1 个答案:

答案 0 :(得分:2)

你设置得很好。只是当它显示时,它会显示为\ x01,这是一个不可打印的角色。

(如果您运行SELECT * FROM events WHERE enabled=1会怎样?)

如果对此列使用tinyint(1)而不是bit(1),事情可能更直观。