Software testing sql learning foreign key constraints

mysql foreign key summary

What we have learned before are all operations on a table. If you want to perform operations between multiple tables, you need to use foreign keys to associate them.
The role of foreign keys: it can associate multiple tables, make connections between tables, and achieve commonality extraction.

Application Scenario

If there are many data items, store all the data in one table. If the table is too large, the operation efficiency will be affected.

The solution is to split a table into multiple tables and use foreign keys to associate them.

If you want to design an employee table
1) Employee table: number, name, age, gender, branch, department
2) Department table: number, department name, department manager, main task
3) Company table: number, branch name , address, telephone, legal person
extract the data of the company and department to form a separate table, and use the company table as the main table, the company table is associated with the department table, and the department table is associated with the employee table, that is, the "number" of the company table is used Point to the "company" of the department table, and use the "number" of the department table to point to the "department" of the employee table.


Summary:
1. The design of the above three tables solves the problem of redundant fields + too large tables☆ (in fact, the department table names can be further subdivided)
2. The foreign key of one table is the primary key corresponding to another table (also It can be the primary key of the current table)
3. It is best to set the foreign key when creating the table, and the corresponding table type is InnoDB type. If it is another type, there will be no constraint. 4. For a
table with foreign key constraints, it must be set first. Create the main table (pointed to), and then create the secondary table (with foreign keys)
5. The two tables of foreign key constraints must be of InnoDB type

foreign key settings

1 Create company table

CREATE TABLE `company` (
  `id` int NOT NULL AUTO_INCREMENT,
  `company_name` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `company_addr` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `company_tel` varchar(16) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `company_person` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
INSERT INTO `company` VALUES ('1', '北京分公司', '北京市朝阳区xxxx号', '010-123456', '李四111');
INSERT INTO `company` VALUES ('2', '上海分公司', '上海市浦东区xx号', '123-5678', '李四222');
INSERT INTO `company` VALUES ('3', '深圳分公司', '深圳市xxx号', '789-111222', '李四333');

2 Create department table

create table department (
  id int primary key not null auto_increment,
  department_name varchar(32),
  department_leader varchar(32),
  worker varchar(128),
  company_fid int,
  constraint fk foreign key(company_fid) references company(id)
)engine=innodb;

3 Create employee table

create table staff (
  id int primary key not null auto_increment,
  staff_name varchar(32),
  staff_age tinyint,
  staff_sex char(1),
  department_fid int,
  CONSTRAINT staff_fk foreign key(department_fid) REFERENCES department(id)
)engine=innodb

foreign key summary

1 Foreign keys can only point to primary keys

2 The field types of the foreign key and the primary key must be the same, usually int type

3 Foreign key is a constraint, the purpose is to facilitate data management. When there are redundant fields, a table can be extracted for association to simplify data storage

4 There can be multiple foreign keys in a table, but only one primary key

5 The table types of foreign keys and primary keys must be InnoDB

6 The characteristic of a relational database is that there is an association between data

7 If a table is associated with another table, it cannot be deleted.

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/132479115