CREATE TABLE roles(
role_id SERIAL NOT NULL,
role_name VARCHAR(20) UNIQUE NOT NULL,
CONSTRAINT roles_pk PRIMARY KEY (role_id)
);

DESCRIBE roles;


CREATE TABLE user_roles(
u_id INTEGER NOT NULL,
role_id INTEGER NOT NULL,
CONSTRAINT user_roles_pk PRIMARY KEY(u_id,role_id)
);


-- foreign key to be added to this table in alter table form  
CREATE TABLE users(
u_id SERIAL NOT NULL,
user_id_number CHAR(12) UNIQUE NOT NULL,
user_name VARCHAR(70) NOT NULL,
user_password VARCHAR(68) NOT NULL,
-- fk to uder_account 
CONSTRAINT user_table_pk PRIMARY KEY(u_id)
);


ALTER TABLE user_roles ADD CONSTRAINT user_lones_U_fk_to_user FOREIGN KEY(u_id) REFERENCES users(u_id);
ALTER TABLE user_roles ADD CONSTRAINT user_lones_R_fk_to_user FOREIGN KEY(role_id) REFERENCES roles(role_id);


INSERT INTO roles(role_id, role_name) VALUES (1, 'ROLE_admin');

SELECT * FROM users;
SELECT * FROM user_roles;
SELECT * FROM roles;er



DROP TABLE IF EXISTS user_roles;
DROP TABLE IF EXISTS users;

SELECT * FROM users;

ALTER TABLE users 

-- ALTER TABLE users
-- RENAME COLUMN user_name TO user_id_number;

-- ALTER TABLE users
-- ADD COLUMN user_name varchar(60) NOT NULL;
CREATE TABLE race_venue(
	race_venue_id SERIAL NOT NULL,
	venue VARCHAR(10) UNIQUE NOT NULL,
	CONSTRAINT race_venue_id_pk PRIMARY KEY (race_venue_id)
);


CREATE TABLE race_number(
	race_id SERIAL NOT NULL,
	sys_data_only DATE NOT NULL,
	race_venue_id INTEGER NOT NULL,
	number_of_races SMALLINT,
	CoNSTRAINT race_number_id_pk PRIMARY KEY (race_id)
);

CREATE TABLE ip_to_device(
	ip_address varchar(20) UNIQUE NOT NULL,
	device_id INTEGER NOT NULL
);

ALTER TABLE race_number 
ADD CONSTRAINT race_venue_id_fk 
FOREIGN KEY (race_venue_id) REFERENCES race_venue(race_venue_id); 


-- 


SELECT * FROM users;

CREATE TABLE race_venue(
	race_venue_id SERIAL NOT NULL,
	venue VARCHAR(10) UNIQUE NOT NULL,
	CONSTRAINT race_venue_id_pk PRIMARY KEY (race_venue_id)
);


CREATE TABLE race_number(
	race_id SERIAL NOT NULL,
	sys_data_only DATE NOT NULL,
	race_venue_id INTEGER NOT NULL,
	number_of_races SMALLINT NOT NULL,
	CONSTRAINT race_number_id_pk PRIMARY KEY (race_id)
);

ALTER TABLE race_number
ADD CONSTRAINT composit_constraint
UNIQUE (sys_data_only, race_venue_id);

CREATE TABLE ip_to_device(
	ip_address varchar(20) NOT NULL,
	device_id INTEGER NOT NULL,
	CONSTRAINT ip_to_device_pk PRIMARY KEY (ip_address)
);

DROP TABLE IF EXISTS race_number;


ALTER TABLE race_number 
ADD CONSTRAINT race_venue_id_fk 
FOREIGN KEY (race_venue_id) REFERENCES race_venue(race_venue_id); 


SELECT * FROM race_venue;
SELECT * FROM race_number;



