fightstarr20
fightstarr20

Reputation: 12628

mySQL adding column breaks data

I have succesfully created a database in mySQL using the commandline and imported some data. It currently looks like this..

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

I would like to add a column to the table called timestamp, I am doing this with..

alter table data add timestamp VARCHAR(20);

But then my table looks like this...

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       |

Where am I going wrong?

Upvotes: 1

Views: 388

Answers (1)

Bernd Buffen
Bernd Buffen

Reputation: 15057

here you can see the backticks

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

SAMPLE

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)

Table Data

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

MariaDB [who]>

Upvotes: 3

Related Questions