How to consolidate my data for SQL Join Clause - sql

I have to write a query with the following criteria:
Write a query to list the title and artist of ONLY the items that have been ordered. Only list each title once.
Here are the CREATE tables that were made that cannot be changed.
CREATE TABLE artists
(
artist_id INT NOT NULL,
artist_name VARCHAR(30),
CONSTRAINT artist_pk PRIMARY KEY (artist_id)
);
CREATE TABLE items
(
item_id INT NOT NULL,
title VARCHAR(50) NOT NULL,
artist_id INT NOT NULL,
unit_price DECIMAL(9,2) NOT NULL,
CONSTRAINT items_pk PRIMARY KEY (item_id),
CONSTRAINT items_fk_artists
FOREIGN KEY (artist_id) REFERENCES artists (artist_id)
);
CREATE TABLE orders
(
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
shipped_date DATE,
employee_id INT,
CONSTRAINT orders_pk PRIMARY KEY (order_id),
CONSTRAINT orders_fk_customers
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_fk_employees
FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);
Here is what I have done for my script.
SELECT
items.title, artists.artist_name, orders.order_date
FROM
items
JOIN
orders
JOIN
artists;
Please let me know how I can consolidate.

You should say the structures of tables with respect to keys between tables.

Related

Getting ORA-00942 Error for "Table or View does not exist"

