# ---------------------------------------------------------------------- #
# Script generated with: DeZign for Databases V7.3.4                     #
# Target DBMS:           MySQL 5                                         #
# Project file:          abliaDbComuni3.dez                              #
# Project name:          DB COMUNI - ABLIA                               #
# Author:                Andrea Colzi                                    #
# Script type:           Alter database script                           #
# Created on:            2014-11-25 11:51                                #
# ---------------------------------------------------------------------- #


# ---------------------------------------------------------------------- #
# Drop foreign key constraints                                           #
# ---------------------------------------------------------------------- #

ALTER TABLE `USERS` DROP FOREIGN KEY `USER_PROFILE_USERS`;

ALTER TABLE `USERS` DROP FOREIGN KEY `POSTAZIONE_USERS`;

ALTER TABLE `USERS` DROP FOREIGN KEY `CLIFOR_USERS`;

ALTER TABLE `USER_ACCESS` DROP FOREIGN KEY `ACCESS_USER_ACCESS`;

ALTER TABLE `USER_ACCESS` DROP FOREIGN KEY `USERS_USER_ACCESS`;

ALTER TABLE `USER_ACCESS` DROP FOREIGN KEY `DITTA_USER_ACCESS`;

ALTER TABLE `LOG` DROP FOREIGN KEY `USERS_LOG`;

ALTER TABLE `LOG` DROP FOREIGN KEY `BLACKLIST_LOG`;

ALTER TABLE `LOG` DROP FOREIGN KEY `DITTA_LOG`;

ALTER TABLE `ACCESS_GROUP` DROP FOREIGN KEY `DITTA_ACCESS_GROUP`;

ALTER TABLE `NEWS` DROP FOREIGN KEY `TIPO_NEWS_NEWS`;

ALTER TABLE `BLACKLIST` DROP FOREIGN KEY `DITTA_BLACKLIST`;

ALTER TABLE `POSTAZIONE` DROP FOREIGN KEY `REG_CASSA_POSTAZIONE`;

ALTER TABLE `POSTAZIONE` DROP FOREIGN KEY `TIPO_POSTAZIONE_POSTAZIONE`;

ALTER TABLE `POSTAZIONE` DROP FOREIGN KEY `DITTA_POSTAZIONE`;

ALTER TABLE `WHITELIST` DROP FOREIGN KEY `DITTA_WHITELIST`;

ALTER TABLE `TIPO_POSTAZIONE` DROP FOREIGN KEY `DITTA_TIPO_POSTAZIONE`;

ALTER TABLE `USER_DITTA` DROP FOREIGN KEY `DITTA_USER_DITTA`;

ALTER TABLE `USER_DITTA` DROP FOREIGN KEY `USERS_USER_DITTA`;

ALTER TABLE `USER_DITTA` DROP FOREIGN KEY `USER_PROFILE_USER_DITTA`;

ALTER TABLE `ACCESS_DITTA` DROP FOREIGN KEY `ACCESS_ACCESS_DITTA`;

ALTER TABLE `ACCESS_DITTA` DROP FOREIGN KEY `DITTA_ACCESS_DITTA`;

ALTER TABLE `PARM_DITTA` DROP FOREIGN KEY `PARM_PARM_DITTA`;

ALTER TABLE `PARM_DITTA` DROP FOREIGN KEY `DITTA_PARM_DITTA`;

ALTER TABLE `USER_ACCESS_GROUP` DROP FOREIGN KEY `ACCESS_GROUP_USER_ACCESS_GROUP`;

ALTER TABLE `USER_ACCESS_GROUP` DROP FOREIGN KEY `USERS_USER_ACCESS_GROUP`;

ALTER TABLE `ACCESS_GROUP_ACCESS` DROP FOREIGN KEY `ACCESS_ACCESS_GROUP_ACCESS`;

ALTER TABLE `ACCESS_GROUP_ACCESS` DROP FOREIGN KEY `ACCESS_GROUP_ACCESS_GROUP_ACCESS`;

ALTER TABLE `CLIFOR_USERS` DROP FOREIGN KEY `USERS_CLIFOR_USERS`;

ALTER TABLE `ALLEGATO_NEWS` DROP FOREIGN KEY `NEWS_ALLEGATO_NEWS`;

ALTER TABLE `SPESA` DROP FOREIGN KEY `USERS_SPESA`;

