简体   繁体   English

如何从ID列递增1的derby表中删除行?

[英]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,则有以下几种可能性:

  1. make a piece of code to check if there is a free ID in the sequence and use it in INSERT 编写一段代码,检查序列中是否有空闲的ID,并在INSERT中使用它
  2. make a piece of code to save free IDs somewhere and reuse them when you insert. 编写一段代码将免费ID保存在某处,并在插入时重复使用它们。

In other terms: you have to manage IDs directly. 换句话说:您必须直接管理ID。 The way the DB manages them is: 数据库管理它们的方式是:

  1. Deleting a record produces a hole that will not be filled automatically 删除记录会产生一个不会自动填充的孔
  2. Adding a new record increment the last id 添加新记录将最后一个ID递增

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用一个 sql 查询从多个表中删除一行,其中 email id 相同 - How to Delete a row from more than one table with one sql query where email id is same 如何从表列javafx中删除行 - How to delete row from table column javafx 如何在 Java derby 中自动增加数据库列? - How to auto increment database column in Java derby? 如何在具有CachedRowSet增量ID的表中插入行? - How to insert a row into a that table has an increment id with CachedRowSet? 从表中删除记录如何使用Hibernate(HQL)为表中的已删除行重新生成ID - Delete records from table how regenerate id for deleted row in the table using Hibernate(HQL) Apache Derby ID列应自动一一递增-但事实并非如此,为什么? - Apache Derby ID column should auto increment one by one - but it does not, why? 创建表用户(“ user_id int auto_increment”); 在derby中不起作用(嵌入式数据库) - create table user(“user_id int auto_increment”); not working in derby (embedded database) Java Derby Database从表中删除所有行 - Java Derby Database delete all rows from a table 通过传递表名和列名从Oracle表中删除行 - Delete Row from Oracle table by passing table name and column name 使用ID从sqlite数据库表中删除一行 - Delete a row from sqlite database table using id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM