Foreign key as a bridge between two tables. It's a column in one table that shows to the main key in another table, creating a connection that makes your data more meaningful.
Imagine you have two tables: the Customer Details table and the Order detail table. The Order detail table, where customers place their departments, has a column called "cus_id" that points directly to the "cus_id" column in the Customer Detail table. It's like a digital handshake between the two, ensuring every order is associated with a specific customer.
The "cus_id" column in the Students table takes on the role of the PRIMARY KEY, uniquely identifying each customer. On the other side, in the Orders table, "cus_id" becomes a FOREIGN KEY, establishing the link back to the Customer Detail table.
The foreign key is great because it stops things from getting messy. The foreign key rule acts like a guard, making sure that nothing messes up the important connections between tables. It quietly protects against wrong data getting into the foreign key column.
CREATE TABLE department_detail
(
dept_id numeric(10) not null,
dept_name varchar2(50) not null,
CONSTRAINT department_id PRIMARY KEY (dept_id)
);
CREATE TABLE employee_detail
(
emp_id numeric(10) not null,
emp_name varchar2(50) not null,
contac_person varchar2(50),
depart_id numeric(10) not null,
CONSTRAINT pk_emp_id PRIMARY KEY (emp_id),
CONSTRAINT fk_department_id FOREIGN KEY (depart_id) REFERENCES department_detail(dept_id)
);
ALTER TABLE employee_detail
ADD CONSTRAINT fk_department_id
FOREIGN KEY(depart_id)
REFERENCES department_detail (dept_id);
Specifies that the modification is applied to the employee_detail table.
Introduces a new constraint named fk_department_id. This name is user-defined and could be different based on the naming conventions used.
Defines the depart_id column in the employee_detail table as a foreign key. This implies that the values in the depart_id column of the employee_detail table must correspond to the values in the dept_id column of the referenced table, which is department_detail.
Specifies the target of the foreign key relationship, indicating that the depart_id column in the employee_detail table refers to the dept_id column in the department_detail table. This establishes a link between the two tables, ensuring that each value in the depart_id column of employee_detail points to a valid dept_id in the department_detail table, maintaining referential integrity.
ALTER TABLE employee_detail
DROP CONSTRAINT fk_department_id;
This SQL code is an alteration statement using the ALTER TABLE command to modify the structure of the employee_detail table. Specifically, it is removing (dropping) a constraint from the table.
ALTER TABLE employee_detail:
Specifies that the modification is applied to the employee_detail table.
DROP CONSTRAINT fk_department_id:
Instructs the database to remove the constraint named fk_department_id from the employee_detail table. The fk_department_id is the user-defined name of the constraint being dropped.