Forum Moderators: coopster

Message Too Old, No Replies

inserting new record to top of table?

         

willg825

6:31 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



Hi everyone - not sure if this is possible, but I want to insert a new record into a mysql table, but I want the record to be at the top of the table (i.e, the first record). I understand all the problems associated with changing primary keys, but this table doesnt even have one - basically, the table exists to outline the various input fields for a form. So the order of the records dictates which order the <input> boxes come up in the browser. I need to add an input box to the top of the webpage, but I cant figure out how to add a new record besides at the end.... Any ideas? Thanks in advance.

if i cant place a new record in the middle of other records, can anyone suggest an alternative way of defining a form's contents outside of the actual php file (i.e, using a table to describe a form application)...

will

baertyp

7:16 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



Willg825,

no way to insert rows at a certain position in mySQL afaik. You can define a primary key with the auto_increment feature and order your query by this column descending to get the table "upside down". You might also take a closer look at the "tabindex" attribute in the various <FORM> input elements, which define the order the inputs get the focus when "tabbing" through the form.

Hope this helps a bit.

Regards
Markus

willg825

8:26 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



Thats too bad. I think what I'll do is create the table in excel, so I can cut and paste records and mess around with the order all I choose...and then import it to mysql. I'm not concerned with primary keys and the efficacy of messing around with records, since the table will be static once it is created...

thanks for your help

will

Salsa

8:37 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



I think your idea is best, Baertyp: Add id field to the table, make the record you want to be at the top, id=1, make the id field an auto_increment index, then when making a query, ORDER BY id. For all practical purposes, that record would be at the top.

Salsa

8:43 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



If it's going to be static, why not just put it in a flat file?

willg825

8:56 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



ive toyed with putting it as a flat file, and also having them defined in some sort of php array. but honestly its easier to just have it in mysql since the process of retrieving the data is much easier to me than from a flat file
thanks for everyones help

baertyp

5:59 am on Nov 15, 2004 (gmt 0)

10+ Year Member



Willg825,


I'm not concerned with primary keys and the efficacy of messing around with records, since the table will be static once it is created...

Well, (relational) databases are mainly all about primary keys, relationships of keys, indices and "messing" around with rows (records). You have no influence whatsoever on the internal storing order of the rows. The database enginge stores the rows in the order which is best practice for storing and retrieving purposes. So when you insert your rows in a certain order, you cannot rely on them being stored in that particular order for all times. SQL gives you total power over the order the rows are presented to you by keys and through the ORDER BY statement, which you indeed should make use of.

If you want your piece of data to remain static for all times, use a file. Filesystems are designed to keep your data in exactly the state you've put it in. MySQL is designed to act exactly as told to using SQL statements. Both work well and are fairly easy to handle.

Regards
Markus