using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exferia_EntityFramework.Script_Actualizacion
{
    class Scripts_Version_1262
    {
        public static Version G_VRS_VERSION = new Version("1.2.6.2");
        public static long G_LNG_VERSION = 1262;
        public static int G_INT_TIPO = 0;
        public static string G_STR_SCRIPT =
                                    //BORRAR TABLA MYT_InstalacionesPrioridades
                                    " IF exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_InstalacionesPrioridades' " +
                                    " ) " +
                                    "BEGIN " +
                                        " DROP TABLE MYT_InstalacionesPrioridades " +
                                    "END " +
                                    //CREAR TABLA MAE_ArticulosTotalizados
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_ArticulosTotalizados' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MAE_ArticulosTotalizados " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] [varchar] (10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                        " CONSTRAINT[PK_MAE_ArticulosTotalizados] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                     //AÑADIR campo esManoDeObra a la tabla MAE_Articulos
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='esManoDeObra' AND TABLE_NAME = 'MAE_Articulos' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Articulos]ADD[esManoDeObra] bit NULL  " +
                                    "END " +
                                     //AÑADIR campo esDesplazamiento a la tabla MAE_Articulos
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='esDesplazamiento' AND TABLE_NAME = 'MAE_Articulos' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Articulos]ADD[esDesplazamiento] bit NULL  " +
                                    "END " +
                                     //AÑADIR campo idArticuloTotalizado a la tabla MAE_Articulos
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='idArticuloTotalizado' AND TABLE_NAME = 'MAE_Articulos' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Articulos]ADD[idArticuloTotalizado] bigint NULL  " +
                                        //Relaciones
                                        " ALTER TABLE[MAE_Articulos] WITH CHECK ADD  CONSTRAINT[FK_MAE_Articulos_MAE_ArticulosTotalizados] FOREIGN KEY([idArticuloTotalizado]) REFERENCES[MAE_ArticulosTotalizados] ([id]) " +
                                        " ALTER TABLE[MAE_Articulos] CHECK CONSTRAINT[FK_MAE_Articulos_MAE_ArticulosTotalizados] " +
                                    "END " +
                                     //AÑADIR campo tipoPantalla a la tabla MAE_Articulos
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='tipoPantalla' AND TABLE_NAME = 'MAE_Articulos' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Articulos]ADD[tipoPantalla] int NULL  " +
                                    "END " +
                                    //CREAR TABLA MAE_Prioridades
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MAE_Prioridades' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MAE_Prioridades " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] [varchar] (10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [colorFondo] [varchar] (20) NULL, " +
                                            " [colorLetra] [varchar] (20) NULL, " +
                                            " [orden] int NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                        " CONSTRAINT[PK_MAE_Prioridades] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                     //AÑADIR campo idPrioridad a la tabla MAE_Clientes
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='idPrioridad' AND TABLE_NAME = 'MAE_Clientes' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Clientes]ADD[idPrioridad] bigint NULL  " +
                                        //Relaciones
                                        " ALTER TABLE[MAE_Clientes] WITH CHECK ADD  CONSTRAINT[FK_MAE_Clientes_MAE_Prioridades] FOREIGN KEY([idPrioridad]) REFERENCES[MAE_Prioridades] ([id]) " +
                                        " ALTER TABLE[MAE_Clientes] CHECK CONSTRAINT[FK_MAE_Clientes_MAE_Prioridades] " +
                                    "END " +
                                    //AÑADIR campo tipoPantalla a la tabla MAE_Clientes
                                    "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='tipoPantalla' AND TABLE_NAME = 'MAE_Clientes' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [MAE_Clientes]ADD[tipoPantalla] int NULL  " +
                                    "END " +
                                    //CREAR TABLA MYT_Administradores
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Administradores' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Administradores " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] [varchar] (10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fax] [varchar] (15) NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                            " [fechaAlta] datetime NOT NULL, " +
                                            " [fechaBaja] datetime NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                        " CONSTRAINT[PK_MYT_Administradores] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                     //AÑADIR campo idAdministrador a la tabla GEN_Direcciones
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='idAdministrador' AND TABLE_NAME = 'GEN_Direcciones' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [GEN_Direcciones]ADD[idAdministrador] bigint NULL  " +
                                        //Relaciones
                                        " ALTER TABLE[GEN_Direcciones] WITH CHECK ADD  CONSTRAINT[FK_GEN_Direcciones_MYT_Administradores] FOREIGN KEY([idAdministrador]) REFERENCES[MYT_Administradores] ([id]) " +
                                        " ALTER TABLE[GEN_Direcciones] CHECK CONSTRAINT[FK_GEN_Direcciones_MYT_Administradores] " +
                                    "END " +
                                    //CREAR TABLA MYT_AdministradoresEmails
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_AdministradoresEmails' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_AdministradoresEmails " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idAdministrador] bigint NOT NULL, " +
                                            " [email] [varchar] (100) NOT NULL, " +
                                         " CONSTRAINT[PK_MYT_AdministradoresEmails] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //Relaciones
                                        " ALTER TABLE MYT_AdministradoresEmails WITH CHECK ADD CONSTRAINT [FK_MYT_AdministradoresEmails_MYT_Administradores] FOREIGN KEY([idAdministrador]) REFERENCES MYT_Administradores ([id]) " +
                                        " ALTER TABLE MYT_AdministradoresEmails CHECK CONSTRAINT [FK_MYT_AdministradoresEmails_MYT_Administradores] " +
                                    " END " +
                                    //CREAR TABLA MYT_AdministradoresTelefonos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_AdministradoresTelefonos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_AdministradoresTelefonos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idAdministrador] bigint NOT NULL, " +
                                            " [telefono] [varchar] (20) NOT NULL, " +
                                            " [idTelefonoTipo] bigint NOT NULL, " +
                                         " CONSTRAINT[PK_MYT_AdministradoresTelefonos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //Relaciones
                                        " ALTER TABLE MYT_AdministradoresTelefonos WITH CHECK ADD CONSTRAINT [FK_MYT_AdministradoresTelefonos_MYT_Administradores] FOREIGN KEY([idAdministrador]) REFERENCES MYT_Administradores ([id]) " +
                                        " ALTER TABLE MYT_AdministradoresTelefonos CHECK CONSTRAINT [FK_MYT_AdministradoresTelefonos_MYT_Administradores] " +

                                        " ALTER TABLE MYT_AdministradoresTelefonos WITH CHECK ADD CONSTRAINT [FK_MYT_AdministradoresTelefonos_MAE_TelefonosTipos] FOREIGN KEY([idTelefonoTipo]) REFERENCES MAE_TelefonosTipos ([id]) " +
                                        " ALTER TABLE MYT_AdministradoresTelefonos CHECK CONSTRAINT [FK_MYT_AdministradoresTelefonos_MAE_TelefonosTipos] " +
                                    " END " +
                                    //CREAR TABLA MYT_EstructurasPartesPeticionDatos_Cabecera
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_EstructurasPartesPeticionDatos_Cabecera' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_EstructurasPartesPeticionDatos_Cabecera " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] [varchar] (15) NOT NULL, " +
                                            " [descripcion] varchar (100) NOT NULL, " +
                                            " [tipo] int NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_EstructurasPartesPeticionDatos_Cabecera] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CREAR TABLA MYT_EstructurasPartesPeticionDatos_Secciones
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_EstructurasPartesPeticionDatos_Secciones' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_EstructurasPartesPeticionDatos_Secciones " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idEstructuraPartePeticionDatoCabecera] bigint NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_EstructurasPartesPeticionDatos_Secciones] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //Relaciones
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Secciones WITH CHECK ADD CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Secciones_MYT_EstructurasPartesPeticionDatos_Cabecera] FOREIGN KEY([idEstructuraPartePeticionDatoCabecera]) REFERENCES MYT_EstructurasPartesPeticionDatos_Cabecera ([id]) " +
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Secciones CHECK CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Secciones_MYT_EstructurasPartesPeticionDatos_Cabecera] " +
                                    " END " +
                                    //CREAR TABLA MYT_EstructurasPartesPeticionDatos_Preguntas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_EstructurasPartesPeticionDatos_Preguntas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_EstructurasPartesPeticionDatos_Preguntas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idEstructuraPartePeticionDatoSeccion] bigint NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [tipoPregunta] [int] NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_EstructurasPartesPeticionDatos_Preguntas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //Relaciones
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Preguntas WITH CHECK ADD CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Preguntas_MYT_EstructurasPartesPeticionDatos_Secciones] FOREIGN KEY([idEstructuraPartePeticionDatoSeccion]) REFERENCES MYT_EstructurasPartesPeticionDatos_Secciones ([id]) " +
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Preguntas CHECK CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Preguntas_MYT_EstructurasPartesPeticionDatos_Secciones] " +
                                    " END " +
                                    //CREAR TABLA MYT_EstructurasPartesPeticionDatos_Repuestas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_EstructurasPartesPeticionDatos_Repuestas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_EstructurasPartesPeticionDatos_Repuestas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idEstructuraPartePeticionDatoPregunta] bigint NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_EstructurasPartesPeticionDatos_Repuestas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //Relaciones
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Repuestas WITH CHECK ADD CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Repuestas_MYT_EstructurasPartesPeticionDatos_Preguntas] FOREIGN KEY([idEstructuraPartePeticionDatoPregunta]) REFERENCES MYT_EstructurasPartesPeticionDatos_Preguntas ([id]) " +
                                        " ALTER TABLE MYT_EstructurasPartesPeticionDatos_Repuestas CHECK CONSTRAINT [FK_MYT_EstructurasPartesPeticionDatos_Repuestas_MYT_EstructurasPartesPeticionDatos_Preguntas] " +
                                    " END " +
                                    //CREAR TABLA MYT_Protocolos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Protocolos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Protocolos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] [varchar] (10) NOT NULL, " +
                                            " [descripcion] varchar (50) NOT NULL, " +
                                            " [clasificacionInterna] int NOT NULL, " +
                                            " [idProtocoloTipo] bigint NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_Protocolos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_Protocolos_MYT_ProtocolosTipos] FOREIGN KEY([idProtocoloTipo]) REFERENCES MYT_ProtocolosTipos ([id]) " +
                                        " ALTER TABLE MYT_Protocolos CHECK CONSTRAINT [FK_MYT_Protocolos_MYT_ProtocolosTipos] " +
                                    " END " +
                                    //CREAR TABLA MYT_ProtocolosPreguntas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_ProtocolosPreguntas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_ProtocolosPreguntas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [descripcion] varchar (100) NOT NULL, " +
                                            " [idProtocolo] bigint NOT NULL, " +
                                            " [tipo] int NOT NULL, " +
                                            " [orden] int NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_ProtocolosPreguntas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_ProtocolosPreguntas WITH CHECK ADD CONSTRAINT [FK_MYT_ProtocolosPreguntas_MYT_Protocolos] FOREIGN KEY([idProtocolo]) REFERENCES MYT_Protocolos ([id]) " +
                                        " ALTER TABLE MYT_ProtocolosPreguntas CHECK CONSTRAINT [FK_MYT_ProtocolosPreguntas_MYT_Protocolos] " +
                                    " END " +
                                    //CREAR TABLA MYT_ProtocolosRespuestas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_ProtocolosRespuestas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_ProtocolosRespuestas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [texto] varchar (300) NOT NULL, " +
                                            " [idProtocoloPregunta] bigint NOT NULL, " +
                                            " [orden] int NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_ProtocolosRespuestas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_ProtocolosRespuestas WITH CHECK ADD CONSTRAINT [FK_MYT_ProtocolosRespuestas_MYT_ProtocolosPreguntas] FOREIGN KEY([idProtocoloPregunta]) REFERENCES MYT_ProtocolosPreguntas ([id]) " +
                                        " ALTER TABLE MYT_ProtocolosRespuestas CHECK CONSTRAINT [FK_MYT_ProtocolosRespuestas_MYT_ProtocolosPreguntas] " +
                                    " END " +
                                    //CREAR TABLA MYT_Marcas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Marcas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Marcas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] varchar(10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_Marcas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CREAR TABLA MYT_Modelos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Modelos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Modelos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] varchar(10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [idMarca] [bigint]  NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_Modelos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_Modelos WITH CHECK ADD CONSTRAINT [FK_MYT_Modelos_MYT_Marcas] FOREIGN KEY([idMarca]) REFERENCES MYT_Marcas ([id]) " +
                                        " ALTER TABLE MYT_Modelos CHECK CONSTRAINT [FK_MYT_Modelos_MYT_Marcas] " +
                                    " END " +
                                    //CREAR TABLA MYT_Modelos_Protocolos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Modelos_Protocolos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Modelos_Protocolos " +
                                        " ( " +
                                            " [idModelo] bigint NOT NULL, " +
                                            " [idProtocolo] bigint NOT NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                         " CONSTRAINT[PK_MYT_Modelos_Protocolos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [idModelo] ASC, " +
                                        " [idProtocolo] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_Modelos_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_Modelos_Protocolos_MYT_Modelos] FOREIGN KEY([idModelo]) REFERENCES MYT_Modelos ([id]) " +
                                        " ALTER TABLE MYT_Modelos_Protocolos CHECK CONSTRAINT [FK_MYT_Modelos_Protocolos_MYT_Modelos] " +

                                        " ALTER TABLE MYT_Modelos_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_Modelos_Protocolos_MYT_Protocolos] FOREIGN KEY([idProtocolo]) REFERENCES MYT_Protocolos ([id]) " +
                                        " ALTER TABLE MYT_Modelos_Protocolos CHECK CONSTRAINT [FK_MYT_Modelos_Protocolos_MYT_Protocolos] " +
                                    " END " +
                                    //CREAR TABLA MYT_ModelosDocumentos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_ModelosDocumentos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_ModelosDocumentos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idModelo] bigint NOT NULL, " +
                                            " [descripcion] [varchar] (100) NOT NULL, " +
                                            " [fichero] [varbinary](max) NOT NULL, " +
                                            " [extension] [varchar] (5) NOT NULL, " +
                                            " [size] [bigint] NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_ModelosDocumentos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_ModelosDocumentos WITH CHECK ADD CONSTRAINT [FK_MYT_ModelosDocumentos_MYT_Modelos] FOREIGN KEY([idModelo]) REFERENCES MYT_Modelos ([id]) " +
                                        " ALTER TABLE MYT_ModelosDocumentos CHECK CONSTRAINT [FK_MYT_ModelosDocumentos_MYT_Modelos] " +
                                    " END " +
                                    //CREAR TABLA MYT_PlanMantenimientosCabeceras
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PlanMantenimientosCabeceras' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PlanMantenimientosCabeceras " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] varchar(10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_PlanMantenimientosCabeceras] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CREAR TABLA MYT_PlanMantenimientosLineas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PlanMantenimientosLineas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PlanMantenimientosLineas " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [idPlanMantenimientoCabecera] [bigint] NOT NULL, " +
                                            " [periodicidad] int NOT NULL, " +
                                            " [periodicidadCalculo] int NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_PlanMantenimientosLineas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_PlanMantenimientosLineas WITH CHECK ADD CONSTRAINT [FK_MYT_PlanMantenimientosLineas_MYT_PlanMantenimientosCabeceras] FOREIGN KEY([idPlanMantenimientoCabecera]) REFERENCES MYT_PlanMantenimientosCabeceras ([id]) " +
                                        " ALTER TABLE MYT_PlanMantenimientosLineas CHECK CONSTRAINT [FK_MYT_PlanMantenimientosLineas_MYT_PlanMantenimientosCabeceras] " +
                                    " END " +
                                    //CREAR TABLA MYT_PlanMantenimientosLineas_Protocolos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PlanMantenimientosLineas_Protocolos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PlanMantenimientosLineas_Protocolos " +
                                        " ( " +
                                            " [idPlanMantenimientoLinea] bigint NOT NULL, " +
                                            " [idProtocolo] bigint NOT NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                         " CONSTRAINT[PK_MYT_PlanMantenimientosLineas_Protocolos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [idPlanMantenimientoLinea] ASC, " +
                                        " [idProtocolo] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_PlanMantenimientosLineas_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_PlanMantenimientosLineas_Protocolos_MYT_PlanMantenimientosLineas] FOREIGN KEY([idPlanMantenimientoLinea]) REFERENCES MYT_PlanMantenimientosLineas ([id]) " +
                                        " ALTER TABLE MYT_PlanMantenimientosLineas_Protocolos CHECK CONSTRAINT [FK_MYT_PlanMantenimientosLineas_Protocolos_MYT_PlanMantenimientosLineas] " +

                                        " ALTER TABLE MYT_PlanMantenimientosLineas_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_PlanMantenimientosLineas_Protocolos_MYT_Protocolos] FOREIGN KEY([idProtocolo]) REFERENCES MYT_Protocolos ([id]) " +
                                        " ALTER TABLE MYT_PlanMantenimientosLineas_Protocolos CHECK CONSTRAINT [FK_MYT_PlanMantenimientosLineas_Protocolos_MYT_Protocolos] " +
                                    " END " +
                                    //CREAR TABLA MYT_PartesTipos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PartesTipos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PartesTipos " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] varchar(10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [protocolosSeleccionadosPorDefecto] [bit] NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_PartesTipos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CREAR TABLA MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera " +
                                        " ( " +
                                            " [idParteTipo] bigint NOT NULL, " +
                                            " [idEstructuraPartePeticionDatoCabecera] bigint NOT NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                         " CONSTRAINT[PK_MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [idParteTipo] ASC, " +
                                        " [idEstructuraPartePeticionDatoCabecera] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera WITH CHECK ADD CONSTRAINT [FK_MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera_MYT_PartesTipos] FOREIGN KEY([idParteTipo]) REFERENCES MYT_PartesTipos ([id]) " +
                                        " ALTER TABLE MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera CHECK CONSTRAINT [FK_MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera_MYT_PartesTipos] " +

                                        " ALTER TABLE MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera WITH CHECK ADD CONSTRAINT [FK_MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera_MYT_EstructurasPartesPeticionDatos_Cabecera] FOREIGN KEY([idEstructuraPartePeticionDatoCabecera]) REFERENCES MYT_EstructurasPartesPeticionDatos_Cabecera ([id]) " +
                                        " ALTER TABLE MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera CHECK CONSTRAINT [FK_MYT_PartesTipos_EstructurasPartesPeticionDatos_Cabecera_MYT_EstructurasPartesPeticionDatos_Cabecera] " +
                                    " END " +
                                    //CREAR TABLA MYT_PartesTipos_Protocolos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_PartesTipos_Protocolos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_PartesTipos_Protocolos " +
                                        " ( " +
                                            " [idParteTipo] bigint NOT NULL, " +
                                            " [idProtocolo] bigint NOT NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                         " CONSTRAINT[PK_MYT_PartesTipos_Protocolos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [idParteTipo] ASC, " +
                                        " [idProtocolo] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_PartesTipos_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_PartesTipos_Protocolos_MYT_PartesTipos] FOREIGN KEY([idParteTipo]) REFERENCES MYT_PartesTipos ([id]) " +
                                        " ALTER TABLE MYT_PartesTipos_Protocolos CHECK CONSTRAINT [FK_MYT_PartesTipos_Protocolos_MYT_PartesTipos] " +

                                        " ALTER TABLE MYT_PartesTipos_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_PartesTipos_Protocolos_MYT_Protocolos] FOREIGN KEY([idProtocolo]) REFERENCES MYT_Protocolos ([id]) " +
                                        " ALTER TABLE MYT_PartesTipos_Protocolos CHECK CONSTRAINT [FK_MYT_PartesTipos_Protocolos_MYT_Protocolos] " +
                                    " END " +
                                    //CREAR TABLA MYT_InstalacionesCabecera
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_InstalacionesCabecera' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_InstalacionesCabecera] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [codigo] [varchar](10) NOT NULL, " +
                                            " [descripcion] [varchar](50) NOT NULL, " +
                                            " [idCliente][bigint] NOT NULL, " +
                                            " [idInstalacionEstado][bigint] NOT NULL, " +
                                            " [idAdministrador][bigint] NULL, " +
                                            " [idEmpresaInstaladora] [bigint] NULL, " +
                                            " [observaciones][varchar](500) NULL, " +
                                            " [fecha][datetime] NOT NULL, " +
                                            " [gps_posicionLatitud] [varchar](50) NULL, " +
                                            " [gps_posicionLongitud][varchar] (50) NULL, " +
                                            " [gps_zoom][INT]NULL, " +
                                            " [gps_tipoProveedor][int] NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_MYT_InstalacionesCabecera] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabecera_MAE_Clientes] FOREIGN KEY([idCliente]) REFERENCES[dbo].[MAE_Clientes] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] CHECK CONSTRAINT[FK_MYT_InstalacionesCabecera_MAE_Clientes] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_InstalacionesEstados] FOREIGN KEY([idInstalacionEstado]) REFERENCES[dbo].[MYT_InstalacionesEstados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] CHECK CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_InstalacionesEstados] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_Administradores] FOREIGN KEY([idAdministrador]) REFERENCES[dbo].[MYT_Administradores] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] CHECK CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_Administradores] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_EmpresasInstaladoras] FOREIGN KEY([idEmpresaInstaladora]) REFERENCES[dbo].[MYT_EmpresasInstaladoras] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabecera] CHECK CONSTRAINT[FK_MYT_InstalacionesCabecera_MYT_EmpresasInstaladoras] " +
                                    " END " +
                                     //AÑADIR campo idInstalacionCabecera a la tabla GEN_Direcciones
                                     "IF not exists " +
                                      "( " +
                                      "    SELECT * " +
                                      "    FROM INFORMATION_SCHEMA.COLUMNS " +
                                      "    WHERE COLUMN_NAME='idInstalacionCabecera' AND TABLE_NAME = 'GEN_Direcciones' " +
                                      ") " +
                                    "BEGIN " +
                                        " ALTER TABLE [GEN_Direcciones]ADD[idInstalacionCabecera] bigint NULL  " +
                                        //Relaciones
                                        " ALTER TABLE[GEN_Direcciones] WITH CHECK ADD  CONSTRAINT[FK_GEN_Direcciones_MYT_InstalacionesCabecera] FOREIGN KEY([idInstalacionCabecera]) REFERENCES[MYT_InstalacionesCabecera] ([id]) " +
                                        " ALTER TABLE[GEN_Direcciones] CHECK CONSTRAINT[FK_GEN_Direcciones_MYT_InstalacionesCabecera] " +
                                    "END " +
                                    //CREAR TABLA MYT_InstalacionesLineas
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_InstalacionesLineas' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_InstalacionesLineas] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idInstalacionCabecera][bigint] NOT NULL, " +
                                            " [idInstalacionEstado][bigint] NOT NULL, " +
                                            " [idPlanMantenimientoCabecera][bigint] NULL, " +
                                            " [idInstalacionUso][bigint] NOT NULL, " +
                                            " [fechaInstalacion][datetime] NOT NULL, " +
                                            " [fechaGarantia][datetime] NULL, " +
                                            " [ubicacion] [varchar](300) NULL, " +
                                            " [observaciones][varchar](500) NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_MYT_InstalacionesLineas] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY]" +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesCabecera] FOREIGN KEY([idInstalacionCabecera]) REFERENCES[dbo].[MYT_InstalacionesCabecera] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] CHECK CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesCabecera] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesEstados] FOREIGN KEY([idInstalacionEstado]) REFERENCES[dbo].[MYT_InstalacionesEstados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] CHECK CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesEstados] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_PlanMantenimientosCabeceras] FOREIGN KEY([idPlanMantenimientoCabecera]) REFERENCES[dbo].[MYT_PlanMantenimientosCabeceras] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] CHECK CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_PlanMantenimientosCabeceras] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesUsos] FOREIGN KEY([idInstalacionUso]) REFERENCES[dbo].[MYT_InstalacionesUsos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineas] CHECK CONSTRAINT[FK_MYT_InstalacionesLineas_MYT_InstalacionesUsos] " +
                                    " END " +
                                    //CREAR TABLA MYT_InstalacionesLineasInfraestructuras
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_InstalacionesLineasInfraestructuras' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idInstalacionLinea][bigint] NOT NULL, " +
                                            " [idInfraestructura][bigint] NOT NULL, " +
                                            " [idInfraestructuraSeccion][bigint] NOT NULL, " +
                                            " [idInfraestructuraElemento][bigint] NULL, " +
                                            " [idInfraestructuraOpcion][bigint] NULL, " +
                                            " [idModelo][bigint] NULL, " +
                                            " [texto][varchar](100) NULL, " +
                                        " CONSTRAINT[PK_MYT_InstalacionesLineasInfraestructuras] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InstalacionesLineas] FOREIGN KEY([idInstalacionLinea]) REFERENCES[dbo].[MYT_InstalacionesLineas] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InstalacionesLineas] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_Infraestructuras] FOREIGN KEY([idInfraestructura]) REFERENCES[dbo].[MYT_Infraestructuras] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_Infraestructuras] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasSecciones] FOREIGN KEY([idInfraestructuraSeccion]) REFERENCES[dbo].[MYT_InfraestructurasSecciones] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasSecciones] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasElementos] FOREIGN KEY([idInfraestructuraElemento]) REFERENCES[dbo].[MYT_InfraestructurasElementos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasElementos] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasOpciones] FOREIGN KEY([idInfraestructuraOpcion]) REFERENCES[dbo].[MYT_InfraestructurasOpciones] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_InfraestructurasOpciones] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_Modelos] FOREIGN KEY([idModelo]) REFERENCES[dbo].[MYT_Modelos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesLineasInfraestructuras] CHECK CONSTRAINT[FK_MYT_InstalacionesLineasInfraestructuras_MYT_Modelos] " +
                                    " END " +
                                    //CREAR TABLA MYT_InstalacionesCabeceraContratos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_InstalacionesCabeceraContratos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_InstalacionesCabeceraContratos] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idInstalacionCabecera][bigint] NOT NULL, " +
                                            " [idTipoContrato][bigint] NOT NULL, " +
                                            " [numero][varchar] (50) NOT NULL, " +
                                            " [descripcion][varchar] (100) NOT NULL, " +
                                            " [fechaInicio][datetime] NOT NULL, " +
                                            " [fechaFin][datetime] NULL, " +
                                            " [observaciones][varchar](500) NULL, " +
                                            " [duracion][int] NULL, " +
                                            " [duracionCalculo][int] NULL, " +
                                            " [fechaBorrado][datetime] NULL, " +
                                        " CONSTRAINT[PK_MYT_InstalacionesCabeceraContratos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        //relaciones

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabeceraContratos] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabeceraContratos_MYT_InstalacionesCabecera] FOREIGN KEY([idInstalacionCabecera]) REFERENCES[dbo].[MYT_InstalacionesCabecera] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabeceraContratos] CHECK CONSTRAINT[FK_MYT_InstalacionesCabeceraContratos_MYT_InstalacionesCabecera] " +

                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabeceraContratos] WITH CHECK ADD CONSTRAINT[FK_MYT_InstalacionesCabeceraContratos_MYT_TiposContratos] FOREIGN KEY([idTipoContrato]) REFERENCES[dbo].[MYT_TiposContratos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_InstalacionesCabeceraContratos] CHECK CONSTRAINT[FK_MYT_InstalacionesCabeceraContratos_MYT_TiposContratos] " +
                     
                                    " END " +
                                    //CREAR TABLA MYT_Inspecciones
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Inspecciones' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_Inspecciones] " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [codigo] [varchar](13) NULL, " +
                                            " [idOrganismoCertificador][bigint] NULL, " +
                                            " [idInspeccionRelacionada][bigint] NULL, " +
                                            " [idInstalacionLinea][bigint] NOT NULL, " +
                                            " [idInspeccionTipoResultado] [bigint]NULL, " +
                                            " [inspectorNombre][varchar](50) NULL, " +
                                            " [inspectorTelefono][varchar](50) NULL, " +
                                            " [inspectorEmail][varchar](80) NULL, " +
                                            " [fechaRecibiAviso][datetime]NULL, " +
                                            " [fechaRealizacionInspeccion][datetime]NULL, " +
                                            " [fechaLlegadaInforme][datetime]NULL, " +
                                            " [fechaPlazo][datetime]NULL, " +
                                            " [fechaRecibiPresupuestoDefecto][datetime]NULL, " +
                                            " [fechaPresupuestoFirmado][datetime]NULL, " +
                                            " [fechaComprobacionDefectos][datetime]NULL, " +
                                            " [fechaPeticionSegundaInspeccion][datetime]NULL, " +
                                            " [observaciones][text]NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_MYT_Inspecciones] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        " ALTER TABLE[dbo].[MYT_Inspecciones] WITH CHECK ADD CONSTRAINT[FK_MYT_Inspecciones_MYT_Inspecciones] FOREIGN KEY([idInspeccionRelacionada]) REFERENCES[dbo].[MYT_Inspecciones] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Inspecciones] CHECK CONSTRAINT[FK_MYT_Inspecciones_MYT_Inspecciones] " +
 
                                        " ALTER TABLE[dbo].[MYT_Inspecciones] WITH CHECK ADD CONSTRAINT[FK_MYT_Inspecciones_MYT_InspeccionesTiposResultados] FOREIGN KEY([idInspeccionTipoResultado]) REFERENCES[dbo].[MYT_InspeccionesTiposResultados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Inspecciones] CHECK CONSTRAINT[FK_MYT_Inspecciones_MYT_InspeccionesTiposResultados] " +

                                        " ALTER TABLE[dbo].[MYT_Inspecciones] WITH CHECK ADD CONSTRAINT[FK_MYT_Inspecciones_MYT_InstalacionesLineas] FOREIGN KEY([idInstalacionLinea]) REFERENCES[dbo].[MYT_InstalacionesLineas] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Inspecciones] CHECK CONSTRAINT[FK_MYT_Inspecciones_MYT_InstalacionesLineas] " +

                                        " ALTER TABLE[dbo].[MYT_Inspecciones] WITH CHECK ADD CONSTRAINT[FK_MYT_Inspecciones_MYT_OrganismosCertificadores] FOREIGN KEY([idOrganismoCertificador]) REFERENCES[dbo].[MYT_OrganismosCertificadores] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Inspecciones] CHECK CONSTRAINT[FK_MYT_Inspecciones_MYT_OrganismosCertificadores] " +
                                    " END " +
                                    //CREAR TABLA MYT_Partes
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Partes' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_Partes] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [codigo][bigint] NOT NULL, " +
                                            " [idInstalacionCabecera] [bigint] NOT NULL, " +
                                            " [idInstalacionLinea] [bigint] NULL, " +
                                            " [idEmpleado] [bigint] NULL, " +
                                            " [idPrioridad][bigint] NOT NULL, " +
                                            " [idParteTipo][bigint] NOT NULL, " +
                                            " [idParteEstado][bigint] NOT NULL, " +
                                            " [idDepartamento][bigint] NULL, " +
                                            " [idPlanMantenimientoLinea][bigint] NULL, " +
                                            " [idInspeccion][bigint] NULL, " +
                                            " [idEstructuraPartePeticionDatoCabecera][bigint]NULL, " +
                                            " [fechaHoraRecepcion] [datetime]NULL, " +
                                            " [fechaHoraCita][datetime]NULL, " +
                                            " [textoCita][varchar](100) NULL, " +
                                            " [incidencias][text] NULL, " +
                                            " [comentarioCita][varchar](250) NULL, " +
                                            " [observaciones][text]NULL, " +
                                            " [guid][varchar](50) NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_MYT_Partes] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_InstalacionesCabecera] FOREIGN KEY([idInstalacionCabecera]) REFERENCES[dbo].[MYT_InstalacionesCabecera] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_InstalacionesCabecera] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_InstalacionesLineas] FOREIGN KEY([idInstalacionLinea]) REFERENCES[dbo].[MYT_InstalacionesLineas] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_InstalacionesLineas] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MAE_Empleados] FOREIGN KEY([idEmpleado]) REFERENCES[dbo].[MAE_Empleados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MAE_Empleados] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MAE_Prioridades] FOREIGN KEY([idPrioridad]) REFERENCES[dbo].[MAE_Prioridades] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MAE_Prioridades] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_PartesTipos] FOREIGN KEY([idParteTipo]) REFERENCES[dbo].[MYT_PartesTipos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_PartesTipos] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_PartesEstados] FOREIGN KEY([idParteEstado]) REFERENCES[dbo].[MYT_PartesEstados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_PartesEstados] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_CTA_Departamentos] FOREIGN KEY([idDepartamento]) REFERENCES[dbo].[CTA_Departamentos] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_CTA_Departamentos] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_PlanMantenimientosLineas] FOREIGN KEY([idPlanMantenimientoLinea]) REFERENCES[dbo].[MYT_PlanMantenimientosLineas] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_PlanMantenimientosLineas] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_Inspecciones] FOREIGN KEY([idInspeccion]) REFERENCES[dbo].[MYT_Inspecciones] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_Inspecciones] " +

                                        " ALTER TABLE[dbo].[MYT_Partes] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_MYT_EstructurasPartesPeticionDatos_Cabecera] FOREIGN KEY([idEstructuraPartePeticionDatoCabecera])REFERENCES[dbo].[MYT_EstructurasPartesPeticionDatos_Cabecera] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes] CHECK CONSTRAINT[FK_MYT_Partes_MYT_EstructurasPartesPeticionDatos_Cabecera] " +

                                    " END " +
                                    //CREAR TABLA MYT_Partes_Protocolos
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Partes_Protocolos' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Partes_Protocolos " +
                                        " ( " +
                                            " [idParte] bigint NOT NULL, " +
                                            " [idProtocolo] bigint NOT NULL, " +
                                            " [observaciones] [varchar] (50) NULL, " +
                                         " CONSTRAINT[PK_MYT_Partes_Protocolos] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [idParte] ASC, " +
                                        " [idProtocolo] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +

                                        //Relaciones
                                        " ALTER TABLE MYT_Partes_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_Partes_Protocolos_MYT_Partes] FOREIGN KEY([idParte]) REFERENCES MYT_Partes ([id]) " +
                                        " ALTER TABLE MYT_Partes_Protocolos CHECK CONSTRAINT [FK_MYT_Partes_Protocolos_MYT_Partes] " +

                                        " ALTER TABLE MYT_Partes_Protocolos WITH CHECK ADD CONSTRAINT [FK_MYT_Partes_Protocolos_MYT_Protocolos] FOREIGN KEY([idProtocolo]) REFERENCES MYT_Protocolos ([id]) " +
                                        " ALTER TABLE MYT_Partes_Protocolos CHECK CONSTRAINT [FK_MYT_Partes_Protocolos_MYT_Protocolos] " +
                                    " END " +
                                    //CREAR TABLA MYT_Terminales
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Terminales' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE MYT_Terminales " +
                                        " ( " +
                                            " [id] [bigint] IDENTITY (1,1) NOT NULL, " +
                                            " [codigo] varchar(10) NOT NULL, " +
                                            " [descripcion] [varchar] (50) NOT NULL, " +
                                            " [fechaBorrado] datetime NULL, " +
                                         " CONSTRAINT[PK_MYT_Terminales] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                    " END " +
                                    //CREAR TABLA MYT_Partes_TrabajosRealizados
                                    " IF not exists " +
                                    " ( " +
                                        " SELECT * " +
                                        " FROM INFORMATION_SCHEMA.TABLES " +
                                        " WHERE TABLE_NAME = 'MYT_Partes_TrabajosRealizados' " +
                                    " ) " +
                                    " BEGIN " +
                                        " CREATE TABLE[dbo].[MYT_Partes_TrabajosRealizados] " +
                                        " ( " +
                                            " [id][bigint] IDENTITY(1,1) NOT NULL, " +
                                            " [idParte][bigint] NOT NULL, " +
                                            " [idEmpleado] [bigint] NULL, " +
                                            " [idTerminal] [bigint] NULL, " +
                                            " [fechaHora][datetime] NOT NULL, " +
                                            " [trabajoRealizado] [text]NULL, " +
                                            " [tipo][int] NULL, " +
                                            " [duracionSegundos][bigint] NULL, " +
                                            " [firma][varbinary] (max) NULL, " +
                                            " [firmanteObservaciones][varchar] (250)NULL, " +
                                            " [guid][varchar](50) NULL, " +
                                            " [fechaBorrado][datetime]NULL, " +
                                        " CONSTRAINT[PK_MYT_Partes_TrabajosRealizados] PRIMARY KEY CLUSTERED " +
                                        " ( " +
                                        " [id] ASC " +
                                        " )WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY] " +
                                        " ) ON[PRIMARY] " +
                                        
                                        //Relaciones
                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MYT_Partes] FOREIGN KEY([idParte]) REFERENCES[dbo].[MYT_Partes] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] CHECK CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MYT_Partes] " +

                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MAE_Empleados] FOREIGN KEY([idEmpleado]) REFERENCES[dbo].[MAE_Empleados] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] CHECK CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MAE_Empleados] " +

                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] WITH CHECK ADD CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MYT_Terminales] FOREIGN KEY([idTerminal]) REFERENCES[dbo].[MYT_Terminales] ([id]) " +
                                        " ALTER TABLE[dbo].[MYT_Partes_TrabajosRealizados] CHECK CONSTRAINT[FK_MYT_Partes_TrabajosRealizados_MYT_Terminales] " +
                                    " END ";


    }
}
