从多个表中删除行

时间:2015-10-11 12:28:35

标签: sql oracle oracle11g

我使用这些表来存储数据。 我想使用所有表中的一个SQL查询删除超过1个月的所有数据(例如)。

-- TABLE AGENT_HISTORY

CREATE TABLE AGENT_HISTORY(
  EVENT_ID INTEGER NOT NULL,
  AGENTID INTEGER NOT NULL,
  EVENT_DATE DATE NOT NULL
)
/

CREATE TABLE CPU_HISTORY(
  CPU_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  CPU_NAME VARCHAR2(50 ) NOT NULL,
  CPU_VALUE NUMBER NOT NULL
)
/

CREATE TABLE CPU_TEMP_HISTORY(
  CPU_TEMP_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  CPU_TEMP_NAME VARCHAR2(50 ) NOT NULL,
  CPU_TEMP_VALUE NUMBER NOT NULL
)
/

CREATE TABLE MEMORY_HISTORY(
  MEMORY_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  MEMORY_NAME VARCHAR2(50 ) NOT NULL,
  MEMORY_VALUE NUMBER NOT NULL
)
/

CREATE TABLE DISK_HISTORY(
  DISK_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  DISK_NAME VARCHAR2(50 ) NOT NULL,
  DISK_VALUE NUMBER NOT NULL
)
/

CREATE TABLE NETWORK_HISTORY(
  NETWORK_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  ADAPTER_NAME VARCHAR2(50 ) NOT NULL,
  TRANSMITBYTES NUMBER NOT NULL,
  TRANSMITSPEED NUMBER,
  RECEIVESPEED NUMBER,
  RECEIVEBYTES NUMBER
)
/

CREATE TABLE SWAP_HISTORY(
  SWAP_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  SWAP_NAME VARCHAR2(50 ) NOT NULL,
  SWAP_VALUE NUMBER NOT NULL
)
/

CREATE TABLE CONNECTIONS_HISTORY(
  CONNECTIONS_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  CONNECTIONS_NAME VARCHAR2(50 ) NOT NULL,
  CONNECTIONS_VALUE NUMBER NOT NULL
)
/

CREATE TABLE PARTITIONS_HISTORY(
  PARTITIONS_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER,
  PARTITIONS_NAME VARCHAR2(50 ) NOT NULL,
  PARTITIONS_VALUE NUMBER NOT NULL
)
/

这可能在Oracle中实现吗? 我在所有表中使用EVENT_ID作为唯一键ID。

1 个答案:

答案 0 :(得分:1)

根据“a_horse_with_no_name”的建议,您可以通过创建所需的关系来使用 ON DELETE CASCADE

例如,

ALTER TABLE table_name
ADD CONSTRAINT constraint_name
   FOREIGN KEY (column1, column2, ... column_n)
   REFERENCES parent_table (column1, column2, ... column_n)
   ON DELETE CASCADE;

Database SQL Language Reference添加书签,然后开始阅读这些概念,以便更好地理解。