I have the following database that I want to use for logging network configutations:
CREATE TABLE ip (
id INT NOT NULL AUTO_INCREMENT,
address VARBINARY(16) NOT NULL, -- IPv4 or IPv6
PRIMARY KEY (id),
UNIQUE (address)
);
CREATE TABLE mac (
id INT NOT NULL AUTO_INCREMENT,
address VARBINARY(100) NOT NULL, -- hardware address (usually MAC)
PRIMARY KEY (id),
UNIQUE (address)
);
CREATE TABLE network (
id INT NOT NULL,
mac INT NOT NULL,
ip INT NOT NULL,
INDEX (id),
FOREIGN KEY (mac) REFERENCES mac(id),
FOREIGN KEY (ip) REFERENCES ip(id)
);
When logging a network configuration, I do not want any rows to be inserted if the network configuration (combination of MAC and IP addresses) already exists. In that case I just want to get the corresponding id from the network table. What is the best way to do this?