Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

HELP in inserting records in a table

records

  • Please log in to reply
3 replies to this topic

#1 sydneytot

sydneytot

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 September 2011 - 07:00 AM

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
  • 0

#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

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.
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 24 September 2011 - 05:32 PM

You could also have a foreign key relationship with auto-population in the child table.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 sydneytot

sydneytot

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 September 2011 - 06:35 PM

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.


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
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download