Hi I need to resolve next problem.
I have a row in mysql:
'start_at' and 'ends_at'
They are Timestamps type.
I need to set default value of ends_at plus 7 days from starts_at
something like this:
ALTER TABLE `a_to_b`
MODIFY COLUMN `ends_at` timestamp NULL DEFAULT 'CURRENT_TIMESTAMP + 604800' AFTER `starts_at`;
If i am right in MSSQL i can do DATE_FROM();
How can I do this in MySql?
Thanks.
mysql does not allow computation in table structure. You need to do the insert afterwards:
insert into myDate (endTime) values ((interval 7 day + current_timestamp))
Related
I have a table in mysql database and when I do an update on it like this:
UPDATE `texts` SET `start_time` = '1444562855' WHERE `texts`.`text_id` = 529;
I'm getting the error:
Modified records: 0
Warning: #1265 Data truncated for column 'start_time' at row 1
The field start_time has a type TIMESTAMP and I have another timestamp field in this table, however I've read that it's possible to store two timestamps in one table if the MySQL verion is higher than 5.6.+ (my version is 5.6.26).
What it doesn't work?
I don't want to change the type of this field to datetime since my backend logic is supporting timestamps only..
Thanks.
Use FROM_UNIXTIME and remove the quotes :
UPDATE `texts` SET `start_time` = FROM_UNIXTIME(1444562855) WHERE `texts`.`text_id` = 529;
I am using timestamp to store the pubdate of uploads. I want to add a filter to sort records by the year they were uploaded so need to change a couple of records to previous years for testing purposes. When I try to manually adjust the date from within mysql by clicking the calendar and picking a date it flags errors.
The value of timestamp is set to CURRENT_TIMESTAMP.
The values of function in the timestamp field are :
NOW
CURDATE
FROM_DAYS
FROM_UNIXTIME
PERIOD_ADD
PERIOD_DIFF
TO_DAYS
I cannot find anything online relating to this. I don't need to perform calculations I need to manually adjust the date.
In case the problem is that you used an ON UPDATE CURRENT_TIMESTAMP on this column you can remove this setting by redefining your column:
ALTER TABLE yourTable MODIFY COLUMN pubdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
You might also check the docs:
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP,
it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP.
And I guess your table was created without defining any of those two resulting in the behavior that you cannot update this column as it is updated automatically.
What happens is:
You pick a row and overwrite the timestamp (like setting the date to 2013)
MySQL detects that the row was updated
as ON UPDATE CURRENT TIMESTAMP is implicitly specified MySQL updates the timestamp field with the current timestamp
Your changes to the timestamp will be overwritten / lost
I was wondering if the following is possible to do through MySQL or will it have to be done using PHP.
Task - "Expiry Date"
User enters product name
User clicks submit form button
Data is POST'ed and then sent to MySQL
Expiry date = date now + 14 days
What I am trying to achieve is a way for mysql to insert an "expiry_date" in a table column that will equal 14 days after the date the row was created in that table.
e.g.
product_name - foo
entry_date - 2012-02-01
expiry_date - 2012-02-15
I have a feeling it may not be possible to do in mysql unless using a stored procedure.
I am happy to do it in PHP however I was hoping if I could do it with mysql it would leave less scope for error in my system.
Thanks
Create a table and set up a trigger for that table.
CREATE TABLE product(
product_id INT PRIMARY KEY,
product VARCHAR(40),
entryDate DATETIME,
expDate DATETIME
);
CREATE TRIGGER test_trigger BEFORE INSERT ON `product`
FOR EACH ROW SET
NEW.entryDate = IFNULL(NEW.entryDate, NOW()),
NEW.expDate = TIMESTAMPADD(DAY, 14, NEW.entryDate);
On each insert into the table, the trigger sets the entryDate to the current time and expDate to 14 days time.
You can use this by using DateTime methods in MySQL:
DATE_ADD(date_starts,INTERVAL 14 DAY)
for example:
UPDATE events SET date_starts = DATE_ADD(date_starts,INTERVAL 14 DAY) WHERE event_id = 3;
For more details go here.
you can try this this function
DATE_ADD(now(), INTERVAL 14 DAY);
date = DATE_ADD(NOW(), INTERVAL 14 DAY)
or as said, alter your table structure for that field (attribute for that field=) to "on Update CURRENT_TIMESTAMP"
According to this source you can't use expressions as a default specifier.
So not sure if it has changed since the bug was posted.
I'm not sure however why I assumed that the OP wants to define it as a default value.
I believe MySQL won't allow you to use expressions in the default value. You might want to write a trigger.
MySQL does provide a TIMESTAMP datatype which can be set to CURRENT_TIMESTAMP when a row is created or updated. You might find this feature somewhat useful:
CREATE TABLE table1 (
`Column1` VARCHAR(50) NULL,
`Created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
Once you're sure that MySQL is filling that column as expected, you can write queries like this:
SELECT `Created`, `Created` + INTERVAL 14 DAY AS `expiry_date`
FROM table1
The easiest way to solve that is just insert the value you want - without trigger or later update, eg.
INSERT INTO <your_table> (name, expiry_date) VALUES ('foo', DATE_ADD(CURRENT_TIMESTAMP,INTERVAL 7 DAY))
I like this format better:
SELECT NOW()+INTERVAL 14 DAY
I'm working in PHP with a MySQL db and I have a current timestamp field which is created when the field is made, I then have another field which (when a page is hit) I would like a SQL statement to insert a replica of the timestamp - only 2 days ahead. Any ideas on how I would go about doing this?
So you have a table like:
id
current TIMESTAMP DEFAULT CURRENT..
another TIMESTAMP
?
You can do something like
UPDATE MyTable SET another = ADDDATE(current, INTERVAL 2 DAY) WHERE id = :myId
MySQL Date and Time functions
How about INSERT [...] (... , ADDTIME(NOW(),'2 00:00:00' , ...)
How do you add time or days to a current utc_timestamp?
I am using;
new CDbExpression('UTC_TIMESTAMP()')
for both 'created' and 'updated' fields in my mysql table but would like to add an 'expiry' field which would allow 4 days from creation date. I presume this is possible but am unsure how.
for insert/update current time
UPDATE table
SET created = NOW()
for 4 days from creation date
SELECT * FROM table WHERE created > DATE_SUB( NOW( ), INTERVAL 4 DAY )
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
In MySQL :
ALTER TABLE `table`
ADD expiry datetime DEFAULT DATE_ADD( utc_timestamp( ) , INTERVAL 4 DAY);
"The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE."
So, this is explicitely documented limitation
you have to create TRIGGER if your MySQL Version < 5.6.5
BUT
MySQL 5.6.5 changelog stats
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Reference :
http://bugs.mysql.com/bug.php?id=27645
http://optimize-this.blogspot.in/