473,412 Members | 5,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Another TIMESTAMP question

JJ
How do I set one field to have the updated timestamp, and another to have
the created timestamp?

I want to do this directly from code generated from DB Designer if
possible?!
JJ
Jun 6 '06 #1
7 6079
Ike
You must use ver 4.1.2 or higher. See:
http://dev.mysql.com/doc/refman/4.1/...stamp-4-1.html
//Ike
Jun 7 '06 #2
JJ
I'm using 5.0.22.

I understood that simply creating two columns with a TIMESTAMP type, will
result in the first column showing the update time and the second column
show the created time.
I can't get it to work at all. All I get is the updated time - the created
timestamp just ends up as 0000:00 etc.

I haven't altered the MaxDB setting from the default either?
??
"Ike" <rx*@hotmail.com> wrote in message
news:uf******************@newsread4.news.pas.earth link.net...
You must use ver 4.1.2 or higher. See:
http://dev.mysql.com/doc/refman/4.1/...stamp-4-1.html
//Ike

Jun 8 '06 #3
Ike

"JJ" <jj@nospam.com> wrote in message
news:Nj******************@newsfe1-win.ntli.net...
I'm using 5.0.22.

I understood that simply creating two columns with a TIMESTAMP type, will
result in the first column showing the update time and the second column
show the created time.
I can't get it to work at all. All I get is the updated time - the created
timestamp just ends up as 0000:00 etc.

I haven't altered the MaxDB setting from the default either?
??

Beginning with MySQL 4.1.2, you have more flexible control over when
automatic TIMESTAMP initialization and updating occur and which column
should have those behaviors:

a.. For one TIMESTAMP column in a table, you can assign the current
timestamp as the default value and the auto-update value. It is possible to
have the current timestamp be the default value for initializing the column,
for the auto-update value, or both. It is not possible to have the current
timestamp be the default value for one column and the auto-update value for
another column.

b.. You can specify which TIMESTAMP column to automatically initialize or
update to the current date and time. This need not be the first TIMESTAMP
column.

The following discussion describes the revised syntax and behavior. Note
that this information applies only to TIMESTAMP columns for tables not
created with MAXDB mode enabled. As noted earlier in this section, MAXDB
mode causes columns to be created as DATETIME columns.

The following items summarize the pre-4.1.2 properties for TIMESTAMP
initialization and updating:

The first TIMESTAMP column in table row automatically is set to the current
timestamp when the record is created if the column is set to NULL or is not
specified at all.

The first TIMESTAMP column in table row automatically is updated to the
current timestamp when the value of any other column in the row is changed,
unless the TIMESTAMP column explicitly is assigned a value other than NULL.

If a DEFAULT value is specified for the first TIMESTAMP column when the
table is created, it is silently ignored.

Other TIMESTAMP columns in the table can be set to the current TIMESTAMP by
assigning NULL to them, but they do not update automatically.

As of 4.1.2, you have more flexibility in deciding which TIMESTAMP column
automatically is initialized and updated to the current timestamp. The rules
are as follows:

If a DEFAULT value is specified for the first TIMESTAMP column in a table,
it is not ignored. The default can be CURRENT_TIMESTAMP or a constant date
and time value.

DEFAULT NULL is the same as DEFAULT CURRENT_TIMESTAMP for the first
TIMESTAMP column. For any other TIMESTAMP column, DEFAULT NULL is treated as
DEFAULT 0.

Any single TIMESTAMP column in a table can be used as the one that is
initialized to the current timestamp or updated automatically.

In a CREATE TABLE statement, the first TIMESTAMP column can be declared in
any of the following ways:

a.. With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP
clauses, the column has the current timestamp for its default value, and is
automatically updated.

b.. With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

c.. With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the
column has the current timestamp for its default value but is not
automatically updated.

d.. With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP clause,
the column has a default of 0 and is automatically updated.

e.. With a constant DEFAULT value, the column has the given default. If
the column has an ON UPDATE CURRENT_TIMESTAMP clause, it is automatically
updated, otherwise not.

In other words, you can use the current timestamp for both the initial value
and the auto-update value, or either one, or neither. (For example, you can
specify ON UPDATE to get auto-update without also having the column
auto-initialized.)

CURRENT_TIMESTAMP or any of its synonyms (CURRENT_TIMESTAMP(), NOW(),
LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, or LOCALTIMESTAMP()) can be used in
the DEFAULT and ON UPDATE clauses. They all mean "the current timestamp."
(UTC_TIMESTAMP is not allowed. Its range of values does not align with those
of the TIMESTAMP column anyway unless the current time zone is UTC.)

