Can't update table value with schema name - sql

CREATE TABLE DBD_D_155150401111052.STUDENT(
STUDENT_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (STUDENT_ID),
STATE_ID CHAR(3),
CONSTRAINT STATE_ID FOREIGN KEY (STATE_ID) REFERENCES DBD_D_155150401111052.PROPINSI (STATE_ID),
CITY_ID CHAR(3),
CONSTRAINT CITY_ID FOREIGN KEY (CITY_ID) REFERENCES DBD_D_155150401111052.KOTA (CITY_ID),
NAME VARCHAR(45),
BIRTH_DATE DATE,
COURSE VARCHAR(10)
);
INSERT INTO DBD_D_155150401111052.STUDENT (STUDENT_ID, NAME, BIRTH_DATE, CITY_ID, STATE_ID, COURSE)
VALUES ('1111171', 'DEMILOV', '1997-05-01', 'A01', '001', 'SI-A'),
('1111172', 'MICHAEL', '1996-03-20', 'A01', '001', 'SI-A'),
('1111181', 'IRVING', '1998-10-14', 'C01', '003', 'IF-B'),
('1111182', 'JHON', '1998-08-15', 'B01', '002', 'IF-C'),
('1111191', 'THEODORE', '1996-01-02', 'A03', '001', 'IF-A'),
('1111192', 'SALLY', '1995-12-31', 'A02', '001', 'SI-B'),
('1111212', 'JASON', '1997-07-28', 'A02', '001', 'IF-B'),
('1111213', 'DAVID', '1998-11-11', 'B02', '002', 'IF-C'),
('1111214', 'JAMES', '1997-12-12', 'C01', '003', 'SI-B'),
('1111215', 'HELENA', '1996-06-08', 'C01', '003', 'IF-B');
UPDATE TABLE DBD_D_155150401111052.STUDENT
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';
I can't execute the last query.
The warning says "JOIN" was expected instead of ".", but I have no idea how to fix it.
The table value can't updated because table name include schema, is there any way to update the value?

TABLE is not part of UPDATE syntax. I would suggest you try:
UPDATE DBD_D_155150401111052.STUDENT
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';

Table
is a Reserved word try this :
UPDATE [DBD_D_155150401111052.STUDENT]
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';

Related

MYSQL 1:M Recursive Relationship

I've been trying to figure out this question all day, I've created a table and trying to make a Select statement that would answer this question
Who are the mothers in the Simpsons family tree? List them from oldest
to youngest.
With my current data I don't know how I would go about doing this with only one table.
CREATE TABLE person
(
persID INT IDENTITY(1,1),
persFName VARCHAR(40) NOT NULL,
persLName VARCHAR(40) NOT NULL,
persGender CHAR(1) NOT NULL,
persDOB DATE NOT NULL,
persDOD DATE ,
fatherID INT ,
motherID INT ,
CONSTRAINT pk_person_persID PRIMARY KEY(persID),
CONSTRAINT fk_person_fatherID FOREIGN KEY (fatherID) REFERENCES person(persID),
CONSTRAINT fk_person_motherID FOREIGN KEY (motherID) REFERENCES person(persID),
);
And this is my data
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ('Abraham', 'Simpson', 'M', '1944-01-15', '2015-07-21',NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ('Mona', 'Simpson', 'F', '1946-09-22', NULL,NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Herb', 'Simpson', 'M', '1963-11-21',NULL,1,2)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Homer', 'Simpson', 'M', '1965-05-19',NULL,1,2)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Clancy', 'Bouvier', 'F', '1945-02-12',NULL,NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Jackie', 'Bouvier', 'M', '1945-12-01','2016-05-15',NULL,NULL)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Marge', 'Simpson', 'F', '1966-05-18',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Patty', 'Bouvier', 'F', '1964-01-08',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Selma', 'Bouvier', 'F', '1969-03-01',NULL,6,5)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Bart', 'Simpson', 'M', '1990-01-01',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Lisa', 'Simpson', 'F', '1992-05-15',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Maggie', 'Simpson', 'F', '1997-11-28',NULL,4,7)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID) VALUES ( 'Ling', 'Bouvier', 'M', '2000-04-02',NULL,NULL,9)
Try this:
SELECT *
FROM person
WHERE persID IN (SELECT DISTINCT motherID FROM person)
AND persLNAme = 'Simpson'
ORDER BY persDOB ASC;
The idea is to get all mothers' IDs and then to extract them from the base table. Then we are using ORDER BY clause to sort the result.
This should give you what you are looking for but I am not sure if it is the smartest way! I hope this help.
SELECT DISTINCT T2.motherID,
T1.persID,
T1.PersFName,
T1.PersLName,
T1.PersGender,
T1.PersDOB,
T1.PersDOD,
T1.FatherID,
T1.MotherID
FROM person AS T1
LEFT JOIN person AS T2 ON T1.persID = T2.motherID
WHERE T2.motherID IS NOT NULL
AND T1.persLName = 'Simpson'
ORDER BY T1.persDOB

