Homework 9 Solution
Homework 9 Solution
Table Schemas:
vendor_id_seq';
invoice_id_seq';
invoice_archive';
invoice_line_items';
invoices';
vendor_contacts';
vendors';
terms';
general_ledger_accounts';
CREATE TABLE general_ledger_accounts
(
account_number
NUMBER
account_description
VARCHAR2(50)
CONSTRAINT gl_accounts_pk
PRIMARY KEY (account_number),
CONSTRAINT gl_account_description_uq
UNIQUE (account_description)
);
CREATE TABLE terms
(
terms_id
NUMBER
terms_description
VARCHAR2(50)
terms_due_days
NUMBER
CONSTRAINT terms_pk
PRIMARY KEY (terms_id)
);
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT
NOT
NOT
NOT
NULL,
NULL,
NULL,
NULL,
NOT NULL,
NOT NULL,
DEFAULT 0,
DEFAULT 0,
NOT NULL,
NOT NULL,
NOT NULL
);
invoice_id
vendor_id
invoice_number
invoice_date
invoice_total
payment_total
credit_total
terms_id
invoice_due_date
payment_date
NUMBER
NUMBER
VARCHAR2(50)
DATE
NUMBER
NUMBER
NUMBER
NUMBER
DATE
DATE
NOT
NOT
NOT
NOT
NOT
NOT
NOT
NOT
NOT
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
4.5
Write a SELECT statement that returns three columns:
vendor_id
vendor_name
contact_name
5.7
Write a SELECT statement that answers this question: Which vendors are being paid from
more than one account? Return two columns: the vendor name and the total number of
accounts that apply to that vendors invoices. Hint: Use the DISTINCT keyword to count the
account_number column in the Invoice_Line_Items table.
SELECT vendor_name,
COUNT(DISTINCT li.account_number) AS "Number of Accounts"
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
JOIN invoice_line_items li
ON i.invoice_id = li.invoice_id
GROUP BY vendor_name
HAVING COUNT(DISTINCT li.account_number) > 1
ORDER BY vendor_name
6.7
Write a SELECT statement that returns the name, city, and state of each vendor thats
located in a unique city and state. In other words, dont include vendors that have a city and
state in common with another vendor.
SELECT vendor_name, vendor_city, vendor_state
FROM vendors
WHERE vendor_state || vendor_city NOT IN
(SELECT vendor_state || vendor_city
FROM vendors
8.3
Write a SELECT statement that returns these columns from the Vendors table:
1
SELECT vendor_name,
UPPER(vendor_name),
vendor_phone,
SUBSTR(vendor_phone, 11) AS last_digits,
SUBSTR(vendor_name, (INSTR(vendor_name, ' ') + 1),
(INSTR(vendor_name, ' ', (INSTR(vendor_name, ' ') + 1)) (INSTR(vendor_name, ' '))))
AS second_word,
REPLACE((REPLACE((REPLACE(vendor_phone, '(', '')), ') ', '-')),
'-', '.') AS phone_with_dots
FROM Vendors
10.1
Write an ALTER TABLE statement that adds two new check constraints to the Invoices
table of the AP schema. The first should allow (1) payment_date to be null only if
payment_total is zero and (2) payment_date to be not null only if payment_total is greater
than zero. The second constraint should prevent the sum of payment_total and credit_total
from being greater than invoice_total.
ALTER TABLE invoices
ADD CONSTRAINT payment_total_ck1
CHECK ((payment_date IS NULL
AND payment_total = 0) OR
(payment_date IS NOT NULL AND payment_total > 0));
ALTER TABLE invoices
ADD CONSTRAINT payment_total_ck2
CHECK (payment_total + credit_total <= invoice_total);