CREATE DATABASE IF NOT EXISTS my_address_book;
USE my_address_book;
DROP TABLE IF EXISTS person;
CREATE TABLE person(
p_id INTEGER PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100)
);
DROP TABLE IF EXISTS event_type;
CREATE TABLE event_type(
et_id INTEGER PRIMARY KEY,
event_type VARCHAR(100)
);
DROP TABLE IF EXISTS event;
CREATE TABLE event(
event_id INTEGER PRIMARY KEY,
p_id INTEGER,
et_id INTEGER,
event_date VARCHAR(20),
FOREIGN KEY (p_id) REFERENCES person (p_id) on DELETE CASCADE,
FOREIGN KEY (et_id) REFERENCES event_type (et_id) on DELETE CASCADE
);
DROP TABLE IF EXISTS phone;
CREATE TABLE phone(
pno_id INTEGER PRIMARY KEY,
p_id INTEGER,
phone_number VARCHAR(100),
FOREIGN KEY (p_id) REFERENCES person (p_id) on DELETE CASCADE
);
DROP TABLE IF EXISTS address_type;
CREATE TABLE address_type(
at_id INTEGER PRIMARY KEY,
address_type VARCHAR(100)
);
DROP TABLE IF EXISTS address;
CREATE TABLE address(
a_id INTEGER PRIMARY KEY,
at_id INTEGER,
p_id INTEGER,
address VARCHAR(100),
ps_code VARCHAR(20),
town VARCHAR(100),
country VARCHAR(100),
FOREIGN KEY (p_id) REFERENCES person (p_id) on DELETE CASCADE,
FOREIGN KEY (at_id ) REFERENCES address_type (at_id ) on DELETE CASCADE
);
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts(
c_id INTEGER PRIMARY KEY,
a_id INTEGER,
p_id INTEGER,
pno_id INTEGER,
event_id INTEGER,
FOREIGN KEY (p_id) REFERENCES person (p_id) on DELETE CASCADE,
FOREIGN KEY (a_id ) REFERENCES address (a_id ) on DELETE CASCADE,
FOREIGN KEY (pno_id) REFERENCES phone (pno_id) on DELETE CASCADE,
FOREIGN KEY (event_id ) REFERENCES event (event_id ) on DELETE CASCADE
);