Spaß mit MySQL

akretschmer

Datenbank-Guru
Beiträge
10.311
Code:
mysql> create table foo (id int, val enum('','1'));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into foo values (1, '');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values (2, '1');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo where val = 1;
+------+------+
| id  | val  |
+------+------+
|  1 |  |
+------+------+
1 row in set (0.00 sec)

Was ist die Ursache? Es winkt ein 'Like' von mir ;-)
 
Werbung:
Was ist die Ursache? Es winkt ein 'Like' von mir ;-)

Niemand?

MySQL verwaltet solche Enums intern mit Int-Werten. Kann man machen. MySQL macht implizite CAST's. Schon schlecht. MySQL erlaubt, auf solche Enum-Werte mit ihren Int-Index (beginnend bei 1) zuzugreifen. Alles zusammen ist dann schlicht ein Desaster.

Code:
mysql> create table xxx (id int, val enum('foo','bar','batz'));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into xxx values (1, 'foo');
Query OK, 1 row affected (0.03 sec)

mysql> insert into xxx values (2, 'bar');
Query OK, 1 row affected (0.02 sec)

mysql> insert into xxx values (3, 'batz');
Query OK, 1 row affected (0.02 sec)

mysql> select * from xxx;
+------+------+
| id  | val  |
+------+------+
|  1 | foo  |
|  2 | bar  |
|  3 | batz |
+------+------+
3 rows in set (0.02 sec)

mysql> select * from xxx where val = 1;
+------+------+
| id  | val  |
+------+------+
|  1 | foo  |
+------+------+
1 row in set (0.03 sec)

mysql> select * from xxx where val = 3;
+------+------+
| id  | val  |
+------+------+
|  3 | batz |
+------+------+
1 row in set (0.02 sec)

Faszinierend, oder? Inspiriert von http://blog.endpoint.com/2014/04/sanity-thy-name-is-not-mysql.html
 
Zurück
Oben