I have this query
`ALTER TABLE `user_account_keys` CHANGE `DateTime` `DateTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP`
It stops update_currnet_timestamp being enabled on a certain column. I need to disable it on every single column, in every single table, that comes under the type DateTime.
I'm not sure why MySQL has even enabled this without asking me.
You can set it to another default value since the column in NOT NULL. You can either allow nulls and then remove the default value or set it to another default value.
`ALTER TABLE `user_account_keys` CHANGE `DateTime` `DateTime` TIMESTAMP NOT NULL DEFAULT '2000-01-01 00:00:00'`
Change '2000-01-01 00:00:00' to whatever default value you need to use.
To change all the columns in the database, you can use something like this:
SELECT CONCAT('`ALTER TABLE `',TABLE_NAME,'` CHANGE `'
,COLUMN_NAME,'` `',COLUMN_NAME
,'` TIMESTAMP NOT NULL DEFAULT '
,'2000-01-01 00:00:00','`' ) ALTERTABLE
FROM information_schema.columns
WHERE data_type = 'timestamp';
This will give you a list of all the ALTER TABLE statements which you can verify before running using the EXEC command.
Related
I want to create a log with timestamp in mysql.
For this I created a column tm with datatype timestamp.
It works fine and is updated every time I change something.
The problem is that it should only update when I set the exist column to '1'. Not when I change any column.
Here you can view my table ..:
CREATE TABLE admins (
id int NOT NULL AUTO_INCREMENT,
bez varchar(55) DEFAULT 'unknown',
type varchar(7) NOT NULL DEFAULT 'unknown',
exist char NOT NULL DEFAULT '0',
tm TIMESTAMP
PRIMARY KEY ('id')
);
This is my UPDATE:
if ( isset($_GET["id"]) ) {
$id = $_GET["id"];
$sql = "UPDATE admins SET exist='0' WHERE id=$id";
$con->query($sql);
}
header("location: /index.php");
exit;
?>
So the UPDATE should change the timestamp and the exist.
Please dont tell me that i am open to sql injections, i know this and we have a security manager in this project who will change afterwards.
thanks for your help community!
The ON UPDATE CURRENT_TIMESTAMP feature that is implicitly enabled on the first TIMESTAMP on a table is an all-or-nothing feature, it does not allow for the fine grained control you are aiming for.
The behavior you are looking for can only be accomplished by either explicitly setting the TIMESTAMP column value in the UPDATE, or by adding a trigger to the table:
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE UPDATE
ON admins FOR EACH ROW
BEGIN
IF NEW.exists = 1 THEN
SET NEW.tm = NOW();
END IF;
END$$
DELIMITER ;
and chang the column definition from
tm TIMESTAMP;
to
tm TIMESTAMP default CURRENT_TIMESTAMP;
or maybe
tm TIMESTAMP DEFAULT 0;
or even
tm DATETIME;
to prevent the ON UPDATE CURRENT_TIMESTAMP behavior.
There's a log table with a TIMESTAMP column, that currently has precision down to the second.
This is not enough, cause sometimes multiple events happen in the same second, and this way we can't tell the order of the events happening.
I know this could be fixed with an automatically incremented ID (to put them in order of happening), but a Timestamp with millisecond precision is what I'm after since I would like to see how much time happens between two events.
If I change the columns default value from CURRENT_TIMESTAMP to CURRENT_TIMESTAMP(3), I get the following error in MySQL Workbench:
Executing:
ALTER TABLE `my_db`.`logs`
CHANGE COLUMN `timestamp` `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1067: Invalid default value for 'timestamp'
SQL Statement:
ALTER TABLE `my_db`.`logs`
CHANGE COLUMN `timestamp` `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
I already have data in the table, which I wouldn't like to use, so I would like to alter this table.
Is it actually possible to alter the table this way, so from now on the timestamp column will be more precise?
PS:
I tried adding a new (totally blank) column with the added precision, but it threw the same error.
Your alter query has the (6) missing on a second spot:
ALTER TABLE `my_db`.`logs`
CHANGE COLUMN `timestamp` `timestamp` TIMESTAMP(6) NOT NULL DEFAULT
CURRENT_TIMESTAMP(6)
I have tried to Google this so I didn't have to ask, as i'm sure this is a simple task...
I am building an E-commerce site and would like to add the date and time a product is added into the product database?
Apologies if this is simple, but i have researched everywhere else first.
Thanks
This can just be part of your database architecture:
ALTER TABLE `products` ADD `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
This will automatically add a timestamp to the column created whenever a row is created. For more information, try this: Automatic Initialization and Updating for TIMESTAMP
Obviously, in this case the table is called products and you would need to change it to whatever your table name is.
UPDATE
To update all existing records at the same time, just run:
UPDATE `products` SET `created` = NOW()
If you want to be more specific use:
UPDATE `products` SET `created` = NOW() WHERE `created` = '0000-00-00 00:00:00'
Method1 : Pass the Current DateTime as parameter to the Insert
Method2: Set the default value for the date time column in Product table
This seems like a really simple one but I'm struggling to figure it out. I want a column in my database that lists when a record was first created and another column that says when it was updated. It's my understanding I should be able to do all this just using MySQL. All help is appreciated :)
This stinks still no answer, reasons I'm already starting to miss Ruby on Rails...
You will probably need to use a combination of the Datetime datatype and the Timestamp data type. I would set my created column as a DateTime with a DEFAULT NOW(), and my updated column as a Timestamp with DEFAULT CURRENT_TIMESTAMP and an ON UPDATE CURRENT_TIMESTAMP attribute.
Here are the docs for the Timestamp dt:
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
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.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
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.
With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP clause, the column has a default of 0 and is automatically updated.
With a constant DEFAULT value, the column has the given default and is not automatically initialized to the current timestamp. If the column also has an ON UPDATE CURRENT_TIMESTAMP clause, it is automatically updated; otherwise, it has a constant default and is not automatically updated.
To fulfill your question and for others viewing this question, here is the answer. Note this was written for MySQL 5.x.
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
`id` INT NULL AUTO_INCREMENT ,
`name` varchar(50) NOT NULL ,
`created` DATETIME ,
`updated` DATETIME ,
PRIMARY KEY (`id`),
INDEX (`name`)
);
DELIMITER $$
DROP TRIGGER IF EXISTS `test1_created`$$
CREATE TRIGGER `test1_created` BEFORE INSERT ON `test1`
FOR EACH ROW BEGIN
SET NEW.`created` = UTC_TIMESTAMP();
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DROP TRIGGER IF EXISTS `test1_updated`$$
CREATE TRIGGER `test1_updated` BEFORE UPDATE ON `test1`
FOR EACH ROW BEGIN
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DELIMITER ;
Note
You could use TIMESTAMP for the updated column which would have automatically updated the value thus not requiring the BEFORE UPDATE trigger, however TIMESTAMP has a range from 1970 to 2038 which is fast approaching and I like to think my applications will live forever :). Although TIMESTAMP is only 4bytes while DATETIME is 8bytes.
TIMESTAMP range '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
DATETIME range '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
From the MySQL 5.0 Certification Guide:
CREATE TABLE ts_test5 (
created TIMESTAMP DEFAULT 0,
updated TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data CHAR(30)
);
To control the initialization and update behaviour of a TIMESTAMP column, you add either or both of the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes to the column defintion when creating the table with CREATE TABLE...
and
...if you do not specify either of the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes when creating a table, MySQL automatically assigns BOTH to the first TIMESTAMP column
Also
you cannot use DEFAULT CURRENT_TIMESTAMP with one column and ON UPDATE CURRENT_TIMESTAMP with another
If you can't use the timestamp fields with default attributes that Paul W has suggested, you can use AFTER INSERT and AFTER UPDATE triggers to populate the fields.
You will need two fields "Created" and
"Updated" with type datetime. When a
new entry is inserted then insert
"Created" with current time stamp.
When a update is happening insert
"Updated" with the current time stamp,
and let the "Created" field remain as
it is.
For current time stamp you can use
NOW() in your mysql query.
project is written on php.
There is timestamp field in mysql it updates automatically. In one case I don`t need update this field. Can I gibe instruction not to update this field in this queries without getting timestamp value.
You should change it to DEFAULT CURRENT_TIMESTAMP otherwise it will auto-update. From the manual:
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
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.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
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.
Edit the column in phpMyAdmin and unselect the option for "on update current timestamp."