In the code below, The first column I create for the table courses
is a column called department_code
. I then define fk_courses_department_code
and specify that department_code
in this table will be a foreign key that will reference department_code
from the departments
table. What is the relevance, or significance of the CONSTRAINT fk_courses_department_code
line? As far as I'm concerned, it is understood that department_code
is a foreign key in this table. What exactly is the role of fk_courses_department_code
then? Does it even exist as a real column? Is it just an alias for department_code
? What is it, exactly?
CREATE TABLE courses
(
department_code VARCHAR2(2) NOT NULL,
course_number NUMBER(3,0) NOT NULL,
course_title VARCHAR2(64) NOT NULL,
course_description VARCHAR2(512) NOT NULL,
credits NUMBER(3,1) NOT NULL,
CONSTRAINT pk_courses
PRIMARY KEY (department_code, course_number),
CONSTRAINT fk_courses_department_code
FOREIGN KEY (department_code)
REFERENCES departments (department_code),
);
Best Answer
It is the name of the constraint.
This is useful to e.g. drop the constraint later, as that can only be done by supplying the name.