"missing the right parenthesis" in Oracle SQL - sql

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.

Related

Creating a SQL database script for fictional restaurant

im making a fictional database for a ficitonal restaurant. I managed to write most of SQL script myself and it works. Like creating tables, inserting fake data. However I am not sure how to connect a meal to an order and table, and bill to a meal, and all of that to a customer.
Here is my script (the commented out foreign keys are what i dont know how to connect, or how to edit them so its all connected as i mentioned above):
DROP DATABASE IF EXISTS restaurantDB;
CREATE DATABASE restaurantDB;
USE restaurantDB;
CREATE TABLE `bill` (
`billID` int NOT NULL AUTO_INCREMENT,
`payAmount` varchar(6) NOT NULL,
PRIMARY KEY (`billID`)
);
INSERT INTO `bill` VALUES (1, '$258');
CREATE TABLE `customer` (
`customerID` int NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`customerID`)
);
INSERT INTO `customer` VALUES (1, 'Bruce', 'Lee', '420420589');
CREATE TABLE `meal` (
`mealID` int NOT NULL AUTO_INCREMENT,
`sides` varchar(255) NULL,
`main` varchar(255) NULL,
`beverage` varchar(255) NOT NULL,
PRIMARY KEY (`mealID`)
);
INSERT INTO `meal` VALUES (1, 'potatos', 'steak', 'wine');
CREATE TABLE `order` (
`orderID` int NOT NULL,
`time` datetime(2) NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`orderID`)
);
INSERT INTO `order` VALUES (1, '21-09-27 13:36:06');
CREATE TABLE `reservation` (
`reservationID` int NOT NULL AUTO_INCREMENT,
`checkIn` datetime(2) NOT NULL,
`checkOut` datetime(2) NOT NULL,
PRIMARY KEY (`reservationID`)
);
INSERT INTO `reservation` VALUES (1, '21-09-27 13:24:06', '21-09-27 14:17:23');
CREATE TABLE `table` (
`tableID` int NOT NULL AUTO_INCREMENT,
`numOfSeats` int NOT NULL,
PRIMARY KEY (`tableID`)
);
INSERT INTO `table` VALUES (1, 1);
/* ALTER TABLE `table` ADD CONSTRAINT `fk_bill_table_1` FOREIGN KEY (`tableID`) REFERENCES `table` (`billID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_table_1` FOREIGN KEY (`orderID`) REFERENCES `table` (`tableID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_meal_1` FOREIGN KEY (`orderID`) REFERENCES `meal` (`mealID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_bill_1` FOREIGN KEY (`orderID`) REFERENCES `bill` (`billID`);
ALTER TABLE `reservation` ADD CONSTRAINT `fk_reservation_customer_1` FOREIGN KEY (`reservationID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `table` ADD CONSTRAINT `fk_table_reservation_1` FOREIGN KEY (`tableID`) REFERENCES `reservation` (`reservationID`); */
Here is also a picture of the diagram (not sure if it makes sense connected like i have it currently tho):
Thanks to anyone who will be able to explain and help make the connections.
Okay, so finally got it working. Thanks #Austin.
Actually the only issue I had was with the datetime(2) it was just supposed to say datetime.
However #Austin showed me a better alternative, so I will create also a second, extended database with the relations #Austin suggested and add onto that.

Is there an alternative to timestamp in oracle to show the time?

