dbTalk Databases Forums  

UPDATE vs INSERT...

comp.databases.mysql comp.databases.mysql


Discuss UPDATE vs INSERT... in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SpreadTooThin
 
Posts: n/a

Default UPDATE vs INSERT... - 03-16-2011 , 10:32 AM






My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement
work as well?

Reply With Quote
  #2  
Old   
Daniel Mahoney
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-16-2011 , 11:03 AM






On Wed, 16 Mar 2011 09:32:42 -0700, SpreadTooThin wrote:

Quote:
My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement work
as well?
No, Update takes a different syntax.

UPDATE TableName SET Column1='c1', Column2='C2' WHERE key=someValue;

See http://dev.mysql.com/doc/refman/5.0/en/update.html

Reply With Quote
  #3  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-16-2011 , 11:14 AM



On Mar 16, 4:32*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:
Quote:
My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement
work as well?
No. Everything is not the same as everything else.

As it happens, MySQL supports a non-standard INSERT ... ON DUPLICATE
KEY UPDATE ... type statement "REPLACE INTO...". I say "type
statement" because its effect is not always the same as an equivalent
INSERT ... ON DUPLICATE KEY UPDATE ...

It also supports a non standard INSERT syntax using SET (so that
INSERT looks more like an UPDATE statement as opposed to vice versa).

Of course all of this is in the manual so I'm not sure why you asked
here in the first place.

Reply With Quote
  #4  
Old   
SpreadTooThin
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-16-2011 , 11:49 AM



On Mar 16, 11:14*am, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:
Quote:
On Mar 16, 4:32*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:



My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement
work as well?

No. Everything is not the same as everything else.

As it happens, MySQL supports a non-standard INSERT ... ON DUPLICATE
KEY UPDATE ... type statement "REPLACE INTO...". I say "type
statement" because its effect is not always the same as an equivalent
INSERT ... ON DUPLICATE KEY UPDATE ...

It also supports a non standard INSERT syntax using SET (so that
INSERT looks more like an UPDATE statement as opposed to vice versa).

Of course all of this is in the manual so I'm not sure why you asked
here in the first place.
If a table has two columns that must be unique, how do you know which
of the two columns is the one that is not unique during the UPDATE/
INSERT?

Reply With Quote
  #5  
Old   
onedbguru
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-16-2011 , 07:23 PM



On Mar 16, 1:49*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:
Quote:
On Mar 16, 11:14*am, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:









On Mar 16, 4:32*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:

My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement
work as well?

No. Everything is not the same as everything else.

As it happens, MySQL supports a non-standard INSERT ... ON DUPLICATE
KEY UPDATE ... type statement "REPLACE INTO...". I say "type
statement" because its effect is not always the same as an equivalent
INSERT ... ON DUPLICATE KEY UPDATE ...

It also supports a non standard INSERT syntax using SET (so that
INSERT looks more like an UPDATE statement as opposed to vice versa).

Of course all of this is in the manual so I'm not sure why you asked
here in the first place.

If a table has two columns that must be unique, how do you know which
of the two columns is the one that is not unique during the UPDATE/
INSERT?
You are asking the wrong question here... The answer is that the
COMBINATION in this column order is NOT unique. Therefore, if they
should EACH need to be unique, then you need 2 unique indexes. One
for columnA and one for columnB. If the two together must be unique
and you are finding too many cases where they are not unique, then you
may not have chosen this key wisely or may need to add a third column
for absolute uniqueness.

So, let's look at your case. Let's say the columns are
(firstname,lastname)

Data:
Joe, Blow
Jane, Blow
Joe, Blue
Jane, Blue

now you try to insert Joe, Blow again... which firstname OR which
lastname is not unique and causing your problem? You already have 2
firstname "Joe"s and 2 lastname "Blow"s -- which one caused you to
"fail"??? The answer would be "Joe, Blow" because the unique
constraint is firstname+lastname.

Take this to your test server and add a "failed_insert" table that has
no unique constraints and add any row that failed the initial insert
into this table. Then review the data to see where/why you are having
the problem. Only you know your data - or at least you should know
your data...

Reply With Quote
  #6  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-17-2011 , 04:02 AM



On Mar 16, 5:49*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:
Quote:
On Mar 16, 11:14*am, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:









On Mar 16, 4:32*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:

My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

UPDATE TableName Column1Name, Column2Name VALUES ('c1', 'c2') WHERE
something=something;

It appears that the update has a different structure or I've messed up
somewhere else...

UPDATE TableName Column1Name=`c1`, Column2Name=`c2` WHERE
something=something;
Is what the tutorial says, but shouldn't my first UPDATE statement
work as well?

No. Everything is not the same as everything else.

As it happens, MySQL supports a non-standard INSERT ... ON DUPLICATE
KEY UPDATE ... type statement "REPLACE INTO...". I say "type
statement" because its effect is not always the same as an equivalent
INSERT ... ON DUPLICATE KEY UPDATE ...

It also supports a non standard INSERT syntax using SET (so that
INSERT looks more like an UPDATE statement as opposed to vice versa).

Of course all of this is in the manual so I'm not sure why you asked
here in the first place.

If a table has two columns that must be unique, how do you know which
of the two columns is the one that is not unique during the UPDATE/
INSERT?
You don't need to know. The concept is that if any of your unique keys
match then you will wish to perform an update (or a replace).

Reply With Quote
  #7  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-17-2011 , 04:19 AM



SpreadTooThin <bjobrien62 (AT) gmail (DOT) com> wrote:
Quote:
If a table has two columns that must be unique, how do you know which
of the two columns is the one that is not unique during the UPDATE/
INSERT?
What do you mean?

1. That the combination of the columns must be unique?
2. Or that each column must be unique?

In any case your INSERT or UPDATE will fail with a "duplicate entry for
key ..." error. And in the second case the message details will tell
you which key (= UNIQUE constraint) was violated.

For the first case your question makes no sense. Changing the value for
either column will resolve the duplicate problem.


XL

Reply With Quote
  #8  
Old   
John Nagle
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-23-2011 , 01:27 AM



On 3/16/2011 9:32 AM, SpreadTooThin wrote:
Quote:
My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?
No, but the REPLACE statement does. Check that out.

John Nagle

Reply With Quote
  #9  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: UPDATE vs INSERT... - 03-23-2011 , 04:59 AM



On Mar 23, 7:27*am, John Nagle <na... (AT) animats (DOT) com> wrote:
Quote:
On 3/16/2011 9:32 AM, SpreadTooThin wrote:

My Insert statement looks like

INSERT INTO tableName Column1Name, Column2Name VALUES ('c1', 'c2');

Shouldn't the UPDATE statement look very similar?

* * No, but the REPLACE statement does. *Check that out.

* * * * * * * * * * * * * * * * * * * * John Nagle
Yes but as I mentioned in my post a week ago, the effects are
different (between REPLACE and UPDATE/INSERT...UPDATE).

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.