SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 03/08/2015, 09:42 PM   #1
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Difference between mysql staments

I wanna know the difference between:
  • Normal insert statment
  • Insert on duplicate key statment
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   Reply With Quote
Old 03/08/2015, 09:55 PM   #2
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Difference between mysql staments

You should first understand the definition of a key.

A key is used to identify a row in the table and/or aid with performance when sorting or grouping. The most common type of key is the primary key and every table should have one. The primary key uniquely identifies each row in the table. If you try to insert a new row with a value for the primary key that already exists you will get an appropriately named "duplicate key" error. The "on duplicate key" clause catches that error and instead opts to update some values in the row that already exists.
__________________
Vince is offline   Reply With Quote
Old 03/08/2015, 09:58 PM   #3
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Re: Difference between mysql staments

So instead from inserting new row it updates the one which have the same data?
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   Reply With Quote
Old 03/08/2015, 10:04 PM   #4
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,324
Reputation: 652
Default Re: Difference between mysql staments

Quote:
Originally Posted by nezo2001 View Post
So instead from inserting new row it updates the one which have the same data?
No it won't do anything if a key clashes, it returns an error. If you want to update use "UPDATE".
Crayder is offline   Reply With Quote
Old 03/08/2015, 10:08 PM   #5
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Re: Difference between mysql staments

Quote:
Originally Posted by Crayder View Post
No it won't do anything if a key clashes, it returns an error. If you want to update use "UPDATE".
So what is the difference if both give errors?
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   Reply With Quote
Old 03/08/2015, 10:11 PM   #6
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Re: Difference between mysql staments

....
Quote:
Originally Posted by Vince
Code:
INSERT INTO ... ON DUPLICATE KEY UPDATE ...
This query will try to insert the data as normal. If this fails because the data already exists (duplicate key) it will instead perform an update.
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   Reply With Quote
Old 03/08/2015, 10:11 PM   #7
Ahmad45123
Gangsta
 
Ahmad45123's Avatar
 
Join Date: Oct 2013
Location: Egypt
Posts: 826
Reputation: 187
Default Re: Difference between mysql staments

Use update statements if you wish to update
__________________

ExtremeStudio

The Best SAMP IDE.
Ahmad45123 is offline   Reply With Quote
Old 03/08/2015, 10:16 PM   #8
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Re: Difference between mysql staments

I'm asking for the differnece between
Insert into
Insert into duplicate key
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   Reply With Quote
Old 03/08/2015, 10:33 PM   #9
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Difference between mysql staments

Quote:
Originally Posted by nezo2001 View Post
So instead from inserting new row it updates the one which have the same data?
Yes. You can even check this afterwards: cache_affected_rows will return 1 if a new row was inserted or 2 if an existing row was updated.
__________________
Vince is offline   Reply With Quote
Old 03/08/2015, 10:38 PM   #10
nezo2001
Gangsta
 
Join Date: Nov 2014
Location: Cairo, Egypt
Posts: 680
Reputation: 26
Default Re: Difference between mysql staments

Thank you vince
__________________
Quote:
Originally Posted by Human
When im met u, u were nub at scripting xD.

But now, i'm impressed. Good job.
Don't look at my history. Everyone started somewhere
nezo2001 is offline   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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql plugins difference Banditukas Scripting Help 1 18/08/2014 06:40 PM
What's difference between these ? AnonScripter Scripting Help 3 14/01/2014 08:50 PM
SQLite and MYSQL difference. Ballu Miaa Scripting Help 0 08/11/2012 06:34 PM
Difference between || and && zack3021 Help Archive 1 04/11/2010 12:42 AM
Difference between .INC and .PWN and .AMX? DeltaAirlines12 Help Archive 2 22/12/2009 04:10 AM


All times are GMT. The time now is 06:23 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.