/*==============================================================*/
/* 安全 开始                               */
/*==============================================================*/
DROP TABLE IF EXISTS SCT_USER_ROLE;
DROP TABLE IF EXISTS SCT_ROLE_RESOURCE;
DROP TABLE IF EXISTS SCT_RESOURCE;
DROP TABLE IF EXISTS SCT_ROLE;
DROP TABLE IF EXISTS SCT_USER;


CREATE TABLE SCT_ROLE_RESOURCE
(
   RESOURCE_UUID        VARCHAR(64) NOT NULL,
   ROLE_UUID            VARCHAR(32) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE SCT_RESOURCE
(
   UUID                 VARCHAR(64) NOT NULL,
   RES_STRING           VARCHAR(256) NOT NULL,
   RES_TYPE             VARCHAR(32) NOT NULL,
   PRIMARY KEY (UUID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE INDEX SCINX_RES_TYPE ON SCT_RESOURCE
(
   RES_TYPE
);

CREATE TABLE SCT_ROLE
(
   UUID                 VARCHAR(32) NOT NULL,
   ROLE_NAME            VARCHAR(128) NOT NULL,
   PRIMARY KEY (UUID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE SCT_USER
(
   UUID                 VARCHAR(32) NOT NULL,
   PARTY_UUID           VARCHAR(32),
   USERNAME             VARCHAR(128) NOT NULL,
   PASSWORD             VARCHAR(256) NOT NULL,
   ENABLED              CHAR(1) NOT NULL,
   CREATETIME datetime ,
   PRIMARY KEY (UUID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE UNIQUE INDEX SCINX_USER_USERNAME ON SCT_USER
(
   USERNAME
);

CREATE  INDEX SCINX_USER_PARTY_UUID ON SCT_USER
(
   PARTY_UUID
);


CREATE TABLE SCT_USER_ROLE
(
   USER_UUID            VARCHAR(32) NOT NULL,
   ROLE_UUID            VARCHAR(32) NOT NULL
)DEFAULT CHARSET=utf8;

ALTER TABLE SCT_ROLE_RESOURCE ADD CONSTRAINT SCFK_ROLE_RESOURCET_SCT_RESOURCE FOREIGN KEY (RESOURCE_UUID)
      REFERENCES SCT_RESOURCE (UUID) ON DELETE RESTRICT ON UPDATE RESTRICT;

ALTER TABLE SCT_ROLE_RESOURCE ADD CONSTRAINT SCFK_ROLE_RESOURCE_SCT_ROLE FOREIGN KEY (ROLE_UUID)
      REFERENCES SCT_ROLE (UUID) ON DELETE RESTRICT ON UPDATE RESTRICT;

ALTER TABLE SCT_USER_ROLE ADD CONSTRAINT SCFK_USER_ROLE_SCT_ROLE FOREIGN KEY (ROLE_UUID)
      REFERENCES SCT_ROLE (UUID);

ALTER TABLE SCT_USER_ROLE ADD CONSTRAINT SCFK_USER_ROLE_SCT_USER FOREIGN KEY (USER_UUID)
      REFERENCES SCT_USER (UUID) ON DELETE RESTRICT ON UPDATE RESTRICT;

/*==============================================================*/
/* 安全结束                                                                                                                                             */
/*==============================================================*/
      
