Lesson 10: Including Constraints: SQL Sample Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

SQL Sample Questions

Lesson 10: Including Constraints


1. You create the sales table with this command

CREATE TABLE sale


(purchase-no NUMBER(9)
CONSTRAINT sale-purchase-no-pk PRIMARY KEY,
costumer-id NUMBER(9)
CONSTRAINT sale-customer-id-nk NOT NULL);

Which index or indexes are created for this table?

A. No indexes are created for this table.


B. An index is created for purchase_no column.
C. An index is created for the customer_no column.
D. An index is created for each column.
B
2. Write the correct constraint name from column 3 that best suits the scenario stated in
column 1 in column 2.

Sl Column 1 Column 2 Column 3


No.

1 The column must contain a NOT NULL CHECK


value in each row.
2 Each value must be different in UNIQUE NOT NULL
a column or columns.
3 The value must be unique and PRIMARY KEY UNIQUE
present.
4 It defines a condition that each CHECK PRIMARY
row must satisfy. KEY
5 It establishes a relationship Foreign key FOREIGN
between columns. KEY

3. Which script would you use to query the data dictionary to view only the names of the
primary key constraints using a substitution parameter for the table name?

A. SELECT constraint_name
FROM user_constraint
WHERE table_name = UPPER(‘&table’ )
AND constraint_type = ‘P’;
B. SELECT constraint_name
FROM user_constraint
WHERE table_name = UPPER (‘&table’)
AND constraint_type = ‘PRIMARY’;
C. SELECT constraint_name,constraint_type
FROM user_constraint
WHERE table_name = UPPER (‘&table’);
D. SELECT constraint_name
FROM user_cons_columns
WHERE table_name = UPPER (‘&table’)
AND constraint_type = ‘P’;
A

1
SQL Sample Questions

4. Examine the data in the EMPLOYEES and DEPARTMENTS tables:

EMPLOYEES

MGR_I
Employee_id EMP_NAME DEPT_ID D JOB_ID SALARY
101 Smith 20 120 SA_REP 4000
102 Martin 10 105 CLERK 2500
103 Chris 20 120 IT_ADMIN 4200
104 John 30 108 HR_CLERK 2500
105 Diana 30 108 HR_MGR 5000
106 Brayan 40 110 ADD_ASST 3000
108 Jennifer 30 110 HR_DIR 650
110 Bob 40 EX_DIR 8000
120 Ravi 20 110 SA_DIR 6500

DEPARTMENTS

DEPARTMENT_ID DEPARTMENT_NAME

10 Admin
20 Education
30 IT
40 Human Resources

Also examine the SQL statements that create the EMPLOYEES and DEPARTMENTS
tables:

CREATE TABLE departments


(department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(30));

CREATE TABLE employees


(EMPLOYEE_ ID NUMBER PRIMARY KEY,
EMP_NAME VARCHAR2(20),
DEPT_ID NUMBER REFERENCES
departments(department_id),
MGR_ID NUMBER REFERENCES employees(employee id),
MGR_ID NUMBER REFERENCES employees(employee id),
JOB_ID VARCHAR2(15).
SALARY NUMBER);

On the EMPLOYEES table, EMPLOYEE_ID is the primary key.


MGR_ID is the ID of managers and refers to the EMPLOYEE_ID.
DEPT_ID is foreign key to DEPARTMENT_ID column of the DEPARTMENTS table.

On the DEPARTMENTS table, DEPARTMENT_ID is the primary key.

Examine this DELETE statement:

DELETE
FROM departments
WHERE department id = 40;

2
SQL Sample Questions

What happens when you execute the DELETE statement?

A. Only the row with department ID 40 is deleted in the DEPARTMENTS table.


