SET GLOBAL log_bin_trust_function_creators = 1;
SET FOREIGN_KEY_CHECKS=0;

ALTER TABLE `acknowledges` DROP FOREIGN KEY `acknowledges_ibfk_1`,
ADD CONSTRAINT `c_acknowledges_1` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE CASCADE;
ALTER TABLE `acknowledges` DROP FOREIGN KEY `acknowledges_ibfk_2`,
ADD CONSTRAINT `c_acknowledges_2` FOREIGN KEY (`eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;

ALTER TABLE `alerts` DROP FOREIGN KEY `alerts_ibfk_1`;
ALTER TABLE `alerts` DROP FOREIGN KEY `alerts_ibfk_2`;
ALTER TABLE `alerts` DROP FOREIGN KEY `alerts_ibfk_3`;
ALTER TABLE `alerts` ADD CONSTRAINT `c_alerts_2` FOREIGN KEY (`eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;
ALTER TABLE `alerts` ADD CONSTRAINT `c_alerts_3` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE CASCADE;
ALTER TABLE `alerts` ADD CONSTRAINT `c_alerts_5` FOREIGN KEY (`p_eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;

ALTER TABLE `autoreg_host` DROP FOREIGN KEY `autoreg_host_ibfk_1`,
ADD CONSTRAINT `c_autoreg_host_1` FOREIGN KEY (`proxy_hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;

ALTER TABLE `conditions` DROP FOREIGN KEY `conditions_ibfk_1`,
ADD CONSTRAINT `c_conditions_1` FOREIGN KEY (`actionid`) REFERENCES `actions` (`actionid`) ON DELETE CASCADE;

ALTER TABLE `dhosts` DROP FOREIGN KEY `dhosts_ibfk_1`,
ADD CONSTRAINT `c_dhosts_1` FOREIGN KEY (`druleid`) REFERENCES `drules` (`druleid`) ON DELETE CASCADE;

ALTER TABLE `dservices` DROP FOREIGN KEY `dservices_ibfk_1`,
ADD CONSTRAINT `c_dservices_2` FOREIGN KEY (`dcheckid`) REFERENCES `dchecks` (`dcheckid`) ON DELETE CASCADE;

ALTER TABLE `event_recovery` DROP FOREIGN KEY `event_recovery_ibfk_1`,
ADD CONSTRAINT `c_event_recovery_1` FOREIGN KEY (`eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;
ALTER TABLE `event_recovery` DROP FOREIGN KEY `event_recovery_ibfk_2`,
ADD CONSTRAINT `c_event_recovery_2` FOREIGN KEY (`r_eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;
ALTER TABLE `event_recovery` DROP FOREIGN KEY `event_recovery_ibfk_3`,
ADD CONSTRAINT `c_event_recovery_3` FOREIGN KEY (`c_eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;

ALTER TABLE `event_tag` DROP FOREIGN KEY `event_tag_ibfk_1`,
ADD CONSTRAINT `c_event_tag_1` FOREIGN KEY (`eventid`) REFERENCES `events` (`eventid`) ON DELETE CASCADE;

ALTER TABLE `functions` DROP FOREIGN KEY `functions_ibfk_1`,
ADD CONSTRAINT `c_functions_1` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;
ALTER TABLE `functions` DROP FOREIGN KEY `functions_ibfk_2`,
ADD CONSTRAINT `c_functions_2` FOREIGN KEY (`triggerid`) REFERENCES `triggers` (`triggerid`) ON DELETE CASCADE;

ALTER TABLE `graph_discovery` DROP FOREIGN KEY `graph_discovery_ibfk_1`,
ADD CONSTRAINT `c_graph_discovery_1` FOREIGN KEY (`graphid`) REFERENCES `graphs` (`graphid`) ON DELETE CASCADE;
ALTER TABLE `graph_discovery` DROP FOREIGN KEY `graph_discovery_ibfk_2`,
ADD CONSTRAINT `c_graph_discovery_2` FOREIGN KEY (`parent_graphid`) REFERENCES `graphs` (`graphid`);

ALTER TABLE `graphs_items` DROP FOREIGN KEY `graphs_items_ibfk_1`,
ADD CONSTRAINT `c_graphs_items_1` FOREIGN KEY (`graphid`) REFERENCES `graphs` (`graphid`) ON DELETE CASCADE;
ALTER TABLE `graphs_items` DROP FOREIGN KEY `graphs_items_ibfk_2`,
ADD CONSTRAINT `c_graphs_items_2` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;

ALTER TABLE `group_discovery` DROP FOREIGN KEY `group_discovery_ibfk_1`,
ADD CONSTRAINT `c_group_discovery_1` FOREIGN KEY (`groupid`) REFERENCES `hstgrp` (`groupid`) ON DELETE CASCADE;
ALTER TABLE `group_discovery` DROP FOREIGN KEY `group_discovery_ibfk_2`,
ADD CONSTRAINT `c_group_discovery_2` FOREIGN KEY (`parent_group_prototypeid`) REFERENCES `group_prototype` (`group_prototypeid`);

ALTER TABLE `group_prototype` DROP FOREIGN KEY `group_prototype_ibfk_1`,
ADD CONSTRAINT `c_group_prototype_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;

ALTER TABLE `hostmacro` DROP FOREIGN KEY `hostmacro_ibfk_1`,
ADD CONSTRAINT `c_hostmacro_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;

ALTER TABLE `hosts_groups` DROP FOREIGN KEY `hosts_groups_ibfk_1`,
ADD CONSTRAINT `c_hosts_groups_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;
ALTER TABLE `hosts_groups` DROP FOREIGN KEY `hosts_groups_ibfk_2`,
ADD CONSTRAINT `c_hosts_groups_2` FOREIGN KEY (`groupid`) REFERENCES `hstgrp` (`groupid`) ON DELETE CASCADE;

ALTER TABLE `hosts_templates` DROP FOREIGN KEY `hosts_templates_ibfk_1`,
ADD CONSTRAINT `c_hosts_templates_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;
ALTER TABLE `hosts_templates` DROP FOREIGN KEY `hosts_templates_ibfk_2`,
ADD CONSTRAINT `c_hosts_templates_2` FOREIGN KEY (`templateid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;

ALTER TABLE `httpstep` DROP FOREIGN KEY `httpstep_ibfk_1`,
ADD CONSTRAINT `c_httpstep_1` FOREIGN KEY (`httptestid`) REFERENCES `httptest` (`httptestid`) ON DELETE CASCADE;

ALTER TABLE `httpstep_field` DROP FOREIGN KEY `httpstep_field_ibfk_1`,
ADD CONSTRAINT `c_httpstep_field_1` FOREIGN KEY (`httpstepid`) REFERENCES `httpstep` (`httpstepid`) ON DELETE CASCADE;

ALTER TABLE `httpstepitem` DROP FOREIGN KEY `httpstepitem_ibfk_1`,
ADD CONSTRAINT `c_httpstepitem_1` FOREIGN KEY (`httpstepid`) REFERENCES `httpstep` (`httpstepid`) ON DELETE CASCADE;
ALTER TABLE `httpstepitem` DROP FOREIGN KEY `httpstepitem_ibfk_2`,
ADD CONSTRAINT `c_httpstepitem_2` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;

ALTER TABLE `httptestitem` DROP FOREIGN KEY `httptestitem_ibfk_1`,
ADD CONSTRAINT `c_httptestitem_1` FOREIGN KEY (`httptestid`) REFERENCES `httptest` (`httptestid`) ON DELETE CASCADE;
ALTER TABLE `httptestitem` DROP FOREIGN KEY `httptestitem_ibfk_2`,
ADD CONSTRAINT `c_httptestitem_2` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;

ALTER TABLE `interface` DROP FOREIGN KEY `interface_ibfk_1`,
ADD CONSTRAINT `c_interface_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;

ALTER TABLE `item_condition` DROP FOREIGN KEY `item_condition_ibfk_1`,
ADD CONSTRAINT `c_item_condition_1` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;

ALTER TABLE `item_discovery` DROP FOREIGN KEY `item_discovery_ibfk_1`,
ADD CONSTRAINT `c_item_discovery_1` FOREIGN KEY (`itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;
ALTER TABLE `item_discovery` DROP FOREIGN KEY `item_discovery_ibfk_2`,
ADD CONSTRAINT `c_item_discovery_2` FOREIGN KEY (`parent_itemid`) REFERENCES `items` (`itemid`) ON DELETE CASCADE;

ALTER TABLE `maintenances_groups` DROP FOREIGN KEY `maintenances_groups_ibfk_1`,
ADD CONSTRAINT `c_maintenances_groups_2` FOREIGN KEY (`groupid`) REFERENCES `hstgrp` (`groupid`) ON DELETE CASCADE;

ALTER TABLE `maintenances_windows` DROP FOREIGN KEY `maintenances_windows_ibfk_1`,
ADD CONSTRAINT `c_maintenances_windows_2` FOREIGN KEY (`timeperiodid`) REFERENCES `timeperiods` (`timeperiodid`) ON DELETE CASCADE;

ALTER TABLE `opcommand` DROP FOREIGN KEY `opcommand_ibfk_1`,
ADD CONSTRAINT `c_opcommand_1` FOREIGN KEY (`operationid`) REFERENCES `operations` (`operationid`) ON DELETE CASCADE;

ALTER TABLE `opcommand_hst` DROP FOREIGN KEY `opcommand_hst_ibfk_1`,
ADD CONSTRAINT `c_opcommand_hst_1` FOREIGN KEY (`operationid`) REFERENCES `operations` (`operationid`) ON DELETE CASCADE;

ALTER TABLE `opconditions` DROP FOREIGN KEY `opconditions_ibfk_1`,
ADD CONSTRAINT `c_opconditions_1` FOREIGN KEY (`operationid`) REFERENCES `operations` (`operationid`) ON DELETE CASCADE;

ALTER TABLE `operations` DROP FOREIGN KEY `operations_ibfk_1`,
ADD CONSTRAINT `c_operations_1` FOREIGN KEY (`actionid`) REFERENCES `actions` (`actionid`) ON DELETE CASCADE;

ALTER TABLE `opmessage` DROP FOREIGN KEY `opmessage_ibfk_1`,
ADD CONSTRAINT `c_opmessage_1` FOREIGN KEY (`operationid`) REFERENCES `operations` (`operationid`) ON DELETE CASCADE;

ALTER TABLE `problem_tag` DROP FOREIGN KEY `problem_tag_ibfk_1`,
ADD CONSTRAINT `c_problem_tag_1` FOREIGN KEY (`eventid`) REFERENCES `problem` (`eventid`) ON DELETE CASCADE;

ALTER TABLE `rights` DROP FOREIGN KEY `rights_ibfk_1`,
ADD CONSTRAINT `c_rights_2` FOREIGN KEY (`id`) REFERENCES `hstgrp` (`groupid`) ON DELETE CASCADE;

ALTER TABLE `sysmap_shape` DROP FOREIGN KEY `sysmap_shape_ibfk_1`,
ADD CONSTRAINT `c_sysmap_shape_1` FOREIGN KEY (`sysmapid`) REFERENCES `sysmaps` (`sysmapid`) ON DELETE CASCADE;

ALTER TABLE `sysmaps_elements` DROP FOREIGN KEY `sysmaps_elements_ibfk_1`,
ADD CONSTRAINT `c_sysmaps_elements_1` FOREIGN KEY (`sysmapid`) REFERENCES `sysmaps` (`sysmapid`) ON DELETE CASCADE;

ALTER TABLE `task_acknowledge` DROP FOREIGN KEY `task_acknowledge_ibfk_1`,
ADD CONSTRAINT `c_task_acknowledge_1` FOREIGN KEY (`taskid`) REFERENCES `task` (`taskid`) ON DELETE CASCADE;

ALTER TABLE `task_check_now` DROP FOREIGN KEY `task_check_now_ibfk_1`,
ADD CONSTRAINT `c_task_check_now_1` FOREIGN KEY (`taskid`) REFERENCES `task` (`taskid`) ON DELETE CASCADE;

ALTER TABLE `task_close_problem` DROP FOREIGN KEY `task_close_problem_ibfk_1`,
ADD CONSTRAINT `c_task_close_problem_1` FOREIGN KEY (`taskid`) REFERENCES `task` (`taskid`) ON DELETE CASCADE;

ALTER TABLE `task_remote_command` DROP FOREIGN KEY `task_remote_command_ibfk_1`,
ADD CONSTRAINT `c_task_remote_command_1` FOREIGN KEY (`taskid`) REFERENCES `task` (`taskid`) ON DELETE CASCADE;

ALTER TABLE `task_remote_command_result` DROP FOREIGN KEY `task_remote_command_result_ibfk_1`,
ADD CONSTRAINT `c_task_remote_command_result_1` FOREIGN KEY (`taskid`) REFERENCES `task` (`taskid`) ON DELETE CASCADE;

ALTER TABLE `trigger_depends` DROP FOREIGN KEY `trigger_depends_ibfk_1`,
ADD CONSTRAINT `c_trigger_depends_1` FOREIGN KEY (`triggerid_down`) REFERENCES `triggers` (`triggerid`) ON DELETE CASCADE;
ALTER TABLE `trigger_depends` DROP FOREIGN KEY `trigger_depends_ibfk_2`,
ADD CONSTRAINT `c_trigger_depends_2` FOREIGN KEY (`triggerid_up`) REFERENCES `triggers` (`triggerid`) ON DELETE CASCADE;

ALTER TABLE `trigger_discovery` DROP FOREIGN KEY `trigger_discovery_ibfk_1`,
ADD CONSTRAINT `c_trigger_discovery_1` FOREIGN KEY (`triggerid`) REFERENCES `triggers` (`triggerid`) ON DELETE CASCADE;
ALTER TABLE `trigger_discovery` DROP FOREIGN KEY `trigger_discovery_ibfk_2`,
ADD CONSTRAINT `c_trigger_discovery_2` FOREIGN KEY (`parent_triggerid`) REFERENCES `triggers` (`triggerid`);

ALTER TABLE `trigger_tag` DROP FOREIGN KEY `trigger_tag_ibfk_1`,
ADD CONSTRAINT `c_trigger_tag_1` FOREIGN KEY (`triggerid`) REFERENCES `triggers` (`triggerid`) ON DELETE CASCADE;

ALTER TABLE `widget_field` DROP FOREIGN KEY `widget_field_ibfk_1`,
ADD CONSTRAINT `c_widget_field_1` FOREIGN KEY (`widgetid`) REFERENCES `widget` (`widgetid`) ON DELETE CASCADE;
ALTER TABLE `widget_field` DROP FOREIGN KEY `widget_field_ibfk_2`,
ADD CONSTRAINT `c_widget_field_3` FOREIGN KEY (`value_hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE;




SET FOREIGN_KEY_CHECKS=1;
SET GLOBAL log_bin_trust_function_creators = 0;


SET GLOBAL FOREIGN_KEY_CHECKS=0;