SQL Server - The INSERT statement conflicted with the FOREIGN KEY constraint - sql

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?

Related

ORA-02291 error. Unsure what is causing the problem

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.

Trying to fill table with default value

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

SQL Developer - Integrity Constraint , parent key not found ( while inserting values )

I know that in order to insert values in a table which relies on foreign keys you need to have data in that primary key of that table .
These are my constraints :
ALTER TABLE DIDACT
MODIFY (CONSTRAINT id_prof_fk FOREIGN KEY(id_prof) REFERENCES profs (id_prof));
ALTER TABLE DIDACT
MODIFY (CONSTRAINT id_course_fk FOREIGN KEY(id_course) REFERENCES courses (id_course));
Next I insert values in both profs and courses tables :
INSERT INTO courses VALUES ('21', 'Logic', 1, 1, 5);
INSERT INTO courses VALUES ('22', 'Math', 1, 1, 4);
INSERT INTO courses VALUES ('23', 'OOP', 1, 2, 5);
INSERT INTO courses VALUES ('24', 'DB', 2, 1, 8);
INSERT INTO courses VALUES ('25', 'Java', 2, 2, 5);
INSERT INTO profs VALUES ('p1', 'Mary', 'Banks', 'Prof');
INSERT INTO profs VALUES ('p2', 'Francis', 'Steven', 'Conf');
INSERT INTO profs VALUES ('p3', 'John', 'Jobs', 'Prof');
INSERT INTO profs VALUES ('p4', 'Alex', 'Brown', 'Prof');
INSERT INTO profs VALUES ('p5', 'Dan', 'Lovelace', 'Lect');
INSERT INTO profs VALUES ('p6', 'Roxanne', 'Smith', 'Conf');
Then I'm trying to populate the DIDACT table:
INSERT INTO didact VALUES ('p1','21');
INSERT INTO didact VALUES ('p3','21');
INSERT INTO didact VALUES ('p5','22');
But this occurs :
INSERT INTO didact VALUES ('p1','21') Error report - SQL Error:
ORA-02291: integrity constraint (user.ID_COURSE_FK) violated - parent
key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
These are my tables , in case it will help :
CREATE TABLE courses(
id_course CHAR(2),
course_name VARCHAR2(15),
year NUMBER(1),
semester NUMBER(1),
credits NUMBER(2)
)
CREATE TABLE profs(
id_prof CHAR(4),
name CHAR(10),
surname CHAR(10),
grade VARCHAR2(5)
)
CREATE TABLE didact(
id_prof CHAR(4),
id_course CHAR(4)
)
I'm struggling with this for about an hour and I still haven't managed to find my mistake.
Thank you.
You seem to have different formats for id_course in your tables. In didact it's id_course CHAR(4) and in courses it's id_course CHAR(2).
And since you use a fixed-length type the value in didact will differ from the one in courses by two added blanks.

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

Multiple inserts with composite type

Consider the following schema
CREATE TYPE episode_id AS
(
season_nr int,
episode_nr int,
show_id bigint
);
CREATE TABLE episodes
(
id episode_id primary key,
title varchar(255) not null,
release_date timestamptz null
);
I want to insert to multiple rows in one query;
insert into episodes (id.season_nr, id.episode_nr, id.show_id, title, release_date)
values
(1, 1, 58811, 'Pilot', null),
(1, 2, 58811, 'Vector', null),
(1, 3, 58811, '274', null),
(1, 4, 58811, 'Single Strand', null),
(1, 5, 58811, 'The White Room', null);
It throws:
> ERROR: multiple assignments to same column "id" SQL state: 42601
My client library does not support ROW syntax. How could I make this work?
Proper syntax:
INSERT INTO episodes (id, title, release_date)
VALUES
((1, 1, 58811), 'Pilot', null),
((1, 2, 58811), 'Vector', null);
You cannot reference individual attributes of id in the column list of an INSERT statement, just the whole column.
The added parentheses make a row type / composite type out of the three values.