Not sure why, but referenced tabled not able to be identified. The following is the code:
CREATE TABLE Store (
Store_ID NUMBER NOT NULL,
Dept_ID NUMBER NOT NULL,
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
CONSTRAINT Store_PK PRIMARY KEY (Store_ID),
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID) REFERENCES Manager (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE Manager
(
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
Manager_Lname VARCHAR(30) NOT NULL,
Manager_Fname VARCHAR(30) NOT NULL,
CONSTRAINT Manager_PK PRIMARY KEY (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE OrderTable
(
Order_ID NUMBER NOT NULL,
Customer_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Tracking_Num NUMBER NOT NULL,
CONSTRAINT Order_PK PRIMARY KEY (Order_ID),
CONSTRAINT Customer_FK FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Shipping_FK FOREIGN KEY (Tracking_Num) REFERENCES Shipping (Tracking_Num)
);
CREATE TABLE Customer
(
Customer_ID NUMBER NOT NULL,
Customer_Lname VARCHAR(30),
Customer_Fname VARCHAR(30),
Email VARCHAR(30),
Payment_Type VARCHAR(15),
CONSTRAINT Customer_PK PRIMARY KEY (Customer_ID)
);
CREATE TABLE Product
(
Product_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Brand VARCHAR(30),
Classification VARCHAR(30),
CONSTRAINT Product_PK PRIMARY KEY (Product_ID)
);
CREATE TABLE Employee
(
Employee_ID NUMBER NOT NULL,
Title INT,
Employee_Lname VARCHAR(30),
Employee_Fname VARCHAR(30),
CONSTRAINT Employee_PK PRIMARY KEY (Employee_ID)
);
CREATE TABLE Vendor
(
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Quantity NUMBER,
CONSTRAINT Vendor_PK PRIMARY KEY (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID)
);
CREATE TABLE Retail
(
Retail_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Quantity NUMBER NOT NULL,
CONSTRAINT Retail_PK PRIMARY KEY (Retail_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID)
);
CREATE TABLE Shipping
(
Tracking_Num NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Address VARCHAR2(50),
Shipping_Date NUMBER NOT NULL,
CONSTRAINT Shipping_PK PRIMARY KEY (Tracking_Num),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID)
);
Assuming this is the order you're creating your tables, this is likely your problem:
CREATE TABLE Store (
...
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID)
REFERENCES Manager (Manager_ID)
...
You're referencing a table in your table definition that doesn't exist yet.

Utilizing subqueries in sql. Displaying no output

I have a task of retrieving the first and last names of customers where their orders have not shipped out. Below is the tables made to get this result ↓↓↓
CREATE TABLE employees
(
employee_id INT NOT NULL,
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
manager_id INT,
CONSTRAINT employees_pk PRIMARY KEY (employee_id),
CONSTRAINT emp_fk_mgr
FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
);
CREATE TABLE orders
(
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
shipped_date DATE,
employee_id INT,
CONSTRAINT orders_pk PRIMARY KEY (order_id),
CONSTRAINT orders_fk_customers
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_fk_employees
FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);
CREATE TABLE order_details
(
order_id INT NOT NULL,
item_id INT NOT NULL,
order_qty INT NOT NULL,
CONSTRAINT order_details_pk PRIMARY KEY (order_id, item_id),
CONSTRAINT order_details_fk_orders
FOREIGN KEY (order_id) REFERENCES orders (order_id),
CONSTRAINT order_details_fk_items
FOREIGN KEY (item_id) REFERENCES items (item_id)
);
Currently, I have the following query made up, but it keeps generating No Output.
SELECT c.customer_first_name
FROM order_details od
JOIN orders o ON od.order_id = o.order_id
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IN (SELECT shipped_date FROM orders WHERE shipped_date IS NULL);
Can someone help me to get the result I need?
Presumably, your customers are not dates and you actually intend:
WHERE c.customer_id IN (SELECT o.customer_id FROM orders o WHERE shipped_date IS NULL)

I can't create table error ORA_00903

DROP TABLE Orders CASCADE CONSTRAINTS;
DROP TABLE Order_Items CASCADE CONSTRAINTS;
CREATE TABLE Orders (
ORDER_NO VARCHAR(5),
ORDER_DATE DATE CONSTRAINT BNL_ORDER_DATE_NN NOT NULL,
CUSTOMER_NAME VARCHAR(20) CONSTRAINT BNL_CUSTOMER_NAME_NN NOT NULL,
POSTAGE NUMBER(5,2) CONSTRAINT BNL_POSTAGE_NN NOT NULL,
TOTAL NUMBER(10,2) CONSTRAINT BNL_TOTAL_NN NOT NULL,
CONSTRAINT ORD_ID_PK PRIMARY KEY (ORDER_NO)
);
CREATE TABLE Order_ITEMS (
ITEM_NO VARCHAR(10),
ITEM_DES VARCHAR(20),
IETM_SIZE VARCHAR(5) CONSTRAINT ITE_ITEM_SIZE_NN NOT NULL,
ITEM_COST NUMBER(10,2) CONSTRAINT ITE_ITEM_COST_NN NOT NULL,
QTY NUMBER(5) CONSTRAINT ITE_QTY_NN NOT NULL,
ORDER_NO NUMBER(5),
CONSTRAINT ITE_ID_PK PRIMARY KEY (ITEM_NO),
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
);
The Orders table is working but the Order_Items table is not working show error ORA_00903. I had change many other names, but it still show the error ORA-00903: invalid table name.
Your foreign key constraint is incorrect:
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
^
You have named the referenced table ORDER instead of ORDERS.

Database Create table Primary key not found

I have two tables i'm trying to create with foreign keys.
The statements are below
Book_Copy Table
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT primary_key PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
History table
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( bid, datetaken )
);
Now when I run it the first one says table created but i get the following for the second.
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( datetaken )
)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
There are several errors in the statements above and also not all the relevant information was made available.
Your table book_copy has a foreign key reference to the table book_catalog via the isbn column:
CREATE TABLE book_copy (
...
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
To proceed, I took the liberty to add a simple one:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT pk_ct PRIMARY KEY (isbn));
And similar, they history table also references another table member via the mid column:
CREATE TABLE history (
...
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
I also create that one in order to proceed:
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT pk_mem PRIMARY KEY (mid));
The error that you get above means what it says: There is no such primary key or unique key on the other table that you try to reference, hence a value cannot be uniquely identified and hence the integrity of the data not verified. So the database stops you from doing so in the first place.
The culprit is that you specify the PRIMARY KEY of your table book_copy to be bid,isbn while your foreign key reference in the history table only references the bid:
CREATE TABLE history (
...
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
So you ask the database to check the integrity on something that is not uniquely identifiable. In order to solve this you have to expand your foreign key of the history table to include the isbn column as well:
So the full DDL looks like this:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT book_catalog_pk PRIMARY KEY (isbn));
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT member_pk PRIMARY KEY (mid));
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT book_copy_pk PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
isbn VARCHAR2(15) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid, isbn )
REFERENCES book_copy ( bid, isbn ),
CONSTRAINT history_pk PRIMARY KEY ( bid, datetaken )
);
There is another problem in your DDL above. In Oracle, constraint names are globally unique! That means that whatever you put after CONSTRAINT has to be unique. In the case above you use the same name primary_key for both your tables which will fail with an ORA-02264: name already used by an existing constraint error on creating the history table. You will see that I have given the primary key constraints on the tables more meaningful names.
Err: Foreign key(BID) references Book_Copy(BID)
The primary key of Book_Copy is (BID, DateTaken) not just (BID)
You need a primary or unique constraint in the target table referenced in a FK of another table.
1) Add DateTaken to you FK definition on HISTORY or replace the FK with a CHECK constraint something like (EXISTS (select * from book_copy c where c.BID = BID)

Alter Table syntax Assignment

I have Assignment due in which i'm stuck on a question.
Add a “Sales Detail” table to your database. This table is related to the Orders and Products tables. It shows the product and quantity ordered at least (add other fields if you wish but explain why you added them on your paper).
There is no description of this table on the diagram provided. Use your best database design skills here!
Create Table SalesDetail
(
SaleDetailID int,
ProductID char(5),
ManufactureID char(3) not null,
OrderNo int,
qtyOrdered int
PRIMARY
)
Alter Table SalesDetail
Add FOREIGN KEY (ProductID)
REFERENCES Products(ProductID)
My Error is I can not get it to link SalesDetail table to Products table.
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Products' that match the referencing column list in the foreign key 'FK__SalesDeta__Produ__5EBF139D'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Create Table Customers
(
CustomerNo char(4)
Constraint ck_CustomerNoHas4positionsWithNumbers
Check(CustomerNo like'[0-9],[0-9],[0-9],[0-9]'),
Company varchar(50) not null,
CustomerRep char(3),
CreditLimt money default(20000.00),
PRIMARY KEY(CustomerNo)
)
Create Table Salesreps
(
EmployeeNo char(3)
Constraint ck_EmployeeNoHasDigits check(EmployeeNo like'[0-9],[0-9],[0-9]'),
FirstName varchar(25) not null,
LastName varchar(25) not null,
Age int,
SalesRepOffice char(2) not null,
Title varchar(50),
HireDate Date not null,
Manager char(3) not null,
Quota money,
Sales money not null,
PRIMARY KEY(EmployeeNo)
)
Create Table Offices
(
Office char(2) Constraint ck_checkOfficeHasNumbersOnly check(Office like'[0-9],[0-9]'),
City varchar(25) not null,
Region varchar(10) not null,
Manager char(3) not null,
Target money,
Sales money not null
PRIMARY KEY(Office)
)
Create Table Orders
(
OrderNo int,
OrderDate Date not null,
CustomerNo char(4) not null,
SalesRep char(3) not null
PRIMARY KEY(OrderNo)
)
Create Table Products
(
ManufactureID char(3)
Constraint ck_ManufactureIDifItHasLettersOnly check(ManufactureID like'[a-z],[a-z],[a-z]'),
ProductID char(5)
Constraint ck_ProductIDhasTwoLettersAndThreeNumbers check(ProductID like'[0-9],[0-9],[a-z],[a-z],[a-z]'),
Description varchar(50) not null,
Price money not null,
QtyOnHand int not null,
PRIMARY KEY(ManufactureID, ProductID)
)
--Add Foreign Keys to all tables who needs them
Alter Table Customers
Add constraint fk_customerrep
FOREIGN KEY (CustomerRep)
REFERENCES Salesreps(EmployeeNo)
Alter Table Salesreps
Add constraint fk_salesrepoffice
FOREIGN KEY (SalesRepOffice)
REFERENCES Offices(Office),
constraint fk_manager
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)
Alter Table Offices
Add constraint fk_officesmanger
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)
Alter Table Orders
Add constraint fk_customerno
FOREIGN KEY (CustomerNo)
REFERENCES Customers(CustomerNo),
constraint fk_salesrep
FOREIGN KEY (SalesRep)
REFERENCES Salesreps(EmployeeNo)
The table Products has a composite key (ManufactureID, ProductID), so you cannot uniquely identify a product by just the ProductId. Therefore you have to create a composite foreign key that references to both ManufactureId and ProductID:
Alter Table SalesDetail
Add FOREIGN KEY (ManufactureId, ProductID)
REFERENCES Products(ManufactureID, ProductID)
ProductID is not a primary key like the error says. In your code
PRIMARY KEY(ManufactureID, ProductID)
This creates a primary key that both of those columns combined.
The primary key for Products is (ManufactureID, ProductID). So the SalesDetail table should contain both these columns, and both should be part of the foreign key constraint:
Alter Table SalesDetail
Add FOREIGN KEY (ManufactureID, ProductID)
REFERENCES Products(ManufactureID, ProductID)