MYSQL ON UPDATE CURRENT_TIMESTAMP not updating when using INSERT OR REPLACE? - php

I unable to update current time(NOW) in last_updated cloumn.
Because i have read this query from text file.
INSERT OR REPLACE INTO table_name (kb_corporate_guid,kb_user_guid,
name,last_updated)
VALUES ('100','121','FAQ','2013-02-07 07:06:05');
I want to ignore last_updated cloumn value '2013-02-07 07:06:05' even if i specified in query and replace with NOW() value into last_updated cloumn.
I tried this one but it won't work for me.
ALTER TABLE table_name
CHANGE `last_updated` `last_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP;

Option 1
You may create trigger on insert/update which will update last_updated field with MySql NOW() function, it will overwrite the field value, but it might slow down the process when you do bulk insert/update.
Option 2
Find and Replace the text for last_updated field and timestamp values from text file.
Option 3
Create temporary table with same schema and import into temporary table then use INSERT INTO main_table SELECT corp_id, user_id, name, NOW() FROM temp_table table to insert into main table.

Default is used when the value is not sent in the query, use now in the query instead of timestamp..

Related

PDO: update mysql `date` column from (d/m/y) to timestamp

I have a mysql users table with column join_date in a format "d/m/Y". I just want to update this column to a timestamp of that existed data.
id name join_date
1 john 08/02/2014
Now I need a code to run (like while loop or foreach) to update all dates data in column join_date to a timestamp [time():] as below:
id name join_date
1 john 1391814000
Any suggestion guys? Thanks in advance.
In the comments we could find out that not TIMESTAMP but DATE is the best datatype for your application. Assuming that join_date is a varchar column, you can issue the following SQL commands in order to change the column type and convert the data.
First you need to add a new, temporary, column:
ALTER TABLE `users` ADD `temp_date` DATE;
Then convert the existing strings to DATE values:
UPDATE `users` SET `temp_date` = STR_TO_DATE(`join_date`, '%c/%e/%Y');
Now you can drop the existing column:
ALTER TABLE `users` DROP `join_date`
And finally rename the temporary column:
ALTER TABLE `users` CHANGE `temp_date` `join_date` DATE;

Add Date/Time when adding product to database

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

mysql trigger or php code

I have MySQL database that has 21 tables, in each table I have 'created' column(timestamp).
this field holds insert time of row.
However:
1- I can write a trigger for fill the column for each table like this:
CREATE TRIGGER `created` BEFORE INSERT ON `some_table`
FOR EACH ROW SET NEW.`created`=NOW()
2- I can handle it by PHP:
$result = mysqli_query($con,"INSERT INTO table_name (column1, column2, column3,created)
VALUES (value1, value2, value3,now()) ");
I concerned about writing trigger for each table has overhead. Is it right?
which one is better specific on performance?
which one is better at all?
You should not need to fill this column yourself if you configure the TIMESTAMP properties for it...
http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
To clarify from the documentation here is a working example of how subsequent updates do not change a timestamp field when correctly configured for the desired behaviour
create temporary table example (apID int auto_increment,
audit timestamp DEFAULT CURRENT_TIMESTAMP,
name varchar(50),
primary key(apID));
insert into example(name) values('test');
select * from example;
update example set name='tes' where apID=1;
select * from example;

storing the updated timestamp whenevr the query from that particular table is called

I want to store the time at which the table was accessed/modified. I know how to put current timestamp.
But what we must do to store the updated timestamp ?
I am not sure what exactly you are looking for but below query might be helpful.
you can create/upate your table to have "On Update Current_timestamp"
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
To update a timestamp when accessing a record you could use a stored procedure to select the records and then update the timestamp fields.
#Suresh Kamrushi has suggested a solution for updating the timestamp when the record is modified.

Adding Timestamp to Database Table

I have an html/php form that updates entries on the database server. I need to add a field to each row indicating when that entry is added, so in other words a timestamp of when the entry was created. I have been searching and found this:
http://www.createafreewebsite.net/phpmysql/alter.html
Would I do something like:
$timestamp = time();
mysql_query("ALTER TABLE notification
ADD timestamp CHAR(30) AFTER names);
mysql_query("INSERT INTO notification (`timestamp`) values ('$timestamp');
is this the correct way to approach it, and am I using the correct datatype? I would need to compare the timestamp with another timestamp generated from a javascript file later on. For example, if timestamp1 is smaller than timestamp2 than perform following functions...
Any information would be helpful, thanks!
EDIT:
Provided information as requested:
So far I have:
mysql_query("INSERT INTO notification (`program`, `month`, `day`, `year`, `sponsor`, `type`, `category`, `range`, `desc`) values ('$pName' , '$month' , '$day' , '$year' , '$sponsor' , '$type' , '$category' , '$range' , '$desc')");
time() in PHP will produce a timestamp, your MySQL table might be expecting another format, so you can just do:
mysql_query("INSERT INTO notification (`timestamp`) values (NOW());
and it will work with date and datetime fields too.
Even though your table is CHAR(30) you still have one less variable to use.
Of if you change your column data type to TIMESTAMP then you can use on update CURRENT_TIMESTAMP to fill the table cell for you.
timestamp should have a timestamp datatype. http://dev.mysql.com/doc/refman/5.0/en/datetime.html
You definitely do not want to use a column with CHAR or VARCHAR datatype to store a date or timestamp - it can make comparisons difficult later on. Also, you should consider putting a default on the timestamp column so that it is automatically populated when you insert a row, or using an insert trigger on the notification table to do the population. That way the chance of developer error is reduced.
CREATE TRIGGER notification_timestamp BEFORE INSERT
ON notification FOR EACH ROW
BEGIN
SET new.timestamp = NOW();
END;
Apologies if the syntax isn't quite right.

Categories