Discussion:
[MySQL] Fecha y hora actuales como valor por defecto al insertar registro
(demasiado antiguo para responder)
Kepler
2004-08-25 10:39:07 UTC
Permalink
En una tabla, tengo entre otros estos dos campos: "fecha" y "hora", y
quisiera que por defecto, al insertar un nuevo registro, se les asignara la
fecha y hora actuales del servidor.

He probado creando la tabla de esta forma:

create table ... (
...
fecha date not null default curdate(),
hora time not null default curtime(),
...
)

pero al ir a crear la tabla, me da error en la sintaxis. He sustituido
curdate() y curtime() por now(), para ver si colaba, y de nuevo el mismo
error.

Por fin, he puesto los valores de los campos de esta forma:

...
fecha date default null,
hora time default null,
...

para ver si asignando null como valor por defecto, MySQL automáticamente
rellenaba los campos date y time con la fecha y hora respectivamente, igual
que hace con los campos timestamp, pero tampoco: si pones default null, eso
es lo que ocurre, que los rellena con null.

Saludos...
stripTM
2004-08-25 23:17:00 UTC
Permalink
Post by Kepler
En una tabla, tengo entre otros estos dos campos: "fecha" y "hora", y
quisiera que por defecto, al insertar un nuevo registro, se les asignara la
fecha y hora actuales del servidor.
Creo que lo que buscas está aquí
http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html#IDX1640

# NULL values are handled differently for TIMESTAMP columns than for
other column types. You cannot store a literal NULL in a TIMESTAMP
column; setting the column to NULL sets it to the current date and time.
Because TIMESTAMP columns behave this way, the NULL and NOT NULL
attributes do not apply in the normal way and are ignored if you specify
them. On the other hand, to make it easier for MySQL clients to use
TIMESTAMP columns, the server reports that such columns can be assigned
NULL values (which is true), even though TIMESTAMP never actually will
contain a NULL value. You can see this when you use DESCRIBE tbl_name to
get a description of your table. Note that setting a TIMESTAMP column to
0 is not the same as setting it to NULL, because 0 is a valid TIMESTAMP
value.
# With one exception, a DEFAULT value must be a constant; it cannot be a
function or an expression. 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. The exception is that you can specify
CURRENT_TIMESTAMP as the default for a TIMESTAMP column as of MySQL
4.1.2. See section 12.3.1.2 TIMESTAMP Properties as of MySQL 4.1. If no
DEFAULT value is specified for a column, MySQL automatically assigns
one, as follows. If the column can 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
12.3 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.

BLOB and TEXT columns cannot be assigned a default value.
--
Saludos -=stripTM=-
Kepler
2004-08-27 10:17:44 UTC
Permalink
Post by stripTM
Post by Kepler
En una tabla, tengo entre otros estos dos campos: "fecha" y "hora", y
quisiera que por defecto, al insertar un nuevo registro, se les asignara la
fecha y hora actuales del servidor.
Creo que lo que buscas está aquí
http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html#IDX1640
Pues sí, como me imaginaba; tendré que asignar a mano los valores de
fecha y hora actuales cuando haga un INSERT.

Gracias.
Pumuki
2004-09-01 10:27:26 UTC
Permalink
En Oracle, al hacer el CREATE de la tabla puedes poner un DEFAUL VALUE(sysdate).
Post by Kepler
Post by stripTM
Post by Kepler
En una tabla, tengo entre otros estos dos campos: "fecha" y "hora",
y
Post by stripTM
Post by Kepler
quisiera que por defecto, al insertar un nuevo registro, se les asignara
la
Post by stripTM
Post by Kepler
fecha y hora actuales del servidor.
Creo que lo que buscas está aquí
http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html#IDX1640
Pues sí, como me imaginaba; tendré que asignar a mano los valores de
fecha y hora actuales cuando haga un INSERT.
Gracias.
Continúe leyendo en narkive:
Loading...