Can you modify a foreign key on insert with a trigger? - mysql

I have an issue where a query is inserting into table A, which references table B, but it's using the wrong foreign key for table B. Is it possible to create a trigger in oracle where if the input foreign key is 'ASDF', we modify it to 'FDSA' before the insert so that we can fix this issue?

In either MySQL or Oracle, you can do this using a before insert trigger.
I don't recommend using a trigger for this purpose. Fixing the input data or adding the new value to the reference table seem to me to be more sensible approaches.

Following up on #GordonLinoff's post - I agree with (what appears to be) his point that a trigger is inappropriate here. However, recognizing that sometimes you have to work with what you're given, if you were to use a trigger you'd use a BEFORE INSERT trigger, as in:
CREATE TRIGGER TABLE_A_BI
BEFORE INSERT ON TABLE_A
FOR EACH ROW
BEGIN
IF :NEW.FK_FIELD = 'ASDF' THEN
:NEW.FK_FIELD = 'FDSA';
END IF;
END YOUR_TABLE_BI;
Best of luck.

Related

MySQL merge data on duplicate key

I'm adopting dashboard now and I created two tables for selecting from frontend;
DATA_SELECTED_HISTORY
DATA_SELECTED_NOW
My frontend page get data from DATA_SELECTED_NOW and my backend algorithm put new data to this database.
I want to put my new data to DATA_SELECTED_NOW,
and the former data to be pushed to DATA_SELECTED_HISTORY when being faced with duplicate key.
I think I could use a swap table solution or insert(select subquery) + insert on duplicate key solution, but I don't get an idea anymore.
How can I use this solution in SQL?
you can use trigger in this case, to check duplication before insert to DATA_SELECTED_NOW and insert in DATA_SELECTED_HISTORY if it duplicates, check the below code
CREATE TRIGGER TRIGGER_Name
BEFORE INSERT ON DATA_SELECTED_NOW
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT 1 FROM User WHERE key = NEW.Key)) THEN
-- you can replace "key = NEW.Key " with your logic to check
-- inset into DATA_SELECTED_HISTORY
END IF;
END$$

using trigger to update another database's table on same server

I am trying to insert record in db2.inventory whenever there is an insert event in db1.sales using triggers.
DROP TRIGGER IF EXISTS `insertintodb2`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `insertintodb2` AFTER INSERT ON `sales`
FOR EACH ROW
INSERT INTO `db2.dbo.inventory` (ID) VALUES (0);
it is giving me error
db1.db2.dbo.inventory doesn't exist.
how can i make this work.
thank you
As you can see here quotes (backtick) are used to identify objects names, but you must use them on every single object:
`db2`.`dbo`.`inventory`
or without them if object names are not misleading:
db2.dbo.inventory
MySQL and MariaDB do not use the .dbo. syntax. There is only database (aka schema) name and table name:
db2.inventory
or
`db2`.`inventory`
(The presence or absence of backtics does not matter in this situation.)

SQL server 2008 trigger before insert

I am very new to SQL and sorry for asking this is very primary knowledge. My issue as follow.
I have a table called Group and minimumVal and maximumVal are two columns. minimumVal always should be less than maximumVal. I want to check this before inserting a new record to the table. If this condition failed data should not inserted into the DB.
So as per my understanding I though of having a trigger but have no idea how to write this.
This is what I write so far;
CREATE TRIGGER tr_Group
ON Table_Group
for INSERT
AS
????
Please advise me.
I recommend you use check constraints for this:
ALTER TABLE dbo.Table_Group ADD CONSTRAINT CK_Group
CHECK (minimumVal < maximumVal)
If you still want to use triggers, then you need an INSTEAD OF trigger:
CREATE TRIGGER tr_Group ON Table_Group
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Table_Group
SELECT *
FROM INSERTED
WHERE minimumVal < maximumVal
END

Is it possible to declare to mysql queries?