ALTER TABLE `USER_WHITELIST` DROP FOREIGN KEY `WHITELIST_USER_WHITELIST`;

ALTER TABLE `USER_WHITELIST` DROP FOREIGN KEY `USERS_USER_WHITELIST`;

ALTER TABLE `DOCUMENTO` DROP FOREIGN KEY `USERS_DOCUMENTO_interv`;

ALTER TABLE `DOCUMENTO` DROP FOREIGN KEY `USERS_DOCUMENTO_chiusura`;

ALTER TABLE `DOCUMENTO` DROP FOREIGN KEY `USERS_DOCUMENTO`;

# ---------------------------------------------------------------------- #
# Drop table "ACCESS_DITTA"                                              #
# ---------------------------------------------------------------------- #

# Remove autoinc for PK drop #

ALTER TABLE `ACCESS_DITTA` MODIFY `id_accessDitta` INTEGER NOT NULL;

# Drop constraints #

ALTER TABLE `ACCESS_DITTA` DROP PRIMARY KEY;

# Drop table #

DROP TABLE `ACCESS_DITTA`;

# ---------------------------------------------------------------------- #
# Drop table "PARM_DITTA"                                                #
# ---------------------------------------------------------------------- #

# Remove autoinc for PK drop #

ALTER TABLE `PARM_DITTA` MODIFY `id_parmDitta` INTEGER NOT NULL;

# Drop constraints #

ALTER TABLE `PARM_DITTA` DROP PRIMARY KEY;

# Drop table #

DROP TABLE `PARM_DITTA`;

# ---------------------------------------------------------------------- #
# Modify table "USERS"                                                   #
# ---------------------------------------------------------------------- #

ALTER TABLE `USERS` ADD COLUMN `flgNews` INTEGER;

# ---------------------------------------------------------------------- #
# Modify table "ACCESS"                                                  #
# ---------------------------------------------------------------------- #

ALTER TABLE `ACCESS` DROP COLUMN `flgCondivisa`;

ALTER TABLE `ACCESS` ADD COLUMN `versione` VARCHAR(20);

# ---------------------------------------------------------------------- #
# Modify table "USER_ACCESS"                                             #
# ---------------------------------------------------------------------- #

DROP INDEX `IDX_USER_ACCESS_1` ON `USER_ACCESS`;

ALTER TABLE `USER_ACCESS` DROP COLUMN `id_ditta`;

CREATE UNIQUE INDEX `IDX_USER_ACCESS_1` ON `USER_ACCESS` (`id_users`,`id_access`);

# ---------------------------------------------------------------------- #
# Modify table "LOG"                                                     #
# ---------------------------------------------------------------------- #

ALTER TABLE `LOG` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "ACCESS_GROUP"                                            #
# ---------------------------------------------------------------------- #

ALTER TABLE `ACCESS_GROUP` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "NEWS"                                                    #
# ---------------------------------------------------------------------- #

ALTER TABLE `NEWS` ADD COLUMN `dataInvio` DATE;

# ---------------------------------------------------------------------- #
# Modify table "BLACKLIST"                                               #
# ---------------------------------------------------------------------- #

ALTER TABLE `BLACKLIST` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "POSTAZIONE"                                              #
# ---------------------------------------------------------------------- #

ALTER TABLE `POSTAZIONE` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "WHITELIST"                                               #
# ---------------------------------------------------------------------- #

ALTER TABLE `WHITELIST` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "TIPO_POSTAZIONE"                                         #
# ---------------------------------------------------------------------- #

ALTER TABLE `TIPO_POSTAZIONE` DROP COLUMN `id_ditta`;

# ---------------------------------------------------------------------- #
# Modify table "DITTA"                                                   #
# ---------------------------------------------------------------------- #

ALTER TABLE `DITTA` DROP COLUMN `nome`;

ALTER TABLE `DITTA` DROP COLUMN `indirizzo`;

ALTER TABLE `DITTA` DROP COLUMN `numeroCivico`;

ALTER TABLE `DITTA` DROP COLUMN `descrizioneComune`;

ALTER TABLE `DITTA` DROP COLUMN `provinciaComune`;

ALTER TABLE `DITTA` DROP COLUMN `capComune`;

ALTER TABLE `DITTA` DROP COLUMN `capZona`;

