开发者

How to get specific row, knowing the primary key value?

This is probably very easy question for you sql gurus, but I never used sql before.

If the table "Person" has 3 rows : Name (primary key), Age and City, I know I can get all rows like this :

开发者_JAVA技巧
SELECT * FROM Person;

But if the table looks like this :

Name   Age   City
-------------------
A       2     NY
B       4     BE
C       6     PA

What sql command do I have to use to get (for example) the 2nd row? I know the Name is B.


SELECT * FROM Person WHERE Name = 'B';

This solves this particular problem, but you can visit w3schools sql tutorial for the starting.


 SELECT * FROM Person
 WHERE Name = 'B';


Try SELECT * FROM Person WHERE Name='B' more about select syntax you can find here http://dev.mysql.com/doc/refman/5.0/en/select.html


You should avoid using primary keys that are related to any data in the entity. There is almost never a good pick. Primary keys should not change... Names can change (Both for cities and Persons) Also a SSN might "look" like a good candidate, but even they rotate over time, and you might happen to employ an illigal alien with a faked SSN Number ;)

So please allways use an integer that just counts up, or a guid.

Besides that, the answer has been posted several times already...


Rather than ask a question about each aspect of sql on here (you'll probably have quite a few), do a bit of reading first:

http://w3schools.com/sql

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