B. The statement fails because there are child records in the EMPLOYEES table with
department ID 40.
C. The row with department ID 40 is deleted in the DEPARTMENTS table. Also the rows
with employee IDs 110 and 106 are deleted from the EMPLOYEES table.
D. The row with department ID 40 is deleted in the DEPARTMENTS table. Also the rows
with employee IDs 106 and 110 and the employees working under employee 110 are
deleted from the EMPLOYEES table.
E. The row with department ID 40 is deleted in the DEPARTMENTS table. Also all the
rows in the EMPLOYEES table are deleted.
F. The statement fails because there are no columns specifies in the DELETE clause of
the DELETE statement.
B
5. Which SQL statement defines the FOREIGN KEY constraint on the DEPTNO column of
the EMP table?
A. CREATE TABLE EMP
(empno NUMBER(4),
ename VARCNAR2(35),
deptno NUMBER(7,2) NOT NULL CONSTRAINT emp_deptno_fk FOREIGN
KEY deptno REFERENCES dept deptno);
B. CREATE TABLE EMP
(empno NUMBER(4),
ename VARCNAR2(35),
deptno NUMBER(7,2) CONSTRAINT emp_deptno_fk REFERENCES dept
(deptno));
C. CREATE TABLE EMP
(empno NUMBER(4)
ename VARCHAR2(35),
deptno NUMBER(7,2) NOT NULL,
CONSTRAINT emp_deptno_fk REFERENCES dept (deptno) FOREIGN KEY
(deptno));
D. CREATE TABLE EMP (empno NUMBER(4),
ename VARCNAR2(35),
deptno NUMBER(7,2) FOREIGN KEY CONSTRAINT emp deptno fk
REFERENCES dept (deptno));
B
6. Examine the SQL statements that creates ORDERS table:

CREATE TABLE orders


(SER_NO NUMBER UNIQUE,
ORDER_ID NUMBER,
ORDER_DATE DATE NOT NULL
STATUS VARCHARD2(10)
CHECK (status IN (‘CREDIT’,’CASH’)),
PROD_ID_NUMBER
REFERENCES PRODUCTS(PRODUCT_ID),
ORD_TOTAL NUMBER,
PRIMARY KEY (order id, order date));

3
SQL Sample Questions

For which columns would an index be automatically created when you execute the above
SQL statement? (Choose two)

A. SER_NO
B. ORDER_ID
C. STATUS
D. PROD_ID
E. ORD_TOTAL
F. Composite index on ORDER_ID and ORDER_DATE
AF
7. For which two constraints does the Oracle Server implicitly create a unique index?
(Choose two.)

A. NOT NULL
B. PRIMARY KEY
C. FOREIGN KEY
D. CHECK
E. UNIQUE
BE
8. Examine the structure of the EMPLOYEES and NEW_EMPLOYEES tables:

EMPLOYEES
EMPLOYEE_ID NUMBER Primary Key
LAST_NAME VARCHAR2(25)
FIRST_NAME VARCHAR2(25)
HIRE_DATE DATE

NEW EMPLOYEES

EMPLOYEE_ID NUMBER Primary Key


NAME VARCHAR2(60)

Which UPDATE statement is valid?

A. UPDATE new_employees
SET name = (Select last_name||first_name
FROM employees
WHERE employee_id =180)
WHERE employee_id =180;
B. UPDATE new_employees
SET name = (SELECT last_name||first_name
FROM employees)
WHERE employee_id =180;
C. UPDATE new_employees
SET name = (SELECT last_name||first_name
FROM employees
WHERE employee_id=180)
WHERE employee_id =(SELECT employee_id
FROM new employees);
D. UPDATE new_employees
SET name = (SELECT last name||first_name
FROM employees
WHERE employee_id=(SELECT employee_id
FROM new_employees))
WHERE employee_id=180;

4
SQL Sample Questions

A
9. Which three statements correctly describe the functions and use of constraints? (Choose
three.)

A. Constraints provide data independence.


B. Constraints make complex queries easy.
C. Constraints enforce rules at the view level.
D. Constraints enforce rules at the table level.
E. Constraints prevent the deletion of a table if there are dependencies.
F. Constraints prevent the deletion of an index if there are dependencies.
DEF
10. Which constraint can be defines only at the column level?

A. UNIQUE
B. NOT NULL
C. CHECK
D. PRIMARY KEY
E. FOREIGN KEY
B
11. Examine the data in the EMPLOYEES table.

EMPLOYEES

MGR_I
Employee_id EMP_NAME DEPT_ID D JOB_ID SALARY
101 Smith 20 120 SA_REP 4000
102 Martin 10 105 CLERK 2500
103 Chris 20 120 IT_ADMIN 4200
104 John 30 108 HR_CLERK 2500
105 Diana 30 108 HR_MGR 5000
106 Brayan 40 110 ADD_ASST 3000
108 Jennifer 30 110 HR_DIR 6500
110 Bob 40 EX_DIR 8000
120 Ravi 20 110 SA_DIR 6500

On the EMPLOYEES table, EMPLOYEE_ID is the primary key. MGR_ID is the ID of


managers and refers to the EMPLOYEE_ID. The JOB_ID column is a NOT NULL
column.

Evaluate this DELETE statement:

DELETE employee_id, salary, job_id


FROM employees
WHERE dept_id = 90;

Why does the DELETE statement fail when you execute it?

