19 foreign key constraint

- foreign key

- effect: when the word is inserted in the data table is not present in the parent table, is automatically given
- concept: when the value of a field in a table need to rely on a field of another table, the use of external key. Wherein the dependent table is called the active word list, the dependent tables as the parent table. Foreign key added to the word list.

--use:

- After you create Table fields: cno number (10) references the parent table (parent table field name)
- in the final table creation: constraints fk_ table _ column names foreign key (field name) references the parent table name (parent table field names)
- Add after the table has been created completed: alter table table name add constraints fk_ table _ column names foreign key (field name) references the parent table (parent table field names)

- Cons:

- When the child table is not cleared finished record in the parent table can not be deleted, unless you use cascading deletes

delete from clazz where cno = 1; - error: Word table records found

--Solution:

- utilizes a Cascade delete: when deleting the parent table data, automatically delete all records dependent field or sets this field to the table and the related child table is empty

  • - When word table is created: cno number (10) references clazz (cno) on delete cascade delete the parent will delete the word table Table records related records
  • - When you create a child table: when cno number (10) references clazz (cno) on delete set null so delete the parent table records, related word table fields related records will be set to null, the word table records themselves will not be deleted
    •   - NOTE: The word tables can not be set dependent field is non-empty, otherwise invalid on delete set null 

example:

 

create table student(
sno number(10) primary key,
sname varchar2(10) not null,
sage number(3) check(sage>0 and sage <150),
ssex char(4)check(ssex='男' or ssex='女'),
sfav varchar2(500),
sqq varchar2(30) unique,
Who are number (10)
)
--Inquire
select * from student;
select * from clazz;
- add a student test data
insert into student values ​​(1, 'John Doe 001', 18, 'M', 'singing', '112414545', 1);
insert into student values ​​(2, 'John Doe 002', 18, 'M', 'singing', '112414546', 1);
insert into student values ​​(3, 'John Doe 001', 18, 'M', 'singing', '112414547', 2);
insert into student values ​​(4, 'John Doe 002', 18, 'M', 'singing', '112414548', 2);
- Create a class table
create table clazz(
Who are number (10) primary key,
cname varchar2(100) not null,
cdesc varchar2(300)
)
- Add a class test data
insert into clazz values ​​(1, 'java class high-paying jobs,' 'amazing');
insert into clazz values ​​(2, 'python employment class', 'really fierce');
- joint inquiry: student and class information
select * from student
join clazz
on student.cno = clazz.cno;
- find the problem: You can insert the class a student but the student is a class that does not exist
insert into student values ​​(5, 'the king', 19, 'M', 'basketball', 213123,3);
- Solution: When you create a table of students, cno (class number) is set as the primary key of another table, this behavior is called the foreign key
  - to delete the original student table
drop table student;
  - create a new set of student foreign key table, and then perform the execution of the above statement added student
create table student(
sno number(10) primary key,
sname varchar2(10) not null,
sage number(3) check(sage>0 and sage <150),
ssex char(4)check(ssex='男' or ssex='女'),
sfav varchar2(500),
sqq varchar2(30) unique,
cno number (10) references clazz (cno) - or references another table name (field names) in the last foreign key (field)
)
- Foreign key concepts
  - effect: when the word is inserted in the data table is not present in the parent table, it is automatically given
  - concept: when the value of a field in a table need to rely on a field in another table, the foreign key.
    - wherein the dependent table is called the active word list, the dependent tables as the parent table. Add foreign key word in the table
  --use:
    - After you create Table fields: cno number (10) references the parent table (parent table field names)
    - created at the end of the table: constraints fk_ table _ column names foreign key (field name) references the parent table (parent table field names)
    - After completion of the table has been created to add: alter table table name add constraints fk_ table _ column names foreign key (field name) references the parent table (parent table field names)
  - Cons:
    - When the child table is not cleared finished record in the parent table can not be deleted, unless you use cascading deletes
    delete from clazz where cno=1;
    - Cascade delete: When you delete the parent table data, automatically delete this field and all records related to the child table
      - setting mode of use, creating a foreign key word is added after the table on delete cascade: When word table creation: cno number (10) references clazz (cno) on delete cascade 
                           - to delete the original constrained to add
                            select  * from user_cons_columns order by table_name; 
                            alter table student drop constraints SYS_C007041;
    alter table student add constraints fk_student_cno foreign key(cno) references clazz(cno) on delete cascade;
                            - successfully removed, we try to delete the parent table records
    delete from clazz where cno=1;
    - Query: Related himself recorded also be deleted
    select * from student;
    select * from clazz;
     - Cons: deletes the word table records when using the above parent table, delete the record
      - solution, when you create a child table in the foreign key settings added later on delete set null, so when the parent table to delete records, the relevant word table fields related records will be set to null, the record itself is not deleted
       - NOTE: The word tables can not be set dependent field is non-empty, otherwise invalid on delete set null
          - to delete the original constraints
          alter table student drop constraints fk_student_cno;
          - add constrained delete set null keyword
                       - remove students: delete from student;
          delete from student;
                       - Add Constraint
    alter table student add constraints fk_student_cno foreign key(cno) references clazz(cno) on delete set null;
                       - Add Class
                       - Add Students
insert into student values ​​(1, 'John Doe 001', 18, 'M', 'singing', '112414545', 1);
insert into student values ​​(2, 'John Doe 002', 18, 'M', 'singing', '112414546', 1);
insert into student values ​​(3, 'John Doe 001', 18, 'M', 'singing', '112414547', 2);
insert into student values ​​(4, 'John Doe 002', 18, 'M', 'singing', '112414548', 2);
            - Delete records clazz
            delete from clazz;
            - Query students
            select * from student;
   

 

  

 

Guess you like

Origin www.cnblogs.com/Scorpicat/p/12306745.html