如何从 table 中删除行而不从引用的 table 中删除行?
How to delete row from table without delete a row from referenced table?
当我尝试从 table company_catalog 中删除一行时,我不想从引用的 table store_catalog。 tablestore_catalog 中的约束 on delete no action,但服务器 return 异常:
org.hibernate.exception.ConstraintViolationException: could not execute statement
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`practick`.`store_catalog`, CONSTRAINT `store_catalog_ibfk_2` FOREIGN KEY (`idGoodsOnFirm`) REFERENCES `company_catalog` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 87 more
DAOImpl:
@Override
public void deleteGoods(CatalogCompany catalogCompany) throws SQLException {
Session session = null;
Transaction tx = null;
try {
session = this.sessionFactory.openSession();
tx = session.beginTransaction();
session.delete(catalogCompany);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
if (session != null && session.isOpen())
session.close();
}
}
您所指的删除动作是为了在子级table中级联。您不能从引用父 table 的子 table 中删除数据。考虑到您将留下孤立数据,您将违反 ACID 原则,例如数据完整性。您首先必须对 table 进行反规范化(中断规范化),以便从父 table.
中删除您想要的行
请参考这个link:https://dba.stackexchange.com/questions/44956/good-explanation-of-cascade-on-delete-update-behavio
当我尝试从 table company_catalog 中删除一行时,我不想从引用的 table store_catalog。 tablestore_catalog 中的约束 on delete no action,但服务器 return 异常:
org.hibernate.exception.ConstraintViolationException: could not execute statement
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`practick`.`store_catalog`, CONSTRAINT `store_catalog_ibfk_2` FOREIGN KEY (`idGoodsOnFirm`) REFERENCES `company_catalog` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 87 more
DAOImpl:
@Override
public void deleteGoods(CatalogCompany catalogCompany) throws SQLException {
Session session = null;
Transaction tx = null;
try {
session = this.sessionFactory.openSession();
tx = session.beginTransaction();
session.delete(catalogCompany);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
if (session != null && session.isOpen())
session.close();
}
}
您所指的删除动作是为了在子级table中级联。您不能从引用父 table 的子 table 中删除数据。考虑到您将留下孤立数据,您将违反 ACID 原则,例如数据完整性。您首先必须对 table 进行反规范化(中断规范化),以便从父 table.
中删除您想要的行请参考这个link:https://dba.stackexchange.com/questions/44956/good-explanation-of-cascade-on-delete-update-behavio