473,788 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to insert data in the first column of a table in MySQL

vijcbe
16 New Member
Hello friends!!

I have a question..

Let me have a table which consists the details of 5 employees, starting from employee id 1 to 5 as given below.

emp_id fname ... .... address
1 john ... ... ...
2
.
.
.
5 Shiva ............... .

What should I do if I want to include a data in the position of employee john so that the series of data pushed to one column downwards and so on. This should happen in every occasion when I insert a new employee detail. I am using MYSQL database.

Please someone help me doing it.
Thanks a million in advance.

VJ
Nov 15 '07 #1
5 2542
jx2
228 New Member
Hello friends!!

emp_id fname ... .... address
1 john ... ... ...
2
.
.
.
5 Shiva ............... .
i'm not qute sure i understand you
you want new_Emploee_ID= 1 ? if that is not a case the order in table is not a problem you can use
Expand|Select|Wrap|Line Numbers
  1. select * from myTable order by DESC 
  2.  
that woud give you last employee as first
but i am not sure if that is what you wanted

regards
jx2
Nov 15 '07 #2
Atli
5,058 Recognized Expert Expert
Hi VJ.

Do you mean that you want the latest row inserted into the table to be at ID number 1, adding one to the ID of all the old rows?

If so, that is a very very bad idea. It would pretty much suck the life out of your server every time you added a row.
Imagine having a million rows. Every time you added a row, each of those million rows would have to be updated!

A far more sensible way is to do what we all do. Give the new row the lowest unoccupied ID.
Nov 16 '07 #3
vijcbe
16 New Member
heya Atli,
I dont want to change the ID number of an employee each an everytime. When we insert a new data, it gets inserted to the bottom of the table. I want it to get inserted at the top of the table. So, whatever the new data I insert, that should be the in the first position of the table. Is it possible??

Thanks,
VJ.
Nov 16 '07 #4
Atli
5,058 Recognized Expert Expert
The top row of the table will always be the first row you insert and the last row the latest row you insert, unless you do something like what I suggested in my previous post.

You can, however, reverse the order in which MySQL displays the rows, effectively putting the latest row at the top. Like what jx2 suggested in his post, using the ORDER BY clause.

So, for example, if you have these rows:
Expand|Select|Wrap|Line Numbers
  1. ID  NAME
  2. 1   John
  3. 2   Jane
  4. 3   Shane
  5.  
An "ORDER BY `ID` DESC" clause will display them like this:
Expand|Select|Wrap|Line Numbers
  1. ID  NAME
  2. 3   Shane
  3. 2   Jane
  4. 1   John
  5.  
Nov 16 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, VJ.

You can force MySQL to re-order the default ordering of the table by using ALTER TABLE:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `tableName` ORDER BY `ID` DESC;
  2.  
I wouldn't recommend using this code in production, as it will get *slow* for large datasets, but it does do what I think is what you're asking for.

If you're looking for a faster, more efficient solution, though, go with Atli's suggestion.

Be sure to index the ID column, as then MySQL won't have to manually sort the result set.
Nov 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
34338
by: Frank Natoli | last post by:
Having created a table with a longblob column, what is the insert syntax to set the value of the longblob column? The data will be read from a file.
5
4901
by: me | last post by:
I'm also having problems getting the bulk insert to work. I don't know anything about it except what I've gleened from BOL but I'm not seeming to get anywhere...Hopefully there is some little (or big) problem with my code that someone can point out that may save me some time. TIA CBL
20
8581
by: akej via SQLMonster.com | last post by:
Hi, i have table with 15 columns CREATE TABLE . ( PRIMARY KEY , NULL , NULL , NULL , NULL , (50) NULL , NULL
6
12358
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of 10 columns into a table with 12. How can I specify which columns to insert to? I think format files are what I'm supposed to use, but I can't figure them out. I've also tried using a view, as was suggested on one of the many websites I've...
10
52801
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable string or NULL. Becaus i have a if statement: if .... cursor.execute(".................insert NULL ..............") if ....
16
17020
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
16
3877
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the table, or perhaps bytes, being updated where the engine just decides, screw it, i'll just make a new one. surfed this group and google, but couldn't find anything. the context: we have some java folk who like to parametize/
2
3211
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request : INSERT INTO temp_tab VALUES (1,2,3)
15
5268
by: abracad_1999 | last post by:
I am trying to populate a table with the following insert query run through phpmyadmin. When I attempt to run it phpmyadmin just freezes. After a while "Fatal error: Maximum execution time of 300 seconds exceeded in /web/myadmin/283/libraries/import.lib.php on line 19" appears but no indication as why the command fails. Any advice most welcome. INSERT INTO `countries` ( `id` , `country` ) VALUES ('1', 'Abkhazia'), ('2',...
6
3471
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns, there's another column in the DB table named 'RegDateTime' whose datatype is Date/Time which is...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.