Understanding Primary and Foreign Keys in Oracle

1. Primary key

In the real world, many data have unique characteristics, such as ID card numbers. In the national population basic information table, there must not be multiple people using the same ID number, such as mobile phone numbers, QQ numbers, bank account numbers Wait, there is also a student management system. The combination of the three fields of the student's grade, class, and student number is a unique identifier.

If the value of a field or a combination of multiple fields in the table is unique, it can be used as the primary key of the table. When creating or modifying the table, use the primay key keyword to specify the primary key. A table can only have one primary key, and each field value that makes up the primary key cannot be empty.

The role of the primary key:

1. Reflect the rationality of the data structure design.

2. Improve the speed of data operation.

3. To ensure the integrity of the data, when adding or modifying a record in the table, the database will check the value of the primary key of the record, and it is not allowed to duplicate the value of the primary key of other records. This practice has a professional term: primary key constraint.

For example, in the basic information table of super girls, the field name of the number is id. In the super girl draft activity, the number of each super girl must be unique. It is impossible to have two super girls with the same number, otherwise it will cause confusion. We can set the id field to It is the primary key of the T_GIRL table, and the subsequent work is handed over to the database. If you try to insert multiple records with the same id into the table, the database will reject it.

There are two ways to specify the master of a table.

1. Specify when creating table.

create table T_GIRL
(
  id        char(4)         not null,   -- 编号
  name      varchar2(30)    not null,   -- 姓名
  yz        varchar2(20)        null,   -- 颜值
  sc        varchar2(20)        null,   -- 身材
  weight    number(4,1)     not null,   -- 体重
  height    number(3)       not null,   -- 身高
  birthday  date            not null,   -- 出生时间
  memo      varchar2(1000)      null,   -- 备注
  primary key(id)                       -- 指定id为表的主键
);

2. Modify the already built table and add primary key constraints.

alter table 表名 add constraint 主键名 primary key(字段名1,字段名2,......字段名n);

For example:

alter table T_GIRL add constraint PK_GIRL primary key(id);

In the Oracle database, although the primary key is not required, it is best to set a primary key for each table, whether it is a single-field primary key or a multi-field primary key (composite primary key), its existence represents the integrity of the table structure, and the primary key is also It can be used for foreign key associations of other tables, and the knowledge of foreign keys will be introduced below.

Two, foreign key

2.1, the concept of foreign key

Foreign key (foreign key) is used to express the relationship between two table data, add the primary key field in the table to another table, and then create a constraint relationship between the two tables, these fields become the second table The foreign key.

The Supergirl Draft event has two data tables:

1) Competition area parameter table

Division code, division name, ….

2) Supergirl Basic Information Form

Competition area code, super girl number, name, face value, figure, height, weight, ....

When entering the basic information of Super Girls, you must select the competition area. In order to ensure the validity of the data, when it is required to enter the competition area code, you must ensure that the competition area code is included in the competition area parameter table, otherwise the data will be inconsistent. In order to ensure the integrity of the data, it must be in the program. Judge the legitimacy of the data. In view of this situation, a foreign key is used in the table structure design to constrain the competition area code fields of the two tables.

For the competition area parameter table, the competition area code is the primary key of the table.

For the Supergirl basic information table, the competition area code is the foreign key of the table.

The competition area parameter table is also called the master table, and the Supergirl basic information table is also called the slave table.

2.2, the role of foreign keys

Reasonable data structure design, the data in the table must have consistency constraints, use foreign keys, let the database constrain the consistency of the data, and do not give anyone a chance to make mistakes. What happens without foreign keys? It doesn’t matter if you don’t use it. If you don’t use foreign keys, you need to write code to judge in the program, and you must be careful when manipulating data manually.

2.3. Foreign key constraints

1. When operating the slave table, the database will:

a: When inserting a new record from the table, if the foreign key value does not exist in the main table, prevent the insertion.

b: When modifying the records of the slave table, if the value of the foreign key does not exist in the master table, the modification is prevented.

2. When modifying the main table, the database will:

a: When the master table modifies the primary key value, the modification will be prevented if the old value exists in the slave table.

3. When deleting the main table, the database will (choose one of three):

a: When the primary table deletes a row, its primary key value exists in the secondary table to prevent deletion.

b: When a row is deleted from the master table, the relevant rows from the slave table are deleted together.

c: When deleting a row from the main table, set the foreign key field of the related row from the table to null.

2.4, create a foreign key

grammar:

alter table 从表名
   add constraint 外键名 foreign key (从表字段列表)
      references 主表名 (主表字段列表)
      [on delete cascade|set null];

illustrate:

The foreign key name is an Oracle identifier. It is recommended to use the method of FK_slave table name_primary table name .

When the master table deletes a row, its primary key value exists in the slave table to prevent deletion. If it is on delete cascade, it will delete the related rows of the slave table; if it is on delete set null, it will set the foreign key field of the slave table to is null.

2.5. Delete foreign key

alter table 从表名 drop constraint 外键名;

2.6. Examples

/* 创建赛区参数表。 */
create table T_AREACODE
(
  areaid   number(2)   not null,    -- 赛区代码,非空。
  areaname varchar(20) not null,    -- 赛区名称,非空。
  memo     varchar(300),            -- 备注
  primary key(areaid)               -- 创建主健。
);
 
/* 创建超女基本信息表。 */
create table T_GIRL
(
  id        char(4)         not null,   -- 编号
  name      varchar2(30)        null,   -- 姓名
  areaid    number(2)           null,   -- 赛区代码
  yz        varchar2(20)        null,   -- 颜值
  sc        varchar2(20)        null,   -- 身材
  memo      varchar2(1000)      null,   -- 备注
  primary key(id)                       -- 创建主健。
);
 
/* 以下三种创建外键的方式只能三选一  */
/* 为T_GIRL创建外键,无on delete选项。 */
alter table T_GIRL
   add constraint FK_GIRL_AREACODE foreign key(areaid)
      references T_AREACODE(areaid);
 
/* 为T_GIRL创建外键,采用on delete cascade选项。 */
alter table T_GIRL
   add constraint FK_GIRL_AREACODE foreign key(areaid)
      references T_AREACODE(areaid)
      on delete cascade;
 
/* 为T_GIRL创建外键,采用on delete set null选项。 */
alter table T_GIRL
   add constraint FK_GIRL_AREACODE foreign key(areaid)
      references T_AREACODE(areaid)
      on delete set null;

Guess you like

Origin blog.csdn.net/KevinChen2019/article/details/127955913