Avatar billede tipsen Nybegynder
19. august 2003 - 12:51 Der er 6 kommentarer og
1 løsning

DATE-felt med default-value = NOW()

Hej eksperter

Er det muligt at have en default-value der er lig dags dato (NOW()?) i et date-felt i en mysql-tabel? Hvis ja - hvordan?

Mvh

tipsen
Avatar billede clausjul Nybegynder
19. august 2003 - 13:09 #1
Kan man ikke skrive
CREATE TABLE tabel (dato DATE default NOW()) ?
Avatar billede tipsen Nybegynder
19. august 2003 - 13:17 #2
Det var jo den oplagte... - som desværre ikke fungerer :-)
Avatar billede detox Nybegynder
19. august 2003 - 15:08 #3
Hvad så med et TIMESTAMP?
Avatar billede tipsen Nybegynder
19. august 2003 - 17:03 #4
jeg ved godt det lyder af et timestamp, men jeg kan desværre ikke bruge et timestamp i denne sammenhæng af andre årsager.
Avatar billede detox Nybegynder
19. august 2003 - 17:30 #5
Nu har jeg ikke prøvet det, men iflg. manualen skulle man kunne bruge DATETIME med default NOW().

"On the other hand, you may find it just as easy to use a DATETIME column that you initialise to NOW() when the row is created and leave alone for subsequent updates."
Avatar billede Slettet bruger
19. august 2003 - 18:02 #6
Nej. Det er ikke muligt for en DATE type.

A DEFAULT value has to be a constant, it cannot be a function or an expression. If no DEFAULT value is specified for a column, MySQL automatically assigns one, as follows. If the column may take NULL as a value, the default value is NULL. If the column is declared as NOT NULL, the default value depends on the column type:
For numeric types other than those declared with the AUTO_INCREMENT attribute, the default is 0. For an AUTO_INCREMENT column, the default value is the next value in the sequence.
For date and time types other than TIMESTAMP, the default is the appropriate zero value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See section 6.2.2 Date and Time Types.
For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
Default values must be constants. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE.

Fra http://www.mysql.com/doc/en/CREATE_TABLE.html
Avatar billede tipsen Nybegynder
19. august 2003 - 18:51 #7
Jeg siger tak.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester