create table official_summaries(
summary_no bigserial,
summary_open_id text,
summary_price integer,
summary_type_cd smallint default 1,
summary_update_ts timestamp,
summary_regist_ts timestamp,
summary_continue_account_id text,
summary_reward integer default 0,
mem_no integer,
summary_its timestamp default now(),
summary_uts timestamp default now(),
summary_mp smallint,
summary_withdrawals_flg boolean default false,
summary_withdrawals_ts timestamp,
summary_payment_type_cd smallint default 1,
med_no integer,
summary_session_id text,
CONSTRAINT official_summaries_pkey PRIMARY KEY (summary_no)
);

create table official_summaries_payments(
payment_no bigserial,
summary_no bigint,
payment_id text,
payment_date timestamp,
payment_its timestamp default now(),
payment_uts timestamp default now(),
CONSTRAINT official_summaries_payments_pkey PRIMARY KEY (payment_no)
);

ALTER TABLE ONLY official_summaries_payments
    ADD CONSTRAINT official_summaries_payments_summary_no_fkey FOREIGN KEY (summary_no) REFERENCES official_summaries(summary_no) ON DELETE CASCADE;

CREATE INDEX official_summaries_payments_payment_date_idx on official_summaries_payments(payment_date);

create table withdrawals_histories(
wd_no serial,
wd_its timestamp default now(),
wd_uts timestamp default now(),
CONSTRAINT withdrawals_histories_pkey PRIMARY KEY (wd_no)
);

create table withdrawals_histories_data(
data_no bigserial,
wd_no integer,
summary_no bigint,
CONSTRAINT withdrawals_histories_data_pkey PRIMARY KEY (data_no)
);

ALTER TABLE ONLY withdrawals_histories_data
    ADD CONSTRAINT withdrawals_histories_data_wd_no_fkey FOREIGN KEY (wd_no) REFERENCES withdrawals_histories(wd_no) ON DELETE CASCADE;

ALTER TABLE ONLY withdrawals_histories_data
    ADD CONSTRAINT withdrawals_histories_data_summary_no_fkey FOREIGN KEY (summary_no) REFERENCES official_summaries(summary_no) ON DELETE CASCADE;

CREATE INDEX members_deals_sp_kessai_id_idx on members_deals(sp_kessai_id);

