SQLBungler
Technical User
I'm VERY new to SQL and would really like some assistance in setting up a database.
My ER diagram is here:
I began to enter SQL into Microsoft Access but had quite a few problems. My code is listed below. I'm not sure if it's 100% correct, I imagine that it's not.
Thanks very much,
Sqlbungler.
My ER diagram is here:
I began to enter SQL into Microsoft Access but had quite a few problems. My code is listed below. I'm not sure if it's 100% correct, I imagine that it's not.
Code:
CREATE TABLE Assessment (
date_assessed DATETIME NOT NULL,
client_number INTEGER NOT NULL,
staff_assessor_name TEXT (30) NOT NULL,
type_of_facility TEXT(30) NOT NULL,
CONSTRAINT pkassessment PRIMARY KEY (date_assessed, client_number, type_of_facility),
CONSTRAINT fk1assessment FOREIGN KEY (client_number) REFERENCES Client,
CONSTRAINT fk2assessment FOREIGN KEY (type_of_facility) REFERENCES FacilityType
);
CREATE TABLE Client (
client_number INTEGER NOT NULL,
name TEXT(40),
date_of_birth DATETIME,
emergency_contact_number TEXT NOT NULL,
next_of_kin_name TEXT(40),
date_joined DATETIME,
CONSTRAINT pkclient PRIMARY KEY (client_number)
);
CREATE TABLE Equipment (
equipment_reference_number INTEGER NOT NULL,
brand_name TEXT (30),
model_number TEXT (30),
installation_date DATETIME,
type_of_facility TEXT (30),
CONSTRAINT pkequipment PRIMARY KEY (equipment_reference_number),
CONSTRAINT fkequipment FOREIGN KEY (type_of_facility) REFERENCES FacilityType
);
CREATE TABLE FacilityType (
type_of_facility TEXT(30) NOT NULL,
CONSTRAINT pkfacilitytype PRIMARY KEY (type_of_facility)
);
CREATE TABLE Usage (
client_number INTEGER NOT NULL,
date_of_visit DATETIME NOT NULL,
start_time TIME NOT NULL,
end_time TIME NOT NULL,
equipment_reference_number INTEGER NOT NULL,
CONSTRAINT pkusage PRIMARY KEY (start_time,date_of_visit,client_number, equipment_reference_number),
CONSTRAINT fk1usage FOREIGN KEY (client_number,date_of_visit,start_time) REFERENCES Visit,
CONSTRAINT fk2usage FOREIGN KEY (client_number) REFERENCES Client,
CONSTRAINT fk3usage FOREIGN KEY (equipment_reference_number) REFERENCES Equipment
);
CREATE TABLE Visit (
client_number INTEGER NOT NULL,
date_of_visit DATETIME NOT NULL,
entry_time TIME NOT NULL,
exit_time TIME,
CONSTRAINT pkvisit PRIMARY KEY (client_number,date_of_visit,entry_time),
CONSTRAINT fkvisit FOREIGN KEY (client_number) REFERENCES Client
);
Sqlbungler.