SQL Trigger - Check for string - mysql

Is it possible to run a trigger whenever a new row is inserted into lets say shipping_rate, and it will check column company within that row for a certain string. If that certain string is is there, I then want to perform an equation on column cost to change the current number. Is this feasible in MySQL?

Yes, it is entirely possible as trigger can do essentially anything to the database that a normal query could. You can read up on how to do that in official manual.

Related

SQL how to do string replace for any SQL query data which comes out of column without using SQL

I have a table with a column for different fruit names - Apple, Orange, Banana etc. These fruit names can have duplicates.
Right now if I do a SQL Select, I get the names as it is. I want to change the data so that every "Apple" gets replaced with "Sweet Apple" and every "Orange" gets replaced with "Mandarin".
I know I can use the replace function in my SQL queries. However I don't want to/can't modify my SQL queries. I was trying to leave changing the SQL as a last resort because that needs to be done on several different nodejs scripts.
I am wondering if there is some way in the database itself which can make it return these altered data automatically. Sort of like a filter / pipeline / constraint (I am not sure what to call it) which is set on a specific column of a table and makes it automatically do this replace function for any data which is queried from this table.
I would like an answer for mainly Postgres and MySQL and if possible for SQL Server too.
No, the closest would be triggers on Insert and Update to replace the data as it comes in, but you cannot override data that is being queried without specifying it in the query. You can create a view that would show the replaced strings.
It has its drawbacks, but you could do this:
Change the name of the Fruit column to something else, and then create a new Computed column that does the REPLACE you want on the newly-renamed Fruit column, and give this new column the name of the old column, so that all your existing queries will hit the new column.
Drawback is that any existing INSERT/UPDATE queries have to be changed to INSERT/UPDATE to the new name of the old column.
Well, The Idea of a database is "storing values". So, if you are querying these values, you only have little options on modification.
Your expectation of handling is clearly up to your programming language. Whenever you output a value retrieved from the database, wrap it, filter it - return whatever you need instead of the stored value, i.e.:
public static string filter(value){
if (value == "Apple")
return "Sweet Apple";
if (value == "Orange")
return "Mandarine";
return value;
}

MySQL and Matlab - Replacing a whole column of data

I'm working on a program that takes in some data from a database in MySQL, changes some numbers, and then overwrites the old data on MySQL with the new stuff. (Specifically, the data I'm taking in is output from a weather forecasting model). What I'm struggling with is being able to replace the old data in the database with the edited data.
In my program, the new data (solar radiation values) is a column of numbers in the 3rd column of the matrix WxData (so it can be accessed with WxData(:,3)).
In the MySQL database, the values I want to change are under the column titled "radiation" in the table "wrf". "dbConn" is the name of the database connection.
I tried something like
update(dbConn, 'wrf', {'radiation'}, WxData(:,3), 'WHERE radiation > -1')
The update function in the database toolbox in Matlab requires a where clause input so I just put something that is always true. But this method doesn't seem to work...it ends up changing every single radiation value in the database table to the same number (possibly the value at WxData(1,3)).
I've tried a couple other ways but nothing worked. How can I just replace the whole column of radiation values in the database with a new column? Seems like it should be simple.
SQL does not think in columns or vectors.
Therefore your method simply cannot work, it is not a syntax issue.
If you want to do this, you can assign ID's to the rows in SQL, import these into matlab as well and then put the ID in the where clause and run it for each line.

How to update data in database with query that not change original value?

Lets say I have multiple commands like 5000 which updates some column and row in databse - it is number for example
UPDATE salary SET money=2000000 WHERE person=55;
Is there any way how to update such data without destroying the original value? Lets say the money has already exiting and different value? I have searched google and MySQL manual but I did not find any useful query for this. Is there any way how to update such data without destroying the original value? Thank you.
Your guess was almost perfect.
UPDATE salary SET money=money+2000000 WHERE person=55;

What are some alternatives to using a cursor in SQL Server 2008?

I've been creating imports that use SSIS to import data into a temp table, then using a stored procedure, steps through the data one by one with a cursor to process the data and insert information into 3 different tables. The inserts in the first 2 tables are complicated because if there is a record that already exists with the same data the record is not created. Whether the a record is inserted or not in the first 2 tables the ID of the record or matching record is returned to be used on the 3rd table. Is there an alternative to using the cursor?
Without seeing your current code it is difficult to know whether this would be suitable but I'd look at
the MERGE statement (allows actions to be specified for the different cases "when matched", "when not matched by target", "when not matched by source") and
the OUTPUT clause (allows you to capture the newly updated records for processing).
Hopefully some ideas from this will help. If you still need help avoiding a cursor, we need to see a better example of the processing you are doing in the cursor.
http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them
This sounds like the perfect candidate for replacing a cursor with a combination of table variables and a while loop (which, multiple people have tested and confirmed, is nearly always more performant than a cursor).

Detect time of last change on a Microsoft Access database table

Does anyone know of a way to detect when the last time a Microsoft Access table has been changed (inserted into or updated)? We used OLEDB via ADO COM to communicate with an access database programmatically and were looking for a way of detecting changes to specific tables. We don't need to know what those changes are, just that changes were made.
The only way to detect if data in the table has changed is to perform a query against the table.
You must add a column of type DATETIME to the table e.g. named LastUpdatedDate that indicates the last updated date/time of each row. Make it NOT NULL so that you will have to write an updated DATETIME value to that column for each INSERT or UPDATE. Also, set the column to have a default of DATE() for the current date stamp or NOW() for the current date/time stamp. Then add a Validation Rule or CHECK constraint e.g. CHECK (LastUpdatedDate = NOW()) to ensure the column is actually updated on each UPDATE and INSERT.
Finally, run a MAX(LastUpdatedDate) query and you will get what you need.
There isn't a way without "manually" writing to a column each time you access the table.
As others have indicated there is no way to track changes without coding it yourself.
There's a simple example at
ACC2000: How to Create an Audit Trail of Record Changes in a Form
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q197592
Audit Trail - Log changes at the record level at:
http://allenbrowne.com/AppAudit.html
The article addresses edits, inserts, and deletes for a form and subform.
Modules: Maintain a history of changes
http://www.mvps.org/access/modules/mdl0021.htm
The History Table routine is designed to write history records that track the changes made to fields in one or more tables.
You will need to implement a timestamp column in your table, and update the value during your data changes.