Trying to fill table with default value - sql

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

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.

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

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?

Creating a trigger to adjust product stock - APEX ORACLE

I'm trying to create a trigger on APEX ORACLE so that when a purchase goes through the orderline table, the quantity that is set when the customer buys the products is taken off the stock in the products table.
CREATE or Replace TRIGGER updatestock
AFTER DELETE OR UPDATE OF QUANTITY.ORDERLINE ON ORDERLINE
FOR EACH ROW
BEGIN
SET PRODUCT_STOCK.PRODUCTS = PRODUCT_STOCK.PRODUCTS - QUANTITY.ORDERLINE
WHERE PRODUCT_ID.ORDERLINE = PRODUCT_ID.PRODUCTS
END;
Basically I want the trigger to notice the quantity in the orderline and minus it from the product stock. However, I get the following error:
ORA-01748: only simple column names allowed here
Could anyone point me in the right direction?
SQL:
DROP TABLE ADMIN CASCADE CONSTRAINTS ;
DROP TABLE USERS CASCADE CONSTRAINTS ;
DROP TABLE STALLS CASCADE CONSTRAINTS ;
DROP TABLE PRODUCTS CASCADE CONSTRAINTS ;
DROP TABLE STALLHOLDER CASCADE CONSTRAINTS ;
DROP TABLE CUSTOMERORDER CASCADE CONSTRAINTS;
DROP TABLE ORDERLINE CASCADE CONSTRAINTS;
DROP TABLE COLLECTION CASCADE CONSTRAINTS;
drop sequence ADMIN_ID_SEQ;
drop sequence USER_ID_SEQ;
drop sequence STALL_ID_SEQ;
drop sequence PRODUCT_ID_SEQ;
drop sequence STALLHOLDER_ID_SEQ;
drop sequence CUSTOMERORDER_ID_SEQ;
drop sequence ORDERLINE_ID_SEQ;
drop sequence COLLECTION_ID_SEQ;
create sequence ADMIN_ID_SEQ start with 1;
create sequence USER_ID_SEQ start with 1;
create sequence STALL_ID_SEQ start with 1;
create sequence PRODUCT_ID_SEQ start with 1;
create sequence STALLHOLDER_ID_SEQ start with 1;
create sequence CUSTOMERORDER_ID_SEQ start with 1;
create sequence COLLECTION_ID_SEQ start with 1;
create sequence ORDERLINE_ID_SEQ start with 1;
CREATE table COLLECTION (
COLLECTION_ID NUMBER(20) NOT NULL PRIMARY KEY,
STATUS VARCHAR(10) NOT NULL,
TIME NUMBER(4) NOT NULL);
CREATE table CUSTOMERORDER (
ORDER_ID NUMBER(15) NOT NULL PRIMARY KEY,
ORDER_DATE NUMBER(8) NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
COLLECTION_ID NUMBER(20) NOT NULL);
CREATE TABLE STALLS (
STALL_ID NUMBER(15) NOT NULL PRIMARY KEY,
STALL_NAME VARCHAR(25) NOT NULL,
STALL_DESC VARCHAR(100),
STALL_TYPE VARCHAR(25) NOT NULL,
STALLHOLDER_ID NUMBER(25) NOT NULL);
CREATE TABLE PRODUCTS (
PRODUCT_ID NUMBER(15) NOT NULL PRIMARY KEY,
PRODUCT_NAME VARCHAR(25) NOT NULL,
PRODUCT_TYPE VARCHAR(15) NOT NULL,
PRODUCT_PRICE NUMBER(6,2) NOT NULL,
PRODUCT_STOCK NUMBER(3) NOT NULL,
STALL_ID NUMBER(15) NOT NULL);
CREATE table ORDERLINE (
ORDERLINE_ID NUMBER(15) NOT NULL PRIMARY KEY,
QUANTITY NUMBER(2) NOT NULL,
TOTALPRICE DECIMAL(19,4) NOT NULL,
ORDER_ID NUMBER(15) NOT NULL,
PRODUCT_ID NUMBER(15) NOT NULL);
CREATE table USERS (
USER_ID NUMBER(15) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL,
NAME VARCHAR2(25) NOT NULL,
SURNAME VARCHAR2(25) NOT NULL,
ADDRESS VARCHAR2(100) NOT NULL,
CONTACTNO NUMBER(11) NOT NULL);
CREATE table STALLHOLDER (
STALLHOLDER_ID NUMBER(25) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL,
NAME VARCHAR2(25) NOT NULL,
SURNAME VARCHAR2(25) NOT NULL,
CONTACTNO NUMBER(11) NOT NULL);
CREATE table ADMIN (
ADMIN_ID NUMBER(15) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL);
INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Test', 'Test');
INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Admin', 'Admin');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder1', 'Stallholder1', 'Stall 1', 'Stall 1', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder2', 'Stallholder2', 'Stall 2', 'Stall 2', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder3', 'Stallholder3', 'Stall 3', 'Stall 3', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder4', 'Stallholder4', 'Stall 4', 'Stall 4', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder5', 'Stallholder5', 'Stall 5', 'Stall 5', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder6', 'Stallholder6', 'Stall 6', 'Stall 6', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test', 'Test', 'Test', 'Test', 'Test', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test2', 'Test2', 'Test2', 'Test2', 'Test2', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test3', 'Test3', 'Test3', 'Test3', 'Test3', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test4', 'Test4', 'Test4', 'Test4', 'Test4', '0');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store', '', 'Meat', '1');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store 2', '', 'Meat', '2');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store', '', 'Confectionary', '3');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store 2', '', 'Confectionary', '4');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Clothing Store', '', 'Clothing', '5');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Phone Store', '', 'Clothing', '6');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15', '0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets','1.50','50','0','3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets', '1.50', '50','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'T-Shirts', 'Clothing', '10.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Shorts', 'Clothing', '15.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Jeans', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Hoodies', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Vests', 'Clothing', '10.00', '20','0', '5');
ALTER TABLE STALLS ADD CONSTRAINT FK_STALLHOLDER_ID FOREIGN KEY (STALLHOLDER_ID) REFERENCES STALLHOLDER(STALLHOLDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES CUSTOMERORDER(ORDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_PRODUCT_ID FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID);
ALTER TABLE CUSTOMERORDER ADD CONSTRAINT FK_COLLECTION_ID FOREIGN KEY (COLLECTION_ID) REFERENCES COLLECTION(COLLECTION_ID);
ALTER TABLE PRODUCTS ADD CONSTRAINT FK_STALL_ID FOREIGN KEY (STALL_ID) REFERENCES STALLS(STALL_ID);
​
The error is presumably coming from the OF QUANTITY.ORDERLINE because you're using a table.column pattern rather than just the column name. You also seem to be consistently putting the table and column names the wrong way around, and you're missing the UPDATE before SET, and you are trying to refer to columns in the table row you're updating rather than using the NEW pseudorow. So this should be closer:
CREATE or Replace TRIGGER updatestock
AFTER DELETE OR UPDATE OF QUANTITY ON ORDERLINE
FOR EACH ROW
BEGIN
UPDATE PRODUCTS
SET PRODUCT_STOCK = PRODUCT_STOCK - :NEW.QUANTITY
WHERE PRODUCT_ID = :NEW.PRODUCT_ID
END;
But NEW values are null on delete; you aren't doing any adjustment in insert; and even on update you maybe want to be taking both the old and new quantity into account.
Even then trying to maintain a running total in another table only really works in a single-user system, or where updates are serialised. If two people update the same orderline row at the same time, the trigger will apply both updates to the products table, but the second will block until the first has committed, and will be applied to the old value at the start of that statement - so you'll lose the first update, effectively.

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.

Getting error creating a basic sql table

Here is the code I used to create the table:
CREATE TABLE product
(
ProductID int NOT NULL PRIMARY KEY,
ProductName varchar (50) NOT NULL,
Brand varchar (50) NOT NULL,
Price money NOT NULL,
Quantity int NOT NULL,
DateAdded date NOT NULL
)
Here is what I'm trying to insert:
INSERT INTO product
VALUE (100, 'Radio', 'Sony', 29.99, 30, '2012-08-22')
The error I'm getting is:
"Incorrect syntax near 'VALUE'."
Any ideas?
use VALUES in you query,
INSERT INTO product
VALUES (100, 'Radio', 'Sony', 29.99, 30, '2012-08-22')
It's VALUES and not VALUE:
INSERT INTO product VALUES (100, 'Radio', 'Sony', 29.99, 30, '2012-08-22')