Delimiter //
/**
 * Author:  armen
 * Created: 04 nov. 2019
 */
Drop Procedure if exists LOADER_CREATE_TABLE_ATTACHEDMEMBERS_REFDATA//
Create Procedure LOADER_CREATE_TABLE_ATTACHEDMEMBERS_REFDATA ()
Begin
    Drop Table If Exists LOADER_ATTACHEDMEMBERS_REFDATA;
    
    Create Table LOADER_ATTACHEDMEMBERS_REFDATA (
        ID_LOADER_ATTACHEDMEMBERS_REFDATA Int Unsigned Not Null Auto_increment
        -- champs pour le traitements de l'import
        , LOADER_STATUS_ID_LOADER_STATUS Int Unsigned Not Null
        , LOADER_ATTACHEDMEMBERS_ID_LOADER_ATTACHEDMEMBERS Int Unsigned Not Null
        

        , ENTITIES_REF_SEQUENCE smallint Unsigned Null -- numéro de la sequence fourni par le fichier
        , ENTITIES_REF_DATA_CODE varchar(20) -- valeur de la référence, fourni par le fichier

        , ENTITIES_REF_ID_ENTITIES_REF Int Unsigned -- id reference, issu normalisation
        , ID_ENTITIES_REF_DATAS Int Unsigned -- issu normalisation si code trouvé
        , NEW_ID_ENTITIES_REF_DATAS Int Unsigned -- issu normalisation si code non trouvé

		, LOADER_REFDATA_TYPE Varchar(25) Not NULL Default 'ACCRED_ACCOUNTS'

        -- contraintes et indexes
        , Primary Key (ID_LOADER_ATTACHEDMEMBERS_REFDATA)
        , Index (LOADER_STATUS_ID_LOADER_STATUS)
        , Index (LOADER_ATTACHEDMEMBERS_ID_LOADER_ATTACHEDMEMBERS)
        , Index (LOADER_REFDATA_TYPE, ENTITIES_REF_SEQUENCE)
        , Index (ENTITIES_REF_ID_ENTITIES_REF, ENTITIES_REF_DATA_CODE)

    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
End //
Delimiter ;
call LOADER_CREATE_TABLE_ATTACHEDMEMBERS_REFDATA();