ALTER TABLE `DITTA` DROP COLUMN `descrizioneNazione`;

ALTER TABLE `DITTA` DROP COLUMN `codFisc`;

ALTER TABLE `DITTA` DROP COLUMN `pIva`;

ALTER TABLE `DITTA` DROP COLUMN `eMail`;

ALTER TABLE `DITTA` DROP COLUMN `eMailAmm`;

ALTER TABLE `DITTA` DROP COLUMN `cellulare`;

ALTER TABLE `DITTA` DROP COLUMN `telefono`;

ALTER TABLE `DITTA` DROP COLUMN `telefonoAmm`;

ALTER TABLE `DITTA` DROP COLUMN `fax`;

ALTER TABLE `DITTA` DROP COLUMN `nota`;

ALTER TABLE `DITTA` DROP COLUMN `imgTmst`;

ALTER TABLE `DITTA` DROP COLUMN `lastUpdTmst`;

ALTER TABLE `DITTA` DROP COLUMN `lastUpdId_user`;

ALTER TABLE `DITTA` ADD COLUMN `database` VARCHAR(40);

ALTER TABLE `DITTA` ADD COLUMN `user` VARCHAR(40);

ALTER TABLE `DITTA` ADD COLUMN `pwd` VARCHAR(40);

ALTER TABLE `DITTA` ADD COLUMN `initalCons` INTEGER;

ALTER TABLE `DITTA` ADD COLUMN `maxCons` INTEGER;

ALTER TABLE `DITTA` ADD COLUMN `timeout` INTEGER;

ALTER TABLE `DITTA` CHANGE `cognome` `descrizione` VARCHAR(254);

# ---------------------------------------------------------------------- #
# Modify table "USER_DITTA"                                              #
# ---------------------------------------------------------------------- #

DROP INDEX `IDX_USER_DITTA_1` ON `USER_DITTA`;

ALTER TABLE `USER_DITTA` DROP COLUMN `id_userProfile`;

ALTER TABLE `USER_DITTA` MODIFY `id_ditta` INTEGER;

ALTER TABLE `USER_DITTA` MODIFY `id_users` INTEGER;

CREATE UNIQUE INDEX `IDX_USER_DITTA_1` ON `USER_DITTA` ();

# ---------------------------------------------------------------------- #
# Add table "USER_CLIFOR"                                                #
# ---------------------------------------------------------------------- #

CREATE TABLE `USER_CLIFOR` (
    `id_userClifor` INTEGER NOT NULL AUTO_INCREMENT,
    `id_users` INTEGER,
    `id_clifor` INTEGER,
    CONSTRAINT `PK_USER_CLIFOR` PRIMARY KEY (`id_userClifor`)
);

# ---------------------------------------------------------------------- #
# Add table "NEWS_USERS"                                                 #
# ---------------------------------------------------------------------- #


CREATE TABLE `NEWS_USERS` (
    `id_newsUsers` INTEGER NOT NULL AUTO_INCREMENT,
    `id_news` INTEGER,
    `id_users` INTEGER,
    `tmstInvio` TIMESTAMP,
    `result` VARCHAR(254),
    CONSTRAINT `PK_NEWS_USERS` PRIMARY KEY (`id_newsUsers`)
);

# ---------------------------------------------------------------------- #
# Add foreign key constraints                                            #
# ---------------------------------------------------------------------- #

ALTER TABLE `USERS` ADD CONSTRAINT `USER_PROFILE_USERS` 
    FOREIGN KEY (`id_userProfile`) REFERENCES `USER_PROFILE` (`id_userProfile`);

ALTER TABLE `USERS` ADD CONSTRAINT `POSTAZIONE_USERS` 
    FOREIGN KEY (`id_postazione`) REFERENCES `POSTAZIONE` (`id_postazione`);

ALTER TABLE `USERS` ADD CONSTRAINT `CLIFOR_USERS` 
    FOREIGN KEY (`id_clifor`) REFERENCES `CLIFOR` (`id_clifor`);

ALTER TABLE `USER_ACCESS` ADD CONSTRAINT `ACCESS_USER_ACCESS` 
    FOREIGN KEY (`id_access`) REFERENCES `ACCESS` (`id_access`);

