How to normalize a database schema

别来无恙 提交于 2019-12-23 17:13:33

问题


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.


回答1:


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)
)



回答2:


You should create a separate cross-reference many-tomany - table which will be like

customer_flights(id int (pk),customer_id int, flight_id int,timecreated(optional))

one customer may have several flights booked like in past and in future...

and later join the two tables basing on the records in this table



来源:https://stackoverflow.com/questions/9292257/how-to-normalize-a-database-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!