mySQL添加列中断数据

时间:2016-03-10 20:33:36

标签: mysql

我使用命令行在mySQL中成功创建了一个数据库并导入了一些数据。它目前看起来像..

desc data;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| code     | varchar(10) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

SELECT * FROM data;
    +----+----------+
    | id | code     |
    +----+----------+
     | 1 | 123abc
     | 2 | 234def
     | 3 | 567ghi
     | 4 | 890jkl

我想在表中添加一个名为timestamp的列,我正在使用..

alter table data add timestamp VARCHAR(20);

但是我的桌子看起来像这样......

desc data;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| code      | varchar(10) | YES  |     | NULL    |                |
| timestamp | varchar(20) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

SELECT * FROM data;
    +----+----------+-----------+
    | id | code     | timestamp |
    +----+----------+-----------+
     | NULL       |
     | NULL       |
     | NULL       |
     | NULL       |

我哪里错了?

1 个答案:

答案 0 :(得分:3)

在这里你可以看到反引号

alter table `data` add `timestamp` VARCHAR(20);

<强>样品

MariaDB []> desc data;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type                 | Null | Key | Default | Extra          |
+-------+----------------------+------+-----+---------+----------------+
| id    | int(11) unsigned     | NO   | PRI | NULL    | auto_increment |
| e     | enum('x1','x2','x3') | YES  |     | NULL    |                |
+-------+----------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

MariaDB []> alter table `data` add `timestamp` VARCHAR(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB []> desc data;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| id        | int(11) unsigned     | NO   | PRI | NULL    | auto_increment |
| e         | enum('x1','x2','x3') | YES  |     | NULL    |                |
| timestamp | varchar(20)          | YES  |     | NULL    |                |
+-----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

表格数据

MariaDB [who]> select * from `data`;
+----+------+-----------+
| id | e    | timestamp |
+----+------+-----------+
|  1 | x1   | NULL      |
|  2 | x2   | NULL      |
+----+------+-----------+
2 rows in set (0.00 sec)

MariaDB [who]>