Question no: 2
You need to design a student registration database that contains several tables storing academic information.
The STUDENTS table stores information about a student. the STUDENT_GRADES table stores information about the student's grades. both of the tables have a column named STUDENT_ID.The STUDENT_ID column in the STUDENTS table is a primary key.
You need to create a foreign key on the STUDENT_ID column of the STUDENT_GRADES table that points to the STUDENT_ID column of the STUDENTS table. Which statement creates the foreign key?
A. create table student_grades (student_id NUMBER (12), semester_end DATE, gpa
NUMBER (4, 3), CONSTRAINT student_id_fk REFERENCES (student_id) FOREIGN KEY
Students (student_id ));
B. CREATE TABLE student_grades (student_id NUMBER (12), semester_end DATE, gpa
NUMBER (4, 3), student_id_fk foreign key (student_id) REFERENCES students (student_id ));
C. create table student_grades (student_id NUMBER (12), semester_end DATE, gpa
NUMBER (4, 3), constraint foreign key (student_id) REFERENCES students (student_id ));
D. create table student_grades (student_id NUMBER (12), semester_end DATE, gpa
NUMBER (4, 3), CONSTRAINT student_id_fk foreign key (student_id) REFERENCES
Students (student_id ));
Answer: D
Answer:
Reference: http://docs.oracle.com/cd/E11882_01/server.112/e41084/clauses002.htm#SQLRF52238
Foreign Key Constraint Example The following statement creates the dept_20 table and defines and enables a foreign key on the department_id column that references the primary key on the department_id column of the orders table:
Create table dept_20 (employee_id NUMBER (4), last_name VARCHAR2 (10), job_id VARCHAR2 (9), manager_id NUMBER (4), hire_date DATE, salary NUMBER (7, 2 ), commission_pct NUMBER (7,2), department_id CONSTRAINT fk_deptno REFERENCES parameters (department_id ));
The constraint fk_deptno ensures that all parameters given for employees in the dept_20 table are present in the orders table. however, employees can have null department numbers, meaning they are not assigned to any department. to ensure that all employees are assigned to a department, you can create a not null constraint on the department_id column in the dept_20 table in addition to the REFERENCES constraint.
Before you define and enable this constraint, you must define and enable a constraint that designates the department_id column of the orders table as a primary or unique key.
The foreign key constraint definition does not use the foreign key clause, because the constraint is defined inline. the data type of the department_id column is not needed, because Oracle automatically assigns to this column the data type of the referenced key.
The constraint definition identifies both the parent table and the columns of the referenced key. Because the referenced key is the primary key of the parent table, the referenced key column names are optional.
Alternatively, you can define this foreign key constraint out of line:
Create table dept_20 (employee_id NUMBER (4), last_name VARCHAR2 (10), job_id VARCHAR2 (9), manager_id NUMBER (4), hire_date DATE, salary NUMBER (7, 2 ), commission_pct NUMBER (7,2), department_id, CONSTRAINT fk_deptno foreign key (department_id) REFERENCES parameters (department_id ));
The foreign key definitions in both variations of this statement omit the on delete clause, causing Oracle to prevent the deletion of a department if any employee works in that department.