Where and when does this view get populated with data? - php

I've recently been handed a Yii project (PHP) developed by another developer and I've been assigned to take over it's maintenance.
In the database there is the following VIEW that holds data for certain records in other tables. The view gets -somehow- updated frequently as it contains recent records, meaning that somewhere/somehow rows are constantly being added to it.
However, it's not being referenced as part of the project, meaning it's not defined in a model, controller or anywhere else inside the project whatsoever. I've also gone through the database triggers to see where this view gets its data from but I fail to connect the ends.
I thought there might be a cron job that adds data to it but nada.
So, I'm pretty stuck and in awe. Any ideas on how I could find WHEN this views is either dropped and recreated or where I can find which database action functions as a trigger for inserting rows to it?
DELIMITER $$
ALTER ALGORITHM=UNDEFINED DEFINER=`user_name`#`` SQL SECURITY DEFINER VIEW `VW_viewname` AS
SELECT
`DOM`.`CustomerID` AS `CustomerID`,
`DOM`.`User` AS `User`,
`DOM`.`DemoOrderDate` AS `DemoOrderDate`
FROM `Demo_Orders_M` `DOM`
WHERE (`DOM`.`DemoOrderStatus` = 253)
GROUP BY `DOM`.`CustomerID`
HAVING (COUNT(`DOM`.`CustomerID`) > 1)$$
DELIMITER ;

Related

How to copy my table to a dump table and deletes the values in the main table