The order of the DEFAULT and ON UPDATE attributes does not matter. If both
DEFAULT and ON UPDATE are specified for a TIMESTAMP column, either can
precede the other. For example, these statements are equivalent:

CREATE TABLE t (ts TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
DEFAULT CURRENT_TIMESTAMP);

To specify automatic default or updating for a TIMESTAMP column other than
the first one, you must suppress the automatic initialization and update
behaviors for the first TIMESTAMP column by explicitly assigning it a
constant DEFAULT value (for example, DEFAULT 0 or DEFAULT '2003-01-01
00:00:00'). Then for the other TIMESTAMP column, the rules are the same as
for the first TIMESTAMP column, except that if you omit both of the DEFAULT
and ON UPDATE clauses, no automatic initialization or updating occurs.

Example. These statements are equivalent:

CREATE TABLE t (
ts1 TIMESTAMP DEFAULT 0,
ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (
ts1 TIMESTAMP DEFAULT 0,
ts2 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
DEFAULT CURRENT_TIMESTAMP);

Beginning with MySQL 4.1.3, you can set the current time zone on a
per-connection basis, as described in Section 5.10.8, "MySQL Server Time
Zone Support". TIMESTAMP values still are stored in UTC, but are converted
from the current time zone for storage, and converted back to the current
time zone for retrieval. As long as the time zone setting remains constant,
you get back the same value you store. If you store a TIMESTAMP value, and
then change the time zone and retrieve the value, the retrieved value is
different than the value you stored. This occurs because the same time zone
was not used for conversion in both directions. The current time zone is
available as the value of the time_zone system variable.

Beginning with MySQL 4.1.6, you can include the NULL attribute in the
definition of a TIMESTAMP column to allow the column to contain NULL values.
For example:

CREATE TABLE t (
ts1 TIMESTAMP NULL DEFAULT NULL,
ts2 TIMESTAMP NULL DEFAULT 0,
ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

Before MySQL 4.1.6 (and even as of 4.1.6 if the NULL attribute is not
specified), setting the column to NULL sets it to the current timestamp.
Note that a TIMESTAMP column which allows NULL values not take on the
current timestamp except under one of the following conditions:

a.. Its default value is defined as CURRENT_TIMESTAMP

b.. NOW() or CURRENT_TIMESTAMP is inserted into the column

In other words, a TIMESTAMP column defined as NULL will auto-initialize only
if it is created using a definition such as the following:

CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);

Otherwise - that is, if the TIMESTAMP column is defined to allow NULL values
but not using DEFAULT TIMESTAMP, as shown here.

CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT NULL);
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');

..then you must explicitly insert a value corresponding to the current date
and time, for example:

INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);


Jun 9 '06 #4
I hated having to deal with 2 timestamp fields in mysql 4, since your using mysql5, take advantage of triggers. You can use 2 triggers to control exactly how you want the timestamp fields handled on record inserts and updates.

Full example:
http://www.codedumpster.com/sql/mysql5_timestamps
Jun 16 '06 #5
JJ
Thanks Ike,

But I've read all this and it still doesn't work. Have given up on try to
get the values to set automatically, will have to set them explicitly
evertime I update or add a record.

Thanks anyway,

JJ
"Ike" <rx*@hotmail.com> wrote in message
news:KJ*****************@newsread1.news.pas.earthl ink.net...

"JJ" <jj@nospam.com> wrote in message
news:Nj******************@newsfe1-win.ntli.net...
I'm using 5.0.22.

I understood that simply creating two columns with a TIMESTAMP type, will
result in the first column showing the update time and the second column
show the created time.
I can't get it to work at all. All I get is the updated time - the
created
timestamp just ends up as 0000:00 etc.

I haven't altered the MaxDB setting from the default either?
??

Beginning with MySQL 4.1.2, you have more flexible control over when
automatic TIMESTAMP initialization and updating occur and which column
should have those behaviors:

a.. For one TIMESTAMP column in a table, you can assign the current
timestamp as the default value and the auto-update value. It is possible
to
have the current timestamp be the default value for initializing the
column,
for the auto-update value, or both. It is not possible to have the current
timestamp be the default value for one column and the auto-update value
for
another column.

