I have 2 tables, Suppliers, and Products. I'm trying to fill them with random info.
Table Products, Column supplier_id. I set this column default value to -1.
I'm getting error on the line where I'm trying to insert default value in given column.
Check the code below, error msg at the bottom of the post.
SUPPLIERS:
CREATE TABLE Suppliers
(
id int primary key identity(1,1),
"name" nvarchar(50) not null,
telephone nvarchar(50) default 'N/A',
"address" nvarchar(50) default 'N/A'
)
INSERT INTO Suppliers (name,telephone,address)
values ('Supplier 1', '021-555-333', 'Jovana Petrovica 72, Beograd 11000'),
('Supplier 2', '021-555-333', 'Branka Radicevica 13, Zrenjanin 23000'),
('Supplier 3', '021-555-333', 'Bulevar Oslobodjenja 30A, Novi Sad 21000')
PRODUCTS:
CREATE TABLE Products
(
id int primary key identity(1,1),
"name" nvarchar(50) not null,
price float default 0,
supplier_id int default -1,
foreign key (supplier_id) references Suppliers(id) ON DELETE SET DEFAULT
)
INSERT INTO Products (name, price, supplier_id)
values ('Product 1', 800, 3),
('Product 2', 1300, 1),
('Product 3', 230, 2),
('Product 4', 570, 3),
('Product 5', 1225, 2);
INSERT INTO Products (name, price)
values ('Product 6', 80); // I read online this is the way to insert default values (?)
Error msg:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Products__suppli__03F0984C". The conflict occurred in database "Tehna", table "dbo.Suppliers", column 'id'.
The statement has been terminated.
I guess that default 'supplier_id' should be greater than 0 or null, because it's related to 'id' in Suppliers which is primary key and also an integer
I have been attempting to create a query for checking available rooms between two dates but have been thus far unsuccessful. Please find the sql code for creating the tables below. I really really need your help. this is done because the system wants me to add more details
CREATE TABLE rooms (
id NUMERIC(3) NOT NULL,
type VARCHAR2(11) NOT NULL,
CONSTRAINT pk_rooms PRIMARY KEY (id),
CONSTRAINT fk_type
);
INSERT INTO rooms VALUES (101, 'Excellent');
INSERT INTO rooms VALUES (102, 'Excellent');
INSERT INTO rooms VALUES (103, 'Excellent');
INSERT INTO rooms VALUES (104, 'Excellent');
INSERT INTO rooms VALUES (105, 'Excellent');
INSERT INTO rooms VALUES (106, 'Excellent');
INSERT INTO rooms VALUES (107, 'Excellent');
INSERT INTO rooms VALUES (108, 'Excellent');
INSERT INTO rooms VALUES (109, 'Excellent');
INSERT INTO rooms VALUES (110, 'Excellent');
INSERT INTO rooms VALUES (111, 'Excellent');
INSERT INTO rooms VALUES (112, 'Excellent');
CREATE TABLE bookings_roombookings (
book_id VARCHAR(20) NOT NULL,
room_id NUMBER NOT NULL,
fromdate DATE NOT NULL,
todate DATE NOT NULL,
guest1 VARCHAR(40) NOT NULL,
guest2 VARCHAR(40),
guest3 VARCHAR(40),
CONSTRAINT cpk PRIMARY KEY (book_id, room_id),
CONSTRAINT fk_rid FOREIGN KEY (room_id) REFERENCES rooms(id));
INSERT INTO bookings_roombookings VALUES (
'DTR5000000', 320,
'01-JUN-2020', '01-SEP-2020',
'D.Trump', 'H.Clinton', 'S.Daniels');
INSERT INTO bookings_roombookings VALUES (
'DRI0000002', 102,
'11-DEC-19', '01-SEP-20',
'D.Ridley', NULL, NULL);
my query is as follows:
select r.id from rooms r
where r.id not in
( select bkr.room_id from bookings_roombookings bkr
where (fromdate > '15-JUN-20') and (todate < '18-JUN-20') );
but this will return both rows of the bkr table. I would appreciate any help.
You can start from the room table, and use a not exists condition with a correlated subquery to eliminate rooms that have a reservation period which overlaps the target date range:
select r.*
from rooms r
where not exists (
select 1
from bookings_roombookings bkr
where
bkr.room_id = r.id
and bkr.fromdate <= date'2020-06-15'
and bkr.todate >= date'2020-06-18'
)
Note: do not rely on implicit conversions from strings to date (as in fromdate > '15-JUN-20'), that depends on the nls settings of your database and session; instead, you can use date litterals, that are part of the ANSI SQL standard and which Oracle supports, like date'yyyy-mm-dd'.
CREATE TABLE collections
(
id INTEGER IDENTITY(1,1) PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE posts
(
id INTEGER IDENTITY(1, 1) PRIMARY KEY,
title VARCHAR(50) NOT NULL,
image_URL VARCHAR(120) NOT NULL,
author_id INTEGER NOT NULL,
collection_id INTEGER,
FOREIGN KEY(author_id) REFERENCES users(id),
FOREIGN KEY(collection_id) REFERENCES collections(id)
);
INSERT INTO collections (name)
VALUES ('Travel'), ('Nature'), ('Great Outdoors');
INSERT INTO posts (title, image_url, author_id, collection_id)
VALUES ('White Mountains', 'cloudinary.com/snp/balhwpq.jpg', 3, NULL),
('My new car!', 'cloudinary.com/snp/qpcnalpw.jpg', 1, NULL),
('Great weekend for camping', 'cloudinary.com/snp/nmclapa.png', 5, 3),
('Train Ride to France', 'cloudinary.com/snp/rjaptpalw.jpg', 4, 1),
('Small Town in Italy', 'cloudinary.com/snp/aqypqoua.jpg', 4, 1),
('Nice Day At The Air Show', 'cloudinary.com/snp/aswpqlsaq.jpg', 7, NULL),
('Barcelona Beach', 'cloudinary.com/snp/quapwnla.jpg', 9, NULL),
('Gorgeous City of Prague', 'cloudinary.com/snp/anvmapw.png', 3, NULL),
('Nothing like fresh Seafood!', 'cloudinary.com/snp/uqnqhapla.jpg', 8, 4),
('Bought a new Swiss army knife', 'cloudinary.com/snp/qanvpauaq.jpg', 6, NULL),
('Delicious meal in Boston', 'cloudinary.com/snp/malvpqlsa.jpg', 10, NULL),
('Swimming at Ursa Beach', 'cloudinary.com/snp/poqlanoqlav.jpg', 12, NULL),
('Chilly day in Norway', 'cloudinary.com/snp/vqwlnqposp.jpg', 11, NULL),
('Hiking through beautiful Vermont!', 'cloudinary.com/snp/qpoplnalqi.jpg', 5, 3);
Every time I try executing this, it completes the table creation and the first two insert statements and then I get an error
The INSERT statement conflicted with the FOREIGN KEY constraint
I believe the problem is that in the posts table Insert statement I'm setting some of the collection_id values to NULL which is causing the error. I never said that this column couldn't be NULL, so what's the problem?
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