Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
And what you actually want to do might be a little different if you ever want to display more than the first result.
MySQL Blog
http://mysqlearner.blogspot.com/
Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Do not wait to strike till the iron is hot; but make it hot by striking....
Manoj Kumar Jain wrote:This will return you only first record always.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Do not wait to strike till the iron is hot; but make it hot by striking....
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:That's not supported by Access, MS SQL Server or MySQL. Unfortunately, there is no uniform way of doing this. Access and MS SQL Server use TOP X, whereas MySQL uses LIMIT X:
java.sql.Statement does have method setMaxRows but I don't know if this translates to a similar request to the database itself, or if the driver itself cuts of any results it doesn't want.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Martin Vajsar wrote:Most databases have some sort of mechanism to provide default identity values for columns in new records: in Oracle and few others, there are sequences, in other databases, there are identity columns which get automatically populated. Though your solution generally works, it might take more resources than needed and (more importantly) might fail (generate the same value for new record) if two users try to add a record at the same time -- at least in some databases.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:While that is true, it does have one drawback - the ID of the new record will not be available until the record is actually inserted into the database. If you need it before that time you have to resort to other techniques like this.
Two frameworks I've worked with both use the same following technique for this. They have a table with two columns, one for the table name and one for the next / last ID (that's where the two differ). A stored procedure is used to retrieve the next ID for a table, incrementing the value in the database for a next call to the stored procedure. This does lead to IDs being discarded if a record is not committed in the end, but that's better than possibly using the same ID twice.
Nick de Waal wrote:Hi,
I am trying to display the first record of a DB table in a GUI
the method I am using to retrieve it is as follows:
this populates the text-fields but with the last record of the table, I need it to populate with the first but I am unsure as to how.
Any help would be appreciated even its just a hint.
Thank you
Tim Moores wrote:Also be aware that SQL has no concept of a "first record" in a table - records are intrinsically unordered. The same SELECT query may return results in different order each time it is called.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Tim Moores wrote:Also be aware that SQL has no concept of a "first record" in a table - records are intrinsically unordered. The same SELECT query may return results in different order each time it is called.
Agreed, but I've never understood why SQL doesn't intrinsically support the first row (or n rows) of a result. It seems to me a perfectly reasonable data-directed request.
Mind you, I've never liked it. Probably why I found being a DBA so frustrating.
Winston
Wendy Gibbons wrote:I was always told it would be fine, unless the dba had to play with the database in the night, recreating tables, that was why you should never rely on column or row order.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The same SELECT query may return results in different order each time it is called.
I understand that unless there is some change the rows will be retrieved in the order they were inserted, am i correct ?I was always told it would be fine, unless the dba had to play with the database in the night, recreating tables, that was why you should never rely on column or row order.
Anand Athinarayanan wrote:Hi,
I hope I'm not hijacking the original thread by posting this question but I have a couple of questions regarding the order of records retrieved from DB.
If i insert 5 rows in a table in a particular alphabetical order (A,B,C,D,E) and after some days query it with a select * , wouldn't it return in the same order i Inserted them ?
Atleast thats what I thought it would. But this statement suggests otherwiseThe same SELECT query may return results in different order each time it is called.
From this lineI understand that unless there is some change the rows will be retrieved in the order they were inserted, am i correct ?I was always told it would be fine, unless the dba had to play with the database in the night, recreating tables, that was why you should never rely on column or row order.
The reason I'm surprised is- we have a set of questions displayed to the user and the questions are stored in the DB. In our code we retrieve the questions and just display it directly without any order by and the order of the questions are the same on the screen and DB. Its been like that for all users always.
No more Blub for me, thank you, Vicar.
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|