English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

im trying to put NOW() as the default value for the datetime field type ="datetime" but im getting this error

ALTER TABLE `members` ADD `datetime` DATETIME DEFAULT 'NOW()' NOT NULL

MySQL said:
#1067 - Invalid default value for 'datetime'

2006-06-14 09:28:36 · 3 answers · asked by aryaxt 3 in Computers & Internet Programming & Design

3 answers

Firstly off 'now()' isn't a function call, it's a string therefore you CANNOT use it for a default value.

You have to insert a value upon insertion to that table withe the now() string.

Look Carefully about the datatypes of different Time Structures
==============================

--
-- Lets use the test Database
--
mysql> use test;
Database changed

--
-- Lets create a Table (temp for this example)
--
mysql> create table a (
-> x date,
-> y timestamp,
-> z datetime);
Query OK, 0 rows affected (0.02 sec)

--
-- Lets insert only at column x so rest are default
--
mysql> insert into a(x) values(now());
Query OK, 1 row affected (0.00 sec)

--
-- Lets insert only at column y so rest are default
--
mysql> insert into a(y) values(now());
Query OK, 1 row affected (0.00 sec)

--
-- Lets insert only at column z so rest are default
--
mysql> insert into a(z) values(now());
Query OK, 1 row affected (0.00 sec)

--
-- Lets Select everything in that table
--
mysql> select * from a;
+------------+---------------------+---------------------+
| x | y | z |
+------------+---------------------+---------------------+
| 2006-06-15 | 2006-06-15 00:42:17 | NULL |
| NULL | 2006-06-15 00:42:23 | NULL |
| NULL | 2006-06-15 00:42:26 | 2006-06-15 00:42:26 |
+------------+---------------------+---------------------+
3 rows in set (0.00 sec)
==============================

Now notice what happened... TIMESTAMP by default updates the current time..., The others are null. So change your datatype to be TIMESTAMP if you care so much about the default value.

2006-06-14 17:56:07 · answer #1 · answered by ? 6 · 0 0

Use TIMESTAMP instead of DATETIME, make it NOT NULL but do not set a default, and it will automatically fill in with the current date.

Note that if you have multiple fields of type TIMESTAMP, this will only work with the first field.

2006-06-14 09:50:52 · answer #2 · answered by Anonymous · 0 0

Use the Curtime() function instead

2006-06-14 16:20:39 · answer #3 · answered by Beatmaster 4 · 0 0

fedest.com, questions and answers