I am creating a table for users that have been banned from my website. I want to move everything over to the new table, including the TIMESTAMP of when the record was created, but the new table will also have a TIMESTAMP for when they got banned.
When I create a row in table foo, I have a TIMESTAMP that registers when that row was created. I would like to transfer that row to another table (bar) that has a different number of columns, and keep the information from the original TIMESTAMP. How can this be done?
I am fairly new to MySQL, so correct me if I have false assumptions, but it seems that the TIMESTAMP field actually creates a TIMESTAMP when the record is created and that the existing TIMESTAMP value would have to be stored as something else like a VARCHAR in the new table. Am I off base?
EDIT: Solved my own question
I originally had not tried anything because I did not know what to try. In the end, it turns out that simply moving the value from one TIMESTAMP column to another TIMESTAMP column does, in fact, work. The only difference between the two columns is that the original TIMESTAMP has a DEFAULT of CURRENT_TIMESTAMP, whereas the new TIMESTAMP has a DEFAULT of NULL.
I used the following code:
INSERT INTO `bar` (`created`)
SELECT `created` FROM `foo` WHERE `user` = '000'
Related
In the server, I have MySQL version:
5.1.61
It has a table called test with 10 columns and 10K rows.
Now I have decided to add a new column
ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
New column ts is added. but the problem is for existing rows this new column(ts)'s value is 00:000:000 Not current time.
PS: When I add new rows or update any existing row then the ts is updated with current time stamp.
why is default current_timestamp/now() not working for existing rows?
Edited:
I can run a simple update SQL to update existing rows to the current time. But I am new in the database and I am trying to know if it is possible to update existing rows with a default value.
The DEFAULT clause in a MySQL table only concerns what happens when new records get added to the table. There is no legacy behavior where the DBMS goes back to already existing records and applies some default value. In this case, if you want the already existing records to bear the current timestamp, you may do a blanket update:
UPDATE yourTable
SET ts = CURRENT_TIMESTAMP;
After this point, when you add new records and do not specify a value for the ts column, it will be assigned CURRENT_TIMESTAMP.
For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));
I have a users table that has a column for the date that they joined. It is a timestamp datatype. I want the value of the column to always be the current time when the user joined, so the default value is the current time. If the user's profile is updated, the user's join date is updated to the current time. In other words, say a user joins on August 2nd and then updates their profile on September 17th. The new value of the join date will be September 17th. This happens even though I don't tell my sql statements to update that row.
How can I make it so that the timestamp column is only affected when a new row is created?
Sounds like you need to configure the default constraint so that it populates the column on insertion only:
DEFAULT CURRENT_TIMESTAMP
ALTER TABLE table CHANGE datetime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Changing it to only be this means that any revisions will not trigger the timestamp value to be updated.
I am dealing with a legacy application that is using MariaDB to emulate a queue. One of the key things missing is that the original design doesn't insert the time the messages in the queue were inserted meaning that the order the messages are processed is not guaranteed.
So far the messages appear to be processed in order as we're only using a single MariaDB instance but I would like to add a created_on column to ensure this continues.
My question is that I need to backfill the created_on column and i was wondering if MariaDB stored the time a given row was inserted into the database?
I realise that unless it is in the schema it is unlikely but occasionally databases will have non-standard extensions that capture this sort of thing. Oracle for example has similar functionality to this.
MariaDB does not have a hidden timestamp. If the table has an AUTO_INCREMENT, that might suffice since you are asking for order, not specifically time.
My opinion of queuing via MySQL/MariaDB: "Don't queue it, just do it". The effort of queuing and dequeuing can become a burden, especially in end cases.
Yes you can, if you were to create a field make sure when you create the field you have the following:
create table test_created_on_table(
created_on timestamp default now() on update now()
);
If you already have a field just take off the "CURRENT_TIMESTAMP" flag on the created field. Whenever you create a new record in the table, just use "NOW()" for a value.
Or.
On the contrary, remove the 'ON UPDATE CURRENT_TIMESTAMP' flag and send the NOW() for that field. That way actually makes more sense.
This would track when row is inserted or updated.
There's another way of doing it by db trigger:
Adding a ModifiedTime
Adding a modified timestamp to a table is the most straight forward. All your have to do is create the field of type TIMESTAMP, and by default, MySQL will automatically update the field when the row is modified.
There are a couple of things to be aware of:
While you can have multiple TIMESTAMP fields in a row, only one of
these can be automatically updated with the current time on update.
If your UPDATE query contains a value for your ModifiedTime field,
this value will be used.
So, to add your modified timestamp field to an existing table, all you need is:
ALTER TABLE my_table ADD ModifiedTime TIMESTAMP;
Adding a CreatedTime
Adding a CreateTime value is a little more involved.
On the latest versions of MySQL it is apparently possible to create a DateTime field with a default value of CURRENT_TIMESTAMP. This wasn’t an option for me as I was having to support a somewhat older version, besides, even on the newer versions of MySQL it is not possible to have more than one field using CURRENT_TIMESTAMP, which of course we are in order to get ModifiedTime working.
So, in order to get a created timestamp, firstly we must add a DATETIME field to the table.
ALTER TABLE my_table ADD CreatedTime datetime NOT NULL;
Note, that this must be created as NOT NULL in order for the next part to work (this is because setting NOT NULL forces an automatic all zeros default).
Next, we must create a trigger, which will automatically be fired when we insert a value into our table and set the created timestamp.
DELIMITER //
DROP TRIGGER IF EXISTS my_table_insert_trigger//
CREATE TRIGGER my_table_insert_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.CreatedTime = '0000-00-00 00:00:00' THEN
SET NEW.CreatedTime = NOW();
END IF;
END;//
DELIMITER ;
Now, when you insert a value into the table, this trigger will fire and, if you’ve not provided a CreatedTime field in your insert query, it will be set to the current time stamp.
I am trying to set up an sql table which records when an account was created and when it was last modified. I would like sql to handle this so I don't have to do it in my php files.
I have two columns in my users table (both are of type timestamp):
created
modified
I want the "created" time to never change and always contain the date it was created, and the "modified" to be changed each time the users row is modified. I have the table set up so "created" works as I expect, but when I try to update modified:
ALTER TABLE `users`
CHANGE `modified` `modified` TIMESTAMP NOT NULL
DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
I get the following error:
1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Can someone assist me on what I need to do to accomplish this correctly?
It's stil not possible in mysql. You can have them set to the actual time only on INSERTs, only on UPDATEs or on both. However you couldn't have more than one of these auto-TIMESTAMP columns in one table. that's now possible using TRIGGERs if using Mysql 5.x
Refer this article It will help you lot :
Two auto-TIMESTAMP columns in one table with MySQL 5.0