我有这个Oracle表,我想填充随机数据:
CREATE TABLE AGENT_HISTORY(
EVENTID INTEGER NOT NULL,
AGENTID INTEGER NOT NULL,
EVENT_DATE DATE,
CPU INTEGER,
RAM NUMBER,
HDD NUMBER,
NETWORK_UP NUMBER,
NETWORK_DOWN NUMBER
)
/
-- CREATE INDEXES FOR TABLE AGENT_HISTORY
CREATE INDEX IX_RELATIONSHIP1 ON AGENT_HISTORY (AGENTID)
/
-- ADD KEYS FOR TABLE AGENT_HISTORY
ALTER TABLE AGENT_HISTORY ADD CONSTRAINT KEY8 PRIMARY KEY (EVENTID)
/
如何在60天后生成随机数据(第EVENT_DATE列)?我想每天插入一些随机网络流量数据吗?
答案 0 :(得分:3)
使用下面的pl / sql块生成随机数据,它也将插入到您的表中。
declare
v_EVENTID INTEGER;
v_AGENTID INTEGER:=1;
v_EVENT_DATE DATE:=sysdate-60;
v_CPU INTEGER;
v_RAM NUMBER;
v_HDD NUMBER;
v_NETWORK_UP NUMBER;
v_NETWORK_DOWN NUMBER;
begin
for i in 1..100
loop
v_EVENT_DATE:=v_EVENT_DATE-1;
select i,ceil(dbms_random.value(1,1000)),floor(dbms_random.value(1,1000)),ceil(dbms_random.value(1,100))*2,floor(dbms_random.value(1,100))*2,100+i
into v_EVENTID,v_CPU,v_ram,v_HDD,v_NETWORK_UP,v_NETWORK_DOWN from dual;
--insert into AGENT_HISTORY values(v_EVENTID,v_AGENTID,v_EVENT_DATE,v_CPU,v_ram,v_HDD,v_NETWORK_UP,v_NETWORK_DOWN);
dbms_output.put_line(v_EVENTID||' '||v_AGENTID||' '||v_EVENT_DATE||' '||v_CPU||' '||v_ram||' '||v_HDD||' '||v_NETWORK_UP||' '||v_NETWORK_DOWN) ;
end loop;
end;
答案 1 :(得分:1)
INSERT INTO AGENT_HISTORY (EVENTID, AGENTID, EVENT_DATE, CPU, RAM, HDD, NETWORK_UP, NETWORK_DOWN)
SELECT LEVEL, LEVEL, TRUNC(SYSDATE - LEVEL + 1), DBMS_RANDOM.VALUE * 100, DBMS_RANDOM.VALUE, DBMS_RANDOM.VALUE, DBMS_RANDOM.VALUE, DBMS_RANDOM.VALUE
FROM DUAL CONNECT BY LEVEL <= 60;
答案 2 :(得分:0)
如果您希望对代码有程序生成感觉,而不实际上下文切换到PL / SQL,则可以使用MODEL子句生成数据...
INSERT INTO agent_history (EVENTID, AGENTID, EVENT_DATE, CPU, RAM, HDD, NETWORK_UP, NETWORK_DOWN)
SELECT EVENTID, AGENTID, EVENT_DATE, CPU, RAM, HDD, NETWORK_UP, NETWORK_DOWN
FROM agent_history
MODEL DIMENSION BY (0 ITERATION)
MEASURES (EVENTID, AGENTID, EVENT_DATE, CPU, RAM, HDD, NETWORK_UP, NETWORK_DOWN)
RULES ITERATE (60) ( --Or however many days you want to iterate over
EVENTID[ITERATION_NUMBER] = 100-ITERATION_NUMBER,
AGENTID[ITERATION_NUMBER] = 1,
EVENT_DATE[ITERATION_NUMBER] = TRUNC(SYSDATE-ITERATION_NUMBER+1),
CPU[ITERATION_NUMBER] = ceil(dbms_random.value(1,100)),
RAM[ITERATION_NUMBER] = floor(dbms_random.value(1,100)),
HDD[ITERATION_NUMBER] = ceil(DBMS_RANDOM.VALUE * 100),
NETWORK_UP[ITERATION_NUMBER] = DBMS_RANDOM.VALUE * 100+1,
NETWORK_DOWN[ITERATION_NUMBER] = DBMS_RANDOM.VALUE * ITERATION_NUMBER
)
ORDER BY EVENT_DATE;