CREATE DATABASE personal;
USE personal;
CREATE TABLE tbl_upravlenie(
                          upr_id int unsigned NOT NULL auto_increment primary key,
                          upravlenie text(300) NOT NULL,
                          position SMALLINT(5) NOT NULL default '0',
                          visible enum('show','hide') NOT NULL default 'show'
                          )TYPE=MyISAM
                          CHARACTER SET utf8;
CREATE TABLE tbl_otdel(
                         otd_id int unsigned NOT NULL auto_increment primary key,
                         otdel varchar(250) NOT NULL,
                         position SMALLINT(5) NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_dolzhnost(
                         dol_id int unsigned NOT NULL auto_increment primary key,
                         dolzhnost varchar(120) NOT NULL,
                         position SMALLINT(5) NOT NULL default '0'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_city(
                         city_id int unsigned NOT NULL auto_increment primary key,
                         city varchar(120) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_street(
                         str_id int unsigned NOT NULL auto_increment primary key,
                         street varchar(200) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_house(
                         house_id int unsigned NOT NULL auto_increment primary key,
                         house varchar(80) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_organizaciya(
                         org_id int unsigned NOT NULL auto_increment primary key,
                         organizaciya text(300) NOT NULL,
                         position SMALLINT(3) NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_domain(
                         dom_id int unsigned NOT NULL auto_increment primary key,
                         domain varchar(200) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_users(
                         user_id int unsigned NOT NULL auto_increment primary key,
                         first_b varchar(4) NOT NULL,
                         familiya varchar(200) NOT NULL,
                         name varchar(200) NOT NULL,
                         otchestvo varchar(200),
                         position SMALLINT(5) NOT NULL default '0',
                         dom_id int,
                         mail varchar(220),
                         phone_mob varchar(30),
                         phone_int varchar(20),
                         phone_city varchar(30),
                         dol_id int,
                         otd_id int,
                         otd_pos SMALLINT(5) NOT NULL default '0',
                         upr_id int,
                         upr_pos SMALLINT(5) NOT NULL default '0',
                         org_id int,
                         org_pos SMALLINT(3) NOT NULL default '0',
                         room varchar(16),
                         house_id int,
                         str_id int,
                         city_id int,
                         photo varchar(200),
                         subscribe enum('scribe','unscribe') NOT NULL default 'unscribe',
                         visible enum('show','hide') NOT NULL default 'show'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_logs(
                         log_id int unsigned not null auto_increment primary key,
                         data timestamp NOT NULL,
                         ip varchar(50) NOT NULL,
                         who varchar(250),
                         doing varchar(250),
                         what text
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_start(
                         start_id int(2) NOT NULL auto_increment primary key,
                         data timestamp NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_photo(
                         photo_id int unsigned not null auto_increment primary key,
                         name text(300) NOT NULL,
                         small tinytext NOT NULL,
                         big tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         mer_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_meropriyatiya(
                         mer_id int unsigned not null auto_increment primary key,
                         meropriyatie text NOT NULL,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_documents(
                         doc_id int unsigned not null auto_increment primary key,
                         name text NOT NULL,
                         path tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         data timestamp not null,
                         visible enum('show','hide') NOT NULL default 'show',
                         razd_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_razdel(
                         razd_id int unsigned not null auto_increment primary key,
                         razdel text NOT NULL,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_instructions(
                         instr_id int unsigned not null auto_increment primary key,
                         name text NOT NULL,
                         path tinytext NOT NULL,
                         description text,
                         position int NOT NULL default '0',
                         photo text NOT NULL,
                         visible enum('show','hide') NOT NULL default 'show',
                         razd_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_razdel_instr(
                         razd_id int unsigned not null auto_increment primary key,
                         photo tinytext NOT NULL,
                         razdel text NOT NULL,
                         position smallint(11) NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_resource(
                         res_id int unsigned not null auto_increment primary key,
                         resource text NOT NULL,
                         position smallint NOT NULL,
                         user_id int NOT NULL,
                         description text,
                         visible enum('show','hide') NOT NULL default 'show',
                         org_id int,
                         ukk varchar(255),
                         data date,
                         srvname varchar(100),
                         srvput varchar(255),
                         gread text(1000),
                         gwrite text(1000),
                         sppubl varchar(100),
                         srvkvota int(10),
                         size_id int(2),
                         bakup ENUM('yes','no') NOT NULL DEFAULT 'yes',
                         rezim varchar(255),
                         bakrazm varchar(255),
                         bakprog varchar(255),
			 photo varchar(255) 
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_news(
                         new_id int unsigned not null auto_increment primary key,
                         org_id int,
                         header text(300) NOT NULL,
                         news text NOT NULL,
                         position smallint(11) NOT NULL default '0',
                         description text(300),
                         url text,
                         url_text text(300),
                         picture tinytext,
                         picture_big tinytext,
                         data timestamp not null,
                         visible enum('show','hide') NOT NULL default 'show'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_logins(
                         login_id int unsigned not null auto_increment primary key,
                         login text(300) NOT NULL,
                         fullname text,
                         pass text,
                         moderator enum('0','1') NOT NULL default '0',
                         administrator enum('0','1') NOT NULL default '0',
                         user enum('0','1') NOT NULL default '0',
                         manager enum('0','1') NOT NULL default '0',
                         master enum('0','1') NOT NULL default '0',
                         usr_id int
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_forecast(
                         id int not null auto_increment primary key,
                         ind mediumint(9) default NULL,
                         station varchar(255) default NULL,
                         country varchar(255) default NULL,
                         region varchar(255) default NULL,
                         dat date default NULL,
                         tday smallint(6) default NULL,
                         tnight smallint(6) default NULL,
                         prec smallint(6) default NULL,
                         prec_prob smallint(6) default NULL,
                         wind_dir smallint(6) default NULL,
                         wind_speed smallint(6) default NULL,
                         weather_conditions varchar(255) default NULL,
                         pday smallint(6) default NULL,
                         pnight smallint(6) default NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_supp_tema(
                         tema_id int unsigned not null auto_increment primary key,
                         tema varchar(255),
                         position smallint(11) NOT NULL,
                         shablon varchar(255)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_supp_sostoyanie(
                         sost_id int unsigned not null auto_increment primary key,
                         sostoyanie varchar(255),
                         pic_sost tinytext,
                         position smallint(11) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_supp_srochnost(
                         sroch_id int unsigned not null auto_increment primary key,
                         srochnost varchar(255),
                         pic_sr tinytext,
                         position smallint(11) NOT NULL
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_supp_archive(
                         arch_id int unsigned not null auto_increment primary key,
                         data date,
                         master varchar(255),
                         user varchar(255),
                         manager varchar(255),
                         tema varchar(255),
                         organizaciya text(300),
                         time_isp decimal(10,0),
                         zay_id int,
                         yea decimal(4,0),
                         mon decimal(2,0),
                         dy decimal(2,0)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_supp_zayavka(
                         zay_id int unsigned not null auto_increment primary key,
                         data_post datetime,
                         data_k_isp datetime,
                         data_close datetime,
                         man_id int,
                         mast_id int,
                         usr_id int,
                         sost_id int,
                         sroch_id int,
                         tema_id int,
                         problema text NOT NULL,
                         pic_tr tinytext,
                         reshenie text NOT NULL,
                         kod text,
                         time_isp decimal(10,0),
                         org_id int,
                         ob_type enum('0','1','2') NOT NULL default '0'
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_ob_zay(
                         ob_zay_id int unsigned NOT NULL auto_increment primary key,
                         root_zay_id int,
                         par_zay_id int 
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_clear(
                         cl_id int(5) unsigned NOT NULL auto_increment primary key,
                         col_tbl_name varchar(255),
                         col_name varchar(255),
                         by_data int(20),
                         by_count int(20)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;";
CREATE TABLE aliases(
                         local_part varchar(64) NOT NULL,
                         domain varchar(200) NOT NULL,
                         recipients text(2000),
                         PRIMARY KEY (local_part,domain)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE blacklists(
                         address varchar(64) NOT NULL,
                         type enum('SENDER','HOSTREJ','REJRCPT') NOT NULL default 'SENDER',
                         message varchar(255),
                         PRIMARY KEY (address,type)
                         )TYPE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE callbackdomains(
                        domain varchar(200) NOT NULL,
                        type enum('NOENV','NOHDR','NORND','NODNS') NOT NULL default 'NOENV',
                        PRIMARY KEY (domain,type)
                        )TYPE=MyISAM
                        CHARACTER SET utf8;
CREATE TABLE domains(
                       domain varchar(200) NOT NULL,
                       type enum('LOCAL','RELAY','VIRTUAL') default 'LOCAL',
                       PRIMARY KEY (domain,type)
                       )TYPE=MyISAM
                       CHARACTER SET utf8;
CREATE TABLE hostlists(
                       hostname varchar(64) NOT NULL,
                       type enum('AVOIDTLS','TRYAUTH','NOCALL') NOT NULL default 'AVOIDTLS',
                       PRIMARY KEY (hostname,type)
                       )TYPE=MyISAM
                       CHARACTER SET utf8;
CREATE TABLE userforward(
                       local_part varchar(64) NOT NULL,
                       domain varchar(200) NOT NULL,
                       recipients text(2000),
                       PRIMARY KEY (local_part,domain)
                       )TYPE=MyISAM
                       CHARACTER SET utf8;
CREATE TABLE users(
                       id varchar(200) NOT NULL,
                       name varchar(255),
                       user_id int NOT NULL,
                       crypt varchar(255),
                       passwd varchar(255),
                       uid int(10) NOT NULL,
                       gid int(10) NOT NULL,
                       mbox_host varchar(128) NOT NULL,
                       shell varchar(32) NOT NULL,
                       home varchar(128) NOT NULL,
                       quota tinyint(4),
                       active enum('Y','N') NOT NULL default 'Y',
                       PRIMARY KEY (id,mbox_host)
                       )TYPE=MyISAM
                       CHARACTER SET utf8;
CREATE TABLE whitelist(
                       address varchar(64) NOT NULL,
                       PRIMARY KEY (address)
                       )TYPE=MyISAM
                       CHARACTER SET utf8;
CREATE TABLE tbl_kritichnost(
                         krit_id int unsigned not null auto_increment primary key,
                         kritichnost varchar(255) COLLATE utf8_general_ci,
                         pic_krit tinytext,
                         position smallint(11) NOT NULL,
                         UNIQUE KEY uc_kritichnost (kritichnost(255))
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_zhurn_strukt(
                         zh_str_id int unsigned NOT NULL auto_increment primary key,
                         strukt_name varchar(255) NOT NULL COLLATE utf8_general_ci,
                         parent int,
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show'
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;
CREATE TABLE tbl_zhurnal(
                         zhurn_id int unsigned NOT NULL auto_increment primary key,
                         krit_id int,
                         zh_str_id int,
                         zh_tema varchar(255),
                         zh_soobschenie text(4000),
                         position smallint NOT NULL default '0',
                         visible enum('show','hide') NOT NULL default 'show',
                         data timestamp not null,
                         login_id int,
                         KEY krit_id_ind (krit_id),
                         KEY zh_str_id_ind (zh_str_id)
                         )ENGINE=MyISAM
                         CHARACTER SET utf8;
ALTER TABLE `tbl_otdel` ADD COLUMN `phone` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci AFTER `visible`;
INSERT INTO `tbl_kritichnost` VALUES (1,'Для информации','../photo/krit_Dlya_informatsii_20100618114949.png',1),(2,'Не критично','../photo/krit_Ne_kritichno_20100618123543.png',2),(3,'Средне','../photo/krit_Sredne_20100618124557.png',3),(4,'Критично','../photo/krit_Kritichno_20100618123452.png',4);
INSERT INTO `tbl_organizaciya` VALUES (1, '-', 1, 'hide');
INSERT INTO `tbl_otdel` VALUES (1, '-', 1, 'hide');
INSERT INTO `tbl_upravlenie` VALUES (1, '-', 1, 'hide');
INSERT INTO `tbl_sizes` VALUES (1, 'Байт'),(2, 'Килобайт'),(3, 'Мегабайт'),(4, 'Гигабайт'),(5, 'Терабайт');

CREATE INDEX ind_root_zay ON tbl_ob_zay (root_zay_id);
CREATE FULLTEXT INDEX ind_root_zay ON tbl_ob_zay (root_zay_id);
CREATE FULLTEXT INDEX ind_familiya ON tbl_users (familiya(5));
CREATE FULLTEXT INDEX ind_name ON tbl_users (name(5));
CREATE FULLTEXT INDEX ind_first_b ON tbl_users (first_b(4));
CREATE FULLTEXT INDEX ind_room ON tbl_users (room(5));
CREATE FULLTEXT INDEX ind_name ON tbl_documents (name(10));
CREATE FULLTEXT INDEX ind_instr ON tbl_instructions (name(10));
CREATE FULLTEXT INDEX ind_res ON tbl_resource (resource(10));
CREATE FULLTEXT INDEX ind_new ON tbl_news (header(10));
CREATE FULLTEXT INDEX ind_login ON tbl_logins (login(10));
CREATE FULLTEXT INDEX ind_master ON tbl_supp_archive (master(10));
CREATE FULLTEXT INDEX ind_user ON tbl_supp_archive (user(10));
CREATE FULLTEXT INDEX ind_manager ON tbl_supp_archive (manager(10));
CREATE FULLTEXT INDEX ind_tema ON tbl_supp_archive (tema(20));
CREATE FULLTEXT INDEX ind_organizaciya ON tbl_supp_archive (organizaciya(10));
INSERT INTO `tbl_start`(`start_id`,`data`) VALUES('1', null);
INSERT INTO `tbl_logins`(`login_id`,`login`,`fullname`,`pass`,`moderator`,`administrator`,`user`,`manager`,`master`,`usr_id`) VALUES('1','admin','administrator','21232f297a57a5a743894a0e4a801fc3','0','1','0','0','0','0');
GRANT SELECT ON personal.* TO personal_user@'%';
GRANT SELECT ON personal.* TO personal_user@localhost;
GRANT SELECT ON personal.aliases, personal.blacklists, personal.callbackdomains, personal.domains, personal.hostlists, personal.userforward, personal.users, personal.whitelist TO personal_exim@'%';
GRANT SELECT ON personal.aliases, personal.blacklists, personal.callbackdomains, personal.domains, personal.hostlists, personal.userforward, personal.users, personal.whitelist TO personal_exim@localhost;
GRANT SELECT,INSERT ON personal.tbl_supp_archive TO personal_user@'%';
GRANT SELECT,INSERT ON personal.tbl_supp_archive TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_supp_zayavka TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_supp_zayavka TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE ON personal.system_ip TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE ON personal.system_ip TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE ON personal.system_pages TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE ON personal.system_pages TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE ON personal.system_refferer TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE ON personal.system_refferer TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE ON personal.system_searchquerys TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE ON personal.system_searchquerys TO personal_user@localhost;
GRANT SELECT,INSERT ON personal.tbl_logs TO personal_user@'%';
GRANT SELECT,INSERT ON personal.tbl_logs TO personal_user@localhost;
GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES ON personal.* TO personal_admin@'%';
GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES ON personal.* TO personal_admin@localhost;
GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_zhurnal TO personal_user@'%';
GRANT SELECT,INSERT,UPDATE,DELETE ON personal.tbl_zhurnal TO personal_user@localhost;
SET PASSWORD FOR personal_user@'%' = PASSWORD('personal_user');
SET PASSWORD FOR personal_user@localhost = PASSWORD('personal_user');
SET PASSWORD FOR personal_admin@'%' = PASSWORD('Pgt7d34smb9');
SET PASSWORD FOR personal_admin@localhost = PASSWORD('Pgt7d34smb9');
SET PASSWORD FOR personal_exim@'%' = PASSWORD('dFrt34@sOMiqzFF25');
SET PASSWORD FOR personal_exim@localhost = PASSWORD('dFrt34@sOMiqzFF25');
FLUSH PRIVILEGES;
