Unix Technical Forum

UPDATE or INSERT or both

This is a discussion on UPDATE or INSERT or both within the MySQL forums, part of the Database Server Software category; --> I have a simple table BoatTripID (int) (autoincrement) (key) TripTypeID (int) TripDate (date) An html form sends me the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:30 AM
Kevin
 
Posts: n/a
Default UPDATE or INSERT or both

I have a simple table

BoatTripID (int) (autoincrement) (key)
TripTypeID (int)
TripDate (date)

An html form sends me the data that I am going to put into a query. It
will send me the TripTypeID and the TripDate. The data might already
be in the table, or it might have been changed by the form. It also
may already be in the database as a certain trip type, but the user
wants to cancel the trip and delete the record.

Is there a quick way to check for the existence of a record, make
changes if necessary, leave the record unchanged if unchanged, and
delete if necessary? I could do it with lots of queries and code, but
I was wondering if there is some simpler way of doing this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:30 AM
Willem Bogaerts
 
Posts: n/a
Default Re: UPDATE or INSERT or both

Kevin wrote:
> I have a simple table
>
> BoatTripID (int) (autoincrement) (key)
> TripTypeID (int)
> TripDate (date)
>
> An html form sends me the data that I am going to put into a query. It
> will send me the TripTypeID and the TripDate.


I think the form did (or optionally did) show the data in the table. So
you'd have to send the BoatTripID as well for existing records.

> The data might already
> be in the table, or it might have been changed by the form. It also
> may already be in the database as a certain trip type, but the user
> wants to cancel the trip and delete the record.


You wouldn't know if the data was already in the table if you did not
send unique data to check for. If, for instance, there can be only one
trip on a day, you could make the date field unique (by adding a unique
index to that field)

> Is there a quick way to check for the existence of a record, make
> changes if necessary, leave the record unchanged if unchanged, and
> delete if necessary? I could do it with lots of queries and code, but
> I was wondering if there is some simpler way of doing this.


Look in the help for the INSERT command, especially the INSERT... ON
DUPLICATE KEY UPDATE syntax. For this to work, you will need a unique
field. It will never delete something, though

Hope this helps,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:30 AM
Kevin
 
Posts: n/a
Default Re: UPDATE or INSERT or both

Thank You! Very well explained. I have it working now.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com