ALTER TABLE `USER_ACCESS` ADD CONSTRAINT `USERS_USER_ACCESS` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `LOG` ADD CONSTRAINT `USERS_LOG` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `LOG` ADD CONSTRAINT `BLACKLIST_LOG` 
    FOREIGN KEY (`id_blacklist`) REFERENCES `BLACKLIST` (`id_blacklist`);

ALTER TABLE `NEWS` ADD CONSTRAINT `TIPO_NEWS_NEWS` 
    FOREIGN KEY (`id_tipoNews`) REFERENCES `TIPO_NEWS` (`id_tipoNews`);

ALTER TABLE `POSTAZIONE` ADD CONSTRAINT `REG_CASSA_POSTAZIONE` 
    FOREIGN KEY (`id_regCassa`) REFERENCES `REG_CASSA` (`id_regCassa`);

ALTER TABLE `POSTAZIONE` ADD CONSTRAINT `TIPO_POSTAZIONE_POSTAZIONE` 
    FOREIGN KEY (`id_tipoPostazione`) REFERENCES `TIPO_POSTAZIONE` (`id_tipoPostazione`);

ALTER TABLE `USER_DITTA` ADD CONSTRAINT `DITTA_USER_DITTA` 
    FOREIGN KEY (`id_ditta`) REFERENCES `DITTA` (`id_ditta`);

ALTER TABLE `USER_DITTA` ADD CONSTRAINT `USERS_USER_DITTA` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `USER_CLIFOR` ADD CONSTRAINT `USERS_USER_CLIFOR` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `USER_CLIFOR` ADD CONSTRAINT `CLIFOR_USER_CLIFOR` 
    FOREIGN KEY (`id_clifor`) REFERENCES `CLIFOR` (`id_clifor`);

ALTER TABLE `NEWS_USERS` ADD CONSTRAINT `NEWS_NEWS_USERS` 
    FOREIGN KEY (`id_news`) REFERENCES `NEWS` (`id_news`);

ALTER TABLE `NEWS_USERS` ADD CONSTRAINT `USERS_NEWS_USERS` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `USER_ACCESS_GROUP` ADD CONSTRAINT `ACCESS_GROUP_USER_ACCESS_GROUP` 
    FOREIGN KEY (`id_accessGroup`) REFERENCES `ACCESS_GROUP` (`id_accessGroup`);

ALTER TABLE `USER_ACCESS_GROUP` ADD CONSTRAINT `USERS_USER_ACCESS_GROUP` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `ACCESS_GROUP_ACCESS` ADD CONSTRAINT `ACCESS_ACCESS_GROUP_ACCESS` 
    FOREIGN KEY (`id_access`) REFERENCES `ACCESS` (`id_access`);

ALTER TABLE `ACCESS_GROUP_ACCESS` ADD CONSTRAINT `ACCESS_GROUP_ACCESS_GROUP_ACCESS` 
    FOREIGN KEY (`id_accessGroup`) REFERENCES `ACCESS_GROUP` (`id_accessGroup`);

ALTER TABLE `CLIFOR_USERS` ADD CONSTRAINT `USERS_CLIFOR_USERS` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `ALLEGATO_NEWS` ADD CONSTRAINT `NEWS_ALLEGATO_NEWS` 
    FOREIGN KEY (`id_news`) REFERENCES `NEWS` (`id_news`);

ALTER TABLE `SPESA` ADD CONSTRAINT `USERS_SPESA` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `USER_WHITELIST` ADD CONSTRAINT `WHITELIST_USER_WHITELIST` 
    FOREIGN KEY (`id_whitelist`) REFERENCES `WHITELIST` (`id_whitelist`);

ALTER TABLE `USER_WHITELIST` ADD CONSTRAINT `USERS_USER_WHITELIST` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `DOCUMENTO` ADD CONSTRAINT `USERS_DOCUMENTO_interv` 
    FOREIGN KEY (`id_usersIntervento`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `DOCUMENTO` ADD CONSTRAINT `USERS_DOCUMENTO_chiusura` 
    FOREIGN KEY (`id_usersChiusura`) REFERENCES `USERS` (`id_users`);

ALTER TABLE `DOCUMENTO` ADD CONSTRAINT `USERS_DOCUMENTO` 
    FOREIGN KEY (`id_users`) REFERENCES `USERS` (`id_users`);

update USERS set flgNews=1;