问题
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