Salve a tutti. Devo realizzare un database del genere:
Tabella "telefoni" con un solo campo IMEI chiave primaria, utilizzata per identificare un telefono.
Tabella "azioni" con tutte le azioni eseguibili dai telefoni in generale, campo "nome" chiave primaria.
Un telefono può svolgere svariate azioni ed una certa azione può essere svolta da più telefoni, quindi tra queste due tabelle c'è una relazione molti-a-molti che viene implementata tramite una nuova
tabella "ha_azioni" che ha due campi chiave primaria (imei,nome_azione).
Per un dato telefono devo contare quante volte svolge un azione in un determinato periodo della giornata (mattina,pomeriggio,sera), quindi nella tabella "ha_azioni" ho aggiunto 4 colonne
"mattina","pomeriggio","sera","notte" di tipo int.
Poi mi serve una tabella che elenca tutte le azioni che fanno i telefoni, con timestamp di inizio e fine.
Deve essere tipo: "id"|imei|azione|timestamp_start|timestamp_sto p
"Imei" dovrebbe essere tipo foreign key verso l'"imei" di "telefoni" e "azione" dovrebbe essere foreign key verso "nome" di "azioni".
Non so se va bene come gestione della questione. Di fatto un telefono può svolgere 0..* azioni, una specifica azione di quest'ultima tabella è svolta da un telefono solo, ma non so convinto perché di fatto una specifica azione, in generale, può essere svolta da più telefoni.
Questo è il codice MySQL:
codice:CREATE TABLE `actions` ( `name` VARCHAR(255) PRIMARY KEY NOT NULL ); CREATE TABLE `phones` ( `imei` VARCHAR(255) PRIMARY KEY ); CREATE TABLE `has_actions` ( `actions` INTEGER NOT NULL, `phones` VARCHAR(255) NOT NULL, `morning` INTEGER NOT NULL, `afternoon` INTEGER NOT NULL, `evening` INTEGER NOT NULL, `night` INTEGER NOT NULL PRIMARY KEY (`actions`, `phones`) ); CREATE INDEX `idx_has_actions` ON `has_actions` (`phones`); ALTER TABLE `has_actions` ADD CONSTRAINT `fk_has_actions__actions` FOREIGN KEY (`actions`) REFERENCES `actions` (`name`); ALTER TABLE `has_actions` ADD CONSTRAINT `fk_has_actions__phones` FOREIGN KEY (`phones`) REFERENCES `phones` (`imei`); CREATE TABLE `phones_actions` ( `id` INTEGER PRIMARY KEY AUTO_INCREMENT, `timestamp_start` DATETIME NOT NULL, `timestamp_stop` DATETIME NOT NULL, `phones` VARCHAR(255) NOT NULL, `actions` INTEGER NOT NULL ); CREATE INDEX `idx_phones_actions__actions` ON `test` (`actions`); CREATE INDEX `idx_phones_actions__phones` ON `test` (`phones`); ALTER TABLE `test` ADD CONSTRAINT `fk_phones_actions__actions` FOREIGN KEY (`actions`) REFERENCES `actions` (`name`); ALTER TABLE `test` ADD CONSTRAINT `fk_test__phones` FOREIGN KEY (`phones`) REFERENCES `phones` (`imei`)

Rispondi quotando