I'm trying to create a code for a single button where it will perform either of two actions where it will add to the database if the user currently don't have the record while it will update the user's record if the user has records already. I've done it like this:
if() {
mysql_query("INSERT INTO table...");
}
else {
mysql_query("UPDATE table SET...");
}
Is it possible?
Yes, what you've written will work. If you have a way to know if there already exists a row or not without making an additional query just for this bit of code, then do exactly as you wrote.
If, however, you planned to first SELECT from the table to see if a row exists, then conditionally INSERT or UPDATE, you will perform more queries than necessary.
It would be better to either:
Have a PRIMARY KEY or other constraint on the table prevent duplicate INSERTs. Then issue an INSERT ... ON DUPLICATE KEY UPDATE query. This will attempt to INSERT the row, and if it is a duplicate, automatically perform the specified UPDATE to that row instead.
Issue the UPDATE query and check mysql_affected_rows to see if it updated an existing row. If not, then issue the INSERT query to create the new row.
Which one is more appropriate depends on your application.
you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax like:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you have properly set unique keys, you should use REPLACE so you could remove the if.
REPLACE INTO table VALUE (...);
Pay attention that this is a MySQL extension, thus not portable to other DBs.
Yes, you could try the insert then if it fails try the update.
But you could use the MYSQL sql "REPLACE" keyword, which will insert a new record if it doesn't exist or delete the existing record and insert your new one if it does.
You could also use the INSERT ... ON DUPLICATE KEY UPDATE syntax
(explained here - Link to MYSQL ref which seems to be the closest fit to your requirement.
yes it is possible
first write a query for check that record is already exist or not.
Yes it is possible , it will work

What is the best way to ensure that primary key and foreign key never intersect?

Say I have a table with a primary key a_id and foreign key b_id.
It is important that a_id and b_id never intersect, i.e. it should never be the case that there exists an a_id = b_id.
What would be the best way to implement this? Could I somehow do this on the database end (mySql), or should this be ensured programmatically? If I ensure this programmatically, is it bad practice to insert a number for a primary key (rather than have the database autoincrement it)? I assume I would just create a routine that checks the latest primary key and simply increments it (and I would also need to ensure that the range of a_id never intersects the range of b_id). Any suggestions?
You could create another table that has the auto-increment field. Upon insertion of a record, it would insert into that "key table" and use the referenced values there. So if you have two globally unique keys in one table each insert would be two key inserts. This solution would scale beyond 2 as well.
But I have to ask: Why?
In Oracle you could implement this behavior using a sequence that was used to assign id's to rows in both tables. Each table would have a trigger on INSERT where the id would be set from the next number in the sequence.
You can set the AUTO_INCREMENT value such that one table has only odd numbers and the other has only evens.
This is likely to be very performant, but it doesn't leave room for adding a 3rd table with yet another unique key.
I'm not 100% sure about MySQL, but with Oracle you can define a sequence, then simply use that same sequence to select all of your values from, which is probably the best option (if it is available with mysql).
You could use GUIDs (UUIDs) as unique keys.
I have used this on several projects and it works well for uniqueness, although it isn't the best for index performance.
The easiest way to achieve this would be to use a CHECK constraint. Unfortunately, MySQL being MySQL, it doesn't support CHECK.
In order to achieve the same effect in MySQL, you will need to create a BEFORE INSERT and a BEFORE UPDATE trigger to make sure that both values are valid. The FK itself is in charge of making sure the relationship is valid. Here is an example:
CREATE TRIGGER upd_check BEFORE UPDATE ON sometable
FOR EACH ROW
BEGIN
IF NEW.a_id = NEW.b_id THEN
call ERROR_SELFREFERENCING_ID();
END IF;
END;
More information about MySQL TRIGGERS are available in the MySQL Manual:
18.3.1: Trigger Syntax
EDIT: MySQL doesn't currently support RAISE or SIGNAL in their triggers, so I have to resort to calling a non-existent procedure ERROR_SELFREFERENCING_ID() for it to fail. This will cause the INSERT or UPDATE to fail if a_id = b_id, pretty much the same way if you would set an invalid b_id.