I am wondering whether or not there is an alternative to timestamp to display the time as well as date?
I don't really want to use timestamp as it confuses me and I am not sure if I have done it right anyway.
My code can be seen below:
--CREATE SCRIPTS
/*put your create scripts here – your script should not commented out*/
-- this is creating a table called Project that contains 3 variables, the primary key being ProjectID
CREATE TABLE Project
(
Proj_ID integer,
Proj_Name varchar(10),
Proj_Start_Date date,
primary key (Proj_ID)
);
-- this is creating a table called Bug that has 4 variables, BugID being the primary key
CREATE TABLE Bug
(
Bug_ID integer,
Bug_Type varchar(20),
Bug_Desc varchar(20),
Bug_Time timestamp(4),
primary key(Bug_ID)
);
-- this is creating a table called Bug_Project with 2 variables; BugID and ProjectID which combine and make a composite key
CREATE TABLE Bug_Project
(
Bug_ID integer,
Proj_ID integer,
primary key(Bug_ID, Proj_ID),
foreign key(Bug_ID) references Bug (Bug_ID),
foreign key(Proj_ID) references Project (Proj_ID)
);
CREATE TABLE Engineer
(
Engineer_ID integer,
Engineer_Name varchar(10),
Engineer_Type varchar(20),
primary key (Engineer_ID)
);
CREATE TABLE Fix_Allocation
(
Engineer_ID integer,
Bug_ID integer,
primary key(Engineer_ID, Bug_ID),
foreign key(Engineer_ID) references Engineer (Engineer_ID),
foreign key(Bug_ID) references Bug (Bug_ID)
);
CREATE TABLE Test_Allocation
(
Engineer_ID integer,
Bug_ID integer,
primary key(Engineer_ID, Bug_ID),
foreign key(Engineer_ID) references Engineer (Engineer_ID),
foreign key(Bug_ID) references Bug (Bug_ID)
);
CREATE TABLE Note
(
Engineer_ID integer,
Bug_ID integer,
Note_author varchar(10),
Note_contents varchar(20),
primary key(Engineer_ID, Bug_ID),
foreign key(Engineer_ID) references Engineer (Engineer_ID),
foreign key(Bug_ID) references Bug (Bug_ID)
);
COMMIT;
--INSERT SCRIPTS
/*put your insert scripts here – your script should not commented out */
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (00, 'Project 1', DATE '1900-02-14');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (01, 'Project 2', DATE '1950-12-11');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (02, 'Project 3', DATE '1974-07-01');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (03, 'Project 4', DATE '2000-07-22');
INSERT INTO Project(Proj_ID, Proj_Name, Proj_Start_Date) VALUES (04, 'Project 5', DATE '2012-03-19');
INSERT INTO Bug(Bug_ID, Bug_Type, Bug_Desc, Bug_Time) VALUES (00, 'BugType1', 'Bug Description', timestamp '1997-01-31 09:26:50.12' );
INSERT INTO Bug VALUES ();
INSERT INTO Bug VALUES ();
INSERT INTO Bug_Project VALUES ();
INSERT INTO Bug_Project VALUES ();
INSERT INTO Bug_Project VALUES ();
INSERT INTO Engineer VALUES (00, "James", "Tester");
INSERT INTO Engineer VALUES (01, "Jeff", "Fixer");
INSERT INTO Engineer VALUES (02, "Jacob", "Fixer");
INSERT INTO Engineer VALUES (03, "John", "Tester");
INSERT INTO Fix_Allocation VALUES ();
INSERT INTO Fix_Allocation VALUES ();
INSERT INTO Fix_Allocation VALUES ();
INSERT INTO Test_Allocation VALUES ();
INSERT INTO Test_Allocation VALUES ();
INSERT INTO Test_Allocation VALUES ();
INSERT INTO Note VALUES ();
INSERT INTO Note VALUES ();
INSERT INTO Note VALUES ();
COMMIT;
--SELECT SCRIPT
/*put your select scripts here (with indication of which query is answered) – your script should not commented out
-- Query 1: List of all the bugs, and their details.
SELECT * FROM Bug;
-- Query 2: List of all bugs, and their notes.
-- Query 3: List of all bugs, with their notes, and the engineers who have written them; sorted by name of engineer.
-- Query 4: List the bugs and how much cumulative time (in hours) they have taken; ordered by time taken.
-- Query 5: The bug that has taken most time to fix, and the projects it is connected to.
COMMIT;
--DROP SCRIPT
/*put your drop scripts here (in the correct order)– your script should not commented out
DROP TABLE Note;
DROP TABLE Test_Allocation;
DROP TABLE Fix_Allocation;
DROP TABLE Engineer;
DROP TABLE Bug_Project;
DROP TABLE Bug;
DROP TABLE Project;
COMMIT;
here is the outcome of the first bug insert statement, also trying to get rid of all the 0s.
Any help would be greatly appreciated!
The DATE data type stores time and date. Read Oracle Docs - DATE Data Type for more information.

SQL, Add a time field in a date column

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.

Making a Database table in SQL, weird error

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

Model a Table that depends on a set of values in referenced table that can or not be present at the same time

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)
);