如何规范化数据库模式 [英] How to normalize a database schema

查看:148
本文介绍了如何规范化数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表格:

  customer(
client_id(PK),
lastname,
firstname,
flightID(Fk)


flight(
flight_id(PK),
flightarrival,
flightdepart

我的问题是:




  • 模式有问题吗?

  • 如何向航班表中添加数据?






$ b

这里是一个批量表的例子:

> customer(
client_id(PK),
lastname,
firstname,
flightarrival,
flightdepart

但我想规范化并将其与客户表格分开,只是链接它们。

解决方案

您建议的模式意味着每个客户可能与一个(不是零,不超过一个)航班有关,这是错误的。



实质上,我认为你有一个多对多的关系,你可以用三个表来做:

 客户(id(PK),姓氏,名字)

航班(id(PK),flightarrival,flightdepart)

customer_flight b $ b customer_id REFERENCES位客户(id),
flight_id REFERENCES航班(id)


I have two tables:

customer (
  client_id(PK), 
  lastname, 
  firstname, 
  flightID (Fk)
)

flight (
  flight_id(PK), 
  flightarrival, 
  flightdepart
)

My questions are:

  • Are there any problems with the schema?
  • How can I add data to the flight table?

I really want to separate the two because bulk tables are confusing.

Here is an example of a "bulk table":

customer(
  client_id(PK), 
  lastname, 
  firstname, 
  flightarrival, 
  flightdepart
)

but I want to normalize it and separate it from the customer table and just link them.

解决方案

The schema you proposed would mean that each customer could be related to one (not zero, not more than one) flight, which feels wrong.

In essence, I think you have a many-to-many relationship, which you can do with three tables:

customer (id(PK), lastname, firstname)

flight (id(PK), flightarrival, flightdepart)

customer_flight (
    customer_id REFERENCES customer(id),
    flight_id REFERENCES flight(id)
)

这篇关于如何规范化数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