map column values with other table column in mysql - mysql

I have on column rais_date in one table in let test and i want to map this column with it's transaction table column let rais_trans_date. in mysql is it is possible.
Means all the value of rais_date automatically copy to rais_trans_date, either add or update in master table column i.e. rais_date.
if yes please suggest me how..

You can use MySql Triggers to do this...
CREATE TRIGGER ` BEFORE INSERT ONTableName` FOR EACH ROW BEGIN
-- Your Logic Goes here
END

Related

SQL Trigger to update OLD value in another table

I am trying to create a trigger to simultaneously update a different table than the one I have updated, with the same data.
I have two different database with the same tables and i'm trying to sync them, when i insert, update or delete data from one, i want to do automaticaly the same to the other table, with triggers.
This is the trigger code:
CREATE DEFINER=`Ivan_test`#`%` TRIGGER `Prueba_Ivan`.`mag_articulos_PI_AFTER_UPDATE` AFTER UPDATE ON `mag_articulos_PI` FOR EACH ROW
BEGIN
IF OLD.Prueba_Ivan.mag_articulos_PI NOT IN (SELECT * FROM ivan_test.mag_articulos_IT) THEN
INSERT INTO ivan_test.mag_articulos_IT
VALUES (new.xempresa_id, new.xarticulo_id, new.xcategoria_id, new.xvisible_web, new.xnovedad,new.xpromocion,new.ximagen_prelim,new.ximagen_amp,new.xtexto1,new.xtexto2,new.xtexto3,new.xtexto4,new.xtexto5);
ELSE
UPDATE ivan_test.mag_articulos_IT SET OLD.ivan_test.mag_articulos_IT = NEW.Prueba_Ivan.mag_articulos_PI;
END IF;
END
but I have this error:
Error Code: 1109. Unknown table 'OLD.Prueba_Ivan' in IN/ALL/ANY subquery
Can someone help me to find the mistake?
Thank you!!
OLD.Prueba_Ivan.mag_articulos_PI
OLD is an alias to the triggered row. Your trigger applies to Prueba_Ivan, which means that OLD and NEW are representing your Prueba_Ivan record before the change, and after it, respectively. This means that when you intend to reference mag_articulos_PI, you will need to do it via OLD.mag_articulos_PI, so remove the tablename from that expression.
OLD.ivan_test.mag_articulos_IT
As mentioned in the previous section, here OLD is an alter-ego of the updated Prueba_Ivan record, you do not need it in order to reference ivan_test.
Further explanation
An expression of the form of
a.b.c
reads as follows:
In database a, table b, column c. When you do something of the like of
OLD.t.c
it reads: In the OLD database, table t, column c.

I want to link one column from one table to same column name with another table in mysql so that i am able to carryout the add,update,delete opeations

I have two table
1: Roll_no Name Address Branch
2: Roll_no Branch Name Percentage Marks
Is it possible in database that when i will add/update/Delete the data from one table it automatically do the operation on another with out front end programming...
As #Gordan said the triggers are the best way to do this.
Follow the below steps to achieve your requirement,
Create Trigger using the below query,
CREATE TRIGGER trigger_name
AFTER INSERT
ON 'User' FOR EACH ROW
Insert into 'UserCreatedTime' values(NOW())
What it meant, After inserting the values into 'User' table insert query for 'UserCreatedTime' table will be triggered automatically.
Please check the youtube video for more details,
https://www.youtube.com/watch?v=oKDqZCkBRZ8

Insert into table with auto-increment field

I have two tables called HRData and HRDataHistory. HRDataHistory has the same structure as HRData except the first column is an autoincrement field and the last column is a DateTime field.
HRData has a trigger:
CREATE TRIGGER [HR].[HRData_History]
ON [HR].[HRData]
AFTER INSERT, UPDATE
AS
INSERT INTO HR.HRDataHistory
SELECT *, GETDATE()
FROM inserted
;
GO
This is working on an existing development machine. I am trying to mirror this relationship on my local sql server instance so that I can test some changes. Using SSMS I used 'Script Table as Create To...' and created the structure of each table and index on my local sql server instance. However when I do this for the trigger I get the following error:
An explicit value for the identity column in table 'HR.HRDataHistory' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I know the preferred method would be to specify the columns, but I want to mirror production which does not currently do that and further I want to understand why it is working in production but not on my test database.
You're getting this error because you're trying to insert data into an IDENTITY column, which auto-populates itself whenever you insert another row in that table.
Off the top of my head, you can do something like below (although I believe there are more elegant solutions and I do not guarantee that this is a safe solution, nor have I tried something like this and I recommend testing on a TEST database before trying in production/LIVE):
add another column to HRDataHistory table which does not have identity set on it (because you cannot remove identity form a colum once set), but must have the same datatype as the current ID (IDENTITY) column
use a UPDATE query to move all of your ID's from your IDENTITY column to your new column:
UPDATE HRDataHistory
SET new_column = ID
Drop the IDENTITY column (but this might have grave implications if you have any FK set on it and possibly other objects that use it):
ALTER TABLE HRDataHistory
DROP COLUMN ID
Rename the "new_column" to the name of your previous IDENTITY column:
EXEC sp_RENAME 'HRDataHistory.new_column' , 'ID', 'COLUMN'
At this point I believe you can use your trigger to "copy" the newly inserted data from the HRData table into the HRDataHistory, since the column names should match and there is no more conflict due to IDENTITY.
Again, this might (not guaranteed) work so I recommend you first check on a TEST environment.

Trigger on Update of a specific field in mySQL

I am trying to replicate the Username field in my database. Specifically I was looking to add/remove the Username across different tables, whenever the row that contains Username is added inside UserDatabase. To that end, I was thinking of using the trigger mechanism.
I am thinking along the lines of:
CREATE TRIGGER 'addUsername' AFTER INSERT ON UserDatabase FOR EACH ROW
IF (UPDATE(Username))
BEGIN
INSERT INTO anothertable (Username) VALUES ('NewUser');
END
My question is that is how do I capture the updated Username from UserDatabase and replicate it into NewUser? And also, is there a way to remove FOR EACH ROW as I only want the loop to run once?
Thanks!
From the manual:
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. OLD.col_name refers to a column of an existing row before it is updated or deleted. NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.
And no, you can not remove for each row and I think you misunderstood it a little. There actually is no loop. for each row refers to multiple rows in your insert or update statement. When you add multiple users with one insert statement, you want all of them replicated, right? Not just one.
Then you should note, that there's a difference between single-quotes and backticks. Backticks should be used, if a column or tablename or whatever includes characters that shouldn't be there, like a space, or if the name is actually a reserved keyword. Single-quotes, like you used them for the trigger name are used to tell MySQL it's a string.
Your trigger should look something like this:
CREATE TRIGGER 'addUsername' AFTER INSERT ON UserDatabase
FOR EACH ROW
INSERT INTO anothertable (Username) VALUES (NEW.Username);
For an update statement, you have to create another trigger.

How do you assign a column default in MySQL to another column's value?

I want to add a new column to a table in MySQL database that should get the value of another column in the same table. Is this possible? If so, how do you do it?
Starting with MySQL 5.0.2 you can write a stored procedure linked to a TRIGGER which can examine the new column each time a row is inserted, and automatically copy the other column's value into the new column if no value was supplied for the new column.
You Can use this following two command to do your work.
This command will be used to add new column in you table. Remember data type from where you want to copy to use those data type in new Column.
ALTER TABLE table_name ADD new_column_name VARCHAR(60);
2: This command to copy data from old column name to new column name.
UPDATE table_name SET new_column_name = old_column_name;
Then if you want to delete previous column, Then you can use following command
ALTER TABLE table_name DROP COLUMN old_column_name;
As of 20101212 mysql does not support defaulting 2 timestamp columns, which means you can't do a 'created' and 'updated' on the same table.
If this is what you were trying to do, then the trigger with the stored proc is the way to go.
create a view, and you can select the same column twice and give it different name, then the application can use the view instead use the table directly.