我正在Java中使用PreparedStatement。我有一个来自多个表的sql查询。如果有可能,如何使该查询更短和更优化? DB是“ Oracle 12c数据库”。另外,我的任务是使查询sql-injection安全并防止注入。
preparedStatement = connection.prepareStatement("delete from " +
"TBL_ORG_DATA, " +
"TBL_ORG_CONTACTS, " +
"TBL_HEALTH_SERVICE, " +
"TBL_EDUCATION, " +
"TBL_LENGTH_WORK, " +
"TBL_CONTRACTS, " +
"TBL_EVENTS, " +
"TBL_TRADE_UNIONS, " +
"TBL_COMMITTEES, " +
"TBL_HEALTH_CHECK, " +
"TBL_ACCIDENTS, " +
"TBL_VICTIMS, " +
"TBL_ACCIDENTS_DAMAGE, " +
"TBL_ATTESTATION, " +
"TBL_ATTESTATION_FIVE_YEAR, " +
"TBL_TRANING, " +
"TBL_TRAINING_MANAGER, " +
"TBL_TRANING_THREE_YEAR, " +
"TBL_MANAGEMENT " +
"where TBL_ORG_DATA.ORG_ID = ? AND\n" +
"TBL_ORG_CONTACTS.ORG_ID = ? AND\n" +
"TBL_HEALTH_SERVICE.ORG_ID = ? AND\n" +
"TBL_EDUCATION.ORG_ID = ? AND\n" +
"TBL_LENGTH_WORK.ORG_ID = ? AND\n" +
"TBL_CONTRACTS.ORG_ID = ? AND\n" +
"TBL_EVENTS.ORG_ID = ? AND\n" +
"TBL_TRADE_UNIONS.ORG_ID = ? AND\n" +
"TBL_COMMITTEES.ORG_ID = ? AND\n" +
"TBL_HEALTH_CHECK.ORG_ID = ? AND\n" +
"TBL_ACCIDENTS.ORG_ID = ? AND\n" +
"TBL_VICTIMS.ORG_ID = ? AND\n" +
"TBL_ACCIDENTS_DAMAGE.ORG_ID = ? AND\n" +
"TBL_ATTESTATION.ORG_ID = ? AND\n" +
"TBL_ATTESTATION_FIVE_YEAR.ORG_ID = ? AND\n" +
"TBL_TRANING.ORG_ID = ? AND\n" +
"TBL_TRAINING_MANAGER.ORG_ID = ? AND\n" +
"TBL_TRANING_THREE_YEAR.ORG_ID = ? AND\n" +
"TBL_MANAGEMENT.ORG_ID = ? AND\n" +
"TBL_ORG_DATA.DATE_PERIOD = ? AND\n" +
"TBL_ORG_CONTACTS.DATE_PERIOD = ? AND\n" +
"TBL_HEALTH_SERVICE.DATE_PERIOD= ? AND\n" +
"TBL_EDUCATION.DATE_PERIOD= ? AND\n" +
"TBL_LENGTH_WORK.DATE_PERIOD = ? AND\n" +
"TBL_CONTRACTS.DATE_PERIOD = ? AND\n" +
"TBL_EVENTS.DATE_PERIOD = ? AND\n" +
"TBL_TRADE_UNIONS.DATE_PERIOD = ? AND\n" +
"TBL_COMMITTEES.DATE_PERIOD = ? AND\n" +
"TBL_HEALTH_CHECK.DATE_PERIOD = ? AND\n" +
"TBL_ACCIDENTS.DATE_PERIOD = ? AND\n" +
"TBL_VICTIMS.DATE_PERIOD = ? AND\n" +
"TBL_ACCIDENTS_DAMAGE.DATE_PERIOD = ? AND\n" +
"TBL_ATTESTATION.DATE_PERIOD = ? AND\n" +
"TBL_ATTESTATION_FIVE_YEAR.DATE_PERIOD = ? AND\n" +
"TBL_TRANING.DATE_PERIOD = ? AND\n" +
"TBL_TRAINING_MANAGER.DATE_PERIOD = ? AND\n" +
"TBL_TRANING_THREE_YEAR.DATE_PERIOD = ? AND\n" +
"TBL_MANAGEMENT.DATE_PERIOD = ?");
答案 0 :(得分:0)
据我在Oracle中所知,您必须为每个表创建单独的delete
命令。
delete from a where org_id = 101 and date_period = date '2018-05-25';
delete from b where org_id = 101 and date_period = date '2018-05-25';
如果可以修改表,则可以使用约束on delete cascade
。我不知道您的表格的组织方式,但是如果您可以在其中一个表格中定义唯一的约束,那么其他人可能会依赖它,例如:
create table a (
id number primary key, org_id number, date_period date, cola varchar2(5),
constraint uq_org_id_period unique (org_id, date_period) );
insert into a values (1, 101, date '2018-05-25', 'DEL');
insert into a values (2, 101, date '2018-05-26', 'PQR');
insert into a values (3, 102, date '2018-06-17', 'CBA');
create table b (
id number primary key, org_id number, date_period date, colb varchar2(5),
constraint fk_org_id_period
foreign key (org_id, date_period)
references a(org_id, date_period) on delete cascade);
insert into b values (1, 101, date '2018-05-26', 'AAXXX');
insert into b values (2, 102, date '2018-06-17', 'PCCQR');
insert into b values (3, 101, date '2018-05-25', 'DEL1');
insert into b values (4, 101, date '2018-05-25', 'DEL2');
insert into b values (5, 102, date '2018-06-17', 'XYZYX');
现在,如果您运行:
delete from a where (org_id, date_period) in ((101, date '2018-05-25'));
不仅将删除表a
中的第1行,而且还将删除b
中的第3和4行。您也可以使用触发器,但是约束在这里更合适。