Zacznijmy od struktury
CREATE TABLE `items` (
`Id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`Lang` char(2) NOT NULL,
`Type` varchar(30) NOT NULL,
`Name` varchar(255) DEFAULT NULL,
`CreatedBy` int(10) UNSIGNED DEFAULT NULL,
`CreateDate` datetime NOT NULL,
`EditedBy` int(10) UNSIGNED DEFAULT NULL,
`EditDate` datetime DEFAULT NULL,
`System` enum('Y','N') NOT NULL DEFAULT 'N',
`Published` enum('Y','N') NOT NULL DEFAULT 'N',
`AllowAccess` text,
`DenyAccess` text,
`AllowEdit` text,
`DenyEdit` text,
`AllowDelete` text,
`DenyDelete` text,
PRIMARY KEY (`Id`,`Lang`),
KEY `Type` (`Type`,`Name`,`CreatedBy`,`EditedBy`,`System`,`Published`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `docs` (
`Id` int(10) UNSIGNED NOT NULL,
`Lang` char(2) NOT NULL,
`Content` text NOT NULL,
PRIMARY KEY (`Id`,`Lang`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Docs są jakby uzupełnieniem itemsów. Połączone są wartościami Id i Lang. Chciałbym mieć do tych danych łatwy dostęp (przez jedną tabelę) więc utworzyłem widok:
CREATE VIEW `itemsdocs` AS SELECT
`i`.`Id` AS `Id`,
`i`.`Lang` AS `Lang`,
`i`.`Type` AS `Type`,
`i`.`Name` AS `Name`,
`i`.`CreatedBy` AS `CreatedBy`,
`i`.`CreateDate` AS `CreateDate`,
`i`.`EditedBy` AS `EditedBy`,
`i`.`EditDate` AS `EditDate`,
`i`.`System` AS `System`,
`i`.`Published` AS `Published`,
`i`.`AllowAccess` AS `AllowAccess`,
`i`.`DenyAccess` AS `DenyAccess`,
`i`.`AllowEdit` AS `AllowEdit`,
`i`.`DenyEdit` AS `DenyEdit`,
`i`.`AllowDelete` AS `AllowDelete`,
`i`.`DenyDelete` AS `DenyDelete`,
`d`.`Content` AS `Content`
FROM (`docs` `d` JOIN `items` `i`)
WHERE (`i`.`Id` = `d`.`Id`) AND (`i`.`Lang` = `d`.`Lang`) AND (`i`.`Type` = _latin1'docs');
Teraz sprawa wstawiania//edytowania//usuwania danych. Zwykły insert do widoku nie zadziała ponieważ stworzony jest on z joina. Chciałem utworzyć triggery i przykładowo:
DELIMITER |
CREATE TRIGGER itemsdocs_insert AFTER INSERT ON itemsdocs FOR EACH ROW
BEGIN INSERT INTO `items` (Id, Lang, `Type`, Name, CreatedBy, CreateDate, EditedBy, EditDate, System, Published,
AllowAccess, DenyAccess, AllowEdit, DenyEdit, AllowDelete, DenyDelete)
VALUES (
NEW.Id, NEW.Lang, NEW.`Type`, NEW.Name, NEW.CreatedBy, NEW.CreateDate, NEW.EditedBy,
NEW.EditDate, NEW.System, NEW.Published, NEW.AllowAccess, NEW.DenyAccess, NEW.AllowEdit,
NEW.DenyEdit, NEW.AllowDelete, NEW.DenyDelete);
INSERT INTO `docs` (`Id`, `Lang`, `Content`)
VALUES (NEW.Id, NEW.Lang, NEW.Content);
END;
Odpowiedzialny za wstawianie danych, nie utworzy się ponieważ itemdocs nie jest "BASE TABLE" tzn jest widokiem. Przeskoczę to jakoś na tym systemie bazodanowym? Chodzi o to aby jednym zapytaniem mieć możliwość aktualizowania danych zawartych w tym widoku.