How can I create a record table when a client adds a new row on a table, which keeps ;
the client's Id,
when the data is added,
Id of the data.
For example I have a PersonTable in mysql; and when a client(with the Id of 2 in users table) adds a new person with the Id of 12, my record table automatically adds cliend_id(2),related_Id(12) and creation time of the new record.
I think there is a way in mysql, writing a trigger would be possible, but is there any possible ways. I'm very beginner of mysql. Thanks
I'm not sure which version you run of MySQL but from version 5.5 onwards this is possible when using TIMESTAMP as a type. Versions >=5.6.5 supports this functionality for type DATETIME as well.
-- TIMESTAMP example with 1 column
CREATE TABLE foo (
'current_ts' TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- DATETIME example with 2 columns
CREATE TABLE foo (
'creation_time' DATETIME DEFAULT CURRENT_TIMESTAMP,
'modification_time' DATETIME ON UPDATE CURRENT_TIMESTAMP
)
From the docs:
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. The following notes first
describe automatic initialization and updating for MySQL 5.6.5 and up,
then the differences for versions preceding 5.6.5.
https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
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.
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 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'
I have a table where I have a date column. Is there a way for MySQL to auto fill this field whenever I insert a new registry with the current date? Or is this made automatically by default?
P.S.: I'm using PHPMyAdmin
Although it is an old post, maybe this image will help as it is more explicit:
(For phpMyAdmin users)
This configuration sets that field with a value like:
2015-12-11 07:50:47
PS: Note that the timestamp will set the time OF your server!! (i.e. the example above got the time from Pacific Time (07:50:47) but it could have been from a Spanish user at 16:50:47 local time) Keep this in mind.
Also, if you already have a "Created Date" you might need another column that updates the modification date whenever there is an update:
You only need to set on update CURRENT TIME STAMP in Attributes Field.
Ready to rock!
Set Default to in your mySql query
CURRENT_TIMESTAMP
you have to use
now()
function where you want to fill current time.
i.e.:
INSERT INTO user_rights (`user_id`,`right`,`group_id`,`created_date`) VALUES ( '42', '160', '1', now());
I realize this may not be a direct answer to the question but I do believe this is the most useable solution.
I highly recommend using a DATETIME or TIMESTAMP data type for the column in question.If you are utilizing a fairly current version of MySQL, MySQL will do the work for you.
Details:
To be very clear, as of 5.6.5, for both the TIMESTAMP & DATETIME datatypes, you can do the following:
Set a DEFAULT value of the current date & time (using NOW() or one of its aliases such as CURRENT_TIMESTAMP)This means every time you insert a new row into this table a TIMESTAMP or DATETIME column with this default will get the current date and time
Set an ON UPDATE constraint that will UPDATE a column to the current date & time when, (you guessed it) the row is updated
Here's how:
An Example in a CREATE TABLE statement:
CREATE TABLE t1 (
ts1 DATETIME ON UPDATE CURRENT_TIMESTAMP
,ts2 DATETIME DEFAULT NOW()
);
Please note that DATETIME can be replaced with TIMESTAMP for effectively the same functionality.
Additionally I suggest the use of the DATETIME data type over TIMESTAMP as DATETIME has a much larger range of dates it can support. It's worth mentioning that TIMESTAMP is smaller for those few cases that matters. For more details please read my answer here: https://stackoverflow.com/a/26117532/1748266
I have added this to my table and it works
ALTER TABLE Medewerkers ADD med_created TIMESTAMP DEFAULT now();
When you insert data into your record it update automatically the med_created
MySQL unfortunately doesn't allow specifying values other than constants as the default for columns other than TIMESTAMPs.
This is a feature available in MySQL versions 8.0+, but for older versions the only solution for a database defined default would be to use a trigger.
You can do something like this from the SQL screen
ALTER TABLE `table_name` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL
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