1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE TABLE Addresses (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE Groups (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE Group_Members (
grp_id INTEGER REFERENCES Groups(id),
adr_id INTEGER REFERENCES Addresses(id)
);
1
2
3
4
5
CREATE TABLE IF NOT EXISTS Addr_Groups_Members (
grp_id INTEGER REFERENCES Address_Groups(id),
obj_id INTEGER REFERENCES Addresses(id),
constraint unique_mapping unique (grp_id, obj_id)
);
1
2
INSERT INTO Addr_Groups_Members SELECT g.id as grp_id, a.id as obj_id from Address_Groups as g, Addresses as a where g.name = "dummy" and a.name = "any";
INSERT INTO Addr_Groups_Members SELECT g.id as grp_id, a.id as obj_id from Address_Groups as g, Addresses as a where g.name = "dummy" and a.name = "localhost";
INSERT INTO Addr_Groups_Members SELECT g.id as grp_id, a.id as obj_id from Address_Groups as g, Addresses as a;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
INSERT INTO
Addr_Groups_Members
SELECT
g.id as grp_id
, a.id as obj_id
from
Address_Groups as g
join Addresses as a on
g.name = "dummy"
and
a.name = "any"
left outer join Addr_Groups_Members as ag on
g.id = ag.grp_id
and
a.ID = ag.obj_id
where
ag.obj_id is null
;
2017-05-22T13:20:07 rostiNun, die Objekt-ID muss ja nicht die IP-Adresse sein, die kann davon völlig unabhängig vergeben werden, z.B. eine fortlaufende Nummer.
2017-05-22T13:20:07 rostiUnd wenn ein Objek zu mehreren Gruppen gehört, dann ist diese Eigenschaft eben ein Array bzw. eine Arrayreferenz. Wie willst Du denn gerade sowas in einer relationalen Datenbank abbilden?
QuotePS: Wenn's unbedingt eine Datenbank sein muss, die gibt's auch objektorientiert bzw. ORM.