I am working on php codeigniter framework with mysql workbech as back end tool...I have a doubt regarding executing one of the sql query that should be scheduled for one month....like I will explain it in clear...
I have a table named packet data in that table I will get 700K of records per day so totally for 1 month I may get 200K records roughly..and my application runs mainly on this table so because of this, my application runs slow....
So I want to move every month of data to a dump table and should be deleted from the main table so that my application may run fast....
This copying of 1 month data should be done every month and should be deleted form the main table.....
Please can anybody give me the solution how to perform the same delete and copy queries that should be performed in the same query....and whether it is possible or not can you tell me....
Can you try this code :-
INSERT INTO table1 (col`)
SELECT col
FROM table2
WHERE month(col)=month(curdate() - INTERVAL 1 month)
http://www.sitepoint.com/how-to-create-mysql-events/
You can create a mysql event which will do this. The link above explain it brifely but MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. When you create an event, you are creating a named database object containing one or more SQL statements to be executed at one or more regular intervals, beginning and ending at a specific date and time.
there are a few other options like cron events however i would recommend using mysql events because they are made for these types of queries.
There is an example from the website link.
DELIMITER $$
CREATE
EVENT `archive_blogs`
ON SCHEDULE EVERY 1 WEEK STARTS '2011-07-24 03:00:00'
DO BEGIN
-- copy deleted posts
INSERT INTO blog_archive (id, title, content)
SELECT id, title, content
FROM blog
WHERE deleted = 1;
-- copy associated audit records
INSERT INTO audit_archive (id, blog_id, changetype, changetime)
SELECT audit.id, audit.blog_id, audit.changetype, audit.changetime
FROM audit
JOIN blog ON audit.blog_id = blog.id
WHERE blog.deleted = 1;
-- remove deleted blogs and audit entries
DELETE FROM blog WHERE deleted = 1;
END */$$
DELIMITER ;
BTW: I do not know too much about your database design, business model or even what type of queries you are running however if you are copying the data over for backup reasons then I would rethink your database design and also think about weather or not you need to store the data in another table or exporting it as a csv file and holding csv files as backup is a more appropriate. Storing Csv files could be a better solution depending on weather or not you need to access that data again through out the months. Think about it however creating a mysql event would do what you wanted to do.

PHP MyAdmin After Update Trigger not working

I am using PHP MyAdmin Version 4.1.12.
I am trying to create a simple trigger that, after an update, sets 'dateModified' in table 'person' to CURRENT_TIMESTAMP. dateModified is of type TIMESTAMP. The way in which the update occurs to person is the setting of a single attribute in a single record through a X-Editable enabled grid view on a web page. After performing validation against the model with the updated attribute, a new database command is created with the relevant update SQL and executed. So each update only ever modifies a single row within 'person'.
Here is the SQL I wrote to create the trigger:
DELIMITER |
CREATE TRIGGER PERSON_AUPD AFTER UPDATE ON person
FOR EACH ROW BEGIN
SET #dateModified = CURRENT_TIMESTAMP ;
END;
|
DELIMITER ;
After performing updates, I see that the trigger hasn't fired, and the timestamp remains unchanged from the one they were created with (the default for dateModified, and dateCreated, are both CURRENT_TIMESTAMP, so they get set automatically on insert).
I have looked around for answers, and even looked into alternate methods to getting the update (the alternate method was calling a model's afterupdate method and performing separate SQL there on dateModified). I would prefer to exhaust every opportunity to use the triggers, before I go putting more code into my model.
Any help would be greatly appreciated.
Thanks to #juergend, the solution was the following:
Set trigger type to before update because after update cannot update attributes, and are best used to insert new records in related audit tables, etc.
Add NEW. to the front of the attribute you wish to modify.

Auto erase table data in php myadmin

i'm developing a feedback form, where students will be allowed to give feedback on the particular subjects.
I have a table with 3 fields "ID, Unique No, Password", where students admission number are stored. Now here is what i want.
As soon as each students completes giving the feedback, his particular data's from the table must be deleted automatically.
please help.
This can be done with a JOIN, but I'll demonstrate a trigger here, because I mentioned it in my comment above.
I assume you've got another table where you store the students feedback data. Let's call it students_feedback and the other students_admission for this example.
Using MySQL Triggers, you assing the Database to delete the student admission data automatically ON INSERT. You'll want to use on create, because as soon as the feedback data is stored in the students_feedback table, the INSERT event is triggered.
So, for example:
CREATE TRIGGER delete_admission AFTER INSERT
ON students_feedback FOR EACH ROW
BEGIN
DELETE FROM students_admission WHERE students_admission.id=OLD.id LIMIT 1;
END;
Use whatever DELETE query you want here.
NOTE: Before MySQL 5.0.10, triggers cannot contain direct references to tables by name.
Like explained before use a trigger. Simply click on triggers and create a trigger that occurs after an INSERT in the table that records the feedback of the students. You could do something like this
I don't really agree though that using triggers is a good practice. Triggers are business logic and their logic should be implemented in your code. Separating business logic in your app and in your database makes it harder for the next developer to work on since he doesn't know where to look. The only reason that i think is viable to use them is when it is used for distributed databases to keep them updated in relation to each other.

auto sync two mysql tables with exception

i have 2 sql tables of a script that i need to be sync to another, this can be done with php cron (this was my plan) exept from one row
Table 1 Table 2
row 1 <----> Row 1
Row 2 <----> row 2
row 3 no sync row 3
both databases on same server
and the same user has full rights for both
i am looking for a php code to do this via a cpanel cron
on an after thought would it be best to merge the two so both updates with new data?
the issue is that in the example above i am needing row 3 to not change on both databases
I am very noob so please be nice lol Thx in advance
Update *
i should learn how to explain a bit better.
both the databases are control panels for sites, one of the tables rows has the system url in it, so if i share the database "site 2" links refers back to "site 1" this is a complex problem for me as i am very new to this.
what i need is to keep both databases upto date except that single row which in turn be different for both databases.
i have not tried anything just yet as i wouldn't know where to start :( lol
You dont have to use cron. MySQL in current version supports TRIGGERS and EVENTS.
You can use TRIGGER to copy data to another table. That copy (or any other operation) may be triggered by some event (like insert, update or delete in table). TRIGGER may run any query or any PL/SQL code.
Other option is an EVENT. This is something like internal task sheduler built in MySQL. It can also run queries, or any PL/SQL code, but it is triggered by system time (like Linux Cron). It has many advantages compared to cron.
PL/SQL is procedural SQL, with loops, variables and more.
If you think you are "noob" - i have cure for you. Read books about MySQL or if you are lazy - watch some tutorials ( http://thenewboston.org , http://phpacademy.org ).
Nobody here will write code for you. We can only fix a bug, give advice etc. :)
EDIT.
Example of EVENT:
-- this is comment in SQL language (line starts with --)
CREATE EVENT event_daily_copy_something
ON SCHEDULE
EVERY 1 DAY
COMMENT 'This text will appear in MySQL Workbench as description of event'
DO
BEGIN
INSERT INTO your_db_name.target_table_name(id, field)
SELECT id, something
FROM your_db_name.source_table_name
WHERE id = 3;
END
Synchronization of tables is quite complicated. I think you need few operations in event.
Check for new rows and copy
Check for deleted rows and delete them in "copy" table
Check for changed rows (here trigger on source table would be very useful, because trigger "knows" what row is edited and you can access new field values in table 1 and use them to update table 2).
One of MySQL tutorials - thenewboston#youtube.

mysql show table / columns - performance question

I'm working on a basic php/mysql CMS and have a few questions regarding performance.
When viewing a blog page (or other sortable data) from the front-end, I want to allow a simple 'sort' variable to be added to the querystring, allowing posts to be sorted by any column. Obviously I can't accept anything from the querystring, and need to make sure the column exists on the table.
At the moment I'm using
SHOW TABLES;
to get a list of all of the tables in the database, then looping the array of table names and performing
SHOW COLUMNS;
on each.
My worry is that my CMS might take a performance hit here. I thought about using a static array of the table names but need to keep this flexible as I'm implementing a plugin system.
Does anybody have any suggestions on how I can keep this more concise?
Thankyou
If you using mysql 5+ then you'll find database information_schema usefull for your task. In this database you can access information of tables, columns, references by simple SQL queries. For example you can find if there is specific column at the table:
SELECT count(*) from COLUMNS
WHERE
TABLE_SCHEMA='your_database_name' AND
TABLE_NAME='your_table' AND
COLUMN_NAME='your_column';
Here is list of tables with specific column exists:
SELECT TABLE_SCHEMA, TABLE_NAME from COLUMNS WHERE COLUMN_NAME='your_column';
Since you're currently hitting the db twice before you do your actual query, you might want to consider just wrapping the actual query in a try{} block. Then if the query works you've only done one operation instead of 3. And if the query fails, you've still only wasted one query instead of potentially two.
The important caveat (as usual!) is that any user input be cleaned before doing this.
You could query the table up front and store the columns in a cache layer (i.e. memcache or APC). You could then set the expire time on the file to infinite and only delete and re-create the cache file when a plugin has been newly added, updated, etc.
I guess the best bet is to put all that stuff ur getting from Show tables etc in a file already and just include it, instead of running that every time. Or implement some sort of caching if the project is still in development and u think the fields will change.

Categories