从Oracle表中删除数据

时间:2016-02-14 20:09:14

标签: sql oracle oracle11g

我想删除用于存储数据的Oracle表中的数据。

  -- TABLE AGENT_HISTORY

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

-- CREATE INDEXES FOR TABLE AGENT_HISTORY

CREATE INDEX IX_RELATIONSHIP1 ON AGENT_HISTORY (AGENT_ID)
/

-- ADD KEYS FOR TABLE AGENT_HISTORY

ALTER TABLE AGENT_HISTORY ADD CONSTRAINT KEY8 PRIMARY KEY (EVENT_ID)
/

-- TABLE CPU_HISTORY

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

-- CREATE INDEXES FOR TABLE CPU_HISTORY

CREATE INDEX IX_RELATIONSHIP5 ON CPU_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE CPU_HISTORY

ALTER TABLE CPU_HISTORY ADD CONSTRAINT KEY9 PRIMARY KEY (CPU_HISTORY_ID)
/

-- TABLE CPU_TEMP_HISTORY

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 INDEXES FOR TABLE CPU_TEMP_HISTORY

CREATE INDEX IX_RELATIONSHIP7 ON CPU_TEMP_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE CPU_TEMP_HISTORY

ALTER TABLE CPU_TEMP_HISTORY ADD CONSTRAINT KEY10 PRIMARY KEY (CPU_TEMP_HISTORY_ID)
/

-- TABLE MEMORY_HISTORY

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 INDEXES FOR TABLE MEMORY_HISTORY

CREATE INDEX IX_RELATIONSHIP9 ON MEMORY_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE MEMORY_HISTORY

ALTER TABLE MEMORY_HISTORY ADD CONSTRAINT KEY11 PRIMARY KEY (MEMORY_HISTORY_ID)
/

-- TABLE DISK_HISTORY

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

-- CREATE INDEXES FOR TABLE DISK_HISTORY

CREATE INDEX IX_RELATIONSHIP10 ON DISK_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE DISK_HISTORY

ALTER TABLE DISK_HISTORY ADD CONSTRAINT KEY12 PRIMARY KEY (DISK_HISTORY_ID)
/

-- TABLE NETWORK_HISTORY

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 INDEXES FOR TABLE NETWORK_HISTORY

CREATE INDEX IX_RELATIONSHIP11 ON NETWORK_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE NETWORK_HISTORY

ALTER TABLE NETWORK_HISTORY ADD CONSTRAINT KEY13 PRIMARY KEY (NETWORK_HISTORY_ID)
/

-- TABLE SWAP_HISTORY

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 INDEXES FOR TABLE SWAP_HISTORY

CREATE INDEX IX_RELATIONSHIP13 ON SWAP_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE SWAP_HISTORY

ALTER TABLE SWAP_HISTORY ADD CONSTRAINT KEY14 PRIMARY KEY (SWAP_HISTORY_ID)
/

-- TABLE CONNECTIONS_HISTORY

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 INDEXES FOR TABLE CONNECTIONS_HISTORY

CREATE INDEX IX_RELATIONSHIP14 ON CONNECTIONS_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE CONNECTIONS_HISTORY

ALTER TABLE CONNECTIONS_HISTORY ADD CONSTRAINT KEY15 PRIMARY KEY (CONNECTIONS_HISTORY_ID)
/

-- TABLE PARTITIONS_HISTORY

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

-- CREATE INDEXES FOR TABLE PARTITIONS_HISTORY

CREATE INDEX IX_RELATIONSHIP15 ON PARTITIONS_HISTORY (EVENT_ID)
/

-- ADD KEYS FOR TABLE PARTITIONS_HISTORY

ALTER TABLE PARTITIONS_HISTORY ADD CONSTRAINT KEY16 PRIMARY KEY (PARTITIONS_HISTORY_ID)
/

如何使用一个SQL查询删除3个月以上Oracle表中的所有数据?

我可以只使用一个SQL查询吗?也许有级联删除?

1 个答案:

答案 0 :(得分:0)

实际上,您应该添加一个外键约束,删除相应的父记录时将删除子记录:

ALTER TABLE CPU_HISTORY 
ADD CONSTRAINT FK_CPU_HIST_EVENT_ID
FOREIGN KEY (EVENT_ID)
     REFERENCES AGENT_HISTORY (EVENT_ID)
     ON DELETE CASCADE;

您只需执行一次以上语句。

请注意,如果CPU_HISTORY中的记录具有未知的EVENT_ID(不在AGENT_HISTORY中),则上述语句将失败。在这种情况下,首先清理这些孤儿记录。

对所有具有引用AGENT_HISTORY的EVENT_ID的表重复此操作。请注意,您需要使用唯一名称命名所有这些外键。

然后用这个删除:

DELETE 
FROM    AGENT_HISTORY
WHERE   EVENT_DATE < ADD_MONTHS(SYSDATE, -3);

...相应的记录(具有相同的EVENT_ID)将从CPU_HISTORY(以及其他已应用相同CASCADE DELETE外键的表中删除。)