A. There is no row with dept_id 90 in the EMPLOYEES table.


B. You cannot delete the JOB_ID column because it is a NOT NULL column.
C. You cannot specify column names in the DELETE clause of the DELETE statement.
D. You cannot delete the EMPLOYEE_ID column because it is the primary key of the
table.

5
SQL Sample Questions

12. Which two statements about creating constraints are true? (Choose two)

A. Constraint names must start with SYS_C.


B. All constraints must be defines at the column level.
C. Constraints can be created after the table is created.
D. Constraints can be created at the same time the table is created.
E. Information about constraints is found in the VIEW_CONSTRAINTS dictionary view.
CD
13. How would you add a foreign key constraint on the dept_no column in the EMP table
referring to the ID column in the DEPT table?

A. Use the ALTER TABLE command with the ADD clause in the DEPT table.
B. Use the ALTER TABLE command with the ADD clause on the EMP table.
C. Use the ALTER TABLE command with the MODIFY clause on the DEPT table.
D. Use the ALTER TABLE command with the MODIFY clause on the EMP table.
E. This task cannot be accomplished.
B
14. Examine the structure of student table:

Name Type Null


STU_ID NUMBER(3) NOT NULL
NAME VARCHAR2(25)
ADDRESS VARCHAR2(50)
GRADUATION DATE

Currently the table is empty. You have decided that null values should not be allowed for
the NAME column.

Which statement restricts NULL values from being entered into column?

A. ALTER TABLE student ADD CONSTRAINT name(NOT NULL);


B. ALTER TABLE student ADD CONSTRAINT NOT NULL (name);
C. ALTER TABLE student MODIFY CONSTRAINT name(NOT NULL);
D. ALTER TABLE student MODIFY(name varcher2(25) NOT NULL);
D
15. Examine the structure of STUDENT table.

NAME TYPE NULL


STUDENT ID NUMBER(3) NOT NULL
NAME VARCHAR2(25) NOT NULL
PHONE VARCHAR2(9) NOT NULL
ADDRESS VARCHAR2(50)
GRADUATION DATE

There are hundred records in the student table. You need to modify the Phone column to
hold only numeric value. Which statement will modify the data type of the Phone column?

A. ALTER TABLE student MODIFY phone NUMBER(9)


B. ALTER STUDENT table MODIFY COLUMN phone NUMBER(9);
C. You can not modify a VARCHAR2 data type to a NUMBER data type for a column.
D. You cannot modify the data type of a column if there is data in the column.

6
SQL Sample Questions

D
16. In SQL Plus You issued this command:

DELETE FROM dept WHERE dept_id = 901

You received an integrated constraint error because the child record was found.

What could you do to make the statement execute?

A. Delete the child record first.


B. You cannot make the command execute.
C. Add a fourth keyword to the command.
D. Add the constraints cascade option to the command.
A
17. Which statement would you use to add a primary key constraint to the patient table using
the id_number column immediately enabling the constraint?

A. This task can’t be accomplished.


B. ALTER TABLE patient
ADD CONSTRAINT p at_id_pk PRIMARY KEY(id_number);
C. ALTER TABLE patient
ADD (id_number CONATRAINT pat_id_pk PRIMARY KEY);
D. ALTER TABLE patient
MODIFY(id_number CONSTRAINT pat_id_pk PRIMARY KEY);
B
18. Which alter command would you use to reinstate a disabled primary constraint?

A. ALTER TABLE
ENABLE PRIMARY KEY(id)
B. ALTER TABLE CARS
ENABLE CONSTRAINT cars_id_pk.
C. ALTER TABLE CARS
ENABLE PRIMARY KEY(id) CASCADE;
D. ALTER TABLE CARS
ADD CONSTRAINT cards_id_pk PRIMARY KEY(id);
B
19. You need to perform a major update on the employee table. You have decided to disable
the primary key constraint on the empid column and the check constraint on the job
column.
What happens when you try to enable the constraint after the update is completed?
A. You need to recreate the constraints once they are disabled.
B. Any existing rows that do not conform with the constraints are automatically deleted.
C. Only th future values are verified to conform with the constraints having the existing
values unchecked.
D. The indexes on both the columns with the primary key constraint and the check
constraints are automatically recreated.
E. All the existing columns values are verified to conform with the constraints and an
error message is narrated if any existing values is not conformed.
E
20. Which syntax turns an existing constraint on?

A. ALTER TABLE table_name


ENABLE constraint_name;
B. ALTER TABLE table_name

7
SQL Sample Questions

