ERROR: insert or update on table "flight" violates foreign key constraint "flight_flight_num_fkey"
DETAIL: Key (flight_num)=(100) is not present in table "booked_on".
drop table if exists CUSTOMER;
drop table if exists BOOKED_ON;
drop table if exists PLANE_TYPE;
drop table if exists EMPLOYEE;
drop table if exists PILOT;
drop table if exists ASSIGNED_TO;
drop table if exists AIRCRAFT;
drop table if exists DEPARTURE;
drop table if exists FLIGHT;
CREATE TABLE CUSTOMER(
Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20) NOT NULL,
Street VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL,
Zip_Code INT NOT NULL,
Phone VARCHAR(20),
PRIMARY KEY (Name) );
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
CREATE TABLE PLANE_TYPE(
Model_no VARCHAR(20) NOT NULL,
Manufacturer VARCHAR(20) NOT NULL,
Unique(Model_no),
PRIMARY KEY (Model_no, Manufacturer) );
CREATE TABLE EMPLOYEE(
employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Salary INT NOT NULL,
PRIMARY KEY (employee_no));
CREATE TABLE PILOT(
Employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
License_Date DATE NOT NULL,
Plane_model_can_fly VARCHAR(20) NOT NULL,
UNIQUE (License_Date),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no));
CREATE TABLE ASSIGNED_TO(
Employee_no INT NOT NULL,
dep_date VARCHAR(20) NOT NULL,
flight_num INT,
PRIMARY KEY (Employee_no),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON(Flight_num)
);
CREATE TABLE AIRCRAFT(
Serial_no VARCHAR(50) ,
Model_no VARCHAR(50),
PRIMARY KEY (Serial_no),
FOREIGN KEY (Model_no) REFERENCES PLANE_TYPE(Model_no)
);
CREATE TABLE DEPARTURE(
Departure_date DATE NOT NULL,
Flight_number INT,
Aircraft_Serial_no VARCHAR(50) ,
Employee_no INT NOT NULL,
PRIMARY KEY(Flight_number),
FOREIGN KEY (Flight_number) REFERENCES BOOKED_ON (Flight_num),
FOREIGN KEY (employee_no) REFERENCES EMPLOYEE(Employee_no),
FOREIGN KEY (Aircraft_Serial_no) REFERENCES AIRCRAFT(Serial_no));
CREATE TABLE FLIGHT(
flight_num INT,
origin VARCHAR(20) NOT NULL,
dest VARCHAR(20) NOT NULL,
dep_time VARCHAR(20) NOT NULL,
arr_time VARCHAR(20) NOT NULL,
PRIMARY KEY(flight_num),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON (Flight_num)
);
/*Populate Customer table*/
INSERT INTO CUSTOMER VALUES
('Rose', 'Parker', '123 Thomas St', 'Toledo', 'OH', 57556, '801-556-2239'),
('Jessica', 'Jones', '234 Pines St', 'Los Angeles', 'CA', 34898, '801-552-2943'),
('John', 'Peters', '345 Star St', 'Raleigh', 'NC', 79999, '801-393-2230'),
('Bryan', 'Brown', '435 Palm St', 'Miami', 'FL', 30533, '801-933-2320'),
('Mark', 'Williams', '348 Andrew St', 'Fort Lauderdale', 'FL', 33318, '801-343-2320'),
('Carol', 'Phillips', '395 Pine St', 'Omaha', 'NE', 88899, '801-323-2320'),
('Madison', 'Parker', '285 Diamond St', 'San Diego', 'CA', 99977, '801-493-2203'),
('Justin', 'Colano', '223 Easy St', 'Tampa', 'FL', 66798, '801-193-2320'),
('Claudia', 'Stevens', '775 Main St', 'Saint Louis', 'MO', 99878, '801-303-2222'),
('Arthur', 'Hooper', '456 Rose St', 'Las Vegas', 'NV', 17878, '313-912-2101'),
('Sergio', 'Ryan', '567 Spruce St', 'Lincoln', 'NE', 87898, '801-228-6729'),
('Julia', 'Maverick', '678 Tulip St', 'Raleigh', 'NC', 79999, NULL),
('Brandon', 'Gordon', '789 First St', 'Miami', 'FL', 30533, NULL);
/*Populate PLANE_TYPE table*/
INSERT INTO PLANE_TYPE VALUES
('B727', 'Boeing'),
('B747', 'Boeing'),
('B757', 'Boeing'),
('DC9', 'MD'),
('DC10', 'MD'),
('A310', 'Airbus'),
('A320', 'Airbus'),
('A330', 'Airbus'),
('DA340', 'Airbus');
/*Populate EMPLOYEE table*/
INSERT INTO EMPLOYEE VALUES
(1001,'John Jones', 90000),
(1002,'Albert Peters',85000),
(1003,'Carl Rowe',115000),
(1004,'Phillips', 25000),
(1005,'William Gates', 50000),
(1006,'Robert Clark', 45000),
(1007,'Joseph Warnock', 50000),
(1008,'Lisa Stuart',75000);
/*Populate FLIGHT table*/
INSERT INTO FLIGHT VALUES
(100, 'SLC', 'BOS', '8:00', '17:50'),
(206, 'DFW', 'STL', '9:00', '11:40'),
(334, 'ORD', 'MIA', '12:00', '14:14'),
(335, 'MIA', 'ORD', '15:00', '17:14'),
(336, 'ORD', 'MIA', '18:00', '20:14'),
(337, 'MIA', 'ORD', '20:30', '23:53'),
(121, 'STL', 'SLC', '7:00', '9:13'),
(122, 'JFK', 'YYV', '16:00', '18:53'),
(330, 'STL', 'YYV', '8:30', '10:19'),
(991, 'BOS', 'ORD', '17:00', '18:22'),
(394, 'DFW', 'MIA', '19:00', '21:30'),
(395, 'MIA', 'DFW', '21:00', '23:43'),
(449, 'CDG', 'DEN', '10:00', '19:29'),
(930, 'YYV', 'DCA', '13:00', '16:10'),
(931, 'DCA', 'YYV', '17:00', '18:10'),
(932, 'DCA', 'YYV', '18:00', '19:10'),
(112, 'DCA', 'DEN', '14:00', '18:07');
/*Populate Pilot table*/
INSERT INTO PILOT VALUES
(1001, 'John Jones', '06/01/1999', 'B727'),
(1001, 'John Jones', '08/14/2000', 'B747'),
(1001, 'John Jones', '04/20/2005', 'DC10'),
(1002, 'Albert Peters', '09/25/2010', 'DC9'),
(1002, 'Albert Peters', '04/20/2005', 'A310'),
(1002, 'Albert Peters', '07/17/2003', 'B757'),
(1002, 'Albert Peters', '05/2/2012', 'A320'),
(1002, 'Albert Peters', '12/01/2015', 'B727'),
(1003, 'Carol Rowe', '01/25/2013', 'A310'),
(1003, 'Carol Rowe', '06/05/2015', 'DC9');
/*Populate Booked_on table*/
INSERT INTO BOOKED_ON VALUES
('Rose Parker','10/31/2018', 100),
('Bryan Brown','10/31/2018', 206),
('Arthur Hooper','10/31/2018', 334),
('Sergio Ryan','10/31/2018', 449),
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
('Mark Williams', '11/01/2018', 100),
('Carol Phillips', '11/01/2018', 100),
('Justin Colano','10/31/2018', 449),
('Claudia Stevens','10/31/2018', 449),
('Julia Maverick','11/01/2018', 991),
('John Peters','11/01/2018', 100);
/*Populate AirCraft table*/
INSERT INTO AIRCRAFT VALUES
('B1110', 'B727'),
('B1325', 'B727'),
('B1088', 'B747'),
('B1419', 'B747'),
('B2244', 'B757'),
('B9377', 'B757'),
('D2101', 'DC9'),
('D2216', 'DC9'),
('D2333', 'DC9'),
('D2429', 'DC9'),
('D2530', 'DC10'),
('A7099', 'A310'),
('A8055', 'A320');
/*Populate DEPARTURE table*/
INSERT INTO DEPARTURE VALUES
('10/31/2018', 100, 'B1110'),
('10/31/2018', 112, NULL),
('10/31/2018', 206, 'D2530'),
('10/31/2018', 334, NULL),
('10/31/2018', 335, NULL),
('10/31/2018', 337, ‘D2216’),
('10/31/2018', 449, NULL),
('11/1/2018', 100, 'D2530'),
('11/1/2018', 112, 'A8055'),
('11/1/2018', 206, NULL),
('11/1/2018', 334, NULL),
('11/1/2018', 395, NULL),
('11/1/2018', 991, 'B1325');
INSERT INTO ASSIGNED_TO VALUES
(1001, '10/31/2018', 100),
(1003, '10/31/2018', 100),
(1004, '10/31/2018', 100),
(1002, '10/31/2018', 206),
(1003, '10/31/2018', 337),
(1004, '10/31/2018', 337),
(1005, '10/31/2018', 337),
(1006, '10/31/2018', 337),
(1001, '11/1/2018', 100),
(1005, '11/1/2018', 100),
(1007, '11/1/2018', 100),
(1003, '11/1/2018', 991),
(1006, '11/1/2018', 991),
(1007, '11/1/2018', 991),
(1002, '11/1/2018', 112),
(1004, '11/1/2018', 112),
(1008, '11/1/2018', 112);
Values will not update due to foreign key and duplicate keys. I attempted to change the create table code. I am unsure if I have to add constraints or change the create table code. Please assist with the code
You insert flight before booke_on, need use go for order of insert.
See new query:
drop table if exists CUSTOMER;
drop table if exists BOOKED_ON;
drop table if exists PLANE_TYPE;
drop table if exists EMPLOYEE;
drop table if exists PILOT;
drop table if exists ASSIGNED_TO;
drop table if exists AIRCRAFT;
drop table if exists DEPARTURE;
drop table if exists FLIGHT;
CREATE TABLE CUSTOMER(
Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20) NOT NULL,
Street VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL,
Zip_Code INT NOT NULL,
Phone VARCHAR(20),
PRIMARY KEY (Name) );
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
CREATE TABLE PLANE_TYPE(
Model_no VARCHAR(20) NOT NULL,
Manufacturer VARCHAR(20) NOT NULL,
Unique(Model_no),
PRIMARY KEY (Model_no, Manufacturer) );
CREATE TABLE EMPLOYEE(
employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Salary INT NOT NULL,
PRIMARY KEY (employee_no));
CREATE TABLE PILOT(
Employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
License_Date DATE NOT NULL,
Plane_model_can_fly VARCHAR(20) NOT NULL,
UNIQUE (License_Date),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no));
CREATE TABLE ASSIGNED_TO(
Employee_no INT NOT NULL,
dep_date VARCHAR(20) NOT NULL,
flight_num INT,
PRIMARY KEY (Employee_no),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON(Flight_num)
);
CREATE TABLE AIRCRAFT(
Serial_no VARCHAR(50) ,
Model_no VARCHAR(50),
PRIMARY KEY (Serial_no),
FOREIGN KEY (Model_no) REFERENCES PLANE_TYPE(Model_no)
);
CREATE TABLE DEPARTURE(
Departure_date DATE NOT NULL,
Flight_number INT,
Aircraft_Serial_no VARCHAR(50) ,
Employee_no INT NOT NULL,
PRIMARY KEY(Flight_number),
FOREIGN KEY (Flight_number) REFERENCES BOOKED_ON (Flight_num),
FOREIGN KEY (employee_no) REFERENCES EMPLOYEE(Employee_no),
FOREIGN KEY (Aircraft_Serial_no) REFERENCES AIRCRAFT(Serial_no));
CREATE TABLE FLIGHT(
flight_num INT,
origin VARCHAR(20) NOT NULL,
dest VARCHAR(20) NOT NULL,
dep_time VARCHAR(20) NOT NULL,
arr_time VARCHAR(20) NOT NULL,
PRIMARY KEY(flight_num),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON (Flight_num)
);
/*Populate Customer table*/
INSERT INTO CUSTOMER VALUES
('Rose', 'Parker', '123 Thomas St', 'Toledo', 'OH', 57556, '801-556-2239'),
('Jessica', 'Jones', '234 Pines St', 'Los Angeles', 'CA', 34898, '801-552-2943'),
('John', 'Peters', '345 Star St', 'Raleigh', 'NC', 79999, '801-393-2230'),
('Bryan', 'Brown', '435 Palm St', 'Miami', 'FL', 30533, '801-933-2320'),
('Mark', 'Williams', '348 Andrew St', 'Fort Lauderdale', 'FL', 33318, '801-343-2320'),
('Carol', 'Phillips', '395 Pine St', 'Omaha', 'NE', 88899, '801-323-2320'),
('Madison', 'Parker', '285 Diamond St', 'San Diego', 'CA', 99977, '801-493-2203'),
('Justin', 'Colano', '223 Easy St', 'Tampa', 'FL', 66798, '801-193-2320'),
('Claudia', 'Stevens', '775 Main St', 'Saint Louis', 'MO', 99878, '801-303-2222'),
('Arthur', 'Hooper', '456 Rose St', 'Las Vegas', 'NV', 17878, '313-912-2101'),
('Sergio', 'Ryan', '567 Spruce St', 'Lincoln', 'NE', 87898, '801-228-6729'),
('Julia', 'Maverick', '678 Tulip St', 'Raleigh', 'NC', 79999, NULL),
('Brandon', 'Gordon', '789 First St', 'Miami', 'FL', 30533, NULL);
/*Populate PLANE_TYPE table*/
INSERT INTO PLANE_TYPE VALUES
('B727', 'Boeing'),
('B747', 'Boeing'),
('B757', 'Boeing'),
('DC9', 'MD'),
('DC10', 'MD'),
('A310', 'Airbus'),
('A320', 'Airbus'),
('A330', 'Airbus'),
('DA340', 'Airbus');
/*Populate EMPLOYEE table*/
INSERT INTO EMPLOYEE VALUES
(1001,'John Jones', 90000),
(1002,'Albert Peters',85000),
(1003,'Carl Rowe',115000),
(1004,'Phillips', 25000),
(1005,'William Gates', 50000),
(1006,'Robert Clark', 45000),
(1007,'Joseph Warnock', 50000),
(1008,'Lisa Stuart',75000);
/*Populate Pilot table*/
INSERT INTO PILOT VALUES
(1001, 'John Jones', '06/01/1999', 'B727'),
(1001, 'John Jones', '08/14/2000', 'B747'),
(1001, 'John Jones', '04/20/2005', 'DC10'),
(1002, 'Albert Peters', '09/25/2010', 'DC9'),
(1002, 'Albert Peters', '04/20/2005', 'A310'),
(1002, 'Albert Peters', '07/17/2003', 'B757'),
(1002, 'Albert Peters', '05/2/2012', 'A320'),
(1002, 'Albert Peters', '12/01/2015', 'B727'),
(1003, 'Carol Rowe', '01/25/2013', 'A310'),
(1003, 'Carol Rowe', '06/05/2015', 'DC9');
/*before inser booked_ontable*/
GO
/*Populate Booked_on table*/
INSERT INTO BOOKED_ON VALUES
('Rose Parker','10/31/2018', 100),
('Bryan Brown','10/31/2018', 206),
('Arthur Hooper','10/31/2018', 334),
('Sergio Ryan','10/31/2018', 449),
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
('Mark Williams', '11/01/2018', 100),
('Carol Phillips', '11/01/2018', 100),
('Justin Colano','10/31/2018', 449),
('Claudia Stevens','10/31/2018', 449),
('Julia Maverick','11/01/2018', 991),
('John Peters','11/01/2018', 100);
/* after insert flight*/
GO
/*Populate FLIGHT table*/
INSERT INTO FLIGHT VALUES
(100, 'SLC', 'BOS', '8:00', '17:50'),
(206, 'DFW', 'STL', '9:00', '11:40'),
(334, 'ORD', 'MIA', '12:00', '14:14'),
(335, 'MIA', 'ORD', '15:00', '17:14'),
(336, 'ORD', 'MIA', '18:00', '20:14'),
(337, 'MIA', 'ORD', '20:30', '23:53'),
(121, 'STL', 'SLC', '7:00', '9:13'),
(122, 'JFK', 'YYV', '16:00', '18:53'),
(330, 'STL', 'YYV', '8:30', '10:19'),
(991, 'BOS', 'ORD', '17:00', '18:22'),
(394, 'DFW', 'MIA', '19:00', '21:30'),
(395, 'MIA', 'DFW', '21:00', '23:43'),
(449, 'CDG', 'DEN', '10:00', '19:29'),
(930, 'YYV', 'DCA', '13:00', '16:10'),
(931, 'DCA', 'YYV', '17:00', '18:10'),
(932, 'DCA', 'YYV', '18:00', '19:10'),
(112, 'DCA', 'DEN', '14:00', '18:07');
/*Populate AirCraft table*/
INSERT INTO AIRCRAFT VALUES
('B1110', 'B727'),
('B1325', 'B727'),
('B1088', 'B747'),
('B1419', 'B747'),
('B2244', 'B757'),
('B9377', 'B757'),
('D2101', 'DC9'),
('D2216', 'DC9'),
('D2333', 'DC9'),
('D2429', 'DC9'),
('D2530', 'DC10'),
('A7099', 'A310'),
('A8055', 'A320');
/*Populate DEPARTURE table*/
INSERT INTO DEPARTURE VALUES
('10/31/2018', 100, 'B1110'),
('10/31/2018', 112, NULL),
('10/31/2018', 206, 'D2530'),
('10/31/2018', 334, NULL),
('10/31/2018', 335, NULL),
('10/31/2018', 337, ‘D2216’),
('10/31/2018', 449, NULL),
('11/1/2018', 100, 'D2530'),
('11/1/2018', 112, 'A8055'),
('11/1/2018', 206, NULL),
('11/1/2018', 334, NULL),
('11/1/2018', 395, NULL),
('11/1/2018', 991, 'B1325');
INSERT INTO ASSIGNED_TO VALUES
(1001, '10/31/2018', 100),
(1003, '10/31/2018', 100),
(1004, '10/31/2018', 100),
(1002, '10/31/2018', 206),
(1003, '10/31/2018', 337),
(1004, '10/31/2018', 337),
(1005, '10/31/2018', 337),
(1006, '10/31/2018', 337),
(1001, '11/1/2018', 100),
(1005, '11/1/2018', 100),
(1007, '11/1/2018', 100),
(1003, '11/1/2018', 991),
(1006, '11/1/2018', 991),
(1007, '11/1/2018', 991),
(1002, '11/1/2018', 112),
(1004, '11/1/2018', 112),
(1008, '11/1/2018', 112);
Something further to add.
In your BOOKED_ON create you specify that flight_num is your primary key
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
However, in your data you are not entering unique values for flight number into that table. e.g.
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
There are a number of confusing relationships in your model, you might want to give the design phase another once over.
Everything runs fine. End part gives an ORA-02291 error. Not sure what would be causing that error, since I thought all the code was running well. This was made in Oracle SQL developer, the error message is ORA-02291. I hope someone can give me an answer. Please help me.
Code:
CREATE TABLE comm_customer
(
Customer_Id int NOT NULL,
Customer_Name VARCHAR(50) NOT NULL,
Address VARCHAR(255) NOT NULL,
Billing_Address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
phone INT NOT NULL,
primary key(Customer_id)
);
CREATE TABLE comm_orders
(
order_Id INT NOT NULL,
Customer_id INT NOT NULL,
order_date DATE NOT NULL,
amount DECIMAL(5,2) NOT NULL,
Order_status VARCHAR(20) NOT NULL,
primary key(order_id),
FOREIGN KEY (customer_id ) REFERENCES comm_customer(customer_id)
);
CREATE TABLE comm_products
(
product_id INT NOT NULL,
product_name VARCHAR(255) NOT NULL,
product_price decimal(5,2) NOT NULL,
product_quantity decimal(5,2) NOT NULL,
product_status VARCHAR(255) NOT NULL,
customer_id int NOT NULL,
primary key(product_id),
FOREIGN KEY (customer_id ) REFERENCES comm_customer(customer_id)
);
CREATE TABLE comm_shipments
(
shipment_id INT NOT NULL,
order_id INT NOT NULL,
shipment_date DATE NOT NULL,
PRIMARY KEY (shipment_id),
FOREIGN KEY (order_id ) REFERENCES comm_orders(order_id)
);
CREATE TABLE comm_shopping_cart
(
orderdetails_id INT NOT NULL,
order_id INT NOT NULL,
product_id int NOT NULL,
quantity int NOT NULL,
price decimal(5,2),
primary key(orderdetails_id),
FOREIGN KEY (order_id ) REFERENCES comm_orders(order_id),
FOREIGN KEY (product_id ) REFERENCES comm_products(product_id)
);
--For Table Comm_customer
insert into comm_customer values(1011, 'John', '48 Maple Heights Road', '48 Maple Heights Road', 'Toronto', 'Canada', 9988766779);
insert into comm_customer values(1012, 'James', '32 St.Jordan Cressent', '32 St.Jordan Cressent', 'Chennai', 'India', 9988722779);
insert into comm_customer values(1013, 'Anderson', '5 Thornway Street', '#1755 JBS Colony', 'Surat', 'India', 9988123779 );
insert into comm_customer values(1014, 'Jose', '88 Greenbelt Drive', '#1983 ABS Nagar', 'Mumbai', 'India', 9988766885 );
insert into comm_customer values(1015, 'Leo', '#1765 XSX Nagar', '#10993 ACD Nagar', 'Hoshiarpur', 'India', 9955466779 );
--For Table Comm_products:
INSERT into comm_products values(01, 'XI Phone', 250, 4, 'checked out', 1015);
INSERT into comm_products values(02, 'Book', 120, 2, 'not checked out', 1011);
INSERT INTO comm_products values(03, 'Vegetable',100, 5, 'not checked out', 1012);
INSERT into comm_products values(04, 'Biscuits', 250, 1, 'checked out', 1013);
INSERT into comm_products values(05, 'Shampoo', 500, 2, 'not checked out', 1014);
--For table Comm_orders:
insert into comm_orders values(11, 1014, '2020-MAY-01', 500, 'checked out');
insert into comm_orders values(12, 1011, '2019-JUL-18', 140, 'not checked out');
insert into comm_orders values(13, 1013, '2020-JAN-31', 170, 'checked out');
insert into comm_orders values(14, 1012, '2019-FEB-15', 120, ' not checked out');
insert into comm_orders values(15, 1011, '2018-JUN-21', 600, 'checked out');
--For table comm_shippments:
insert into comm_shipments values(001, 13, '2020-FEB-05');
insert into comm_shipments values(002, 15, '2018-JUL-01');
insert into comm_shipments values(003, 12, '2019-07-25');
insert into comm_shipments values(004, 11, '2020-MAY-05');
insert into comm_shipments values(005, 14, '2019-FEB-25');
--For table comm_shopping_cart:
insert into comm_shopping_cart values(701, 01, 11, 4, 250);
insert into comm_shopping_cart values(702, 02, 12, 3, 120);
insert into comm_shopping_cart values(704, 03, 13, 6, 100);
insert into comm_shopping_cart values(703, 04, 14, 2, 250);
insert into comm_shopping_cart values(705, 05, 15, 1, 500);
OK, so you say the statement that is erroring out is this
insert into comm_shopping_cart values(701, 01, 11, 4, 250);
I'd strongly urge you to list the columns in your insert statement. That helps to document things so that someone doesn't have to constantly refer up to the table definition to see what order columns are defined in. And it means that your statements won't suddently fail in the future when you add new columns.
insert into comm_shopping_cart( orderdetails_id,
order_id,
product_id,
quantity,
price)
values(701,
01,
11,
4,
250);
OK, so having done that, the error is that the parent key isn't found. Your actual error message should include the name of the constraint which would tell you which column is the problem. You haven't given us that information so we'll have to figure it out. There are two foreign keys on comm_shopping_cart, the order_id and the product_id. So which one doesn't exist?
You're trying to insert a row with an order_id of 01 (I don't understand why you're putting leading 0's in a numeric column). But you only have order_id values in the comm_orders table between 11 and 15.
You're also trying to insert a row with a product_id of 11. But you only have product_id values between 1 and 5 in your comm_products table. So both foreign key constraints would fail.
My guess is that you intended to specify an order_id of 11 and a product_id of 1 in your insert statement and reversed the order of the columns. Since you didn't include the column list in your insert statement, you made it much harder on yourself to debug the problem because your insert statements aren't self-documenting. Had you specified the column list, you could also have listed the columns in whatever order you wanted so if you wanted to specify the product_id before the order_id you could have.
I made this code for SQL Server to select how many times the rider "AsdoVolante" came first in the first semester of 2017. But it does not work. What could be wrong?
SELECT P.Data, Nm.Nome, epp.PosicaoFinal
FROM Prova P, Piloto Nm, EquipaParticipaProva epp
WHERE epp.PosicaoFinal = (SELECT PosicaoFinal FROM EquipaParticipaProva where PosicaoFinal = 1)
HAVING Nm.Nome ='AsdoVolante' AND P.Data BETWEEN '2017-01-01' AND '2017-06-30';
Tables created in SQL Server for the code:
CREATE TABLE Carro(
IdCarro INT NOT NULL,
Marca VARCHAR(20) NOT NULL,
Modelo VARCHAR(50),
PRIMARY KEY (IdCarro));
CREATE TABLE Piloto(
IdPiloto INT NOT NULL,
Nome VARCHAR(20) NOT NULL,
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdPiloto));
CREATE TABLE Equipa(
IdEquipa INT NOT NULL,
Nome VARCHAR(20),
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdEquipa));
CREATE TABLE Patrocinador(
IdPatrocinador INT NOT NULL,
Nome VARCHAR(20)NOT NULL,
Morada VARCHAR(50),
PRIMARY KEY (IdPatrocinador));
CREATE TABLE Prova(
IdProva INT NOT NULL,
Nome VARCHAR(20) NOT NULL,
Local VARCHAR(50) NOT NULL,
Data DATE NOT NULL,
NrVoltas INT NOT NULL,
MelhorTempo TIME,
PRIMARY KEY (IdProva));
CREATE TABLE EquipaParticipaProva(
IdInscricao INT NOT NULL,
IdEquipa INT NOT NULL,
IdCarro INT NOT NULL,
IdPiloto INT NOT NULL,
IdProva INT NOT NULL,
PosicaoFinal INT,
TempoFinal TIME,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
FOREIGN KEY (IdCarro) REFERENCES Carro(IdCarro),
FOREIGN KEY (IdPiloto) REFERENCES Piloto(IdPiloto),
FOREIGN KEY (IdEquipa) REFERENCES Equipa(IdEquipa),
PRIMARY KEY (IdInscricao));
CREATE TABLE Voltas(
IdVolta INT NOT NULL,
IdInscricao INT NOT NULL,
PosicaoRelativa INT NOT NULL,
TempoVolta TIME,
FOREIGN KEY (IdInscricao) REFERENCES EquipaParticipaProva(IdInscricao),
PRIMARY KEY (IdVolta, IdInscricao));
CREATE TABLE PatrocinadorOficial(
IdPatrocinador INT NOT NULL,
IdProva INT NOT NULL,
Valor MONEY NOT NULL,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
PRIMARY KEY (IdPatrocinador, IdProva));
CREATE TABLE PatrocinadorNaoOficial(
IdPatrocinador INT NOT NULL,
IdProva INT NOT NULL,
Valor MONEY NOT NULL,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
PRIMARY KEY (IdPatrocinador, IdProva));
CREATE TABLE Mecanico(
IdMecanico INT NOT NULL,
Nome VARCHAR(20),
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdMecanico));
CREATE TABLE Afinacao(
IdAfinacao INT NOT NULL,
IdMecanico INT NOT NULL,
IdEquipa INT NOT NULL,
TipoAfinacao VARCHAR(20) NOT NULL,
TempoDespendido TIME,
Data DATE NOT NULL,
FOREIGN KEY (IdEquipa) REFERENCES Equipa(IdEquipa),
FOREIGN KEY (IdMecanico) REFERENCES Mecanico(IdMecanico),
PRIMARY KEY (IdAfinacao));
Inserts created in SQL Server:
INSERT INTO Carro
VALUES (1, 'Nissan', 'Skyline');
INSERT INTO Carro
VALUES (2, 'Subaru', 'Impreza');
INSERT INTO Carro
VALUES (3, 'Mitsubishi', 'Evolution');
INSERT INTO Piloto
VALUES (111, 'Piloto 1', 'Castelo Branco', 111111111);
INSERT INTO Piloto
VALUES (112, 'Piloto 2', 'Castelo Branco', 111111112);
INSERT INTO Piloto
VALUES (113, 'JaGanhou', 'Lisboa', 111111113);
INSERT INTO Piloto
VALUES (114, 'AsdoVolante', 'Lisboa', 111111114);
INSERT INTO Piloto
VALUES (115, 'AsdoPedal', 'Beja', 111111115);
INSERT INTO Piloto
VALUES (116, 'Pelo Nome', 'Santarém', 111111116);
INSERT INTO Equipa
VALUES (001, 'OsAtolados', 'Castelo Branco', 272722123);
INSERT INTO Equipa
VALUES (002, 'Sparco', 'Lisboa', 212312432);
INSERT INTO Equipa
VALUES (003, 'ESTeam', 'Castelo Branco', 272343573);
INSERT INTO Prova
VALUES (010, 'RallycrossCB', 'Castelo Branco', '2017-02-02', 4, '00:04:20');
INSERT INTO Prova
VALUES (11, 'RallyLels', 'Bells', '2017-03-04', 10, '00:10:00');
INSERT INTO Prova
VALUES (12, 'Rally Portugal', 'Fafe', '2017-05-15', 2, '00:15:00');
INSERT INTO Prova
VALUES (13, 'Rally CB', 'Castelo Branco', '2017-01-01', 2, '00:15:00');
INSERT INTO Prova
VALUES (14, 'Rally Norte', 'Porto', '2017-02-02', 2, '00:30:00');
INSERT INTO EquipaParticipaProva
VALUES (441, 002, 3, 113, 010, 1, '00:04:00');
INSERT INTO EquipaParticipaProva
VALUES (442, 1, 2, 111, 11, 1, '00:10:00');
INSERT INTO EquipaParticipaProva
VALUES (443, 3, 1, 112, 10, 1,'00:05:00' );
INSERT INTO EquipaParticipaProva
VALUES (444, 3, 1, 114, 12, 1,'00:15:00' );
INSERT INTO EquipaParticipaProva
VALUES (445, 3, 1, 114, 13, 1,'00:15:00' );
INSERT INTO EquipaParticipaProva
VALUES (446, 3, 2, 114, 14, 5,'01:00:00' );
INSERT INTO Voltas
VALUES (1, 443, 1, '00:01:15');
INSERT INTO Voltas
VALUES (2, 443, 1, '00:01:15');
INSERT INTO Voltas
VALUES (3, 443, 3, '00:01:15');
INSERT INTO Voltas
VALUES (1, 444, 1, '00:07:30');
INSERT INTO Voltas
VALUES (2, 444, 1, '00:07:30');
INSERT INTO Voltas
VALUES (1, 445, 1, '00:07:30');
INSERT INTO Voltas
VALUES (2, 445, 1, '00:07:30');
INSERT INTO Voltas
VALUES (1, 446, 5, '00:30:00');
INSERT INTO Voltas
VALUES (2, 446, 5, '00:30:00');
INSERT INTO Voltas
VALUES (4, 443, 3, '00:01:15');
INSERT INTO Voltas
VALUES (1, 441, 1, '00:01:00' );
INSERT INTO Voltas
VALUES (2, 441, 1, '00:01:20');
INSERT INTO Voltas
VALUES (3, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (4, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (5, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (6, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (7, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (8, 441, 1, '00:01:20');
INSERT INTO Voltas
VALUES (9, 441, 1 ,'00:00:30');
INSERT INTO Voltas
VALUES (10, 441, 1, '00:01:30');
INSERT INTO Voltas
VALUES (1, 441, 10, 2, '00:00:45');
INSERT INTO Voltas
VALUES (2, 441, 10, 2, '00:01:00');
INSERT INTO Voltas
VALUES (3, 441, 10, 2, '00:01:00');
INSERT INTO Voltas
VALUES (4, 441, 10, 2, '00:01:15');
INSERT INTO PATROCINADOR
VALUES(921, 'IPCB', 'Av da Talagueira');
INSERT INTO PATROCINADOR
VALUES(922, 'Bells Bar', 'Praceta Qualquer Coisa');
INSERT INTO PATROCINADOR
VALUES(923, 'MonsterEnergy', 'USA');
INSERT INTO PATROCINADOR
VALUES(924, 'Sical', 'Lisboa');
INSERT INTO PATROCINADOROFICIAL
VALUES(923, 010, 10000);
INSERT INTO PATROCINADOROFICIAL
VALUES(921, 11, 8000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(920, 010, 5000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(922, 11, 3000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(924, 10, 6000);
INSERT INTO MECANICO
VALUES(333, 'Zacarias','Leiria',933628487);
INSERT INTO MECANICO
VALUES(334, 'Zé', 'Bragança', 963628487);
INSERT INTO MECANICO
VALUES(335, 'Luís', 'Lisboa', 913628487);
INSERT INTO AFINACAO
VALUES(20, 333, 001, 'Travões', '1:00:00', '2017-02-03');
INSERT INTO AFINACAO
VALUES(21, 334, 002, 'Luzes', '00:15:00', '2017-02-03');
INSERT INTO AFINACAO
VALUES(22, 335, 003, 'Travoes', '1:00:00', '2017-02-01');
try this
SELECT P.Data, Nm.Nome, epp.PosicaoFinal
FROM Prova P, Piloto Nm, EquipaParticipaProva epp
WHERE epp.PosicaoFinal **IN** (SELECT PosicaoFinal FROM EquipaParticipaProva where PosicaoFinal = 1)
HAVING Nm.Nome ='AsdoVolante' AND P.Data BETWEEN '2017-01-01' AND '2017-06-30';
It will work for you
So I'm working on a SQL and I keep getting errors and I have triple checked it and still haven't come up with a reason as to why the queries aren't showing up correctly. I'm using Microsoft SQL Server. These are the errors that I'm currently getting:
Msg 1767, Level 16, State 0, Line 2
Foreign key 'FK_PatientID' references invalid table 'Patient'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'TreatmentDetails'.
Code:
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Hospital')
DROP DATABASE [Hospital]
GO
CREATE DATABASE [Hospital]
GO
USE Hospital
GO
CREATE TABLE [dbo].[Physician]
(
PhysicianID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Specialty VARCHAR(30) NOT NULL,
GraduationDate DATE NOT NULL,
CONSTRAINT [PK_PhysicianID] PRIMARY KEY (PhysicianID)
);
GO
CREATE TABLE [dbo].[TreatmentDetails]
(
TreatmentID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
StartDateTime DATE NOT NULL,
EndDateTime DATE NULL,
Results VARCHAR(30),
CONSTRAINT [PK_TreatmentID] PRIMARY KEY (TreatmentID),
CONSTRAINT [FK_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
CREATE TABLE [dbo].[Patient]
(
PatientID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
DateOfBirth DATE NOT NULL,
CONSTRAINT [PK_PatientID] PRIMARY KEY (PatientID),
);
GO
CREATE TABLE [dbo].[AdmissionDate]
(
AdmissionID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
AdmissionDate DATE NOT NULL,
DischargeDate DATE NULL,
CONSTRAINT [PK_AdmissionID] PRIMARY KEY (AdmissionID),
CONSTRAINT [FK_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
INSERT INTO TreatmentDetails VALUES (1, 12345, 1234, '2014-4-5', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (2, 12346, 1235, '2013-5-6', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (3, 12347, 1236, '2012-7-8', '2014-9-10', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (4, 12348, 1237, '2011-9-10', '2013-11-12', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (5, 12349, 1238, '2010-11-12', NULL, 'NOT DONE')
INSERT INTO Physician VALUES (12345, 'Will', 'Smith', 'Surgeon', '2014-5-9');
INSERT INTO Physician VALUES (12346, 'Jim', 'Carey', 'Pediatrictian', '2013-2-4');
INSERT INTO Physician VALUES (12347, 'Adam', 'Sandler', 'Immunologist', '2012-6-12');
INSERT INTO Physician VALUES (12348, 'Seth', 'Rogan', 'Neurologist', '2010-9-19');
INSERT INTO Physician VALUES (12349, 'James', 'Bond', 'Dermatologist', '2011-5-2');
INSERT INTO Patient VALUES (1234, 'Christopher', 'Thompson', '1989-7-9');
INSERT INTO Patient VALUES (1235, 'Mac', 'Miller', '1970-9-5');
INSERT INTO Patient VALUES (1236, 'Abraham', 'Lincoln', '1988-1-22');
INSERT INTO Patient VALUES (1237, 'George', 'Washington', '1965-2-8');
INSERT INTO Patient VALUES (1238, 'Franklin', 'Roosevelt', '1992-5-19');
INSERT INTO AdmissionDate VALUES (001, 12345, 1234,'2014-2-9', NULL);
INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-15-9');
INSERT INTO AdmissionDate VALUES (003, 12347, 1236,'2014-3-7', '2014-4-9');
INSERT INTO AdmissionDate VALUES (004, 12348, 1237, '2014-8-6', NULL);
INSERT INTO AdmissionDate VALUES (005, 12349, 1238, '2014-5-5', NULL);
GO
USE Hospital
SELECT * FROM Physician
SELECT * FROM Patient
SELECT * FROM AdmissionDate
SELECT * FROM TreatmentDetails
SELECT Patient.PatientID, Patient.FirstName, Patient.LastName, Patient.DateOfBirth, AdmissionDate.AdmissionDate, AdmissionDate.DischargeDate, Physician.Specialty
FROM Patient, AdmissionDate, Physician
WHERE AdmissionDate IS NOT NULL
ORDER BY Patient.LastName, Patient.FirstName, AdmissionDate.AdmissionDate
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NOT NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
You have incorrect order of creating tables.
You are creating foreign keys with same name in different tables.
You are inserting data in tables in incorrect order.
You have provided incorrect date format. Default format is YYYY-MM-DD
and you provide INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-15-9'); 2014-15-9 this is to change.
Here is working script:
USE master
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Hospital')
DROP DATABASE [Hospital]
GO
CREATE DATABASE [Hospital]
GO
USE Hospital
GO
CREATE TABLE [dbo].[Physician]
(
PhysicianID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Specialty VARCHAR(30) NOT NULL,
GraduationDate DATE NOT NULL,
CONSTRAINT [PK_PhysicianID] PRIMARY KEY (PhysicianID)
);
GO
CREATE TABLE [dbo].[Patient]
(
PatientID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
DateOfBirth DATE NOT NULL,
CONSTRAINT [PK_PatientID] PRIMARY KEY (PatientID),
);
GO
CREATE TABLE [dbo].[TreatmentDetails]
(
TreatmentID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
StartDateTime DATE NOT NULL,
EndDateTime DATE NULL,
Results VARCHAR(30),
CONSTRAINT [PK_TreatmentDetails_TreatmentID] PRIMARY KEY (TreatmentID),
CONSTRAINT [FK_TreatmentDetails_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_TreatmentDetails_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
CREATE TABLE [dbo].[AdmissionDate]
(
AdmissionID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
AdmissionDate DATE NOT NULL,
DischargeDate DATE NULL,
CONSTRAINT [PK_AdmissionDate_AdmissionID] PRIMARY KEY (AdmissionID),
CONSTRAINT [FK_AdmissionDate_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_AdmissionDate_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
INSERT INTO Physician VALUES (12345, 'Will', 'Smith', 'Surgeon', '2014-5-9');
INSERT INTO Physician VALUES (12346, 'Jim', 'Carey', 'Pediatrictian', '2013-2-4');
INSERT INTO Physician VALUES (12347, 'Adam', 'Sandler', 'Immunologist', '2012-6-12');
INSERT INTO Physician VALUES (12348, 'Seth', 'Rogan', 'Neurologist', '2010-9-19');
INSERT INTO Physician VALUES (12349, 'James', 'Bond', 'Dermatologist', '2011-5-2');
INSERT INTO Patient VALUES (1234, 'Christopher', 'Thompson', '1989-7-9');
INSERT INTO Patient VALUES (1235, 'Mac', 'Miller', '1970-9-5');
INSERT INTO Patient VALUES (1236, 'Abraham', 'Lincoln', '1988-1-22');
INSERT INTO Patient VALUES (1237, 'George', 'Washington', '1965-2-8');
INSERT INTO Patient VALUES (1238, 'Franklin', 'Roosevelt', '1992-5-19');
INSERT INTO TreatmentDetails VALUES (1, 12345, 1234, '2014-4-5', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (2, 12346, 1235, '2013-5-6', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (3, 12347, 1236, '2012-7-8', '2014-9-10', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (4, 12348, 1237, '2011-9-10', '2013-11-12', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (5, 12349, 1238, '2010-11-12', NULL, 'NOT DONE')
INSERT INTO AdmissionDate VALUES (001, 12345, 1234,'2014-2-9', NULL);
INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-9-15');
INSERT INTO AdmissionDate VALUES (003, 12347, 1236,'2014-3-7', '2014-4-9');
INSERT INTO AdmissionDate VALUES (004, 12348, 1237, '2014-8-6', NULL);
INSERT INTO AdmissionDate VALUES (005, 12349, 1238, '2014-5-5', NULL);
GO
USE Hospital
SELECT * FROM Physician
SELECT * FROM Patient
SELECT * FROM AdmissionDate
SELECT * FROM TreatmentDetails
SELECT Patient.PatientID, Patient.FirstName, Patient.LastName, Patient.DateOfBirth, AdmissionDate.AdmissionDate, AdmissionDate.DischargeDate, Physician.Specialty
FROM Patient, AdmissionDate, Physician
WHERE AdmissionDate IS NOT NULL
ORDER BY Patient.LastName, Patient.FirstName, AdmissionDate.AdmissionDate
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NOT NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName