tags:

views:

28

answers:

3

Hello,

I have table like;

CREATE TABLE IF NOT EXISTS `user_t` (
  `user_id` int(11) NOT NULL auto_increment,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

And lets say there is 1000 records in it.

Now I want to make a query where the user_id = 500.

Is it a good practice to user LIMIT 1 at the end of my query, as I know that my query is going to return only 1 row?

in fact is there a need for LIMIT at all as I am doing a query on a primary key, which is unique?

A: 

Using a limit when searching on a unique value is pointless. Either one record will be returned, or no records will be returned.

Ignacio Vazquez-Abrams
A: 

It's unnecessary to use LIMIT when querying with the primary key.

OMG Ponies
+1  A: 

There is no need to use LIMIT 1 or TOP 1. Furthermore, it will likely confuse any developers that see it.

You will never get more than one row returned when querying against a single table using its primary key in the WHERE clause.

RedFilter
Ok, so I assume the same applies when I do an update, by having a WHERE by primary key?
Adnan
@Adnan: Unless the resultset risks returning more than one record, using `LIMIT` just because only communicates that whomever did it is unfamiliar with their data.
OMG Ponies
@Adnan: Yes, by query I mean `SELECT`, `UPDATE` and `DELETE`.
RedFilter