[英]how to delete a row from table in derby where ID column increment by 1?
i have problem with my table and exactly in ID column, when i insert record>>the ID will increment by 1 automatically, but the problem that when i delete old record and insert new record the increment continue counting and the ID of new record didn't take the ID of old record by sequence ; 我的表和ID列中的表存在问题,当我插入记录>>时ID会自动增加1,但是当我删除旧记录并插入新记录时,增量会继续计数,而新记录的ID却没有增加不要按顺序获取旧记录的ID ;
my delete statment : String Q="DELETE FROM EMPLOYEE ( ID , NAME) WHERE NAME='"+lst1.getSelectedValue()+"'"; stmt.execute(Q);
我的删除语句:
String Q="DELETE FROM EMPLOYEE ( ID , NAME) WHERE NAME='"+lst1.getSelectedValue()+"'"; stmt.execute(Q);
String Q="DELETE FROM EMPLOYEE ( ID , NAME) WHERE NAME='"+lst1.getSelectedValue()+"'"; stmt.execute(Q);
create statment: stmt.execute("CREATE TABLE EMPLOYEE(ID INTEGER NOT NULL PRIMARY KEY" + " GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1)" + ",NAME varchar(50),BIRTHDAY varchar(50),BIRTHMONTH varchar(50)" + ",BIRTHYEAR varchar(50),SEX varchar(50),DEPARTMENT varchar(50))");}
创建语句:
stmt.execute("CREATE TABLE EMPLOYEE(ID INTEGER NOT NULL PRIMARY KEY" + " GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1)" + ",NAME varchar(50),BIRTHDAY varchar(50),BIRTHMONTH varchar(50)" + ",BIRTHYEAR varchar(50),SEX varchar(50),DEPARTMENT varchar(50))");}
insert statment: String Query="INSERT INTO EMPLOYEE(NAME , BIRTHDAY , BIRTHMONTH , " + "BIRTHYEAR , SEX , DEPARTMENT) VALUES('"+txname.getText()+"','" + ""+compd.getSelectedItem()+"','"+compm.getSelectedItem()+"','" + ""+compy.getSelectedItem()+"','"+composx.getSelectedItem()+"','" + ""+txdep.getText()+"')"; stmt.execute(Query);
插入语句:
String Query="INSERT INTO EMPLOYEE(NAME , BIRTHDAY , BIRTHMONTH , " + "BIRTHYEAR , SEX , DEPARTMENT) VALUES('"+txname.getText()+"','" + ""+compd.getSelectedItem()+"','"+compm.getSelectedItem()+"','" + ""+compy.getSelectedItem()+"','"+composx.getSelectedItem()+"','" + ""+txdep.getText()+"')"; stmt.execute(Query);
String Query="INSERT INTO EMPLOYEE(NAME , BIRTHDAY , BIRTHMONTH , " + "BIRTHYEAR , SEX , DEPARTMENT) VALUES('"+txname.getText()+"','" + ""+compd.getSelectedItem()+"','"+compm.getSelectedItem()+"','" + ""+compy.getSelectedItem()+"','"+composx.getSelectedItem()+"','" + ""+txdep.getText()+"')"; stmt.execute(Query);
thanks very good firstly for answer and try help 首先非常感谢您的回答并尝试帮助
You can solve that on the application side adopting the strategy of not really deleting the row, but adding a boolean field 'deleted', 0 if not deleted or 1 if deleted. 您可以在应用程序端采用以下策略解决此问题:不真正删除行,而是添加一个布尔字段“已删除”,如果未删除则为0,如果删除则为1。
This in the case you want to avoid holes in the ID sequence. 在这种情况下,您要避免ID序列出现漏洞。
Otherwise you live with the fact that there will be holes in the ID sequence. 否则,您将忍受ID序列中会有漏洞的事实。 What is the problem with that?
这是什么问题?
By the way, what is the DB you are working with? 顺便说一下,您正在使用的数据库是什么?
I read you comment. 我读过你的评论。 The point is that all the DBs append new records at the end of the table.
关键是所有数据库都在表的末尾附加新记录。 So if you want to reuse IDs you have these possibilities:
因此,如果您想重复使用ID,则有以下几种可能性:
In other terms: you have to manage IDs directly. 换句话说:您必须直接管理ID。 The way the DB manages them is:
数据库管理它们的方式是:
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.