MySQL - a fractal of bad design

Tudom, az eredeti cikk PHP-val volt

De nezzetek, mennyivel maskepp viselkedik a MySQL egy int es egy timestamp eseten a NULL-lal:

...

...

[code]
CREATE TABLE nulltest(
nulltest_id BIGINT NOT NULL PRIMARY KEY auto_increment,
intdef int DEFAULT 11, ## can be null
intdef2 int NOT NULL DEFAULT 22, ## cannot be null
intdef3 int NOT NULL, ## cannot be null, must define
time1 timestamp DEFAULT CURRENT_TIMESTAMP, ## cannot be null
time2 timestamp NULL ## can be null
## , time3 timestamp ## error, as "needs a default value" on mysql 5.7, if a table was created on mysql <=5.6 it exports "DEFAULT 0000-00-00 00:00:00"
);

INSERT INTO nulltest (
intdef3, time2
) VALUES
(
33, NULL
);

## 1 11 22 33 2017-05-25 12:30:33 NULL

INSERT INTO nulltest (
intdef, intdef2, intdef3, time1, time2
) VALUES
(
NULL, NULL, 33, NULL, NULL
);

## error - intdef2 cannot be null

INSERT INTO nulltest (
intdef, intdef2, intdef3, time1, time2
) VALUES
(
NULL, 22, 33, NULL, NULL ## yes, I needed to default for an int, but not for a timestamp
);

## 2 NULL 22 33 2017-05-25 12:32:51 NULL

## intdef NULL became NULL, not the DEFAULT 11
## timestamp NULL became the DEFAULT CURRENT_TIMESTAMP

[/code]

Ha kicserelem a CREATE TABLE-ben:


-time1 timestamp DEFAULT CURRENT_TIMESTAMP, ## cannot be null
+time1 timestamp NULL DEFAULT CURRENT_TIMESTAMP,

Akkor viszont ugy viselkedik az INSERT INTO, mint az integernel (a VALUES timestamp NULL, akkor NULL lesz az ertek a column-ban, nem a DEFAULT CURRENT_TIMESTAMP)

MySQL 5.7.18 - macOS

Hozzászólások

Az egy pozitivum, hogy legalabb le van dokumentalva megfeleloen ez a logikatlan viselkedes.

Ettol meg viszont nem lesz logikus, hogy
- int-nel miert a NOT NULL-t, timestampnel miert a "can be NULL"-t kell kiemelni
- timestamp not nullable-kent miert veszi fel a default value-t, es kozben int not null-ablekent default value-val miert dob errort NULL-ra
- miert kotelezo a mysql 5.7-ben a timestampnek default value ha nem nullable
- mysql 5.6-ig mi volt az a default 0000-00-00 00:00:00

> timestamp not nullable-kent miert veszi fel a default value-t, es kozben int not null-ablekent default value-val miert dob errort NULL-ra
A not nullable timestamp SET NULL-nál nem hibát dob, hanem felveszi a defaultot? Ezt nem tudtam, valóban fura.

> miert kotelezo a mysql 5.7-ben a timestampnek default value ha nem nullable
Mert nem integer, ahol értelmes lenne a 0 érték.

> mysql 5.6-ig mi volt az a default 0000-00-00 00:00:00
Balfaszság.