MySQ- table relationships - foreign key - Modify Table Structure - replicate tables -03

Foreword

The irrational structure of the table (case)

First look at this table below

emp_info employee information table

id name gender dep_name dep_desc
1 jason male Education Department Teaching
2 there male Ministry of Foreign Affairs Vagrancy
3 tank male Education Department Teaching
4 kevin male Technology Technology development

All the information is recorded in a table of

Problems caused by

  • The table structure is not clear
  • Waste of hard disk space, data redundancy more
  • Table scalability, maintainability poor (can not ignore the shortcomings)

How to solve the problem?

Split the table to determine the table, and setting up tables associated

To determine the relationship between the table and the table, be sure to empathy (to reach a conclusion after the two parties must be thoughtful)

以员工表和部门表为例:
    先站在员工表看能否有多个员工对应一个部门
    翻译过来:
        一个部门能付否有多个员工
        可以!!!(暂时只能确定员工单向多对一部门)
        再站在部门表看能否有多个员工一起对应一个部门
    翻译过来
    ....乱了

How to determine the relationships between tables?

Lookup table relationships, be sure to stand in the perspective of the two tables were all considered completed before a conclusion , otherwise you can not get the right answer

Table Relationships

  • Many
  • Many to many
  • One to One

Or two tables does not matter

Many

Many-to-one way is the "one to many" foreign key relationships

Whether to-many or many-are-many relationship, call-to-many, many-to-no relationship

Many to many

If both sides are one way of many to one relationship, then the relationship between the two is many to many

analysis

Many relationship, you must create additional third table, used to record special relationship between the two tables

  • If you come, both tables must be associated with each other one to one idea, let the other party must first establish, it can not build a table, a foreign key relationship is stored, it would open a separate table, save relationship

Case establishment

Delete the foreign key is updated simultaneously on

One to One

Scenarios

  • Split the table to optimize performance (user details and display information) when particularly large table
  • Customers and students (customers may be a student, the student must be the client)

If the two-way-to-many is not established, then only two cases between two tables

  1. One relationships
  2. No relationship

The foreign key field must be limited with a foreign key + unique, unique and must appear

The easiest table to determine the relationship between grammar

Three relationships common case

Many

There may be a relationship is one to many ( and only one )

Books and Publishing

Can you have more than one book publishers? Can not

Can a publishing house more than the first edition of this book, you can! ! !

Many relationship

Many to many

There are two possible relationship is many to many

Books and Authors table

Can a book can have multiple authors! !

Can an author can write more than one book! !

Many relationship

Or no-one relationship

Neither can, either one to one relationship or no relationship

Author and author details

Can an author can not have more details! !

Can an author details have multiple authors can not! ! !

One relationships

How to create table relationships?

Foreign key foreign key

In MySQL by foreign keys to establish a rigid relationship between the table and the table

The relationship field generally referred to as the foreign key field

Determining foreign key field homed

  • Many of the foreign key fields should be built in the "many" of that party
  • Many to many foreign key field to build in an additional third table on
  • One of the foreign key field built in either will do, but it is recommended to build a query in the higher frequencies party (foreign key field must be guaranteed to be unique)

Precautions have foreign key relationships

  • In when creating the table , you must first create the associated table
  • When inserting data should also be inserted into the first associated data
  • Cascade update , cascading deletes

Note the comma foreign key, (comma represents the end of a field) (do not forget to build the table or field definition statements do not add the final piece, comma)

Although the foreign key table can help you to force a relationship, but also to increase between two rows of the table constraints related data

Modify table

mysql is not case sensitive

Be sure to note the sign in English

Modify the table name

ALTER TABLE 表名 RENAME 新表名;
alter table 表名 rename 新表名;

Increase the field

ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…], ADD 字段名  数据类型 [完整性约束条件…];
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST;  # 直接移到最前面
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名;  # 选择要插在哪个字段后面   

Delete field

ALTER TABLE 表名 DROP 字段名;

Modify the field

# modify只能改字段数据类型完整约束,不能改字段名,但是change可以!
ALTER TABLE 表名 MODIFY  字段名 数据类型 [完整性约束条件…];
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

Copy the table

Copy table structure records +

key will not be copied: primary keys, foreign keys and indexes

# 查询语句执行的结果也是一张表,可以看成虚拟表

# 复制表结构+记录 (key不会复制: 主键、外键和索引)
create table new_service select * from service;

like 可以吗? 试试

Condition for use only copy the table structure

The condition is false, can not find the data

select * from service where 1=2;        //条件为假,查不到任何记录

# 只复制表结构
create table new1_service select * from service where 1=2;  
create table t4 like employees;

Today, database manipulation statements

Create a database

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql>
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| just_test          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

Split employee and department information into two tables

An employee belongs to a department

A department has multiple employees

---> many

In the reservation department number of employees over there

Create a table (employee and department information table)

create table dep(
    id int primary key auto_increment,
    dep_name varchar(30),
    dep_comment varchar(60)
);

create table emp(
    id int primary key auto_increment,
    name varchar(50),
    gender enum('male', 'famale', 'others') not null default 'male',
    dep_id int,
    foreign key(dep_id) references dep(id)
);

insert into dep (dep_name, dep_comment) values('研发部', '敲代码的那一群人'), ('教育部', '成天这啊那啊找麻烦的那群人'), ('后勤保障部', '搬水的关门的收拾卫生的');

insert into emp (name, dep_id) values('jason', 1), ('egon', 2), ('agong', '3');


alter table emp modify foreign key(dep_id) refrences dep(id)
on update cacade
on delete cacade;

select emp.id,name,gender,dep.dep_name,dep.dep_comment from emp left join dep on dep.id=emp.id;

Guess you like

Origin www.cnblogs.com/suwanbin/p/11396371.html