I am trying to find out when something was added to a database I maintain but the script that adds the date was working.
Is there a way to retrieve the date of the original INSERT command?
Something like this?
http://dev.mysql.com/doc/refman/5.0/en/query-log.html
make a new table with the key of the table to watch create a after insert trigger that inserts a new line into the watchout table with the id and the time inserted
Related
I'm quiet new in the SQL field. Thus I have a way of working question.
Every week, I will send data from an Excel spreadsheet on my MySQL DB through a pHp code. This is already working. Thus I have a table which I can update.
Actually I'm sending price of a specific underlying on my DB. What is the best way to archive my data as following.
On my DB, I have the below structure :
tabe t_index
Label = VARCHAR(255)
Price = float
Date = date()
Let's say I sent my data on my db last week, thus I stored :
Stock A
102.85
2013-03-18
Today, I want to send new price for the same Stock A which is 103.54, but I would like to archive and keep the 102.85 price to be able to make some return or whatever between the two prices.
How should I proceed ?
I hope my question is not too messy...
Thank you for your help
One way of doing this is, create a UPDATE trigger which inserts old value in another table. So when you update an existing entry, old data will be copied/archived to another table.
CREATE TABLE t_index_archive (
Label VARCHAR(255),
Price float,
Date datetime);
Now create a trigger on your existing table
DROP TRIGGER IF EXISTS archive_t_index;
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER archive_t_index BEFORE UPDATE ON t_index
FOR EACH ROW BEGIN
INSERT INTO t_index_archive VALUES (OLD.Label, OLD.Price, OLD.Date);
END;
$$
DELIMITER ;
You can add another column named like is_active ENUM type with value active,inactive
By default is_active's value will be 'active'
and when you enter new entry in database just update old entry's is_active with 'inactive' and then add new entry
Fetch new data using query using where clause WHERE is_active='active'
How can I compare data before and after update PHP and MySQL?
If I add some data in the database that can be updated, is there a way of comparing the newly updated data with the previous data?
Yes, that is what TRIGGERS are for.
Using the CREATE TRIGGER syntax you can create a trigger before an UPDATE or INSERT
CREATE TRIGGER insert_trigger_name BEFORE INSERT ON table
or
CREATE TRIGGER update_trigger_name BEFORE UPDATE ON table
I'm using an INSERT ON DUPLICATE KEY statement for my website. It's for creating news items, so I figured I could use the same MySQL command for both creating and updating news items.
However, when I use the following:
INSERT INTO table (id,title,content) VALUES(NULL,"Test","Test");
Instead of creating a new auto increment value it throws an error. However, the command works on my main development server. But not on my laptop. Both versions of MySQL are the same, the only difference being MySQL was installed manually on my server, and with WAMP on my laptop.
Are there any MySQL Variables that could be causing this?
I would suggest using INSERT INTO table (title,content) VALUES("Test","Test");
This will create a new row in the table with a new incremented ID.
Managed to solve it as best as I can.
I checked my code and found that when I inserted the empty POST'd ID was wrapping it in quotations. I've now changed it so that it puts NULL without quotations. So my query should now look like:
INSERT INTO table (id,title,content) VALUES(NULL,"test","Test")
ON DUPLICATE KEY UPDATE title=VALUES(title), content=VALUES(content);
That now works.
I think you should make query like this,
INSERT INTO table (title,content) VALUES("Test","Test");
If it still doesn't work then check if id column is set as auto-increment or not.
I'm looking to pull a table's create syntax via mysql and php. Is it possible?
I need it for a file that creates table_x automatically every 10 days. Since I update the site constantly and create new fields I'd like the file to be dynamic and use the previous table (instead of me updating it manually each time).
Yes you can - use
SHOW CREATE TABLE tablename;
Actually if you want to 'reset' your database I would use TRUNCATE instead
TRUNCATE TABLE tablename
SHOW CREATE TABLE table_x;
...will output one record with the CREATE syntax. Is that what you're looking for?
Table in MySQL have 1 problem column: creation_date.
During inserting a new row through PHP, I thought that there would be correct to insert the date directly in the query, MySQL has to do it himself.
Do I need to do the trigger, or it would be better to use PHP for this, as intended?
How would you have done to?
PS: MySQL: How to create trigger for setting creation date for new rows
Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP field
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
If you create a trigger for your table (or adopt the suggestion above) you won't have to remember to do it in your PHP. This advantage will show when someone else creates another PHP to insert into the same table.