I have a quotes table and a jobs table in a MySQL database. I want to automatically create a job number when I change the quote disposition to PO obtained. The disposition field in an enum so their will be no variance.
The JobNum field is auto-incremented so I felt inserting the bid number into the table would auto generate a new row and thus a job number.
This is the script I was playing with.
DELIMITER $$
CREATE
TRIGGER create_job AFTER UPDATE
ON Quotes
FOR EACH ROW BEGIN
IF Quotes.QuoteDispo = PO obtained
INSERT INTO Jobs (BidNum)
SELECT BidNum
FROM enjouede_delham.Quotes
END$$
DELIMITER ;
I would also like to create a pop-up warning prior to the action in case of entry error. Is this possible with PHP or must I use JavaScript? I am using PHPRunner to create the data entry forms but can insert custom scripts for functionality.
Related
I have a SQL databases of users forms, and want to set up some routine or maybe even PHP that will automatically delete empty rows which looks like this:
Column1=(Name=John Surname=Doe) | Column2=(Name=John Surname=) | Column3=(Name=John Surname=Doe)
In this case I would like for database to automatically delete Column2 because surname is missing...
Manually, I could do it like this:
DELETE FROM myTable WHERE Surname='';
Can this be helpful? Never used it.
Or to make some PHP script on load of my website and onload of display of my table (I forgot to mention that I have another web page where I view my table...) which will delete emtpy rows, aka run this code: DELETE FROM myTable WHERE Surname='';
I cannot forbid or put required on my input form because of other reasons...
Any help is apricated, thank You.
First solution:
In Cpanel/DirectAdmin/... create a Cron Job for PHP.
Second solution:
CREATE EVENT IF NOT EXISTS delete_event
ON SCHEDULE EVERY 30 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 24 HOUR
DO
DELETE FROM myTable WHERE Surname='';
Third solution:
When running the script related to the table, put the delete command.
Add trigger. This will remove the inserted row inmediatly. The new keyword is for access the columns in the inserted row
replace id with the primary key of myTable
DELIMITER $$
CREATE TRIGGER delete_if_empty
AFTER INSERT ON myTable
FOR EACH ROW
BEGIN
IF new.Surname IS NULL OR new.Surname = '' THEN
DELETE FROM myTable WHERE id = new.id;
END IF;
END$$
DELIMITER ;
Another solution could be adding NOT NULL constraint on surname column to prevent insert row like this. Depending on how you handle result of the insert query, probably you will need to add some lines to your code add to avoid breaking of the posterior logic
I have a table named clients, in that table there's two columns of importance; id and client. I have a secondary table in the same database named calendar. I really want the two columns of id and client in the calendar table to sync with the ones in client table.
Right now I am using this PHP to execute this in MySQL:
INSERT IGNORE INTO calendar (id, client) SELECT id, client FROM clients;
Is there a better way of accomplish this task? Maybe a built in function in MySQL that I have overlooked or something like that?
Use Triggers : The MySQL trigger is a database object that is associated with a table. It will be activated when a defined action is executed for the table.
The trigger can be executed when you run one of the following MySQL statements on the table: INSERT, UPDATE and DELETE and it can be invoked before or after the event.
You can make trigger when you insert or update a row in main table and make the changes in another table
Example:
DELIMITER $$
CREATE TRIGGER my_sync_trigger
AFTER INSERT ON `clients` for each row
begin
INSERT INTO calender (id,client)
Values (new.id, new.client);
END$$
DELIMITER ;
"new" stands for the new value inserted into clients table. The same value will be inserted into id and client column in calender.
Note: single quotes are removed from table name because quotes effectively make it a string literal instead of a proper identifier.
DELIMITER command will change the ending of each statement from ";" to "$$" so that MySQL is not confused with ";" inside and outside the trigger
Make similar triggers for update and delete also
Simple guide for examples and syntax:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
I am quite new to SQL and am working on a small project, but I got stuck when wanting to implement triggers to my model.
I am designing a database for a sports league. I have a table "GamePlayed" with a column called "GoalsScored" and another one "Team" with a column called "GoalsFor". Those two tables are connected by the "TeamID" key.
I would like to have a trigger that updates the value of "GoalsFor" by adding to the previous value the value of "GoalsScored". In this way, with every game, the total quantity of goals scored will be constantly updated.
I tried the following:
CREATE TRIGGER "Goalsscored" AFTER UPDATE
ON `GamePlayed`
FOR EACH ROW
BEGIN
UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored`
WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`
END
Unfortunately, it says there is a problem in my SQL syntax...
How can I do it?
You need to change your delimiter. Something like this
delimiter $$
CREATE TRIGGER "Goalsscored" AFTER UPDATE
ON `GamePlayed`
FOR EACH ROW
BEGIN
UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored`
WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`;
END;
$$
See eg http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
Well, let me explain this as simple as possible, basically i have a table doc_info, which stores information regarding uploaded files; like file name, date created, uploaded by etc;
what i want is to create an INSERT trigger on this table, which will get two things, the primary key ID of this newly inserted row and the extension of the uploaded filename which will be in the document name; and concatenate them and update that same row, with this concatenated value
Example,
If someone uploads "document.docx", then ID will be auto generated as x and document name will be document.docx, thus the value to store will be "x.docx" using update on that same row.
I am new to this MySQL, and have little knowledge if operations like this can be performed with MySQL.
To implement such action within db you should create two triggers: after insert and on update. They should be like this one
delimiter |
CREATE TRIGGER changeProperty AFTER INSERT ON doc_info
FOR EACH ROW
BEGIN
UPDATE doc_info SET doc_info.someProperty = CONCAT(doc_info.id, doc_info.extension) WHERE doc_info.id = NEW.id;
END;
|
You can calculate extension on you file name by following expression: SUBSTRING_INDEX(doc_id.fileName, '.', -1);
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'