Inserting foreign key into SQL table

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.

Microsoft SQL Server

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

joining three tables together using Inner Joins

Using Table aliases, list the first name, last name and start date of students enrolled on the java fundamentals module:
I am having some trouble when running the query below.
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol
ON stu.StudID = enrol.StudID
INNER JOIN Module mod
ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Structure:
CREATE TABLE Student
(StudID INTEGER PRIMARY KEY,
StudFName VARCHAR(10) NOT NULL,
StudLName VARCHAR(10) NOT NULL,
DoB DATE NOT NULL,
Sex CHAR(1) NOT NULL CHECK (Sex IN ('M', 'F')),
Email VARCHAR(30) UNIQUE);
CREATE TABLE Staff
(StaffID INTEGER PRIMARY KEY,
Title VARCHAR(4) CHECK (Title IN ('Prof', 'Dr', 'Mr', 'Mrs', 'Miss')),
StaffFName VARCHAR(10) NOT NULL,
StaffLName VARCHAR(10) NOT NULL,
Email VARCHAR(30) UNIQUE,
Department VARCHAR(25) DEFAULT 'Not Assigned',
Extension INTEGER CHECK (Extension BETWEEN 0001 AND 9999));
CREATE TABLE Module
(ModCode CHAR(4) PRIMARY KEY,
ModName VARCHAR(25) NOT NULL,
ModCredits INTEGER NOT NULL CHECK (ModCredits IN (15, 30, 45, 60)),
ModLevel CHAR(3) NOT NULL CHECK (ModLevel IN ('UG1', 'UG2', 'UG3', 'MSc')),
ModLeader INTEGER NOT NULL,
Foreign Key (ModLeader) REFERENCES Staff (StaffID));
CREATE TABLE Enrolment
(ModCode CHAR(4) NOT NULL,
StudID INTEGER NOT NULL,
StartDate DATE NOT NULL,
PRIMARY KEY (ModCode, StudID),
Foreign Key (StudID) REFERENCES Student (StudID),
Foreign Key (ModCode) REFERENCES Module (ModCode));
The answer is... there is no trouble with your query.
It works just fine.
Check here
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(1, 'Jack', 'Black', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'jack#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(2, 'Andrew', 'Wiggin', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'andrew#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(3, 'Bob', 'Marley', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'bob#email.com');
INSERT INTO Staff (StaffID, Title, StaffFName, StaffLName, Email, Extension) VALUES
(1, 'Prof', 'Joe', 'Smith', 'stuff#emal.com', 0001);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(1, 'Java Fundamentals', 30, 'UG1', 1);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(2, 'C# Fundamentals', 15, 'UG2', 1);
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 1, TO_DATE('2015/01/01', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 2, TO_DATE('2015/01/02', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(2, 3, TO_DATE('2015/01/03', 'yyyy/mm/dd'));
-------------------------------------------------------------
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol ON stu.StudID = enrol.StudID
INNER JOIN Module mod ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Result
STUDFNAME STUDLNAME STARTDATE
----------------------------------------
Jack Black January, 01 2015
Andrew Wiggin January, 02 2015

Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint

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.