04|Oracle learning (foreign key constraints)

Remarks: add constraints appearing below should be changed to add constraint, no need to add s

1. Introduction to foreign key constraints

1.1 What is a foreign key constraint:

insert image description here
If you follow the design below and directly add the column names of "class number", "class name", "class teacher", and "class description" to the original table, what will happen?
insert image description here

  • It can be seen that some of the contents drawn in yellow above are the same (that is, the contents are stored repeatedly). If the amount of data exceeds 100 million, then this repeated data is stored multiple times, which is also called "data redundancy".
  • The above "data redundancy" will cause serious problems: if the head teacher of class C01, Mr. Wang, is replaced by Mr. Li, then in the data of over 100 million, we have to replace all the teachers of class C01 with Mr. Li. The amount is very large, and it is also prone to operational errors.

Commonly used operations: Take out the "class number", "class name", "class teacher", and "class description", make a separate class information table, and then bind a "class
insert image description here
number" to the student table. Let the two tables form an association.
insert image description here

1.2 Summary:

  • Foreign key constraint: The value of the foreign key field must come from the associated field in other data tables.
    insert image description here

2. Add foreign key constraints

2.1 Add when creating a table

  • Cascading deletion: as shown below, when the primary key C01 is deleted, Zhang San and Wang Wu using C01 will also be deleted.
    insert image description here
2.1.1 Case
  • Create a class information table: class number, class name, class teacher, class description
create table tb_classes(
    class_id char(3) not null,
    class_name varchar(40) not null,
    class_leader varchar(10) not null,
    class_desc varchar(200),
    primary key(class_id)
);
  • Create a student information form: student number, name, gender, age, phone number, number
create table tb_students(
    stu_num char(5) not null,
    stu_name varchar(10) not null,
    stu_sex char(1) not null,
    stu_age number(2) not null,
    stu_tel char(11) not null,
    stu_cid char(3) not null,
    primary key(stu_num),
    constraint fk_students_class foreign key(stu_cid)
    references tb_classes(class_id) on delete cascade
);
  • You can see the final effect:
    insert image description here

2.2 After creating the table, add

Note: If you previously created interdependent tb_classes and tb_students tables. Then delete the tb_students of the foreign key first, and then delete the tb_classes of the primary key. (Delete the slave first, then the master)

drop table tb_students;
drop table tb_classes;

insert image description here

2.2.1 Case
  • Create a class information table: class number, class name, class teacher, class description
create table tb_classes(
    class_id char(3) not null,
    class_name varchar(40) not null,
    class_leader varchar(10) not null,
    class_desc varchar(200),
    primary key(class_id)
);
  • Create a student table (this time do not establish a foreign key constraint at the time of creation): student number, name, gender, age, phone number, number
create table tb_students(
    stu_num char(5) not null,
    stu_name varchar(10) not null,
    stu_sex char(1) not null,
    stu_age number(2) not null,
    stu_tel char(11) not null,
    stu_cid char(3) not null,
    primary key(stu_num)
);
  • Next, alter the student information table tb_students and add a foreign key:
alter table tb_students 
add constraint fk_student_class foreign key(stu_cid)
references tb_classes(class_id) on delete cascade;

3. Delete foreign key constraints

insert image description here

  • Remove the constraint in tb_student above:
alter table tb_students
drop constraint fk_student_class;

Guess you like

Origin blog.csdn.net/qq_41714549/article/details/132056405