Solution

advertisement
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,
CREATE TABLE vendors
(
vendor_id
NUMBER
vendor_name
VARCHAR2(50)
vendor_address1
VARCHAR2(50),
vendor_address2
VARCHAR2(50),
vendor_city
VARCHAR2(50)
vendor_state
CHAR(2)
vendor_zip_code
VARCHAR2(20)
vendor_phone
VARCHAR2(50),
vendor_contact_last_name
VARCHAR2(50),
vendor_contact_first_name
VARCHAR2(50),
default_terms_id
NUMBER
default_account_number
NUMBER
CONSTRAINT vendors_pk
PRIMARY KEY (vendor_id),
CONSTRAINT vendors_vendor_name_uq
UNIQUE (vendor_name),
CONSTRAINT vendors_fk_terms
FOREIGN KEY (default_terms_id)
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
REFERENCES terms (terms_id),
CONSTRAINT vendors_fk_accounts
FOREIGN KEY (default_account_number)
REFERENCES general_ledger_accounts (account_number)
);
CREATE TABLE invoices
(
invoice_id
NUMBER,
vendor_id
NUMBER
invoice_number
VARCHAR2(50)
invoice_date
DATE
invoice_total
NUMBER(9,2)
payment_total
NUMBER(9,2)
credit_total
NUMBER(9,2)
terms_id
NUMBER
invoice_due_date
DATE
payment_date
DATE,
CONSTRAINT invoices_pk
PRIMARY KEY (invoice_id),
CONSTRAINT invoices_fk_vendors
FOREIGN KEY (vendor_id)
REFERENCES vendors (vendor_id),
CONSTRAINT invoices_fk_terms
FOREIGN KEY (terms_id)
REFERENCES terms (terms_id)
);
NOT
NOT
NOT
NOT
NULL,
NULL,
NULL,
NULL,
DEFAULT 0,
DEFAULT 0,
NOT NULL,
NOT NULL,
CREATE TABLE invoice_line_items
(
invoice_id
NUMBER
NOT NULL,
invoice_sequence
NUMBER
NOT NULL,
account_number
NUMBER
NOT NULL,
line_item_amt
NUMBER(9,2)
NOT NULL,
line_item_description
VARCHAR2(100) NOT NULL,
CONSTRAINT line_items_pk
PRIMARY KEY (invoice_id, invoice_sequence),
CONSTRAINT line_items_fk_invoices
FOREIGN KEY (invoice_id) REFERENCES invoices (invoice_id),
CONSTRAINT line_items_fk_acounts
FOREIGN KEY (account_number) REFERENCES general_ledger_accounts
(account_number)
);
CREATE TABLE vendor_contacts
(
vendor_id
NUMBER
last_name
VARCHAR2(50)
first_name
VARCHAR2(50)
);
CREATE TABLE invoice_archive
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_id from the Vendors table
vendor_name
vendor_name from the Vendors table
contact_name
A concatenation of vendor_contact_first_name and
vendor_contact_last_name with a space in between
SELECT v1.vendor_id, v1.vendor_name,
v1.vendor_contact_first_name || ' ' ||
v1.vendor_contact_last_name AS contact_name
FROM vendors v1 JOIN vendors v2
ON v1.vendor_id <> v2.vendor_id
WHERE v1.vendor_contact_last_name = v2.vendor_contact_last_name
ORDER BY v1.vendor_contact_last_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 vendor’s 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 that’s
located in a unique city and state. In other words, don’t 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
GROUP BY vendor_state || vendor_city
HAVING COUNT(*) > 1)
ORDER BY vendor_state, vendor_city
8.3
Write a SELECT statement that returns these columns from the Vendors table:
The vendor_name column
The vendor_name column in all capital letters
The vendor_phone column
The last four digits of each phone number
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);
Download