-1

"TIMESTAMP" return NULL in table

MySQL 8.0.17

CREATE TABLE towary(
    id SERIAL,
    nazwa VARCHAR(255),
    przyjecie TIMESTAMP
);
INSERT INTO towary (nazwa) VALUES ('AAA');
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Geri Geri
  • 1
  • 1

1 Answers1

1

You are inserting a row with three columns. The first has a "default" value because it is serial, so it gets assigned.

The timestamp column has no default value, so it is assigned NULL.

If you want it to default to the current timestamp, you need to assign a default value:

CREATE TABLE towary(
    id SERIAL,
    nazwa VARCHAR(255),
    przyjecie TIMESTAMP DEFAULT current_timestamp
);

Here is a db<>fiddle.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Is it change from version 5.6? Because on video course i see exactly the same above text and timestamp return correct result (date). – Geri Geri Sep 24 '19 at 11:07
  • przyjecie TIMESTAMP default now() - it's worked, but if it is optimal (clean) solution? – Geri Geri Sep 24 '19 at 11:12
  • @GeriGeri It is changed from version 8.0.2 onwards. Check: https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp – Madhur Bhaiya Sep 24 '19 at 11:12