Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize a database schema

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.

like image 372
rj tubera Avatar asked May 27 '26 10:05

rj tubera


2 Answers

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)
)
like image 68
zgpmax Avatar answered May 30 '26 05:05

zgpmax


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

like image 20
Sergey Benner Avatar answered May 30 '26 05:05

Sergey Benner