我有两个 Oracle 表:
CREATE TABLE MANAGEDSYSTEMS(
MSYSTEMID INTEGER NOT NULL,
MSYSTEMGROUPID INTEGER,
SPECIALNUMBER VARCHAR2(40 ),
SERIALNUMBER VARCHAR2(30 ),
MSYSTEMSTATUS VARCHAR2(30 ),
MSYSTEMNAME VARCHAR2(60 ),
IPADDRESS VARCHAR2(30 ),
DATEMSYSTEMADDED TIMESTAMP(6),
DESCRIPTION CLOB
)
/
-- ADD KEYS FOR TABLE MANAGEDSYSTEMS
ALTER TABLE MANAGEDSYSTEMS ADD CONSTRAINT MSKEY PRIMARY KEY (MSYSTEMID)
/
CREATE TABLE AGENTS(
AGENTID INTEGER NOT NULL,
MSYSTEMID INTEGER,
AGENTGROUPID INTEGER,
AGENTSERIALNUMBER VARCHAR2(60 ),
AGENTSTATUS VARCHAR2(30 ),
AGENTOS VARCHAR2(60 ),
AGENTIPADDRESS VARCHAR2(40 ),
LASTSYNC TIMESTAMP(6),
DATEAGENTADDED TIMESTAMP(6),
CPULOADLIMIT INTEGER,
RAMLOADLIMIT INTEGER,
HDDSPACELIMIT INTEGER,
NETWORKUPLIMIT INTEGER,
NETWORKDOWNLIMIT INTEGER,
REPORTUSERLOGINS VARCHAR2(30 ),
CANEXECCOMMANDS VARCHAR2(30 ),
SYNCHRONIZATIONTIME VARCHAR2(30 ),
DATALIMITSPEAKTIMES INTEGER,
DESCRIPTION CLOB
)
/
-- ADD KEYS FOR TABLE AGENTS
ALTER TABLE AGENTS ADD CONSTRAINT AGENTID PRIMARY KEY (AGENTID)
ALTER TABLE AGENTS ADD CONSTRAINT MSYSTEMID FOREIGN KEY (MSYSTEMID) REFERENCES MANAGEDSYSTEMS (MSYSTEMID)
我想将“代理”分配到“托管系统”表中。如您所见,我有一个表外键 - 用户必须先创建托管系统,然后再创建代理。但我也想给出一个选项来创建代理而不分配托管系统。当我尝试将托管系统 ID 的键更改为代理表时,我收到此错误:
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (ADMIN.MSYSTEMID) violated - parent key not found
看来我不能分配空值。我该如何解决这个问题?
更新:
我使用这个 SQL 语句来更新代理表:
String sql = "UPDATE AGENTS "
+ " SET AGENTID = ?, MSYSTEMID = ?, AGENTGROUPID = ?, AGENTSERIALNUMBER = ?, AGENTSTATUS = ?, AGENTOS = ?, AGENTIPADDRESS = ?, "
+ " LASTSYNC = to_timestamp(?, " + ts_template + "), DATEAGENTADDED = to_timestamp(?, " + ts_template + "), "
+ " CPULOADLIMIT = ?, RAMLOADLIMIT = ?, HDDSPACELIMIT = ?, NETWORKUPLIMIT = ?, NETWORKDOWNLIMIT = ?, REPORTUSERLOGINS = ?, "
+ " CANEXECCOMMANDS = ?, SYNCHRONIZATIONTIME = ?, DATALIMITSPEAKTIMES = ?, "
+ " DESCRIPTION = ? WHERE AGENTID = ?";