DROP DATABASE bank;
CREATE DATABASE bank;
USE bank;

CREATE TABLE usuario (
  id          int(10) NOT NULL AUTO_INCREMENT, 
  nombre      varchar(255) NOT NULL, 
  password    binary(32) NOT NULL, 
  salt        binary(16) NOT NULL, 
  role        varchar(255) DEFAULT 'Cliente' NOT NULL, 
  inserted_at timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE cliente (
  id              int(10) NOT NULL AUTO_INCREMENT, 
  rut             varchar(255) NOT NULL, 
  nombre          varchar(255) NOT NULL, 
  ciudad          varchar(255) NOT NULL, 
  inserted_at     timestamp DEFAULT current_timestamp() NOT NULL, 
  linea_sobregiro int(10) UNIQUE, 
  usuario         int(10) NOT NULL UNIQUE, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE linea_sobregiro (
  id          int(10) NOT NULL AUTO_INCREMENT, 
  sobregiro   int(10) NOT NULL, 
  limite      int(10) NOT NULL, 
  inserted_at timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE tarjeta_credito (
  id                   int(10) NOT NULL AUTO_INCREMENT, 
  limite_nacional      int(10) NOT NULL, 
  limite_internacional int(10) NOT NULL, 
  deuda_nacional       int(10) DEFAULT 0 NOT NULL, 
  deuda_internacional  int(10) DEFAULT 0 NOT NULL, 
  inserted_at          timestamp DEFAULT current_timestamp() NOT NULL, 
  cliente              int(10) NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE tarjeta_debito (
  id          int(10) NOT NULL AUTO_INCREMENT, 
  saldo       int(10) DEFAULT 0 NOT NULL, 
  inserted_at timestamp DEFAULT current_timestamp() NOT NULL, 
  cliente     int(10) NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE cuenta_corriente (
  id          int(10) NOT NULL AUTO_INCREMENT, 
  saldo       int(10) DEFAULT 0 NOT NULL, 
  inserted_at timestamp DEFAULT current_timestamp() NOT NULL, 
  cliente     int(10) NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE compra_tarjeta_credito (
  id              int(10) NOT NULL AUTO_INCREMENT, 
  monto           int(10) NOT NULL, 
  comentario      varchar(255), 
  inserted_at     timestamp DEFAULT current_timestamp() NOT NULL, 
  tarjeta_credito int(10), 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE giro_cuenta_corriente (
  id               int(10) NOT NULL AUTO_INCREMENT, 
  monto            int(10) NOT NULL, 
  comentario       varchar(255), 
  metodo           int(10), 
  cuenta_corriente int(10), 
  inserted_at      timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE deposito_cuenta_corriente (
  id               int(10) NOT NULL AUTO_INCREMENT, 
  monto            int(10) NOT NULL, 
  comentario       varchar(255), 
  metodo           int(10), 
  cuenta_corriente int(10), 
  inserted_at      timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE transferencia_cuenta_corriente (
  id                    int(10) NOT NULL AUTO_INCREMENT, 
  monto                 int(10) NOT NULL, 
  comentario            varchar(255), 
  cuenta_corriente_to   int(10), 
  cuenta_corriente_from int(10), 
  inserted_at           timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE metodo (
  id          int(10) NOT NULL AUTO_INCREMENT, 
  nombre      varchar(255) NOT NULL, 
  inserted_at timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE transferencia_tarjeta_debito (
  id                  int(10) NOT NULL AUTO_INCREMENT, 
  monto               int(10) NOT NULL, 
  comentario          varchar(255), 
  tarjeta_debito_to   int(10), 
  tarjeta_debito_from int(10), 
  inserted_at         timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE deposito_tarjeta_debito (
  id             int(10) NOT NULL AUTO_INCREMENT, 
  monto          int(10) NOT NULL, 
  comentario     varchar(255), 
  metodo         int(10), 
  tarjeta_debito int(10), 
  inserted_at    timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE giro_tarjeta_debito (
  id             int(10) NOT NULL AUTO_INCREMENT, 
  monto          int(10) NOT NULL, 
  comentario     varchar(255), 
  metodo         int(10), 
  tarjeta_debito int(10), 
  inserted_at    timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE pago_tarjeta_credito (
  id              int(10) NOT NULL AUTO_INCREMENT, 
  monto           int(10) NOT NULL, 
  comentario      varchar(255), 
  inserted_at     timestamp DEFAULT current_timestamp() NOT NULL, 
  tarjeta_credito int(10), 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
CREATE TABLE compra_tarjeta_debito (
  id             int(10) NOT NULL AUTO_INCREMENT, 
  monto          int(10) NOT NULL, 
  comentario     varchar(255), 
  tarjeta_debito int(10) NOT NULL, 
  inserted_at    timestamp DEFAULT current_timestamp() NOT NULL, 
  PRIMARY KEY (id)) CHARACTER SET UTF8;
ALTER TABLE cliente ADD CONSTRAINT FKcliente571606 FOREIGN KEY (linea_sobregiro) REFERENCES linea_sobregiro (id) ON DELETE Cascade;
ALTER TABLE tarjeta_credito ADD CONSTRAINT FKtarjeta_cr81196 FOREIGN KEY (cliente) REFERENCES cliente (id) ON DELETE Cascade;
ALTER TABLE cuenta_corriente ADD CONSTRAINT FKcuenta_cor278524 FOREIGN KEY (cliente) REFERENCES cliente (id) ON DELETE Cascade;
ALTER TABLE tarjeta_debito ADD CONSTRAINT FKtarjeta_de160614 FOREIGN KEY (cliente) REFERENCES cliente (id) ON DELETE Cascade;
ALTER TABLE cliente ADD CONSTRAINT FKcliente901955 FOREIGN KEY (usuario) REFERENCES usuario (id) ON DELETE Cascade;
ALTER TABLE deposito_cuenta_corriente ADD CONSTRAINT FKdeposito_c403217 FOREIGN KEY (metodo) REFERENCES metodo (id);
ALTER TABLE giro_cuenta_corriente ADD CONSTRAINT FKgiro_cuent832803 FOREIGN KEY (metodo) REFERENCES metodo (id);
ALTER TABLE deposito_tarjeta_debito ADD CONSTRAINT FKdeposito_t164090 FOREIGN KEY (metodo) REFERENCES metodo (id);
ALTER TABLE giro_tarjeta_debito ADD CONSTRAINT FKgiro_tarje519906 FOREIGN KEY (metodo) REFERENCES metodo (id);
ALTER TABLE compra_tarjeta_credito ADD CONSTRAINT FKcompra_tar218956 FOREIGN KEY (tarjeta_credito) REFERENCES tarjeta_credito (id) ON DELETE Set null;
ALTER TABLE pago_tarjeta_credito ADD CONSTRAINT FKpago_tarje245778 FOREIGN KEY (tarjeta_credito) REFERENCES tarjeta_credito (id) ON DELETE Set null;
ALTER TABLE transferencia_cuenta_corriente ADD CONSTRAINT FKtransferen788163 FOREIGN KEY (cuenta_corriente_to) REFERENCES cuenta_corriente (id) ON DELETE Set null;
ALTER TABLE deposito_cuenta_corriente ADD CONSTRAINT FKdeposito_c722018 FOREIGN KEY (cuenta_corriente) REFERENCES cuenta_corriente (id) ON DELETE Set null;
ALTER TABLE giro_cuenta_corriente ADD CONSTRAINT FKgiro_cuent986448 FOREIGN KEY (cuenta_corriente) REFERENCES cuenta_corriente (id) ON DELETE Set null;
ALTER TABLE transferencia_tarjeta_debito ADD CONSTRAINT FKtransferen939970 FOREIGN KEY (tarjeta_debito_to) REFERENCES tarjeta_debito (id) ON DELETE Set null;
ALTER TABLE deposito_tarjeta_debito ADD CONSTRAINT FKdeposito_t747169 FOREIGN KEY (tarjeta_debito) REFERENCES tarjeta_debito (id) ON DELETE Set null;
ALTER TABLE giro_tarjeta_debito ADD CONSTRAINT FKgiro_tarje431166 FOREIGN KEY (tarjeta_debito) REFERENCES tarjeta_debito (id) ON DELETE Set null;
ALTER TABLE transferencia_cuenta_corriente ADD CONSTRAINT FKtransferen295143 FOREIGN KEY (cuenta_corriente_from) REFERENCES cuenta_corriente (id) ON DELETE Set null;
ALTER TABLE transferencia_tarjeta_debito ADD CONSTRAINT FKtransferen713576 FOREIGN KEY (tarjeta_debito_from) REFERENCES tarjeta_debito (id) ON DELETE Set null;
ALTER TABLE compra_tarjeta_debito ADD CONSTRAINT FKcompra_tar267191 FOREIGN KEY (tarjeta_debito) REFERENCES tarjeta_debito (id);
INSERT INTO usuario(id, nombre, password, salt, role, inserted_at) VALUES (1, 'ryuuji', 0xEC65288218545FB29831D2025CEE99704C900B4B8E0B7DB35A610E2D1673BF35, 0x6260AD9369D01E48EC34F0315FAD3565, 'Admin', current_timestamp());
INSERT INTO metodo(id, nombre) VALUES (1, 'Cheque');
INSERT INTO metodo(id, nombre) VALUES (2, 'Vale Vista');
INSERT INTO metodo(id, nombre) VALUES (3, 'Efectivo');
INSERT INTO metodo(id, nombre) VALUES (4, 'Caja');