b.. You can specify which TIMESTAMP column to automatically initialize or
update to the current date and time. This need not be the first TIMESTAMP
column.

The following discussion describes the revised syntax and behavior. Note
that this information applies only to TIMESTAMP columns for tables not
created with MAXDB mode enabled. As noted earlier in this section, MAXDB
mode causes columns to be created as DATETIME columns.

The following items summarize the pre-4.1.2 properties for TIMESTAMP
initialization and updating:

The first TIMESTAMP column in table row automatically is set to the
current
timestamp when the record is created if the column is set to NULL or is
not
specified at all.

The first TIMESTAMP column in table row automatically is updated to the
current timestamp when the value of any other column in the row is
changed,
unless the TIMESTAMP column explicitly is assigned a value other than
NULL.

If a DEFAULT value is specified for the first TIMESTAMP column when the
table is created, it is silently ignored.

Other TIMESTAMP columns in the table can be set to the current TIMESTAMP
by
assigning NULL to them, but they do not update automatically.

As of 4.1.2, you have more flexibility in deciding which TIMESTAMP column
automatically is initialized and updated to the current timestamp. The
rules
are as follows:

If a DEFAULT value is specified for the first TIMESTAMP column in a table,
it is not ignored. The default can be CURRENT_TIMESTAMP or a constant date
and time value.

DEFAULT NULL is the same as DEFAULT CURRENT_TIMESTAMP for the first
TIMESTAMP column. For any other TIMESTAMP column, DEFAULT NULL is treated
as
DEFAULT 0.

Any single TIMESTAMP column in a table can be used as the one that is
initialized to the current timestamp or updated automatically.

In a CREATE TABLE statement, the first TIMESTAMP column can be declared in
any of the following ways:

a.. With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP
clauses, the column has the current timestamp for its default value, and
is
automatically updated.

b.. With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

c.. With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the
column has the current timestamp for its default value but is not
automatically updated.

d.. With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP
clause,
the column has a default of 0 and is automatically updated.

e.. With a constant DEFAULT value, the column has the given default. If
the column has an ON UPDATE CURRENT_TIMESTAMP clause, it is automatically
updated, otherwise not.

In other words, you can use the current timestamp for both the initial
value
and the auto-update value, or either one, or neither. (For example, you
can
specify ON UPDATE to get auto-update without also having the column
auto-initialized.)

CURRENT_TIMESTAMP or any of its synonyms (CURRENT_TIMESTAMP(), NOW(),
LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, or LOCALTIMESTAMP()) can be used
in
the DEFAULT and ON UPDATE clauses. They all mean "the current timestamp."
(UTC_TIMESTAMP is not allowed. Its range of values does not align with
those
of the TIMESTAMP column anyway unless the current time zone is UTC.)

The order of the DEFAULT and ON UPDATE attributes does not matter. If both
DEFAULT and ON UPDATE are specified for a TIMESTAMP column, either can
precede the other. For example, these statements are equivalent:

CREATE TABLE t (ts TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
DEFAULT CURRENT_TIMESTAMP);

To specify automatic default or updating for a TIMESTAMP column other than
the first one, you must suppress the automatic initialization and update
behaviors for the first TIMESTAMP column by explicitly assigning it a
constant DEFAULT value (for example, DEFAULT 0 or DEFAULT '2003-01-01
00:00:00'). Then for the other TIMESTAMP column, the rules are the same as
for the first TIMESTAMP column, except that if you omit both of the
DEFAULT
and ON UPDATE clauses, no automatic initialization or updating occurs.

Example. These statements are equivalent:

CREATE TABLE t (
ts1 TIMESTAMP DEFAULT 0,
ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (
ts1 TIMESTAMP DEFAULT 0,
ts2 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
DEFAULT CURRENT_TIMESTAMP);

Beginning with MySQL 4.1.3, you can set the current time zone on a
per-connection basis, as described in Section 5.10.8, "MySQL Server Time
Zone Support". TIMESTAMP values still are stored in UTC, but are converted
from the current time zone for storage, and converted back to the current
time zone for retrieval. As long as the time zone setting remains
constant,
you get back the same value you store. If you store a TIMESTAMP value, and
then change the time zone and retrieve the value, the retrieved value is
different than the value you stored. This occurs because the same time
zone
was not used for conversion in both directions. The current time zone is
available as the value of the time_zone system variable.

Beginning with MySQL 4.1.6, you can include the NULL attribute in the
definition of a TIMESTAMP column to allow the column to contain NULL
values.
For example:

CREATE TABLE t (
ts1 TIMESTAMP NULL DEFAULT NULL,
ts2 TIMESTAMP NULL DEFAULT 0,
ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

Before MySQL 4.1.6 (and even as of 4.1.6 if the NULL attribute is not
specified), setting the column to NULL sets it to the current timestamp.
Note that a TIMESTAMP column which allows NULL values not take on the
current timestamp except under one of the following conditions:

a.. Its default value is defined as CURRENT_TIMESTAMP

b.. NOW() or CURRENT_TIMESTAMP is inserted into the column

In other words, a TIMESTAMP column defined as NULL will auto-initialize
only
if it is created using a definition such as the following:

CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);

Otherwise - that is, if the TIMESTAMP column is defined to allow NULL
values
but not using DEFAULT TIMESTAMP, as shown here.

CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT NULL);
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');

.then you must explicitly insert a value corresponding to the current date
and time, for example:

INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);

