Creating a trigger to adjust product stock - APEX ORACLE - sql

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.

Related

SQL script for multiple reservations by one customer (Restaurant)

I need my SQL db to be able to hold more than one reservation for a customer.
However I get some duplicate pk errors.
Here is my SQL script: (I wanna be able to let one customer have more than 1 reserv.)
/*
db_name: Restaurant DB
update_timestamp: 6.10.2021 # 09:48
sign: The Actual Daniel
*/
DROP DATABASE IF EXISTS restaurantDB;
CREATE DATABASE restaurantDB;
USE restaurantDB;
CREATE TABLE `bill` (
`billID` int NOT NULL AUTO_INCREMENT,
`payAmount` varchar(255) NOT NULL,
PRIMARY KEY (`billID`)
);
INSERT INTO `bill` VALUES (1, '$258');
INSERT INTO `bill` VALUES (2, '$420');
INSERT INTO `bill` VALUES (3, '$114');
INSERT INTO `bill` VALUES (4, '$88');
INSERT INTO `bill` VALUES (5, '$137');
INSERT INTO `bill` VALUES (6, '$286');
INSERT INTO `bill` VALUES (7, '$97');
INSERT INTO `bill` VALUES (8, '$110');
INSERT INTO `bill` VALUES (9, '$358');
INSERT INTO `bill` VALUES (10, '$190');
CREATE TABLE `customer` (
`customerID` int NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`customerID`)
);
INSERT INTO `customer` VALUES (1, 'Nero', 'Lee', '420420589');
INSERT INTO `customer` VALUES (2, 'Lucius', 'Malt', '578632221');
INSERT INTO `customer` VALUES (3, 'Aurelian', 'Keen', '639456773');
INSERT INTO `customer` VALUES (4, 'Jasmine', 'Wang', '227453891');
INSERT INTO `customer` VALUES (5, 'Sophie', 'Clinck', '905674321');
INSERT INTO `customer` VALUES (6, 'Augustus', 'Smith', '523764112');
INSERT INTO `customer` VALUES (7, 'Isabelle', 'Niaom', '786554330');
INSERT INTO `customer` VALUES (8, 'Alix', 'Black', '009886775');
INSERT INTO `customer` VALUES (9, 'Leia', 'Mudworth', '345667228');
INSERT INTO `customer` VALUES (10, 'Claudius', 'Reed', '427654221');
CREATE TABLE `meal` (
`mealID` int NOT NULL AUTO_INCREMENT,
`sides` varchar(255) NULL,
`main` varchar(255) NULL,
`beverage` varchar(255) NOT NULL,
PRIMARY KEY (`mealID`)
);
INSERT INTO `meal` VALUES (1, 'potatos', 'steak', 'wine');
INSERT INTO `meal` VALUES (2, 'ice', 'cube steak', 'spring water');
INSERT INTO `meal` VALUES (3, 'rice', 'cake', 'beer');
INSERT INTO `meal` VALUES (4, 'salad', 'pasta', 'wine');
INSERT INTO `meal` VALUES (5, 'radish', 'noodles', 'soju');
INSERT INTO `meal` VALUES (6, 'rice', 'rice', 'pumpkin juice');
INSERT INTO `meal` VALUES (7, 'butter, honey', 'steak', 'wine');
INSERT INTO `meal` VALUES (8, 'ice', 'salad', 'mineral water');
INSERT INTO `meal` VALUES (9, 'sweet potatos', 'pork', 'beer');
INSERT INTO `meal` VALUES (10, 'sourdough bread', 'fruit bowl', 'mineral water');
CREATE TABLE `order` (
`orderID` int NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`orderID`)
);
INSERT INTO `order` VALUES (1, '21-09-27 13:36:06');
INSERT INTO `order` VALUES (2, '21-02-14 17:18:03');
INSERT INTO `order` VALUES (3, '21-03-27 11:34:16');
INSERT INTO `order` VALUES (4, '21-07-14 15:58:32');
INSERT INTO `order` VALUES (5, '21-02-27 18:16:33');
INSERT INTO `order` VALUES (6, '21-01-14 19:28:12');
INSERT INTO `order` VALUES (7, '21-08-27 16:27:07');
INSERT INTO `order` VALUES (8, '21-06-14 11:21:09');
INSERT INTO `order` VALUES (9, '21-11-27 16:30:03');
INSERT INTO `order` VALUES (10, '21-03-14 17:22:11');
CREATE TABLE `reservation` (
`reservationID` int NOT NULL AUTO_INCREMENT,
`checkIn` datetime NOT NULL,
`checkOut` datetime NOT NULL,
`customerForeign` int NOT NULL,
PRIMARY KEY (`reservationID`),
FOREIGN KEY (`customerForeign`) REFERENCES customer(customerID)
);
INSERT INTO `reservation` VALUES (1, '21-09-27 13:24:06', '21-09-27 14:17:23', 1);
INSERT INTO `reservation` VALUES (2, '21-02-14 17:25:03', '21-02-14 19:22:14', 2);
INSERT INTO `reservation` VALUES (3, '21-03-27 11:34:16', '21-03-27 13:34:16', 3);
INSERT INTO `reservation` VALUES (4, '21-07-14 15:58:32', '21-07-14 16:58:32', 4);
INSERT INTO `reservation` VALUES (5, '21-02-27 18:16:33', '21-02-27 19:16:33', 5);
INSERT INTO `reservation` VALUES (6, '21-01-14 19:28:12', '21-01-14 20:28:12', 6);
INSERT INTO `reservation` VALUES (7, '21-08-27 16:27:07', '21-08-27 17:27:07', 7);
INSERT INTO `reservation` VALUES (8, '21-06-14 11:21:09', '21-06-14 14:21:09', 8);
INSERT INTO `reservation` VALUES (9, '21-11-27 16:30:03', '21-11-27 17:30:03', 9);
INSERT INTO `reservation` VALUES (10, '21-03-14 17:22:11', '21-03-14 19:22:11', 10);
CREATE TABLE `table` (
`tableID` int NOT NULL,
`numOfSeats` int NOT NULL,
PRIMARY KEY (`tableID`)
);
INSERT INTO `table` VALUES (1, 1);
INSERT INTO `table` VALUES (2, 2);
INSERT INTO `table` VALUES (3, 2);
INSERT INTO `table` VALUES (4, 2);
INSERT INTO `table` VALUES (5, 4);
INSERT INTO `table` VALUES (6, 4);
INSERT INTO `table` VALUES (7, 6);
INSERT INTO `table` VALUES (8, 6);
INSERT INTO `table` VALUES (9, 8);
INSERT INTO `table` VALUES (10, 12);
ALTER TABLE `bill` ADD CONSTRAINT `fk_bill_order_1` FOREIGN KEY (`billID`) REFERENCES `order` (`orderID`);
ALTER TABLE `meal` ADD CONSTRAINT `fk_meal_order_1` FOREIGN KEY (`mealID`) REFERENCES `order` (`orderID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_customer_1` FOREIGN KEY (`orderID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `reservation` ADD CONSTRAINT `fk_reservation_customer_1` FOREIGN KEY (`reservationID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `table` ADD CONSTRAINT `fk_table_reservation_1` FOREIGN KEY (`tableID`) REFERENCES `reservation` (`reservationID`);
This is error I get when trying to insert new reservation for customer with ID of 1

SQL Query: To list the majors with more than 2 students in it

I am trying to figure out a query that will list the major that has more than 2 students in it (CS) but I am not sure how to go about it.
This is the script to create the table for SQL!
REM drop all the tables. Note that you need to drop the
REM dependent table first before dropping the base tables.
drop table Reg;
drop table Student;
drop table Course;
REM Now create all the tables.
create table Student
(
sid char(10) primary key,
sname varchar(20) not null,
gpa float,
major char(10),
dob DATE
);
create table Course
(
cno char(10) primary key,
cname varchar(20) not null,
credits int,
dept char(10)
);
create table Reg
(
sid references Student(sid) on delete cascade,
cno references Course(cno) on delete cascade,
grade char(2),
primary key (sid, cno)
);
REM Now insert all the rows.
insert into Student values ('111', 'Joe', 3.5 , 'MIS', '01-AUG-2000');
insert into Student values ('222', 'Jack', 3.4 , 'MIS', '12-JAN-1999');
insert into Student values ('333', 'Jill', 3.2 , 'CS', '15-MAY-1998');
insert into Student values ('444', 'Mary', 3.7 , 'CS', '17-DEC-2001');
insert into Student values ('555', 'Peter', 3.8 , 'CS', '19-MAR-1999');
insert into Student values ('666', 'Pat', 3.9, 'Math', '31-MAY-2000');
insert into Student values ('777', 'Tracy', 4.0, 'Math', '18-JUL-1997');
insert into Course values ('c101', 'intro', 3 , 'CS');
insert into Course values ('m415', 'database', 4 , 'Bus');
insert into Course values ('m215', 'programming', 4 , 'Bus');
insert into Course values ('a444', 'calculus', 3 , 'Math');
insert into Reg values ('111', 'c101', 'A');
insert into Reg values ('111', 'm215', 'B');
insert into Reg values ('111', 'm415', 'A');
insert into Reg values ('222', 'm215', 'A');
insert into Reg values ('222', 'm415', 'B');
insert into Reg values ('333', 'c101', 'A');
insert into Reg values ('444', 'm215', 'C');
insert into Reg values ('444', 'm415', 'B');
insert into Reg values ('555', 'c101', 'B');
insert into Reg values ('555', 'm215', 'A');
insert into Reg values ('555', 'm415', 'A');
insert into Reg values ('666', 'c101', 'A');
Thank you so much!
this will list group by major within CS and having more than 2
select major ,count(1) from Student
where major ='CS'
group by major
having count(1)>2

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

Running an SQL Script file and having issues printing the Current_Orders table cant understand the errors

I have to prepare and execute a script file to create and populate a relational database in Oracle with data about customers, current orders, and products. These are the relationships: Each customer can place any number of current orders and each current order is placed by one customer.Each current order is for one product but each product can be in any number of
current orders.
I can't figure out the errors when running this script. The Customer and Products table are successfully filled, but the Current_Orders table is not. There are a few error codes that appear when I run it but I am not understanding them as I am fairly new to SQL. Any help would be appreciated. Thanks! ORA-00001, ORA-00955, ORA-02449.
drop table Customer;
drop table Current_Orders;
drop table Products;
create table Customer
(Customer_Number integer not null,
Customer_Name char(20) not null,
Customer_City char(20) not null,
PRIMARY KEY(Customer_Number));
create table Products
(Product_Number integer not null,
Product_Description char(20) not null,
Unit_Price integer not null,
PRIMARY KEY (Product_Number));
create table Current_Orders
(Order_Number integer not null,
Order_Date date not null,
Shipping_Method char(20) not null,
Product_Number integer not null,
Quantity_Ordered integer not null,
Customer_Number integer not null,
PRIMARY KEY(Order_Number), FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE,
FOREIGN KEY(Product_Number) references Products(Product_Number)
ON DELETE CASCADE);
insert into Customer
values (1, 'Khizur Sheikh', 'Milpitas');
insert into Customer
values (2, 'Fatima Sheikh', 'Fremont');
insert into Customer
values (3, 'Syed Sheikh', 'Davis');
insert into Customer
values (4, 'Mohammed Sheikh', 'Oakland');
insert into Customer
values (5, 'Ali Sheikh', 'Dublin');
insert into Products
values (6, 'iphone', 300);
insert into Products
values (7, 'ipad', 400);
insert into Products
values (8, 'imac', 500);
insert into Products
values (9, 'ipod', 600);
insert into Products
values (10, 'ihome', 700);
insert into Products
values (11, 'apple', 800);
insert into Products
values (12, 'banana', 900);
insert into Products
values (13, 'orange', 1000);
insert into Products
values (14, 'grape', 1100);
insert into Products
values (15, 'avocado', 1200);
insert into Products
values (16, 'bread', 1300);
insert into Products
values (17, 'muffin', 1400);
insert into Products
values (18, 'cheese', 1500);
insert into Products
values (19, 'milk', 1600);
insert into Products
values (20, 'brownies', 1700);
insert into Products
values (21, 'candy', 1800);
insert into Products
values (22, 'soup', 1900);
insert into Products
values (23, 'strawberry', 11000);
insert into Products
values (24, 'cookies', 50);
insert into Products
values (25, 'chocolate', 10);
insert into Current_Orders
values (26, '22-oct-2017', 'truck', 6, 1, 1);
insert into Current_Orders
values (27, '22-nov-2017', 'ship', 7, 1, 2);
insert into Current_Orders
values (28, '22-dec-2017', 'train', 8, 3, 3);
insert into Current_Orders
values (29, '22-jan-2017', 'truck', 9, 2, 4);
insert into Current_Orders
values (30, '22-feb-2017', 'train', 10, 1, 5);
insert into Current_Orders
values (31, '22-mar-2017', 'truck', 12, 4, 2);
insert into Current_Orders
values (32, '22-apr-2017', 'plane', 17, 7, 4);
insert into Current_Orders
values (33, '22-may-2017', 'train', 19, 1, 5);
insert into Current_Orders
values (34, '22-jun-2017', 'ship', 22, 3, 2);
insert into Current_Orders
values (35, '22-jan-2017', 'ship', 21, 4, 3);
commit;
All the commands in your script work fine while running for the first time. However Issue seems to occur when you run the DROP TABLE statements on second and successive executions when table exists and has data.
drop table Customer;
drop table Current_Orders;
Error report - ORA-02449: unique/primary keys in table referenced by
foreign keys
02449. 00000 - "unique/primary keys in table referenced by foreign keys"
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
*Action: Before performing the above operations the table, drop the
foreign key constraints in other tables.
I see you have defined a FOREIGN KEY on the column Customer_Number
FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE
So, when you try to DROP from Table Customer before Current_Orders , you are basically deleting parent record before deleting the child record which is not allowed. ORA-00955 and ORA-00001 occur as a consequence.
So, follow this order to drop tables in your script.
drop table Current_Orders;
drop table Products;
drop table Customer;

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