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 :- - 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.
5 2023
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:
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?
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': -
ALTER TABLE tablename DROP id;
-
ALTER TABLE tablename
-
ADD id INT UNSIGED NOT NULL AUTO_INCREMENT FIRST,
-
ADD PRIMARY KEY (id);
Ronald :cool:
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': -
ALTER TABLE tablename DROP id;
-
ALTER TABLE tablename
-
ADD id INT UNSIGED NOT NULL AUTO_INCREMENT FIRST,
-
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'.
You are welcome.
Ronald :cool:
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |