简体   繁体   中英

How to store multiple values in one field of a MySQL table column?

I want to create table stops for all stops with these columns id, name,route, lat, long, arrivaltime but I dont know how can I manage it to get the route column in the stops table? since the one route has many numbers?

{
   "id": 1
   "stops_name": "Amersham ",
   "route": "8,4,7,34,45,8017, 57, 20,......... 30 entries"
   "arrival_time": {
                    "mon-fri": [ "05:38", "06:07","06:37",.....50 entries],
                    "sat": ["05:34","06:01","06:31",...........50 entries],
                    "son": ["06:02","06:34","07:04",...........50 entries]
                   },
    "stops_lat": 83.837994,
    "stops_long": 18.700423
 }



stt.execute("CREATE TABLE IF NOT EXISTS stops"
        + "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
        + " name varchar(30) NOT NULL, "
        + " route INT(11) NOT NULL, "
        + " lat double(10,6) NOT NULL, "
        + " longi double(10,6)NOT NULL) " );

stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
        +  " weekday VARCHAR(20) NOT NULL,"
        + "arrivaltime time NOT NULL,"
        + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );

Think about it this way. Represent this:

"route": "8,4,7,34,45,8017, 57, 20,......... 30 entries"

Like this:

"route": { "8"
         , "4"
         , "7"
         , "34"
         , "45"
         , ...
         }

Consider representing that in a table like this:

CREATE TABLE stop_route 
( id       INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
, stop_id  INT(11) NOT NULL COMMENT 'FK ref stop'
, route_id INT(11) NOT NULL COMMENT 'FK ref route'
, UNIQUE_KEY stop_route_UX1 (stop_id, route_id)
, CONSTRAINT FK_stop_route_stop (stop_id) REFERENCES stop(id)
, CONSTRAINT FK_stop_route_route (route_id) REFERENCES route(id)
}

If route is not an entity in your model, then remove the foreign key constraint.


Entity-Relationship model

I don't know your entity-relationship model.

But I suspect that there's actually a many-to-many relationship between route and stop . To resolve that, we'd introduce a new "relationship" table with foreign keys pointing to both "stop" and "route".

And I'm thinking that arrival_time is not independent of route. The question I'm asking is if every route has the same 50 arrival_time at a given stop ? Or, does each route have it's own set of arrival_time for a given stop . (I'm thinking it's the latter.)

  • a route arrives at zero, one or more stop
  • a stop is serviced by zero, one or more route

We resolve the many-to-many by introducing a relationship table

route >--- route_stop ---< stop

(Personally, in terms of naming things, I think the route_stop relationship table above should be named a "stop", and the stop entity table should be a "location".

On a schedule, a vehicle makes on a particular route makes a "stop" at a particular "location", at (or close to) a particular arrival_time .) But that's just nomenclature. Just depends on how you name things.

CREATE TABLE route_stop 
( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, route_id INT NOT NULL COMMENT 'FK ref route' 
, stop_id INT NOT NULL COMMENT 'FK ref stop'
, UNIQUE KEY route_stop_UX1 (route_id, stop_id)
, CONSTRAINT FK_route_stop_route FOREIGN KEY (route_id) REFERENCES route(id)
, CONSTRAINT FK_route_stop_stop FOREIGN KEY (stop_id) REFERENCES stop(id)
)

CREATE TABLE arrival_time 
( id            INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
, stop_route_id INT(11) NOT NULL COMMENT 'FK ref route_stop'
, weekday     VARCHAR(20) NOT NULL
, arrivaltime TIME NOT NULL
, CONSTRAINT FK_arrival_time_stop_route 
    FOREIGN KEY (route_stop_id) REFERENCES stop_route(id)
)

As far as I understand you want to create a relational database structure. And you want to have a relation between stops and routes table.

So assuming that one stop can belong to several routes and one route can have several stops that's many to many relationship between tables.

For instance you can take a look here http://www.tutorialspoint.com/hibernate/hibernate_many_to_many_mapping.htm

how to implement this relation in Hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM