can i ask a question. how can i insert a record in two different tables at the same time. example if i insert a lastname and firstname record in table1. it will also
automatically insert that record in table2. . is there a possible way i can do that? thanks for helping
3 replies to this topic
#1
Posted 24 September 2011 - 07:00 AM
|
|
|
#2
Posted 24 September 2011 - 11:56 AM
Sure you can ask a question.
There is multiple way to do what you want.
First way, would be to do 2 separated query with a transaction.
So in this way, if you write the first one and their a bug in the second write, you can undo the modification you did in the first table
A other way would be trigger.
I'm guessing you are using mysql, here a guide for mysql trigger MySQL :: MySQL 5.0 Reference Manual :: 17.3 Using Triggers
Trigger are code that will be executed automatically when something happen (when you write a name in the first table for example)
And the last way (I could think about) is to use store procedure
Here a list of tutorial about store procedure: MySQL Stored Procedure Tutorial
Store procedure are like function on the mysql server.
So you call the store procedure with the name as parameter, and the store procedure insert it in your two different table.
Ps. Do you really need to have the name in 2 different table, maybe you should think of using a better structure with relations between your table. But that all depends on what you are trying to do, you might have a good reason to do it so.
There is multiple way to do what you want.
First way, would be to do 2 separated query with a transaction.
So in this way, if you write the first one and their a bug in the second write, you can undo the modification you did in the first table
A other way would be trigger.
I'm guessing you are using mysql, here a guide for mysql trigger MySQL :: MySQL 5.0 Reference Manual :: 17.3 Using Triggers
Trigger are code that will be executed automatically when something happen (when you write a name in the first table for example)
And the last way (I could think about) is to use store procedure
Here a list of tutorial about store procedure: MySQL Stored Procedure Tutorial
Store procedure are like function on the mysql server.
So you call the store procedure with the name as parameter, and the store procedure insert it in your two different table.
Ps. Do you really need to have the name in 2 different table, maybe you should think of using a better structure with relations between your table. But that all depends on what you are trying to do, you might have a good reason to do it so.
#3
Posted 24 September 2011 - 05:32 PM
You could also have a foreign key relationship with auto-population in the child table.
#4
Posted 24 September 2011 - 06:35 PM
Vaielab said:
Sure you can ask a question.
There is multiple way to do what you want.
First way, would be to do 2 separated query with a transaction.
So in this way, if you write the first one and their a bug in the second write, you can undo the modification you did in the first table
A other way would be trigger.
I'm guessing you are using mysql, here a guide for mysql trigger MySQL :: MySQL 5.0 Reference Manual :: 17.3 Using Triggers
Trigger are code that will be executed automatically when something happen (when you write a name in the first table for example)
And the last way (I could think about) is to use store procedure
Here a list of tutorial about store procedure: MySQL Stored Procedure Tutorial
Store procedure are like function on the mysql server.
So you call the store procedure with the name as parameter, and the store procedure insert it in your two different table.
Ps. Do you really need to have the name in 2 different table, maybe you should think of using a better structure with relations between your table. But that all depends on what you are trying to do, you might have a good reason to do it so.
There is multiple way to do what you want.
First way, would be to do 2 separated query with a transaction.
So in this way, if you write the first one and their a bug in the second write, you can undo the modification you did in the first table
A other way would be trigger.
I'm guessing you are using mysql, here a guide for mysql trigger MySQL :: MySQL 5.0 Reference Manual :: 17.3 Using Triggers
Trigger are code that will be executed automatically when something happen (when you write a name in the first table for example)
And the last way (I could think about) is to use store procedure
Here a list of tutorial about store procedure: MySQL Stored Procedure Tutorial
Store procedure are like function on the mysql server.
So you call the store procedure with the name as parameter, and the store procedure insert it in your two different table.
Ps. Do you really need to have the name in 2 different table, maybe you should think of using a better structure with relations between your table. But that all depends on what you are trying to do, you might have a good reason to do it so.
i have a table for list of alumni the fields there are firstname,lastname,middlename and year_graduated. when i register the record is send to the personal_info
table and what i am trying to do is i want to send that record also in the list of alumni table. . i have no idea with that sir on how to do it
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









