473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with create table syntax

157 100+
Hi all

My frontend is Java and backend is MySql.

The tables that I create have auto incrementing primary keys.

This is the syntax of my create statement :-

Expand|Select|Wrap|Line Numbers
  1. Create table ABC(ID Int not null auto_increment primary key,SubjectID Int not null,Level Varchar(20) not null,Duration Int not null,index(SubjectID),foreign key(SubjectID) references Subjects(SubjectID));
My problem is whenever I delete a record the next record that is inserted does have the correct autoincremented ID but it gets inserted in place of the previously deleted record.

For eg I have 10 records in a table each having ID as 1,2,3..so on in a sequential order and now I delete record no 3 and then insert a new record(Id: 11) then its inserted in place of 3..so it now shows 1,2,11,4,5,...10

How can I avoid this?
Thanks in advance.
Feb 12 '07 #1
5 2023
ronverdonk
4,258 Expert 4TB
You cannot avoid this. MySQL nowhere gives any guarantee where its db, tables or data are physically placed. If you want rows to be retrieved/displayed in a particular order, you user the ORDER BY attribute.

Ronald :cool:
Feb 12 '07 #2
abctech
157 100+
You cannot avoid this. MySQL nowhere gives any guarantee where its db, tables or data are physically placed. If you want rows to be retrieved/displayed in a particular order, you user the ORDER BY attribute.

Ronald :cool:
Thank you very much, I used the 'group by' clause and now the records appear in a sequential order in the front-end table.

Ther's another related question if I may,
from the front-end the user is given the facility to delete the records by clicking a 'Delete' button,but even if all the records are deleted and the table becomes empty the next record that is added is alloted the the last inserted row's ID + 1.
That is if the last inserted record had ID: 17 and even after deleting all records from the table I need that the next record added has ID starting again from 1 and not 18. Is this possible?
Feb 13 '07 #3
ronverdonk
4,258 Expert 4TB
Not automatically. Let me explain how it works, example:

You have a table with auto_increment column 'id' and values:
1
2
3
After deleting row with id 2, table will be:
1
3
After inserting a new row, the table will be:
1
3
4
You delete all rows from the table and insert a new one, the table will be:
5

If you want to resequence an auto_increment column, you have to do that by dropping the column and adding it again. Like the following way, assuming that your auto_increment column is named 'id':
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE tablename DROP id;
  2. ALTER TABLE tablename
  3.   ADD id INT UNSIGED NOT NULL AUTO_INCREMENT FIRST,
  4.   ADD PRIMARY KEY (id);
Ronald :cool:
Feb 13 '07 #4
abctech
157 100+
Not automatically. Let me explain how it works, example:

You have a table with auto_increment column 'id' and values:
1
2
3
After deleting row with id 2, table will be:
1
3
After inserting a new row, the table will be:
1
3
4
You delete all rows from the table and insert a new one, the table will be:
5

If you want to resequence an auto_increment column, you have to do that by dropping the column and adding it again. Like the following way, assuming that your auto_increment column is named 'id':
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE tablename DROP id;
  2. ALTER TABLE tablename
  3.   ADD id INT UNSIGED NOT NULL AUTO_INCREMENT FIRST,
  4.   ADD PRIMARY KEY (id);
Ronald :cool:
Thanks a lot once again.I really appreciate your help !

I incorporated the above code in my Java program where I first check the max(ID) for my table, if it comes out to be < 1 then I fire the above queries and thus all the newly added records are numbered from 1 again.

My doubt is resolved now!

P.S : By mistake in my last reply I said I used 'group by' instead of 'order by', but its actually 'order by'.
Feb 13 '07 #5
ronverdonk
4,258 Expert 4TB
You are welcome.

Ronald :cool:
Feb 13 '07 #6

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
0
by: Didier ROS | last post by:
Hi, I am a newbie I want to create a temporary table and I get the following error message : mysql> CREATE TEMPORARY TABLE tempemp AS SELECT * FROM emp; ERROR 1044: Access denied for user:...
2
by: David Heller | last post by:
I have this schema I exported from a database running mysql version 4.01 I'm trying to create this table on a server running mysql 3.23 I keep getting a syntax error near line 11 (I added the line...
7
by: Aleem | last post by:
I need help in writing a stored procedure on SQL Server 2000. Basically the stored procedure's primary task is to generate invoice records and insert the records in a invoice table. In order to...
14
by: bolidev | last post by:
I'm new to SQL and can't figure out how to update my table (StoreItemStatus) that contains the current status for items in each store (STORE_KEY, ITEM_KEY, STATUS,...). I get updated status info...
0
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
4
by: Baoqiu Cui | last post by:
Hi, I was playing with MySQL (4.1.9) during the weekend, but noticed a minor problem and would like someone to explain this to me. Basically I could successfully create a table with one unique...
3
by: J West | last post by:
Warning: Error while executing this query:CREATE TABLE "purchaseorder" ( "PurchaseOrderID" int(10) unsigned NOT NULL auto_increment, "PurchaseCost" double unsigned zerofill NOT NULL default...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.