I have a field that gives me time and it is expressed like this :
7:00 AM
the problem is that when i try to add it to the column that should contain time it gives me error because the format is wrong.
I tried to change the format using to_char but it is giving me an error saying that the function is not implemented.
here is the code.
CREATE TABLE call_info
(
call_id NUMBER(4)
CONSTRAINT cio_call_id_CK CHECK (call_id >= 1000 AND call_id <= 9999),
report_first VARCHAR2(10),
report_last VARCHAR2(11) CONSTRAINT cio_report_last_NN NOT NULL,
report_date DATE CONSTRAINT cio_report_date_NN NOT NULL,
report_time DATE CONSTRAINT cio_report_time_NN NOT NULL,
problem_code NUMBER(1) CONSTRAINT cio_problem_code_NN NOT NULL,
service_code VARCHAR2(4),
CONSTRAINT cio_call_id_PK PRIMARY KEY (call_id),
CONSTRAINT cio_problem_code_FK FOREIGN KEY (problem_code)
REFERENCES problems(code),
CONSTRAINT cio_service_code_FK FOREIGN KEY (service_code)
REFERENCES services(code)
)
;
BEGIN
INSERT INTO call_info
VALUES ('1102','Bill','Madden',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:00 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1103','Anita','Verno',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:01 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1200','Emily','Vandalovsky',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:45 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1111','Gary','Correa',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('8:10 PM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1101','Mickey','Mouse',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:00 PM','hh:mi AM'),'hh:mi-AM'),'6','PSEG');
INSERT INTO call_info
VALUES ('1012','Minnie','Mouse',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:21 PM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1013','Goofy','Disney',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:47 PM','hh:mi AM'),'hh:mi-AM'),'5','OEM');
INSERT INTO call_info
VALUES ('1040','Donald','Duck',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('2:34 AM','hh:mi AM'),'hh:mi-AM'),'4','OEM');
INSERT INTO call_info
VALUES ('1501','Cinderella','Disney',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:15 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1506','Ernie','Sesame',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:16 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1007','Burt','Sesame',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:18 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1081','Bruce','Springsteen',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('4:10 AM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1910','Chris','Christie',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('4:25 AM','hh:mi AM'),'hh:mi-AM'),'7','OEM');
INSERT INTO call_info
VALUES ('1010','Mitt','Romney',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('5:15 AM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1015','Barack','Obama',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('5:17 AM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1019','Bruce','Wayne',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:57 AM','hh:mi AM'),'hh:mi-AM'),'5','OEM');
INSERT INTO call_info
VALUES ('1314','Minas','Kousoulis',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('8:01 AM','hh:mi AM'),'hh:mi-AM'),'4','WTR');
END;
Please note this the date datatype cannot be changed to a different one.
Thank you for the attention.
Danilo
Why are you storing the time in a separate field? Are you aware that DATE has a "time" component? Separating the fields almost never makes sense:
CREATE TABLE call_info (
call_id NUMBER(4)
CONSTRAINT cio_call_id_CK CHECK (call_id >= 1000 AND call_id <= 9999),
report_first VARCHAR2(10),
report_last VARCHAR2(11) CONSTRAINT cio_report_last_NN NOT NULL,
report_datetime DATE CONSTRAINT cio_report_date_NN NOT NULL,
problem_code NUMBER(1) CONSTRAINT cio_problem_code_NN NOT NULL,
service_code VARCHAR2(4),
CONSTRAINT cio_call_id_PK PRIMARY KEY (call_id),
CONSTRAINT cio_problem_code_FK FOREIGN KEY (problem_code)
REFERENCES problems(code),
CONSTRAINT cio_service_code_FK FOREIGN KEY (service_code)
REFERENCES services(code)
);
Then I would suggest inserting values using Oracles TIMESTAMP keyword:
INSERT INTO call_info (call_id, report_first, report_last, report_datetime, problem_code, service_code)
VALUES (1102, 'Bill', 'Madden', TIMETAMP '2017-10-29 19:00:00',
2, 'PSEG');
Note that I also removed the single quotes around the numbers and the insert lists all the columns.
Related
Trying to insert data into a table but keep getting the following error: The INSERT statement conflicted with the CHECK constraint "CK__etudiant__dateNa__3D5E1FD2". The conflict occurred in database "gestionAbsEtud", table "dbo.etudiant", column 'dateNaissance'.
create table etudiant(
numInscription int primary key,
nom char(20),
prenom char(20),
sexe char check(sexe in('M','F')),
dateNaissance date check(datediff(year, datenaissance, getdate()) between 15 and 30 ),
email varchar(20),
adresse varchar(20),
idFiliere varchar(10) foreign key references filier);
go
and this is the value i want to add :
insert into etudiant values
(1,'elbaraa','HAMMAN','m','20001126','contact#baraa.tk','DI1');
List the columns explicitly:
insert into etudiant (numInscription, nom, prenom, sexe, dateNaissance, email, adresse)
values (1, 'elbaraa', 'HAMMAN', 'm', '20001126', 'contact#baraa.tk', 'DI1');
In general, this is a best practice.
Okay so here is the tables I'm referencing with an INSERT statement:
CREATE TABLE IF NOT EXISTS Order_Item
(
autogen INT auto_increment,
order_num_FK VARCHAR(20),
item_num_FK CHAR(20),
CONSTRAINT order_item_PK PRIMARY KEY (autogen),
CONSTRAINT order_item_FK1 FOREIGN KEY (order_num_FK)
REFERENCES Pizza_Order(order_num),
CONSTRAINT order_item_FK2 FOREIGN KEY (item_num_FK)
REFERENCES Pizza(item_num)
);
CREATE TABLE IF NOT EXISTS Pizza_Toppings
(
autogen_FK INT auto_increment,
item_num_FK CHAR(20),
CONSTRAINT Pizza_Toppings_FK1 FOREIGN KEY (autogen_FK)
REFERENCES Order_Item(autogen),
CONSTRAINT Pizza_Toppings_FK2 FOREIGN KEY (item_num_FK)
REFERENCES Toppings(item_num)
);
Here is the INSERT statement:
INSERT INTO Pizza_Toppings(autogen_FK, item_num_FK)
VALUES (1, "I10"), (1, "I12"), (2, "I14"),
(3, "I11"), (3, "I12"), (3, "I13"),
(4, "I12"), (4, "I13"), (5, "I14"),
(6, "I14"), (7, "I10"), (9, "I10"),
(10, "I10"), (10, "I11"), (10, "I12"),
(10, "I13"), (10, "I14"), (11, "I15");
error message:
Cannot add or update a child row:a foriegnkey constrain fails ("pizzadelivery_db.pizza_toppings_FK1" FOREIGN KEY ("autogen_FK) Refrences "order_item(autogen))
I would greatly appreciate some help... Thank you
On table Pizza_Toppings, column autogen_FK is auto_increment and you are passing values to it . seems like you have to remove "auto_increament" because since its a fk it doesn't make sense to be auto increment and it solves your issue.
Also the values you are inserting to autogen_FK , should exists in Order_Item (autogen)
you insert statement is correct , only values are not valid
see db<>fiddle here
I've uploaded all my code. I think I'm using Oracle SQL Developer, which isn't the right one I'm assuming?
It's for a university assignment.
--CREATESCRIPTS
CREATE TABLE Bug --Creating the bug table.
(
Bug_ID VARCHAR2 (6)NOT NULL,
Bug_Name VARCHAR2 (100)NOT NULL,
Bug_Desc VARCHAR2 (255)NOT NULL,
Found_Date DATE NOT NULL,
CONSTRAINT PK_Bug PRIMARY KEY (Bug_ID) --creating the primary key for Bug.
);
CREATE TABLE Engineer --Creating the engineer table.
(
Engineer_ID VARCHAR2(6)NOT NULL,
Engineer_First VARCHAR2 (12)NOT NULL,
Engineer_Last VARCHAR2 (12)NOT NULL,
CONSTRAINT PK_Engineer PRIMARY KEY(Engineer_ID)
);
COMMIT;
CREATE TABLE Project --Creating the project table.
(
Project_ID VARCHAR2 (8)NOT NULL,
Project_Name VARCHAR2(10)NOT NULL,
CONSTRAINT PK_Project PRIMARY KEY (Project_ID)
);
COMMIT;
CREATE TABLE Note --Creating the note table.
(
Note_ID VARCHAR2(4) NOT NULL,
note_name VARCHAR2 (4)NOT NULL,
Note_Hours spent HOUR(4)NOT NULL,
Note_content VARCHAR2(254)NOT NULL,
Engineer_ID VARCHAR2(6)NOT NULL,
Bug_ID VARCHAR2(6)NOT NULL,
CONSTRAINT PK_Note PRIMARY KEY (Note_ID),
CONSTRAINT FK_BugNoteID FOREIGN KEY(bug_ID) REFERENCES Bug(Bug_ID),
CONSTRAINT FK_NoteEngineerID FOREIGN KEY(Engineer_ID) REFERENCES Engineer (Engineer_ID)
);
COMMIT;
CREATE TABLE Project_bug --Creating the Project bug table.
(
Bug_ID VARCHAR2(6),
Project_ID VARCHAR2(8),
CONSTRAINT FK_bug_project_bugID FOREIGN KEY (Bug_ID) REFERENCES bug (bug_ID),
CONSTRAINT FK_bug_project_projectID FOREIGN KEY (Project_ID) REFERENCES Project (Project_ID),
CONSTRAINT PK_Bug_Project PRIMARY KEY (Bug_ID, Project_ID),
);
COMMIT;
CREATE TABLE Allocation_fix--creating the allocation fix table.
(
Engineer_ID VARCHAR2(6),
Bug_ID VARCHAR2(6),
CONSTRAINT FK_Engineer_FixAllocatedID FOREIGN KEY (Engineer_ID) REFERENCES Engineer (Engineer_ID),
CONSTRAINT FK_Bug_FixAllocatedID FOREIGN KEY (Bug_ID) REFERENCES Bug (Bug_ID),
CONSTRAINT PK_Fix_Allocation PRIMARY KEY (Bug_ID, Engineer_ID)
);
COMMIT;
CREATE TABLE Allocation_test --creating the allocation test table.
(
Engineer_ID VARCHAR2(6),
bug_ID VARCHAR2 (8),
CONSTRAINT FK_Engineer_TestAllocatedID FOREIGN KEY (Engineer_ID) REFERENCES Engineer (Engineer_ID),
CONSTRAINT FK_Bug_TestAllocatedID FOREIGN KEY (Bug_ID) REFERENCES Bug (Bug_ID),
CONSTRAINT PK_Test_Allocation PRIMARY KEY (Bug_ID, Engineer_ID)
);
COMMIT;
--INSERT SCRIPTS
INSERT INTO bug VALUES ('B1','bug_1','first bug','6-DEC-98')
INSERT INTO bug VALUES ('B2','bug_2','second bug','7-DEC-98')
INSERT INTO bug VALUES ('B3','bug_3','third bug','8-DEC-98')
INSERT INTO bug VALUES ('B4','bug_4','fourth bug','9-DEC-98')
INSERT INTO bug VALUES ('B5','bug_5','fifth bug','10-DEC-98')
INSERT INTO bug VALUES ('B6','bug_6','sixth bug','11-DEC-98')
COMMIT;
INSERT INTO Engineer VALUES ('E1','Jones','Lee') --tester
INSERT INTO Engineer VALUES ('E2','Friend','Kane') --fixer
INSERT INTO Engineer VALUES ('E3','Ingham','Darcie') --tester
INSERT INTO Engineer VALUES ('E4','Jones','Carly') --fixer
INSERT INTO Engineer VALUES ('E5','Evergreen','Damien') --tester
INSERT INTO Engineer VALUES ('E6', 'Danes', 'Lucy') --showing the optionality of having an engineer that isnt a tester or a fixer
COMMIT;
INSERT INTO Note VALUES ('N1','Test1','4','testing was carried out by E1')
INSERT INTO Note VALUES ('N2','Fix1','5','fix was applied by E2')
INSERT INTO Note VALUES ('N3','Test2','7','testing was carried out by E1')
INSERT INTO Note VALUES ('N4','Fix2','3','fix was applied by E3')
INSERT INTO Note VALUES ('N5','Test3','3','testing was carried out by E4')
INSERT INTO Note VALUES ('N6','Fix3','1','fix was applied by E5')
INSERT INTO Note VALUES ('N7','Test4','5','testing was carries out by E4')
INSERT INTO Note VALUES ('N8','Fix4','9','fix was applied by E5')
COMMIT;
INSERT INTO project VALUES ('P1', 'Bethesda')
INSERT INTO project VALUES ('P2', 'Activsion')
INSERT INTO project VALUES ('P3', 'Valve')
INSERT INTO project VALUES ('P4', 'Mojang')
INSERT INTO project VALUES ('P5', 'Blizzard')
INSERT INTO project VALUES ('P6', 'Rockstar')
COMMIT;
INSERT INTO Project_bug VALUES ('B1', 'P1')
INSERT INTO Project_bug VALUES ('B2', 'P2')
INSERT INTO Project_bug VALUES ('B3', 'P3')
INSERT INTO Project_bug VALUES ('B4', 'P4')
INSERT INTO Project_bug VALUES ('B5', 'P5')
INSERT INTO Project_bug VALUES ('B6', 'P6')
COMMIT;
INSERT INTO Allocation_fix VALUES ('E1','B1' ) --this data set shows the optionality of engineers being "fixers" or "testers"
INSERT INTO Allocation_fix VALUES ('E2','B2')
INSERT INTO Allocation_fix VALUES ('E3','B3')
INSERT INTO Allocation_fix VALUES ('E4','B4')
INSERT INTO Allocation_fix VALUES ('E5','B5')
INSERT INTO Allocation_fix VALUES ('E1','B6')
COMMIT;
INSERT INTO Allocation_test VALUES ('E1','B1') -- this data set shows the optionality of engineers being "fixers" or "testers"
INSERT INTO Allocation_test VALUES ('E2','B2')
INSERT INTO Allocation_test VALUES ('E3','B3')
INSERT INTO Allocation_test VALUES ('E4','B4')
INSERT INTO Allocation_test VALUES ('E5','B5')
INSERT INTO Allocation_test VALUES ('E1','B6')
COMMIT;
--SELECT SCRIPTS
-- 1: List of all the bugs, and their details.
SELECT*From bug;
-- Query 2 : List of all bugs, and their notes.
SELECT bug_type, note_id, note_content
FROM bug,note
WHERE bug.bug_id=note.bug_id;
-- Query 3 : List of all bugs, with their notes, and the engineers who have written them; sorted by name of engineer.
SELECT bug.bug_id,bug_type, note_id, note_content, engineer.engineer_id, engineer_firstname, engineer_lastname
FROM bug, note, engineer
WHERE bug.bug_id=note.bug_id AND note.engineer_id=engineer.engineer_id
ORDER BY engineer_lastname ASC;
--Query 4: List the bugs and how much cumulative time (in hours) they have taken; ordered by time taken.
SELECT bug.bug_id, SUM(note.hours_spent)
FROM note,bug
WHERE bug.bug_id=note.bug_id
GROUP BY bug.bug_id
ORDER BY SUM(note.hours_spent) ASC;
--Query 5 : The bug that has taken most time to fix and the projects it is connected to.
SELECT bug_taken_most_time_to_fix.bug_id, bug_project.project_id, bug_taken_most_time_to_fix."Total_Hours" FROM
(
SELECT bug.bug_id, SUM(note.hours_spent) AS "Total_Hours"
FROM note,bug
WHERE bug.bug_id=note.bug_id
GROUP BY bug.bug_id
ORDER BY SUM(note.hours_spent) DESC FETCH FIRST ROW ONLY
)
bug_taken_most_time_to_fix, bug_project
WHERE bug_taken_most_time_to_fix.bug_id = bug_project.bug_id;
--DROP SCRIPTS
DROP TABLE allocation_test;
DROP TABLE allocation_fix;
DROP TABLE Project_bug;
DROP TABLE Note;
DROP TABLE Engineer;
DROP TABLE Bug;
COMMIT;
You have some syntax error in your CREATE table syntax of note table. -
CREATE TABLE Note --Creating the note table.
(
Note_ID VARCHAR2(4) NOT NULL,
note_name VARCHAR2 (4)NOT NULL,
Note_Hours_spent NUMBER(4)NOT NULL,
Note_content VARCHAR2(254)NOT NULL,
Engineer_ID VARCHAR2(6)NOT NULL,
Bug_ID VARCHAR2(6)NOT NULL,
CONSTRAINT PK_Note PRIMARY KEY (Note_ID),
CONSTRAINT FK_BugNoteID FOREIGN KEY(bug_ID) REFERENCES Bug(Bug_ID),
CONSTRAINT FK_NoteEngineerID FOREIGN KEY(Engineer_ID) REFERENCES Engineer (Engineer_ID)
);
And have a comma at the last of Project_bug table -
CREATE TABLE Project_bug --Creating the Project bug table.
(
Bug_ID VARCHAR2(6),
Project_ID VARCHAR2(8),
CONSTRAINT FK_bug_project_bugID FOREIGN KEY (Bug_ID) REFERENCES bug (bug_ID),
CONSTRAINT FK_bug_project_projectID FOREIGN KEY (Project_ID) REFERENCES Project (Project_ID),
CONSTRAINT PK_Bug_Project PRIMARY KEY (Bug_ID, Project_ID)
);
Last your insert statement into note table is giving error in this fiddle as you do not have enough values to insert into that table.
This is the code for the Database.Its giving me an error that for every TABLE created something has already taken that name, one of the error messages is at the end of the post.
COMMIT;
CREATE TABLE Team (
Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
Team_Name NVARCHAR2(40) NOT NULL,
Team_Manager VARCHAR(20),
CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
);
COMMIT;
insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES ('Red Sox', 'Jon');
insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES('White Sox', 'Tony');
CREATE TABLE Driver (
Driver_id INTEGER NOT NULL,
Teams_ID INTEGER,
Driver_age INTEGER,
Driver_Name NVARCHAR2(20) NOT NULL,
CONSTRAINT DriverAge CHECK (Driver_age BETWEEN '19' AND '65'),
CONSTRAINT driverpk PRIMARY KEY(Driver_ID),
CONSTRAINT Teams_PK FOREIGN KEY (Teams_ID) REFERENCES Team(Team_ID)
);
COMMIT;
insert into Driver(DRIVER_AGE,DRIVER_NAME) VALUES ('21', 'Jon');
insert into Driver(DRIVER_AGE, DRIVER_NAME) VALUES ('20', 'Tony');
CREATE TABLE Participation (
TeamName_ID INTEGER,
Driver_ID INTEGER,
PointsEarned INTEGER,
CONSTRAINT TeamName_FK FOREIGN KEY (TeamName_ID) REFERENCES Team(Team_ID),
CONSTRAINT Driver_FK FOREIGN KEY(Driver_ID) REFERENCES Driver(Driver_ID)
);
COMMIT;
insert into Participation(PARTICIPATION_POINTS_EARNED) VALUES (150);
CREATE TABLE Finish (
Racer_ID INTEGER,
Finish_Position INTEGER NOT NULL,
Fishish_Result VARCHAR(50) NOT NULL,
CONSTRAINT Racer_FK FOREIGN KEY (Racer_ID) REFERENCES Driver(Driver_id)
);
COMMIT;
insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('1', 'Winner');
insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('3', 'Third Place');
CREATE TABLE RaceComponent (
RC_ID INTEGER NOT NULL,
Driver1_ID INTEGER,
RC_Type VARCHAR(25),
CONSTRAINT Rcpk PRIMARY KEY(RC_ID),
CONSTRAINT Driver1_FK FOREIGN KEY (Driver1_ID) REFERENCES Driver(Driver_ID)
);
COMMIT;
insert into RaceComponent(RC_TYPE) VALUES ('Hot Wheels');
insert into RaceComponent(RC_TYPE) VALUES ('Tonka');
CREATE TABLE Race (
Race_Id INTEGER,
RC_ID INTEGER,
Race_Title VARCHAR(30) NOT NULL,
Race_Location VARCHAR(50) NOT NULL,
Race_Date DATE,
CONSTRAINT RACPEK PRIMARY KEY(RACE_ID),
CONSTRAINT RC_FK FOREIGN KEY (RC_ID) REFERENCES RaceComponent(RC_ID)
);
COMMIT;
insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Tonys race', 'Moncton', DATE '2016-04-25');
insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Mikes Racing', 'San-Francisco', DATE '2015-04-25');
ERROR:
Commit complete.
Error starting at line : 3 in command -
CREATE TABLE Team (
Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
Team_Name NVARCHAR2(40) NOT NULL,
Team_Manager VARCHAR(20),
CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
)
Error report -
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
Commit complete.
I thought it might be because I am calling the Foreign keys the same name as their primary key value, but im not 100% sure what is causing it.
If the table was created during an earlier run of your script, you will get that error. It is always good to check to see if your table already exists before trying to create it. Here is a good snippet of code from StackOverflow for checking to see if the table already exists or not.
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'TheSchema'
AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END
I have a table 'medical_observations' that in one field references other table 'sypstoms_at_arriving' that describes a list of possible symptoms.
CREATE TABLE `patients`(
id_patient INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(25) ,
address VARCHAR(50) ,
CONSTRAINT `uc_Info_Patient` UNIQUE (`id_patient`)
);
INSERT INTO `patients` values (1,'joe','joe´s address');
INSERT INTO `patients` values (2,'moe','moe´s address');
INSERT INTO `patients` values (3,'karl','karle´s address');
INSERT INTO `patients` values (4,'lenny','lenny´s address');
CREATE TABLE `symptoms_at_arrival` (
symptom_at_arrival varchar(30) primary key
);
INSERT INTO `symptoms_at_arrival` values ('vomit');
INSERT INTO `symptoms_at_arrival` values ('urine');
INSERT INTO `symptoms_at_arrival` values ('dizziness');
INSERT INTO `symptoms_at_arrival` values ('convulsion');
CREATE TABLE `medical_observations`(
id_medical_observation INTEGER NOT NULL PRIMARY KEY,
id_patient INTEGER NOT NULL,
symptom_at_arrival VARCHAR(30),
FOREIGN KEY (id_patient) references `patients` (id_patient),
FOREIGN KEY (symptom_at_arrival) references `symptoms_at_arrival` (symptom_at_arrival ),
CONSTRAINT `uc_Info_medical_Observation` UNIQUE (`id_medical_observation`,`id_patient`)
);
My doubt is how to model or store th case when patient has several symptoms... and not just one.
If that would be the case the name of symptom would be enough...
But if patient show several symptoms at the same time?
Update
I have done a sqlfiddle, I was thinking to add a kind of table with 1's and 0's representing if patient shows certain symptom... Would that be correct?
You'll have to make connection in the foreign keys
|patient| |medical_observations| |symptoms_at_arriving|
--------- ---------------------- ----------------------
**id** 1 ----| **id_medical_observation** |-----1 **id**
name |-M **id_patient** | symptom_at_arrival
**symptom_at_arrival** M---|
Try this, don't have mysql here to test, making table multi primary key to support multiple symptoms at same time
CREATE TABLE `symptoms_at_arriving` (
id integer not null primary key autoincrement,
symptom_at_arrival varchar(30)
);
INSERT INTO `symptom_at_arrival' values ('vomit');
INSERT INTO `symptom_at_arrival` values ('urine');
INSERT INTO `symptom_at_arrival` values ('dizziness');
INSERT INTO `symptom_at_arrival` values ('convulsion');
CREATE TABLE `medical_observations`(
id_medical_observation INTEGER NOT NULL,
id_patient INTEGER NOT NULL,
symptom_at_arrival integer not null,
FOREIGN KEY (id_patient) references `patients` (id_patient),
FOREIGN KEY (symptom_at_arrival) references `symptoms_at_arriving` (symptom_at_arrival,
PRIMARY KEY (id_medical_observation, id_patient, symptom_at_arrival)
);