I have an unusally worded Question that even my lab professor wasnt 100% sure on what it wants, but does have some idea which ill bring up. The given question is "A query listed below must be implemented with set algebra operations: Find a code of all customers who ordered product Tofu or product Tunnbrod"
My Professor believes its asking for a UNION JOIN in which i have come up with the following:
SELECT CUSTOMER_CODE
FROM ORDERS
WHERE CUSTOMER_CODE = ORDER_ID
UNION ALL
SELECT PRODUCT_NAME
FROM ORDER_DETAIL
WHERE PRODUCT_NAME = 'Tunnbrod' AND ‘Tofu’;
This code acts like a List and just makes Tunnbrod a customer code, so im unsure of what i need to actually do. My professor did mention joing the or and and operands, in which i tried and failed to get the expected results.
below are the tables for reference
CREATE TABLE ORDERS
(
ORDER_ID DECIMAL(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID DECIMAL(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE,
SHIPPED_DATE DATE,
SHIP_VIA VARCHAR(40),
FREIGHT DECIMAL(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40),
SHIP_ADDRESS VARCHAR(60),
SHIP_CITY VARCHAR(15),
SHIP_REGION VARCHAR(15),
SHIP_POSTAL_CODE VARCHAR(10),
SHIP_COUNTRY VARCHAR(15),
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
CREATE TABLE ORDER_DETAIL
(
ORDER_ID DECIMAL(9) NOT NULL,
PRODUCT_NAME VARCHAR(40) NOT NULL,
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
QUANTITY DECIMAL(9) NOT NULL DEFAULT 1 ,
DISCOUNT DECIMAL(4,2) NOT NULL DEFAULT 0,
CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);
This is a strangely formulated question to an otherwise simple thing:
Select * orders inner join order_details where PRODUCT_NAME = 'Tunnbrod' OR PRODUCT_NAME = ‘Tofu’
you can, of course, explicitly use UNION instead of OR:
Select * orders inner join order_details where PRODUCT_NAME = 'Tunnbrod'
UNION
Select * orders inner join order_details where PRODUCT_NAME = ‘Tofu’
Related
I've seen many examples of how to fix this issue "Make sure that the foreign key matches the value of your primary key". But it still doesn't solve my problem
CREATE TABLE `order table`(
OrderId INT PRIMARY KEY NOT NULL,
`Product one` VARCHAR(60),
`Product two` VARCHAR(60),
`Product three` VARCHAR(60),
CustomerId INT NOT NULL,
FOREIGN KEY (CustomerId) REFERENCES customers(CustomerId)
);
INSERT INTO `new_schema`.`order table`(
OrderId, `Product one`, `Product two`, `Product three`, CustomerId)
VALUES ('0', 'Ryzen 7 3700xt',' Power Supply', 'Stuff', 0);
And yes, in my customer table there is a customer id with an int of 0, no typos as far as I'm concerned
I have a question in which im required to delete information without dropping or suspending constraints. The question, if allowed to drop constraints is simple. However i am allowed to change Values from NULL to NOT NULL.
DELETE FROM SUPPLIER
WHERE COUNTRY = 'USA';
however another table PRODUCT is the foreign key of a column SUPPLIER_NAME within SUPPLIER. I am not allowed to drop this Constraint.
Ive tried setting the Foreign key to null but it doesnt Work.
EDIT im not allowed to restrict or suspend constraints
this is the full question
"Delete from the database information about all suppliers located in USA. Information about all products supplied by the suppliers located in USA must remain in the database. You are not allowed to drop and/or to suspend any referential integrity constraints and you must modify one of NULL/NOT NULL consistency constraints. "
The tables being used
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20),
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
UNITS_IN_STOCK DECIMAL(9) NOT NULL DEFAULT 0,
UNITS_ON_ORDER DECIMAL(9) NOT NULL DEFAULT 0,
REORDER_LEVEL DECIMAL(9) NOT NULL DEFAULT 0,
DISCONTINUED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
CREATE TABLE SUPPLIER
(
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30),
CONTACT_TITLE VARCHAR(30),
ADDRESS VARCHAR(60),
CITY VARCHAR(15),
REGION VARCHAR(15),
POSTAL_CODE VARCHAR(10),
COUNTRY VARCHAR(15),
PHONE VARCHAR(24),
FAX VARCHAR(24),
HOME_PAGE VARCHAR(500),
CONSTRAINT PK_SUPPLIER PRIMARY KEY (COMPANY_NAME)
);
As the foreign key to your PRODUCT table doe not specify an ON DELETE action, it'll have the default behaviour which is RESTRICT. Since you can't update this constraint to SET NULL, you'd probably have to set them NULL yourself.
First, alter the table so the SUPPLIER_NAME foreign key can accept NULL values.
Then, update the PRODUCTS whose supplier are in the USA, set their SUPPLIER_NAME to NULL. Something like this:
update PRODUCT set SUPPLIER_NAME = NULL where SUPPLIER_NAME IN (
select SUPPLIER_NAME from SUPPLIER where COUNTRY = 'USA');
And at last you can then delete the SUPPLIERS with COUNTRY = 'USA'.
We’re trying to delete all rows that have data for Account name= Banana Republic....
So far we have:
Delete Account_T
From Account_T
Join Program_T
Where Account_T.AccountName = Program_T.AccountName
And AccountName = ‘Banana Republic’
Here are the tables:
create table Program_T
(AccountName varchar(150) not null unique,
ProgramID int not null,
Revenue int,
Advocates int,
Shares int,
Conversions int,
Impressions int,
LaunchDate date,
CSMID int not null,
constraint Program_PK primary key (AccountName, CSMID),
constraint Program_FK1 foreign key (AccountName) references Account_T(AccountName),
constraint Program_FK2 foreign key (CSMID) references CSM_T(CSMID));
and Account_T table:
create table Account_T
(AccountName varchar(150) not null unique,
Health varchar(10) not null,
EcommercePlatform varchar(50),
CSMID int not null,
Industry varchar(50),
Amount int not null,
constraint Accounts_PK primary key (AccountName),
constraint Accounts_FK foreign key (CSMID) references CSM_T(CSMID));
Why not just do two deletes?
delete a from Account_T a
where AccountName = 'Banana Republic';
delete p from Program_T p
where AccountName = 'Banana Republic';
You used the bad character before and after Banana Republic.
Write the query like this :
DELETE Account_T
FROM Account_T AS A
JOIN Program_T AS P
ON A.AccountName = P.AccountName
Where A.AccountName = 'Banana Republic'
I wrote DELETE, but don't forget to always test with a SELECT first !
Use the ON clause with your JOIN instead of the jointure condition in the WHERE, its important to respect this.
List those publishers who publish paperbacks and the number of paperbacks published by each.
I'm having a difficult time with the counting the paperbacks for each publisher. Any help will be much appreciated. Thanks!
Henry Books Table Schema
**author**
authorNum INT PRIMARY KEY
authorLast VARCHAR(12),
authorFirst VARCHAR(10)
**publisher**
publisherCode CHAR(3) PRIMARY KEY
publisherName VARCHAR(25)
city VARCHAR(20)
**book**
bookCode CHAR(4) PRIMARY KEY
title VARCHAR(40)
publisherCode CHAR(3)
bookType CHAR(3)
paperback ENUM('No', 'Yes')
CONSTRAINT book_fk_publisher
FOREIGN KEY (publisherCode)
REFERENCES publisher(publisherCode)
**branch**
branchNum INT
branchName VARCHAR(50)
branchLocation VARCHAR(50)
**copy**
bookCode CHAR(4)
branchNum INT
copyNum INT PRIMARY KEY
quality ENUM('Excellent', 'Fair', 'Good', 'Poor')
price DECIMAL(8,2)
CONSTRAINT copy_pk
PRIMARY KEY (bookCode, branchNum, copyNum
CONSTRAINT copy_fk_book
FOREIGN KEY (bookCode)
REFERENCES book(bookCode),
CONSTRAINT copy_fk_branch
FOREIGN KEY (branchNum)
REFERENCES branch(branchNum)
**wrote**
bookCode CHAR(4)
authorNum INT
sequence INT
PRIMARY KEY (BookCode, AuthorNum),
CONSTRAINT wrote_fk_book
FOREIGN KEY (bookCode)
REFERENCES book(bookCode),
CONSTRAINT wrote_fk_author
FOREIGN KEY (authorNum)
REFERENCES author(authorNum)
This is what I have:
SELECT publisherName, COUNT(paperback) AS "numPaperback"
FROM publisher, book
WHERE paperback = "Yes";
Seems like you are new. So I will try to walk you through this...
Join the publisher table to the book table. They share a publisher code which appears to link them together.
Filter the joined table by book type using a where clause. You want paperbacks.
Perform a group by on publisher code and name, and then count the book column
Does this help get you started?
You have to use count(*) and Group by for the count of paperbacks. This is how I would write the query:
SELECT publisherName,count(*) AS "Number of paperbacks" FROM
publisher p INNER JOIN book b WHERE
p.publisherCode=b.publisherCode AND b.paperback="Yes"
GROUP BY publisherName;
Nice to meet you and i hope i can get help here as i tried everything and apparently i cant make it work. I have 3 tables :
Clients
(
C_ID int NOT NULL,
C_LastName varchar(255) NOT NULL,
C_FirstName varchar(255),
C_street varchar(255),
C_postcode varchar(255),
C_city varchar(255),
C_DOB date,
C_Phone varchar(255),
C_Email varchar(255),
PRIMARY KEY (C_Id)
)
then
Orders
(
Order_Id int NOT NULL,
Order_date date,
C_Id int,
Employee_Id int,
PRIMARY KEY (Order_Id),
FOREIGN KEY (C_Id) REFERENCES Clients(C_Id),
FOREIGN KEY (Employee_Id) REFERENCES Employee(Employee_Id)
)
and the last one
Orders_line
(
Order_line_id NOT NULL,
Order_Id int NOT NULL,
Product_Id int,
Order_qty int,
Order_unit_price int,
Order_subtotal int,
Order_grand_total int,
PRIMARY KEY (Order_line_Id),
FOREIGN KEY (Order_Id) REFERENCES Orders(Order_Id),
FOREIGN KEY (Product_Id) REFERENCES Product(Product_Id)
)
Question is in my orders_line table I have 3 orders_id which they purchased the same item. The idea is to list all c_id which they bought the common items.
I am very new in this so please have mercy with me :)
My Current attempt
SELECT COUNT(c.C_ID), c.c_firstname, c.c_lastname,o.Order_Id
FROM Clients c, orders_line o
Where c.C_ID=o.Product_Id
And
SELECT COUNT(c.C_ID), c.c_firstname , c.c_lastname ,o.Order_Id
FROM orders c, orders_line o
Where c.C_ID=o.Product_Id
and anything i try is showing me only 1 client with an order id (always order id 1) with different numbers
I'm still not entirely sure what you need, but in your WHERE clauses you have c.C_ID=o.Product_Id. You don't want to get results where the Customer ID equals the Product ID - that doesn't make sense. You'll need to include the Orders table to join customer and product data.
Something like this would be closer:
SELECT c.c_firstname, c.c_lastname, o.Order_Id
FROM Clients c, orders_line ol, orders o
Where c.C_ID=o.C_ID
and o.Order_Id = ol.Order_Id
I recommend writing out samples of your data and what you expect as results, which might help you see more clearly how the data is related.