If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > how to insert data without overwrite old data in the same table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-11, 14:47
annaright annaright is offline
Registered User
 
Join Date: Jul 2011
Location: Kuala Lumpur,Malaysia
Posts: 9
how to insert data without overwrite old data in the same table

hi anyone here know how to insert data without overwrite the existing or current data in a table.

i have a table which is store a health information, this health information will change week by week, so i don't want to overwrite information and i want keep as a history info
Reply With Quote
  #2 (permalink)  
Old 11-12-11, 20:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by annaright View Post
hi anyone here know how to insert data without overwrite the existing or current data in a table.
use a UNIQUE key to prevent duplication
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-14-11, 16:44
annaright annaright is offline
Registered User
 
Join Date: Jul 2011
Location: Kuala Lumpur,Malaysia
Posts: 9
Quote:
Originally Posted by r937 View Post
use a UNIQUE key to prevent duplication
sorry maybe my question a bit confusing, its not INSERT but UPDATE....i want to update a table which is already have a data in the same time i don't want to overwrite the existing data. sorry my english really bad
Reply With Quote
  #4 (permalink)  
Old 11-14-11, 16:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you want to update, but don't want to overwrite existing data?

that doesn't make sense

please give a specific example of a row that already exists, and the update that you would like to make to it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-14-11, 17:14
annaright annaright is offline
Registered User
 
Join Date: Jul 2011
Location: Kuala Lumpur,Malaysia
Posts: 9
Quote:
Originally Posted by r937 View Post
you want to update, but don't want to overwrite existing data?

that doesn't make sense

please give a specific example of a row that already exists, and the update that you would like to make to it
example:

Table person:

P_Id LastName FirstName Address City
1 John Mayer Street 1 London
2 Ray Caroline Street 2 Leeds

UPDATE Persons
SET Address='Street3', City='Manchester'
WHERE LastName='Ray' AND FirstName='Caroline'

The "Persons" table will now look like this:

P_Id LastName FirstName Address City
1 John Mayer Street 1 London
2 Ray Caroline Street 3 Manchester

but I still want to keep the previous data before the update as a history
Reply With Quote
  #6 (permalink)  
Old 11-14-11, 18:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by annaright View Post
but I still want to keep the previous data before the update as a history
aha!!

okay, where would you like to keep it?

obviously not in this table, right?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 11-15-11, 01:34
annaright annaright is offline
Registered User
 
Join Date: Jul 2011
Location: Kuala Lumpur,Malaysia
Posts: 9
Quote:
Originally Posted by r937 View Post
aha!!

okay, where would you like to keep it?

obviously not in this table, right?
yup not in this table...do you have any idea, how to insert to new table in the same time update the data in the existing table, probably this is the solution.
Reply With Quote
  #8 (permalink)  
Old 11-15-11, 04:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by annaright View Post
...do you have any idea, how to insert to new table in the same time update the data in the existing table
an INSERT statement which uses the SELECT option (rather than the VALUES option), followed by an UPDATE statement
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 11-15-11, 05:52
ankur02018 ankur02018 is offline
Registered User
 
Join Date: Jun 2007
Posts: 189
Can we not use trigger before insert
Reply With Quote
  #10 (permalink)  
Old 11-15-11, 08:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by ankur02018 View Post
Can we not use trigger before insert
trigger before update, i should think
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On