tags:

views:

75

answers:

1

Hello,

I would like to put a time & date stamp on each row added to a MySQL table. If I understand correctly, I need to create a column for the time & date stamp. How do I do that for the CREATE TABLE query below?

"CREATE TABLE `$table` (id INT(11) NOT NULL auto_increment, site VARCHAR(1000) NOT NULL, actions1 BIGINT(9) NOT NULL, actions2 BIGINT(9) NOT NULL, PRIMARY KEY(id), UNIQUE (site))"

Also, if I understand correctly, I would then need to add the stamp each time a row is inserted to the table. How would I do that for the INSERT INTO query below?

"INSERT INTO `$find` VALUES (NULL, '$site',1,0)"

Thanks in advance,

John

+2  A: 

You need to add a TIMESTAMP column like this:

CREATE TABLE `$table` (
 id INT(11) NOT NULL auto_increment, 
 site VARCHAR(1000) NOT NULL, 
 actions1 BIGINT(9) NOT NULL, 
 actions2 BIGINT(9) NOT NULL, 
 CreatedDateTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 PRIMARY KEY(id), UNIQUE (site))

That will make a column CreatedDateTime which contains the (server) time the row was created.

You can do the insert as:

INSERT INTO `$find` (site, actions1, actions2) VALUES ('$site', 1, 0)

For further reference see here

Whisk
Thanks... I will give you credit for this if I can get it to work. Also, am I correct in assuming that this time stamp includes year?
John
Yes you are - Also if you want the timestamp to update every time you change the row you can add "ON UPDATE CURRENT_TIMESTAMP" to the column definition
Whisk
Your answered worked... but I did need to alter my INSERT statement; I needed to make it INSERT INTO `$find` VALUES (NULL, '$site',1,0, NULL)
John
Ah sorry yes, the best way to do the insert is: INSERT INTO $find (site, actions1, actions2) VALUES ($site, 1, 0)
Whisk