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.
Related
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’
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.
This may be confusing, but basically what I have is the following tables:
CREATE TABLE Employee(
eid int NOT NULL,
fname varchar(20),
lname varchar(20),
zip int,
PRIMARY KEY (eid));
CREATE TABLE Customer(
cid int NOT NULL,
fname varchar(20),
lname varchar(20),
street varchar(20),
city varchar(20),
zip int,
PRIMARY KEY (cid));
CREATE TABLE Orders(
oid int NOT NULL,
rdate date,
sdate date,
cid int NOT NULL,
eid int NOT NULL,
PRIMARY KEY (oid),
FOREIGN KEY (cid) REFERENCES Customer(cid),
FOREIGN KEY (eid) REFERENCES EMPLOYEE(eid));
One of the questions for my assignment is the following:
Create and execute a query that lists employee information together with the number or orders they have processed
Can someone help get me started? I literally don't even know where to begin...
SELECT ???
FROM ???
WHERE ???
SELECT e.fname, e.lname, e.zip,
OrdersProcessed = COUNT(o.oid)
FROM Orders o
INNER JOIN Employee e ON o.eid = e.eid
GROUP BY e.fname, e.lname, e.zip
The following query was successful but when I enter null values into order id and different values(those which do not exist in the column) for item id, the order id is still getting incremented. How to retain the same order id? Is there a problem with the way the foreign key has been stated?
CREATE TABLE orders(
orderid int not null auto_increment,
itemid int not null,
quantity int not null,
tot_price int not null,
cid int not null,
code varchar(10),
order_time time,
order_date date ,
constraint order_pk primary key (orderid, itemid),
foreign key (itemid) references items(itemid),
foreign key (cid) references customer(cid),
foreign key (code) references coupon(code)
);
The problem stems from trying to do a one-to-many (order to items) relationship in a single table. This will end badly. You need two. One for the order, one for the items in the order. For good or bad, this is how relational databases do lists.
CREATE TABLE orders (
orderid int not null primary key auto_increment,
cid int not null references customer(cid),
code varchar(10) references coupon(code),
when_ordered datetime,
INDEX(when_ordered)
);
CREATE TABLE items_in_an_order (
orderid int not null references orders(id),
itemid int not null references items(id),
quantity int not null,
price_paid int not null
);
quantity moves to items_in_an_order. I changed total_price to the more descriptive price_paid which is likely more descriptive of what you want to be storing. This will let you produce receipts.
order_date and order_time were unnecessarily split into two columns. This makes comparison and sorting awkward. Putting them together into one DATETIME column and indexing it will let you do sorting by date/time and check if an order is inside a date/time range.
To get all the items in an order, do a join.
SELECT items.itemid
FROM items
JOIN items_in_an_order io ON io.itemid = items.id
WHERE io.orderid = ?
To find all the orders an item is in, also do a join.
SELECT orders.id
FROM orders
JOIN items_in_an_order io ON io.orderid = orders.id
WHERE io.itemid = ?
I want to make a link between a table customer and a table product by an IdProduct.
Example:
Create table customer(
idcustomer INT not null,
name Varchar(20),
idproduct INT,
);
create table Product(
idproduct INT not null,
nameProduct varchar(40)
);
How can I link the two together like the foreign key system for, when I select a customer, I can get all his products? It's a question about the structure of the database.
You want to introduce a 3rd table to resolve the many-to-many relationship between customers and products. It should consist of idcustomer and idproduct.
Then, to get all the products for a given customer:
SELECT c.name, p.nameProduct
FROM Customer c
INNER JOIN CustomerProductXref cpx
ON c.idcustomer = cpx.idcustomer
INNER JOIN product p
ON cpx.idproduct = p.idproduct
WHERE c.idcustomer = 12345
In mysql a foreign key is a special type of constraint. It is preferably created with the table, but can also be added afterwards. In this case, you might define the constraint as:
ALTER TABLE customer
ADD FOREIGN KEY (idproduct)
REFERENCES Product (idproduct);
(Note that you have to use the InnoDB engine to take advantage of FK's in mysql. More here
However FK's aren't required to make a JOIN, which is how you would link the tables in a SELECT -
select c.idcustomer, c.name, p.nameproduct
from customer c
join Product p on p.idproduct=c.idproduct;
Here's how you'd make a foreign key constraint (ignoring the cardinality issues that Joe rightly suggests):
CREATE table Product(
idproduct INT not null,
nameProduct varchar(40),
PRIMARY KEY (idproduct )
);
CREATE table customer(
idcustomer INT not null,
name Varchar(20),
idproduct INT,
FOREIGN KEY (idproduct) REFERENCES Product(idproduct )
);
Get your data like this:
SELECT * FROM Product AS P
INNER JOIN Customer AS C ON C.idproduct = P.idproduct
WHERE C.idcustomer = 1