With the tables below, I am trying to answer this question and I have no idea how to answer it:
"Show the sum of HoursWorked for each Type of OWNER but exclude services of employees who have ExperienceLevel of Junior and exclude any Type with less than three members."
CREATE TABLE OWNER
(
OwnerID Int NOT NULL IDENTITY (1,1) PRIMARY KEY,
OwnerName Char(30) NOT NULL,
OwnerEmail VarChar(100) NULL,
OwnerType Char(15) NOT NULL,
);
CREATE TABLE PROPERTY
(
PropertyID Int NOT NULL IDENTITY(1,1) PRIMARY KEY,
PropertyName Char(30) NOT NULL,
Street Char(20) NOT NULL,
City Char(25) NOT NULL,
State Char(10) NOT NULL,
Zip Char(5) NOT NULL,
OwnerID Int NOT NULL,
CONSTRAINT PROP_OWN_FK FOREIGN KEY(OwnerID)
REFERENCES OWNER(OwnerID),
);
CREATE TABLE GG_EMPLOYEE
(
EmployeeID Int NOT NULL IDENTITY(1,1) PRIMARY KEY,
LastName Char(35) NOT NULL,
FirstName Char(35) NOT NULL,
CellPhone Char(20) NOT NULL,
ExperienceLevel Char(25) NOT NULL,
);
CREATE TABLE SERVICE
(
PropertyID Int NOT NULL,
EmployeeID Int NOT NULL,
ServiceDate Char(15) NOT NULL,
HoursWorked Char(5) NOT NULL,
CONSTRAINT SERVICE_PK PRIMARY KEY(PropertyID, EmployeeID, ServiceDate),
CONSTRAINT SER_PRO_FK FOREIGN KEY(PropertyID)
REFERENCES PROPERTY(PropertyID),
CONSTRAINT SER_GG_EMP_FK FOREIGN KEY(EmployeeID)
REFERENCES GG_EMPLOYEE(EmployeeID),
);
INSERT INTO OWNER
VALUES ('Mary Jones', 'Mary.Jones#somewhere.com', 'Individual');
INSERT INTO OWNER
VALUES('DT Enterprises', 'DTE#dte.com', 'Corporation');
INSERT INTO OWNER
VALUES('Sam Douglas', NULL, 'Individual');
INSERT INTO OWNER
VALUES('UNY Enterprises', 'UNYE#unye.com', 'Corporation');
INSERT INTO OWNER
VALUES('Doug Samuels', 'Doug.Samuels#somewhere.com', 'Individual');
INSERT INTO PROPERTY
VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119');
INSERT INTO PROPERTY
VALUES('Elm St Apts', '4 East Elm', 'Lynwood', 'WA', '98223');
INSERT INTO PROPERTY
VALUES('Jefferson Hill', '42 West 7th St', 'Bellevue', 'WA', '98007');
INSERT INTO PROPERTY
VALUES('Lake View Apts', '1265 32nd Avenue', 'Redmond', 'WA', '98054');
INSERT INTO PROPERTY
VALUES('Kodak Heights Apts', '65 32nd Avenue', 'Rochester', 'NY', '14604');
INSERT INTO PROPERTY
VALUES('Private Residence', '1456 48th St', 'Bellevue', 'WA', '98007');
INSERT INTO PROPERTY
VALUES('Private Residence', '1567 51st St', 'Bellevue', 'WA', '98007');
INSERT INTO PROPERTY
VALUES('Private Residence', '718 151st St', 'Rochester', 'NY', '14604');
INSERT INTO GG_EMPLOYEE
VALUES('Smith', 'Sam', '206-254-1234', 'Master');
INSERT INTO GG_EMPLOYEE
VALUES('Evanston', 'John', '206-254-2345', 'Senior');
INSERT INTO GG_EMPLOYEE
VALUES('Murray', 'Dale', '206-254-3456', 'Junior');
INSERT INTO GG_EMPLOYEE
VALUES('Murphy', 'Jerry', '585-545-8765', 'Master');
INSERT INTO GG_EMPLOYEE
VALUES('Fontaine', 'Joan', '206-254-4567', 'Senior');
INSERT INTO SERVICE VALUES('2015-05-05', '4.50');
INSERT INTO SERVICE VALUES('2015-05-08', '2.75');
INSERT INTO SERVICE VALUES('2015-05-08', '4.50');
INSERT INTO SERVICE VALUES('2015-05-19', '3.00');
INSERT INTO SERVICE VALUES('2015-05-12', '7.50');
INSERT INTO SERVICE VALUES('2015-05-10', '2.50');
INSERT INTO SERVICE VALUES('2015-05-19', '2.50');
INSERT INTO SERVICE VALUES('2015-05-15', '2.75');
If someone could please help me, that would be great!
Columns OwnerID in Property Table and PropertyId and EmployeeID in Service table are making trouble for you.
Following are the reason for that.
1) All the mentioned columns are set as NOT NULL. So these column
always expect value for every insert.
2) Also,In 'service' table ,columns PropertyId and EmployeeID are
part of composite primary key. Primary key doesn't allow NULL values.
3)Once you handle the points 1 and 2,You can make the insert by
mentioning columns in your INSERT query,like below
INSERT INTO PROPERTY (PropertyName,Street,City,[State],Zip)
VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119');
INSERT INTO [SERVICE] (ServiceDate,HoursWorked)
VALUES('2015-05-05', '4.50');
http://sqlfiddle.com/#!6/3ea54
You need to say who is the owner, the insert doesnt know that.
here I assing every property to owner = 1
INSERT INTO PROPERTY VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119', 1);
INSERT INTO PROPERTY VALUES('Elm St Apts', '4 East Elm', 'Lynwood', 'WA', '98223', 1 );
INSERT INTO PROPERTY VALUES('Jefferson Hill', '42 West 7th St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Lake View Apts', '1265 32nd Avenue', 'Redmond', 'WA', '98054', 1);
INSERT INTO PROPERTY VALUES('Kodak Heights Apts', '65 32nd Avenue', 'Rochester', 'NY', '14604', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '1456 48th St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '1567 51st St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '718 151st St', 'Rochester', 'NY', '14604', 1);
hey luke problem is that in insert query values count is not matching with column count of tables.
if u want to use advantage of Auto increment property write insert query with column names.
and column 'OwnerID' in PROPERTY table and 'EmployeeID' in SERVICE table can not be null as you restricted in table creation.
Related
CREATE TABLE DON
(
REGI_NUM INTEGER NOT NULL UNIQUE,
MAKE VARCHAR(20) NOT NULL,
MODEL VARCHAR(20) NOT NULL,
TYPE VARCHAR(20) NOT NULL,
CATEGORY VARCHAR(20) NOT NULL,
DAILY_RENTAL_RATE INTEGER NOT NULL,
PRIMARY KEY(REGI_NUM)
);
INSERT INTO CAR
VALUES (**389238**,'TOYOTA','FJ cruise','sedan','luxury',49);
In this data and following data have same REGI_NUM. when I add them in tera term VT, it said unique constraint violated. How can I add them?
INSERT INTO CAR
VALUES (**389238**, 'MITSUBISHI', 'cruise', 'hatchback', 'luxury', 67);
INSERT INTO CAR
VALUES (326372, 'TOYOTA', 'MDX', 'sedan', 'normal', 20);
INSERT INTO CAR
VALUES (324244, 'Acura', 'FJ cruise', 'SUV', 'luxury', 57);
INSERT INTO CAR
VALUES (124345, 'Acura', 'TL 4dr', 'sedan', 'normal', 23);
INSERT INTO CAR
VALUES (326372, 'Aucara', 'D345', 'sedan', 'luxury', 49);
INSERT INTO CAR
VALUES (389238, 'TOYOTA', 'FJ cruise', 'sedan', 'normal', 24);
INSERT INTO CAR
VALUES (324244, 'Honda', 'odyseey', 'sedan', 'luxury', 57);
Update
Sorry it needs to be like this:
CREATE TABLE DON
(REGI_NUM INTEGER NOT NULL,
MAKE VARCHAR(20) NOT NULL,
MODEL VARCHAR(20) NOT NULL,
TYPE VARCHAR(20) NOT NULL,
CATEGORY VARCHAR(20) NOT NULL,
DAILY_RENTAL_RATE INTEGER NOT NULL,
PRIMARY KEY(REGI_NUM));
INSERT INTO CAR
VALUES (389238,'TOYOTA','FJ cruise','sedan','luxury',49);
INSERT INTO CAR
VALUES (389238,'MITSUBISHI','cruise','hatchback','luxury',67);
INSERT INTO CAR
VALUES (326372,'TOYOTA','MDX','sedan','normal',20);
INSERT INTO CAR
VALUES (324244,'Acura','FJ cruise','SUV','luxury',57);
INSERT INTO CAR
VALUES (124345,'Acura','TL 4dr','sedan','normal',23);
INSERT INTO CAR
VALUES (326372,'Aucara','D345','sedan','luxury',49);
INSERT INTO CAR
VALUES (389238,'TOYOTA','FJ cruise','sedan','normal',24);
INSERT INTO CAR
VALUES (324244,'Honda','odyseey','sedan','luxury',57);
PRIMARY KEY(REGI_NUM))
Since REGI_NUM is the primary key every entry for that column should be unique and not null.
If you want to repeat values, create a table without a primary key on REGI_NUM and add another column to have unique/not null values for primary key if there is a purpose.
I have created 2 tables both linked with a (pID) as the primary and foreign key. The pID is an (auto increment field) primary key in the painters table and the foreign in the painter_specs table. How do i auto fill the pID field in the painter_specs to the corresponding pID field in the painters table (which is auto incremented)?
Here is my code
CREATE TABLE painters (
pID INT NOT NULL AUTO_INCREMENT,
First_Name CHAR(45) NOT NULL,
Last_Name CHAR(45) NOT NULL,
Address CHAR(100) NOT NULL,
primary key (pID));
INSERT INTO painters VALUE (NULL, 'John', 'Doe', '123 Markt St Manassas VA 20021');
INSERT INTO painters VALUE (NULL, 'Jane', 'Smith', '342 Water St. Centreville VA 20121');
INSERT INTO painters VALUE (NULL, 'Mike', 'Williams', '390 Bank Ave Fairfax VA 20201');
CREATE TABLE painter_specs (
pID FOREIGN KEY REFERENCES painters(pID);
Title CHAR(100) NOT NULL,
Date DATE NOT NULL,
Painter CHAR(90) NOT NULL,
);
INSERT INTO painter_specs VALUE(NULL, 'Morning Mist', '10/10/2015', 'Beautiful painting about mist', 'John Doe');
INSERT INTO painter_specs VALUE(NULL, 'Evening Shadows', '10/12/2015', 'Beautiful painting about shadows', 'John Doe');
INSERT INTO painter_specs VALUE(NULL, 'Afternoon Thoughts', '10/11/2015', 'Beautiful painting about memories', 'Jane Smith');
Using the insert into statement, I currently have NULL in place of the value to be auto populated in the painter_specs table by the painters table.
To insert values into the painters table, use the following:
INSERT INTO painters (First_Name,Last_Name, Address) VALUES
('John', 'Doe', '123 Markt St Manassas VA 20021'),
( 'Jane', 'Smith', '342 Water St. Centreville VA 20121'),
('Mike', 'Williams', '390 Bank Ave Fairfax VA 20201');
Once you the database populates the pID column in the painters table, replace the [pID] in the insert statements for the painter_specs table below with the pID the row references as part of the FOREIGN KEY - e.g 1, 2, 3, etc.
INSERT INTO painter_specs (pID, Title, Date, Painter) VALUES
([pID], 'Morning Mist', '10/10/2015', 'Beautiful painting about mist', 'John Doe'),
([pID], 'Evening Shadows', '10/12/2015', 'Beautiful painting about shadows', 'John Doe'),
([pID], 'Afternoon Thoughts', '10/11/2015', 'Beautiful painting about memories', 'Jane Smith');
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
I keep having an issue where I'm trying to load all my tables into a database.
Whenever I try load a table which references "paper_code" I keep getting problem errors, which then don't create the table.
I've been trying to nail out the errors and have got some papers loading, but it seems only the tables that reference papers(paper_code) are having issues.
code:
DROP TABLE contacts;
DROP TABLE gender;
DROP TABLE lecture_location;
DROP TABLE enrols;
DROP TABLE teaches;
DROP TABLE staff;
DROP TABLE student;
DROP TABLE papers;
DROP TABLE departments;
CREATE TABLE departments
(dept_id INT PRIMARY KEY,
dept_location VARCHAR2(25) NOT NULL,
dept_name VARCHAR2(25) NOT NULL);
INSERT INTO departments VALUES
(01, 'ALBANY STREET', 'COMPUTER SCIENCE');
INSERT INTO departments VALUES
(02, 'UNION STREET', 'MUSIC');
INSERT INTO departments VALUES
(03, 'HOWE STREET', 'ANATOMY');
INSERT INTO departments VALUES
(04, 'ANZAC AVENUE', 'THEATRE');
CREATE TABLE papers
(paper_code INT,
EFTS INT NOT NULL,
dept_id INT REFERENCES departments(dept_id),
PRIMARY KEY(paper_code, dept_id));
INSERT INTO papers VALUES
(160, 0.18, 01);
INSERT INTO papers VALUES
(241, 0.18, 02);
INSERT INTO papers VALUES
(344, 0.18, 03);
INSERT INTO papers VALUES
(444, 0.18, 04);
CREATE TABLE student
(student_id INT PRIMARY KEY,
fname CHAR(11) NOT NULL,
lname CHAR(11) NOT NULL,
degree CHAR(15) NOT NULL);
INSERT INTO student VALUES
(172384, 'Michael', 'McDonald', 'BSc');
INSERT INTO student VALUES
(849294, 'Matthew', 'Brockie', 'BA');
INSERT INTO student VALUES
(384583, 'Daniel', 'Anderson', 'BSc');
CREATE TABLE staff
(staff_id INT PRIMARY KEY,
dept_id INT REFERENCES departments(dept_id),
fname CHAR(15) NOT NULL,
lname CHAR(15) NOT NULL);
INSERT INTO staff VALUES
(31, 01, 'Tony', 'Michaels');
INSERT INTO staff VALUES
(32, 01, 'Steph', 'Cardy');
INSERT INTO staff VALUES
(33, 02, 'Alex', 'Freeland');
INSERT INTO staff VALUES
(34, 02, 'Sam', 'Stewart');
INSERT INTO staff VALUES
(35, 03, 'Monique', 'Cardy');
INSERT INTO staff VALUES
(36, 03, 'Bayan', 'Zach');
CREATE TABLE teaches
(paper_code INT,
staff_id INT REFERENCES staff(staff_id),
dept_id INT,
PRIMARY KEY(paper_code, staff_id, dept_id),
FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));
INSERT INTO teaches VALUES
(160, 32, 01);
INSERT INTO teaches VALUES
(241, 31, 01);
INSERT INTO teaches VALUES
(344, 33, 02);
INSERT INTO teaches VALUES
(241, 34, 03);
INSERT INTO teaches VALUES
(444, 35, 03);
INSERT INTO teaches VALUES
(444, 36, 04);
CREATE TABLE enrols
(paper_code INT REFERENCES papers(paper_code),
student_id INT REFERENCES student(student_id),
date_enrolled DATE,
PRIMARY KEY(paper_code, student_id));
INSERT INTO enrols VALUES
(160, 172384, TO_DATE('22-Mar-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(444, 849294, TO_DATE('14-Jul-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(444, 172384, TO_DATE('23-Mar-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(160, 384583, TO_DATE('07-Aug-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES --fix
(160, 172384, TO_DATE('30-Jul-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(241, 849294, TO_DATE('08-Sep-1995', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(241, 384583, TO_DATE('25-Dec-1996', 'dd-mon-yyyy'));
CREATE TABLE lecture_location
(paper_code INT REFERENCES papers(paper_code),
dept_id INT REFERENCES departments(dept_id),
lecture_loc VARCHAR2(15),
PRIMARY KEY(paper_code, dept_id, lecture_loc));
INSERT INTO lecture_location VALUES
(160, 'ARCHWAY');
INSERT INTO lecture_location VALUES
(241, 'CASTLE');
CREATE TABLE gender
(student_id INT PRIMARY KEY REFERENCES student(student_id),
gender CHAR(9) NOT NULL);
INSERT INTO gender VALUES
(172384, 'Female');
INSERT INTO gender VALUES
(384583, 'Male');
CREATE TABLE contacts
(contact_details INT,
staff_id INT REFERENCES staff(staff_id),
PRIMARY KEY(contact_details, staff_id));
INSERT INTO contacts VALUES
(022017456, 31);
INSERT INTO contacts VALUES
(034737447, 31);
INSERT INTO contacts VALUES
(02285756, 32);
INSERT INTO contacts VALUES
(034735858, 32);
INSERT INTO contacts VALUES
(034552097, 33);
INSERT INTO contacts VALUES
(022867385, 33);
INSERT INTO contacts VALUES
(021495939, 34);
INSERT INTO contacts VALUES
(034993872, 35);
INSERT INTO contacts VALUES
(027459278, 36);
COMMIT;
All the tables except the ones refering paper_code are working.
Errors are:
INSERT INTO teaches VALUES
*
ERROR at line 1:
ORA-02291: integrity constraint (DAANDERSON.SYS_C00623423) violated - parent
key not found
For enrols:
(paper_code INT REFERENCES papers(paper_code),
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list
For lecture_location
(paper_code INT REFERENCES papers(paper_code),
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list
Been trying for a few hours and stil can't get to work. Any information appreciated!
When creating your table teaches, you have:
FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));
So every (paper_code, dept_id) must exist as (paper_code, dept_id) in papers.
Your 2nd entry (241, 01) does not exist, nor does your fourth value (241, 03).
(others may be missing, I didn't check them all)
You're also not inserting dept_id values into lecture_location
You have a primary key value duplicated in enrols:
(160, 172384)
This part of enrols:
CREATE TABLE enrols
(paper_code INT REFERENCES papers(paper_code),
is invalid, since you must reference a unique foreign key - and the only unique key on papers is its primary key of (paper_code, dept_id)
Ditto for this part of lecture_location:
(paper_code INT REFERENCES papers(paper_code),
On the bright side, after doing the fixes above the schema actually builds: http://sqlfiddle.com/#!4/5ed2c2
Hey everyone I have been having a problem with my SQL database, specifically the foreign key. Here is the table I am trying to enter into.
CREATE TABLE Employee (
EmployeeID int NOT NULL Primary Key,
LastName varchar(30) NOT NULL,
FirstName varchar(30) NOT NULL,
Address varchar(30) NOT NULL,
City varchar(20) NOT NULL,
State char(2) NOT NULL,
TelephoneAreacode char(5) NOT NULL,
TelephoneNumber char(8) NOT NULL,
JobTitle varchar(30) NOT NULL Foreign Key References Job_Title(JobTitle),
EEO1Classification varchar(30) NOT NULL,
HireDate char (10) NOT NULL,
Salary money NOT NULL,
Gender varchar(7) NOT NULL,
Age int NOT NULL
)
And the data
INSERT INTO Employee
VALUES ('95687', 'Edelman', 'Glenn', '175 Bishops Lane', 'LA Jolla', 'CA', '619', '5550199','Cashier', 'Sales Workers', '10/7/2003', '$21,500', 'Male', '64'),
('95688', 'McMullen', 'Eric', '763 Church ST', 'Lemm Grove', 'CA', '619', '5550135','Bagger', 'Sales Workers', '11/1/2002', '$12,500', 'Male', '20'),
('95995', 'Slentz', 'Raj', '123 Torrey DR', 'North Clairmont', 'CA', '619', '5550123','Assistant Manager', 'Officials & Managers', '6/1/2000', '$48,000', 'Male', '34'),
('55978', 'Broun', 'Erin', '2045 Parkway - Apt2B', 'Encinitas', 'CA', '760', '5550100', 'Bagger','Sales Workers', '3/12/2003', '$10,530', 'Female', '24'),
('55928', 'Carpenter', 'Donald', '927 Second St', 'Encinitas', 'CA', '760','5550154', 'Stocker','Office/Clerical', '11/1/2003', '$15,000', 'Male', '18'),
('59852', 'Esquivez', 'David', '10983 N. Coast Hwy Apt 902', 'Encinitas', 'CA', '760', '5550108','Butchers & Seafood Specialists', 'Operatives (Semi skilled)', '8/1/2003', '$19,000', 'Male', '22'),
('52362', 'Sharp', 'Nancy', '10793 Montecino RD', 'Ramona', 'CA', '858', '5550135', 'Cashier','Sales Workers', '7/12/2003', '$21,000', 'Female', '24');
The table with the foreign key is this one,
CREATE TABLE Job_Title (
JobTitle varchar(30) NOT NULL Primary key,
EEO1Classification varchar(30) NOT NULL,
JobDescription varchar(100) NOT NULL,
ExemptNonExempt varchar(30) NOT NULL,
)
And the data already entered there is
INSERT INTO Job_Title
VALUES
('Accounting Clerk','Office/Clerical', 'Records Data', 'Non-Exempt'),
('Assistant Manager','Officials & Managers', 'Supervises and coordinates activities', 'Exempt'),
('Bagger','Sales Workers', 'Places customer orders in bags', 'Non-Exempt'),
('Cashier','Sales Workers', 'Operates cash register to itemize and total customer’s purchases', 'Non-Exempt'),
('Computer Support Specialist','Technician', 'Installs, modifies, and makes minor repairs to personal computers', 'Non-Exempt'),
('Dir. of Fin. & Acct.','Officials & Managers', 'Plans and directs the finance and accounting activities', 'Exempt'),
('Asst. - Bakery & Pastry','Craft Workers (Skilled)', 'Bakes Cakes and Pastries', 'Non-Exempt'),
('Butchers & Seafood Specialists','Operatives (Semi skilled)', 'Cuts Meat and seafood', 'Non-Exempt'),
('Stocker','Office/Clerical', 'Stocks Shelves', 'Non-Exempt');
Please help.
Make sure that the values you are inserting in the table that contains the foreign key ( child table ) has a corresponding value in the table that contains the primary key (parent Table )
i,e you can not insert something into the child table that has no corresponding value in the parent table due to the pk -Fk relation ship
Sometimes the error occurs due to the reason that the Child table does not have the corresponding values already present in the Parent table's column which is being referenced as the foreign key. To do:
Truncating both the tables (make sure to backup the data if its
bulky) could simply fix the problem.
Try entering values in the Child table that are existing already in
the Parent table's column. Hope this would help.