Quiz
database final
This quiz has question types that open in the usual player.
Questions in this quiz (72)
1Create a table with column constraints, two attributes; vendor_id (number, is primary key) and vendor_name (varchar2(50) and not null, is unique):
CREATE [blank_start]TABLE[blank_end] vendors
(
[blank_start]vendor_id NUMBER PRIMARY KEY[blank_end],
[blank_start]vendor_name VARCHAR2(50) NOT NULL UNIQUE[blank_end]
)- ATABLE
- Bvendor_id NUMBER PRIMARY KEY
- Cvendor_name VARCHAR2(50) NOT NULL UNIQUE
2vendor_id NUMBER PRIMARY KEY - [blank_start]unnamed constraint[blank_end]
vendor_id NUMBER CONSTRAINT vendors_pk PRIMARY KEY - [blank_start]named constraint[blank_end]- Aunnamed constraint
- Bnamed constraint
- Cconstraint
3composite primary key constraints are written as:
CONSTRAINT value PRIMARY KEY (value1, value2)- ATrue
- BFalse
4foreign key constraint is also known as reference constraint
- ATrue
- BFalse
5create table with foreign key constraint for vendors table (vendor_id value):
CREATE TABLE invoices(
invoice_id NUMBER PRIMARY KEY,
vendor_id NUMBER [blank_start]REFERENCES vendors[blank_end] ([blank_start]vendor_id[blank_end]),
invoice_number VARCHAR2(50) NOT NULL UNIQUE
);- AREFERENCES vendors
- Bvendor_id
6table level foreign key constraints are written as:
CONSTRAINT named_foreign_key FOREIGN KEY (value) REFERENCES table (value)- ATrue
- BFalse
7Check constraints are used to restrict values, add two checks to the table, check if invoice_total is >= 0 and payment_total is >= 0
CREATE TABLE invoices(
invoice_id NUMBER PRIMARY KEY,
invoice_total NUMBER(9,2) NOT NULL
[blank_start]CHECK[blank_end] ([blank_start]invoice_total >= 0[blank_end]),
payment_total NUMBER(9,2) DEFAULT 0
CHECK ([blank_start]payment_total >= 0[blank_end])
)- ACHECK
- Binvoice_total >= 0
- Cpayment_total >= 0
8What are the following commands:
Rename a table: [blank_start]rename[blank_end] vendors to vendor
delete all data from a table: [blank_start]truncate[blank_end] table vendor
delete a table from the database: [blank_start]drop[blank_end] table vendor
check table structure: [blank_start]desc[blank_end] vendor- Arename
- Btruncate
- Cdrop
- Ddesc
9Insert command is as follows:
INSERT INTO members (value1, value2, value3)
VALUES (‘value1', 'value2', 'value3');- ATrue
- BFalse
10Update table options to change ocost to 115.00 and olist to 200.00 where the ocode is 'CD2'
UPDATE options
[blank_start]SET ocost[blank_end] = 115.00,
[blank_start]olist[blank_end] = 200.00 [blank_start]WHERE ocode[blank_end] = ‘[blank_start]CD2[blank_end]’;
(1 rows updated)- ASET ocost
- Bolist
- CWHERE ocode
- DCD2
11delete command:
DELETE FROM table WHERE value = ‘value’;- ATrue
- BFalse
12What command makes your changes in the database permanent:
- Acommit
- Bsave
- Cprocess
- Dunion
13what command will revert the changes you have made?
- Arollback
- Bundo
- Crevert
- Dcancel
14SELECT • List the columns (and expressions) that should be returned from the query
FROM • Indicate the table(s) or view(s) from which data will be obtained
[blank_start]WHERE[blank_end] • Indicate the conditions under which a row will be included in the result
[blank_start]GROUP BY[blank_end] • Indicate categorization of results
[blank_start]HAVING[blank_end] • Indicate the conditions under which a category (group) will be included
[blank_start]ORDER BY[blank_end] • Sorts the result according to specified criteria- AWHERE
- BGROUP BY
- CHAVING
- DORDER BY
15select all values from hr.employees where the depeartment_id is 100 and salary is greater than 7000
SELECT *
[blank_start]FROM[blank_end] hr.employees
[blank_start]WHERE[blank_end] department_id = [blank_start]100 AND salary >= 7000[blank_end];- AFROM
- BWHERE
- C100 AND salary >= 7000
16write a select command that shows all values from hr.employees where the salary is between 15000 and 20000
SELECT *
FROM hr.employees
[blank_start]WHERE salary BETWEEN[blank_end] 15000 AND 20000;- AWHERE salary BETWEEN
17When using the LIKE operator, % matches and character and _ matches and single character
- ATrue
- BFalse
18select all values from hr.employees where first_name starts with Alex
SELECT *
FROM hr.employees
[blank_start]WHERE first_name LIKE 'Alex%'[blank_end];- AWHERE first_name LIKE 'Alex%'
19select all values from hr.employees where last_name is ols something n
SELECT *
FROM hr.employees
[blank_start]WHERE last_name LIKE 'Ols_n'[blank_end];- AWHERE last_name LIKE 'Ols_n'
20select all from hr.locations where country_id is 'US', or 'CA'
SELECT *
FROM hr.locations
[blank_start]WHERE country_id IN[blank_end] ('US', 'CA');- AWHERE country_id IN
21select department_id and department_name from hr.departments where the manager_id is null
SELECT department_id, department_name FROM hr.departments
WHERE [blank_start]manager_id IS NULL[blank_end];- Amanager_id IS NULL
22select first_name, last_name and years they have been hired for AS years from hr.employees (use SYSDATE and hire_date)
SELECT first_name, last_name,
[blank_start]ROUND((SYSDATE - hire_date)/365)[blank_end] AS years
FROM hr.employees;- AROUND((SYSDATE - hire_date)/365)
23logical conditions:
[blank_start]AND[blank_end] - return true if both conditions are true
[blank_start]OR[blank_end] - return true if either condition is true
[blank_start]NOT[blank_end] - return true if condition is false- AAND
- BOR
- CNOT
24select only unique salary from hr.employees
SELECT [blank_start]DISTINCT[blank_end] salary FROM hr.employees;- ADISTINCT
25Subqueries: A subquery is a SELECT statement that is coded inside another SQL statement
- ATrue
- BFalse
26create a subquery that selects all employee_id from hr.job_history where the end_date is past 2007
SELECT *
FROM hr.employees
WHERE employee_id [blank_start]IN[blank_end] ([blank_start]SELECT employee_idFROM[blank_end] hr.job_history
[blank_start]WHERE end_date[blank_end] = '2007-12-31');- AIN
- BWHERE end_date
- CSELECT employee_id FROM
27ORDER BY clause can be used inside subquery
- ATrue
- BFalse
28select all cname from saleinv where net is the lowest (min) value of that table
SELECT cname FROM saleinv
[blank_start]WHERE net =[blank_end] (SELECT [blank_start]MIN(net)[blank_end] FROM saleinv);- AWHERE net =
- BMIN(net)
29use DISTINCT in subquerys when multiple of the same value can appear, for the sake of efficiency
- ATrue
- BFalse
30select all cname and chphone from customer where cname is In prospect (no duplicate values)
SELECT cname, chphone FROM customer
[blank_start]WHERE cname IN[blank_end] (SELECT [blank_start]DISTINCT[blank_end] cname FROM prospect);- AWHERE cname IN
- BDISTINCT
31JOIN is much more efficient than subquery
- ATrue
- BFalse
32Inner Joins: A join creates combinations from two or more tables in a single result set
- ATrue
- BFalse
33select all first_name, last_name and job_title from hr.employees and also display the job_id of each job_title
SELECT first_name, last_name, job_title
FROM hr.employees [blank_start]JOIN[blank_end] hr.jobs
[blank_start]ON employees.job_id = jobs.job_id[blank_end];- AJOIN
- BON employees.job_id = jobs.job_id
34Explicit join uses JOIN
implicit join uses WHERE- ATrue
- BFalse
35select first_name, last_name and job_title from hr.employees and hr.jobs where the job_id is equal to job_title id
SELECT first_name, last_name, job_title
FROM hr.employees ,hr.jobs
[blank_start]WHERE employees.job_id[blank_end] = [blank_start]jobs.job_id[blank_end];- AWHERE employees.job_id
- Bjobs.job_id
36Explicit join example: (where at end)
SELECT DISTINCT department_name, city, state_province, country_name
FROM hr.departments d
JOIN hr.locations l ON d.location_id = l.location_id
JOIN hr.countries c ON l.country_id = c.country_id
JOIN hr.regions r ON c.region_id = r.region_id
WHERE region_name = 'Europe';- ATrue
- BFalse
37inexplicit join example (where before joins)
SELECT DISTINCT department_name, city, state_province, country_name
FROM hr.departments d, hr.locations l, hr.countries c, hr.regions r
WHERE d.location_id = l.location_id
AND l.country_id = c.country_id
AND c.region_id = r.region_id
AND region_name = 'Europe';- ATrue
- BFalse
38Get max salary from hr.employees:
SELECT [blank_start]MAX(salary)[blank_end]
FROM hr.employees;- AMAX(salary)
39Get count # of entry for hr.employees
SELECT [blank_start]COUNT(*)[blank_end]
FROM hr.employees;- ACOUNT(*)
40Calculate and display the number of business locations in ‘CA’, ‘US’, ‘MX’ and ‘UK’
SELECT c.country_id, count(*)
FROM hr.locations l [blank_start]JOIN[blank_end] hr.countries c
[blank_start]ON[blank_end] l.country_id = c.country_id
[blank_start]WHERE[blank_end] c.country_id [blank_start]IN[blank_end] ('CA', 'US', 'MX', 'UK')
GROUP BY c.country_id
ORDER BY count(*);- AIN
- BWHERE
- CJOIN
- DON
41A SELECT clause that contains an aggregate function can contain only aggregate functions unless:
- AThe column specification results in a literal
- BGROUP BY clause contains all selected columns
- CJOINING 2 tables in a single statement
42HAVING clause determines which groups are included in the final results
– It is similar to WHERE clause, but it is used for searching after the grouping results- ATrue
- BFalse
43Display the department_id, department_name, the number of employees and the average salary for all of the departments that have more than 10 employees. Order them by department_name.
SELECT d.department_id, department_name, COUNT(*)
AS "No. of emp",
ROUND(AVG(salary),2) AS "Average Salary"
FROM hr.departments d JOIN hr.employees e
ON d.department_id = e.department_id GROUP BY d.department_id, department_name
[blank_start]HAVING COUNT(*)[blank_end] > 10
[blank_start]ORDER BY department_name[blank_end];- AORDER BY department_name
- BHAVING COUNT(*)
44display make of a car with 5 or more records in the car table
SELECT make, COUNT(*) FROM s9.car
[blank_start]GROUP BY make HAVING COUNT(*)[blank_end]>=5;- AGROUP BY make HAVING COUNT(*)
45You can only use HAVING with the GROUP BY clause.
- ATrue
- BFalse
46Benefits of Using Views
- ADesign Independence
- BData Security
- CSimplified Queries
- DUpdatability
- EEfficiency
- FEasy to scale
47Create a view in your own schema that brings together the data from countries and regions of ‘hr’ tables:
[blank_start]CREATE OR REPLACE VIEW[blank_end] countries_ids_regions AS
SELECT country_id, country_name, region_name, r.region_id
FROM hr.countries c JOIN hr.regions r ON c.region_id = r.region_id;- ACREATE OR REPLACE VIEW
48Create a VIEW that keeps track of frequent car buyers. A frequent car buyer is considered someone that has bought more than 1 car. Show the number of cars and the total amount they’ve spent.
[blank_start]CREATE OR REPLACE VIEW[blank_end] frequent_customer AS SELECT cname,
[blank_start]COUNT[blank_end](*) AS num_of_cars, [blank_start]SUM[blank_end](net) AS cars_total_sum
FROM s9.saleinv GROUP BY cname
[blank_start]HAVING COUNT[blank_end](*) > 1;- ACREATE OR REPLACE VIEW
- BSUM
- CCOUNT
- DHAVING COUNT
49Oracle allows cases when you can create VIEWs before crating tables
– In that case you need to use FORCE keyword – Example:
CREATE FORCE VIEW products_list AS
SELECT product_description, product_price
FROM products;- ATrue
- BFalse
50Substitution variable [blank_start]starts with p_[blank_end] [blank_start]& in front[blank_end]
Bind variable [blank_start]starts with g_[blank_end] [blank_start]: in front[blank_end]
PL variable starts with v_- Astarts with p_
- B& in front
- C: in front
- Dstarts with g_
51Subsititution variable:
[blank_start]ACCEPT[blank_end] p_name [blank_start]PROMPT[blank_end] 'Enter Name: ' SELECT *
FROM customer
WHERE cname = '[blank_start]&p_name[blank_end]';- APROMPT
- BACCEPT
- C&p_name
52PL block example:
BEGIN
-- do something here;
END; /- ATrue
- BFalse
53-- declare an SQLplus bind variable
VARIABLE g_name VARCHAR2(32000)
BEGIN
-- assign value of substitution variable to the bind variable
:g_name := '&p_name';
END;
/
PRINT g_name
Notes on the script:
• VARIABLE - bind variables:
– must be declared before they can be used
– cannot be declared inside a block
– A variable type and size must be given
• :g_name - The colon:
– must be used in front of bind variables inside a block
– is used for scope resolution. It tells PL to look outside the block and in the SqlPlus environment to find the variable.
• p_name - Since the substitution variable does not yet exist, you will be automatically prompted to enter a value for p_name.
• / - Execute the block
• PRINT - Print the value of the bind variable.
Only bind variable can be printed with the PRINT command.
PRINT p_name is illegal.- ATrue
- BFalse
54Simple script example:
ACCEPT p_name PROMPT 'Enter Customer Name: '
[blank_start]VARIABLE[blank_end] g_output VARCHAR2(200)
DECLARE
v_street VARCHAR2(30);
v_city VARCHAR2(20);
v_prov VARCHAR2(20);
v_postal VARCHAR2(10);
BEGIN
SELECT cstreet, ccity, cprov, cpostal
INTO [blank_start]v_street[blank_end],[blank_start]v_city[blank_end],[blank_start]v_prov[blank_end],[blank_start]v_postal[blank_end]
FROM customer
WHERE cname = '&[blank_start]p_name[blank_end]';
:g_output := '&p_name' || ' ' ||v_street || ' ' || v_city;
:g_output := :g_output || ' ' || v_prov || ' ' || v_postal;
[blank_start]END[blank_end];
[blank_start]/[blank_end]
PRINT g_output- Ap_name
- BVARIABLE
- Cv_street
- Dv_city
- Ev_prov
- Fv_postal
- GEND
- H/
55Get data type of field:
DECLARE
v_street customer.cstreet%TYPE- ATrue
- BFalse
56Get data type of field:
DECLARE
v_customer customer%ROWTYPE;- ATrue
- BFalse
57[blank_start]NO_DATA_FOUND[blank_end]
– no data can be found to be put into a variable in a SELECT - INTO statement.
• [blank_start]TOO_MANY_ROWS[blank_end]
– too may rows have been found for a SELECT - INTO statement. Variables can only hold one value.
• [blank_start]ZERO_DIVIDE[blank_end]
– a division by zero was attempted.
• [blank_start]VALUE_ERROR[blank_end]
– a data size or type conversion was incorrect. E.g. name CHAR(3) := 'ABCDE';- ANO_DATA_FOUND
- BTOO_MANY_ROWS
- CZERO_DIVIDE
- DVALUE_ERROR
58Example of NO_DATA_FOUND:
[blank_start]EXCEPTION[blank_end]
[blank_start]WHEN NO_DATA_FOUND THEN[blank_end]
:g_output := '&p_name' || ' not found';- AEXCEPTION
- BWHEN NO_DATA_FOUND THEN
59User define exception:
ACCEPT p_name PROMPT 'Enter Customer Name: '
VARIABLE g_output VARCHAR2(200)
DECLARE
v_count NUMBER(6);
v_toomany EXCEPTION;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM s9.prospect
WHERE cname = '&p_name';
IF v_count > 5 THEN RAISE v_toomany;
END IF;
:g_output := '&p_name' || ' ' || ' wants ' || v_count | ' cars';
EXCEPTION
WHEN v_toomany THEN
:g_output := '&p_name' || ' wants too many cars';
END;
/
PRINT g_output- ATrue
- BFalse
60In databases, a cursor is a temporary table.
- ATrue
- BFalse
61Cursor is created as so:
CURSOR value IS
SELECT statement...- ATrue
- BFalse
62Cursor for loop:
FOR value IN value LOOP
output...
END LOOP;- ATrue
- BFalse
63Procedure example:
[blank_start]CREATE OR REPLACE PROCEDURE[blank_end] AddServinv
(pl_cname servinv.cname%TYPE
) [blank_start]AS BEGIN[blank_end]
INSERT INTO servinv (cname)
VALUES (pl_cname);
COMMIT;
END AddServinv;
/- ACREATE OR REPLACE PROCEDURE
- BAS BEGIN
64Some rules with procedures:
Parameters are always placed in brackets following the procedure name.
The keyword AS is required (IS may be used as a synonym for AS).
Any variables that need to be declared are placed between AS and BEGIN (without the DECLARE word).- ATrue
- BFalse
65To call a procedure:
ACCEPT p_name PROMPT 'Enter customer name: '
[blank_start]BEGIN[blank_end]
procedurename('&[blank_start]p_name[blank_end]');
[blank_start]END[blank_end];
/- Ap_name
- BBEGIN
- CEND
66Functions
• A function is similar to a procedure except that it returns a single value.
• The CREATE statement must include the data type that is being returned.
• Functions almost always have at least one parameter passed to them.- ATrue
- BFalse
67[blank_start]CREATE OR REPLACE FUNCTION[blank_end] makeavg(v_make IN car.make%TYPE)
[blank_start]RETURN[blank_end] NUMBER
IS
v_avg NUMBER;
BEGIN
SELECT AVG(listprice)
INTO v_avg
FROM car
WHERE make=v_make;
RETURN [blank_start]v_avg[blank_end];
END;
/- ACREATE OR REPLACE FUNCTION
- BRETURN
- Cv_avg
68you can call a function as so:
SELECT DISTINCT make, function_name(make) FROM car;- ATrue
- BFalse
69A trigger is a procedure that is stored in the data dictionary and executed automatically in response to an event. There are three events for which triggers can be defined:
- AInsert
- BUpdate
- CDelete
- DRemove
- ERequest
70A trigger can be defined to take place either BEFORE or AFTER the event takes place.
Each trigger can only handle a single table.- ATrue
- BFalse
71create a trigger which will insure that a customer name is always in upper case when a customer record is inserted:
[blank_start]CREATE OR REPLACE TRIGGER[blank_end] t
[blank_start]BEFORE INSERT[blank_end] ON customer
FOR EACH ROW
BEGIN
:new.cname:=UPPER(:new.cname);
END;
/- ACREATE OR REPLACE TRIGGER
- BBEFORE INSERT
72create trigger for delete, insert and update
CREATE OR REPLACE TRIGGER custtrig
[blank_start]BEFORE DELETE OR INSERT OR UPDATE[blank_end] ON CUSTOMER
FOR EACH ROW
BEGIN
IF INSERTING THEN
:new.cname:=UPPER(:new.cname); :new.czip:=UPPER(:new.czip);
INSERT INTO CUSTLOG
VALUES ('INSERT',USER, SYSDATE,:NEW.CNAME);
END IF;- ABEFORE DELETE OR INSERT OR UPDATE
Information
- Description
- Computer Science Quiz on database final, created by Duncan Levings on 09/08/2018.
- Author
- Duncan Levings
- Language
- English
- Views
- 11
- Tags
- Rating
- No ratings yet Rate this quiz