STATUS = ENABLE CONSTRAINT constraint_name;


C. ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;
D. ALTER TABLE table_name
STATUS ENABLE CONSTRAINT constraint_name;
E. ALTER TABLE table_name
TURN ON CONSTRAINT constraint_name;
F. ALTER TABLE table_name
TURN ON CONSTRAINT constraint_name;
C
21. You need to modify the STUDENTS table to add a primary key on the STUDENT_ID
column. The table is currently empty.

Which statement accomplishes this task?

A. ALTER TABLE students


ADD PRIMARY KEY student_id;
B. ALTER TABLE students
ADD CONSTRAINT PRIMARY KEY (student_id);
C. ALTER TABLE students
ADD CONSTRAINT stud_id_pk PRIMARY KEY student_id;
D. ALTER TABLE students
ADD CONSTRAINT stud_id_pk PRIMARY KEY (student_id);
E. ALTER TABLE students
MODIFY CONSTRAINT stud_id_pk PRIMARY KEY (student_id);
D
22. Which statement explicitly names a constraint?

A. ALTER TABLE student_grades


ADD FOREIGN KEY (student_id) REFERENCES students(student_id);
B. ALTER TABLE student_grades
ADD CONSTRAINT NAME = student_id_fk
FOREIGN KEY (student_id) REFERENCES students(student_id);
C. ALTER TABLE student_grades
ADD CONSTRAINT student_id_fk
FOREIGN KEY (student_id) REFERENCES students(student_id);
D. ALTER TABLE student grades
ADD NAMED CONSTRAINT student_id_fk
FOREIGN KEY (student_id) REFERENCES students(student_id);
E. ALTER TABLE student grades
ADD NAME student_id_fk
FOREIGN KEY (student_id) REFERENCES students(student_id);
C
23. Which four are valid Oracle constraint types? (Choose four.)
A. CASCADE
B. UNIQUE
C. NONUNIQUE
D. CHECK
E. PRIMARY KEY
F. CONSTANT
G. NOT NULL

8
SQL Sample Questions

BDEG
24. Which statement adds a constraint that ensures the CUSTOMER_NAME column of the
CUSTOMERS table holds a value?
A. ALTER TABLE customers
ADD CONSTRAINT cust_name_nn CHECK customer_name IS NOT NULL;
B. ALTER TABLE customers
MODIFY CONSTRAINT cust_name_nn CHECK customer_name IS NOT NULL;
C. ALTER TABLE customers
MODIFY customer_name CONSTRAINT cust_name_nn NOT NULL;
D. ALTER TABLE customers
MODIFY customer_name CONSTRAINT cust_name_nn IS NOT NULL;
E. ALTER TABLE customers
MODIFY name CONSTRAINT cust_name_nn NOT NULL;
F. ALTER TABLE customers
ADD CONSTRAINT cust_name_nn CHECK customer_name NOT NULL;
C
25. Which view should a user query to display the columns associated with the constraints on
a table owned by the user?

A. USER_CONSTRAINTS
B. USER_OBJECTS
C. ALL_CONSTRAINTS
D. USER_CONS_COLUMNS
E. USER_COLUMNS

D
26. Which two statements are true about constraints? (Choose two.)

A. The UNIQUE constraint does not permit a null value for the column.
B. A UNIQUE index gets created for columns with PRIMARY KEY and UNIQUE
constraints.
C. The PRIMARY KEY and FOREIGN KEY constraints create a UNIQUE index.
D. The NOT NULL constraint ensures that null values are not permitted for the column.
BD
27. Which of the following integrity constraints automatically create an index when defined?
(Choose two)

A. Foreign keys
B. Unique constraints
C. NOT NULL constraints
D. Primary keys

BD

28. Which of the following dictionary views give information about the position of a column in
a primary key?

A. ALL_PRIMARY_KEYS
B. USER_CONSTRAINTS
C. ALL_IND_COLUMNS
D. ALL_TABLES

9
SQL Sample Questions

29. Which of the following choices is the strongest indicator of a parent/child relationship?
A. Two tables in the database are named VOUCHER and VOUCHER_ITEM,
respectively.
B. Two tables in the database are named EMPLOYEE and PRODUCTS, respectively.
C. Two tables in the database were created on the same day.
D. Two tables in the database contain none of the same columns.

30. To increase the number of NULLable columns for a table:

A. Use the alter table statement


B. Ensure that all column values are NULL for all rows
C. First increase the size of adjacent column datatypes, then add the column
D. Add the column, populate the column, then add the NOT NULL constraint

10

You might also like