View Full Version : Best type for date with MySQL auto-fill?
JAB Creations
02-14-2008, 02:02 AM
I'm creating a table in a database and I want MySQL to create a time stamp when a new row is added. What's the most efficient type I should use? Here is a minimal example of what I need to know of course modified to what would best serve the goal of being efficient...
date VARCHAR(30),
I'm not sure that I understand the question, so I may need more information, but from what I can gather:
MySQL has a column type called DATETIME that actually is used for dates only (not varchar, which is for any character; I guess, you can use that as well, but I would go with DATETIME).
CREATE TABLE `dates` (
info VARCHAR(30),
date DATETIME
);
and then add the time like this:
INSERT INTO `dates` VALUE (
"Hello!",
NOW()
);
Every new row then show the time (to the second) when it was created.
Embryo
05-10-2008, 04:37 PM
I'm not sure that I understand the question, so I may need more information, but from what I can gather:
MySQL has a column type called DATETIME that actually is used for dates only (not varchar, which is for any character; I guess, you can use that as well, but I would go with DATETIME).
CREATE TABLE `dates` (
info VARCHAR(30),
date DATETIME
);
and then add the time like this:
INSERT INTO `dates` VALUE (
"Hello!",
NOW()
);
Every new row then show the time (to the second) when it was created.
Jas,
I'm seeking to ad a date and time stamp to the Database or in a form field text area automatically when the user hits submit. what would be the best way of doing this to prevent it from being edited. I need it to reflect on the database. help please:)
phpsales
05-10-2008, 04:43 PM
Jas is correct. You would want to use a MySQL DATETIME field and the NOW() module (alternative example below):
Example Column:
`pdate` DATETIME
Example SQL:
-- sql below uses MySQL NOW() function to insert date/time stamp
INSERT INTO `tablename` (`pdate`) VALUES (NOW());
-- example two
UPDATE `tablename` SET `pdate` = NOW();
Reference:
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.