Jun 18 '06 #6
JJ wrote:
I'm using 5.0.22.

I understood that simply creating two columns with a TIMESTAMP type, will
result in the first column showing the update time and the second column
show the created time.
I can't get it to work at all. All I get is the updated time - the created
timestamp just ends up as 0000:00 etc.


The documentation is confusing, but the answer is simple. MySQL 5 only
allows for one column to use the TimeStamp for auto-updating. You can
have two, but one will always default to 0's, and hacking at the column
definition just brings an error.

A common solution is to manually set the dummy column with a NOW() in
the INSERT.

A more elegant solution with MySQL 5 is to use a trigger with regulat
DateTime columns for full control of whatever behavior you want. Here's
a nice article on that:
http://www.futhark.ch/mysql/108.html

Jun 21 '06 #7
JJ
Thanks thats very helpful.

"Skarjune" <dh*@wordimage.com> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
JJ wrote:
I'm using 5.0.22.

I understood that simply creating two columns with a TIMESTAMP type, will
result in the first column showing the update time and the second column
show the created time.
I can't get it to work at all. All I get is the updated time - the
created
timestamp just ends up as 0000:00 etc.


The documentation is confusing, but the answer is simple. MySQL 5 only
allows for one column to use the TimeStamp for auto-updating. You can
have two, but one will always default to 0's, and hacking at the column
definition just brings an error.

A common solution is to manually set the dummy column with a NOW() in
the INSERT.

A more elegant solution with MySQL 5 is to use a trigger with regulat
DateTime columns for full control of whatever behavior you want. Here's
a nice article on that:
http://www.futhark.ch/mysql/108.html

Jun 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lauren Quantrell | last post by:
Is there any reason at all to use a timestamp column in a table having a primarykey column??? lq
2
by: Justin Grieves | last post by:
Hello all, I've been trying to determine the difference in minutes between two DateTime objects (startTime and endTime) in C# using the TimeSpan object (ts). I am populating the two DateTime...
2
by: jay | last post by:
hi, Question on Load/import command. consider a sample table create table table_name ( col1 timestamp not null default current timestamp, col2 int, col3 int, col4 int, primary key(col1) );...
6
by: Jim C. Nasby | last post by:
Is there any reason why there isn't a predefined cast to go from a timestamp to a varchar? Is there a reason not to add one? -- Jim C. Nasby, Database Consultant jim@nasby.net...
0
by: prerak_v_shah | last post by:
Hi All, These days I was working on IBM DB2 database for windows with my .Net Application. Now, it was required to backup database created on one machine to the other machine. I was also able...
4
by: mghale | last post by:
I have a question that I'm hoping has an easy answer. I'm working in DB2 V8.2 on AIX 5.3 I have a timestamp column (i.e. 4/26/2006 1:02:42.000000 PM) that I want to return in a report from...
3
by: wongjoekmeu | last post by:
Hello all, I have a C++ program ( I am not sure if this is the right place to post this question ), One of the function returns two unsigned integers which are referred to be TimestampLo and...
5
by: Henry J. | last post by:
I know this could be a dumb question, I just want to confirm that it is faster to execute inserts, if a timestamp field is set to have a default value, like DEFAULT CURRENT TIMESTAMP, and skip that...
0
gregerly
by: gregerly | last post by:
Hello, I've got a question regarding the comparison of a date and year to a timestamp. I've got blog entries that get a timestamp on their way into my database (mysql). I'm programming an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.