How do I design a bus route database? [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a bus route DB in such a way that it gives the result as follows :
Input :
Source = station-X , Destination = station-Y
Output : Various bus name/nos. which go through Station-X and Station-Y
So far i have :
Bus(bus_no), Bus_Route()
Bus(bus_name/no.)
id | bus_no
1 | 33
2 | 33A
Bus_Route(route_id,bus_no)
id | route_id | bus_no
1 | route-A | 33
2 | route-B | 33A
How do i proceed to store the station names of the respective routes.
I am no database expert, so any help would be valuable .
Thanks in advance.

You'd be best off creating an extra table holding the STATION - ROUTE relationship. You'd define a many-to-many relationship here, meaning that a station can have many routes, and a route can have many stations.
busses (only necessary if multiple physical busses exist with same *bus_no*)
id | bus_no
---------------
1 | 33
2 | 33A
3 | 33A
bus_routes (both *route_id* and *bus_no* have to be unique values)
route_id | bus_no
---------------------
route-A | 33
route-B | 33A
stations_routes (many-to-many, route_order tells us the order within the route)
route_id | station_id | route_order
------------------------------------
route-A | Egham | 1
route-A | Haymarket | 2
route-A | Egham Plaza | 3
route-B | Egham | 1
route-B | Langley | 2

I think that you need the following tables:
BUSROUTE: route_id, routenumber
BUSSTOPS: stop_id, stopname
BUSROUTESTOPS: route_id, stop_id, displayorder
Using the London Underground as an example, the busroute table would hold values like 'Jubilee', 'Central', 'District', 'Bakerloo', etc.
The busstops table would hold the station names, eg Stanmore, Canons Park, Queensbury, Kingsbury, Wembley Park, etc. This table is necessary as a station can service more than one route.
The busroutestops hold the route number, the station name and the order in which the station appears for this route. The primary key would be an amalgamation of route_id and stop_id.
Once you've got past this, you'll probably need a timetable table, which in its simplest form would contain three fields: an id, a route_id and the departure time. It is left 'as an exercise for the reader' how to represent the return journey!
The London Underground was probably not a good example as some lines - especially the Northern and District lines - have multiple routes. These would best be treated as separate routes.

Related

Auto generate columns in Microsoft Access table

How can we auto generate column/fields in microsoft access table ?
Scenario......
I have a table with personal details of my employee (EmployDetails)
I wants to put their everyday attendance in an another table.
Rather using separate records for everyday, I want to use a single record for an employ..
Eg : I wants to create a table with fields like below
EmployID, 01Jan2020, 02Jan2020, 03Jan2020,.........25May2020 and so on.......
It means everyday I have to generate a column automatically...
Can anybody help me ?
Generally you would define columns manually (whether that is through a UI or SQL).
With the information given I think the proper solution is to have two tables.
You have your "EmployDetails" which you would put their general info (name, contact information etc), and the key which would be the employee ID (unique, can be autogenerated or manual, just needs to be unique)
You would have a second table with a foreign key to the empployee ID in "EmployDetails" with a column called Date, and another called details (or whatever you are trying to capture in your date column idea).
Then you simply add rows for each day. Then you do a join query between the tables to look up all the "days" for an employee. This is called normalisation and how relational databases (such as Access) are designed to be used.
Employee Table:
EmpID | NAME | CONTACT
----------------------
1 | Jim | 222-2222
2 | Jan | 555-5555
Detail table:
DetailID | EmpID (foreign key) | Date | Hours_worked | Notes
-------------------------------------------------------------
10231 | 1 | 01Jan2020| 5 | Lazy Jim took off early
10233 | 2 | 02Jan2020| 8 | Jan is a hard worker
10240 | 1 | 02Jan2020| 7.5 | Finally he stays a full day
To find what Jim worked you do a join:
SELECT Employee.EmpID, Employee.Name, Details.Date, Details.Hours_worked, Details.Notes
FROM Employee
JOIN Details ON Employee.EmpID=Details.EmpID;
Of course this will give you a normalised result (which is generally what's wanted so you can iterate over it):
EmpID | NAME | Date | Hours_worked | Notes
-----------------------------------------------
1 | Jim | 01Jan2020 | 5 | ......
1 | Jim | 02Jan2020 | 7 | .......
If you want the results denormalised you'll have to look into pivot tables.
See more on creating foreign keys

Database Design question best way to solve this issue [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm kind of new in database design and I'm trying to find the best way to solve an issue I'm facing.
Let's think about the following example:
Imagine I want to store information about patients and these patients can have 0+ diseases.
What's the best way of storing arranging the tables to display the diseases that each patient can have? I get confused as to what happens when a patient would have 3 diseases; how is this stored in a relational database? e.g. without having repeated rows on the diseases table for example (static number of diseases)..
I'm not sure if I'm making myself totally clear here!
But let's say I don't think it's efficient to have:
Patient table -
Patient_id , disease_id
1, (3,4,5,6)
Any help is appreciated!
As mentioned in the comments, you would use an associative entity for that.
Let say you have the following diseases:
| Disease_id | Disease_name |
-----------------------------
| 1 | Cancer |
-----------------------------
| 2 | Leucemia |
-----------------------------
Your patients may look like this
| Patient_id | Patient_name |
-----------------------------
| 1 | Peter Jones |
-----------------------------
| 2 | Mark Jacobs |
-----------------------------
You now create a table like so (lets call it ill_patients)
| Patient_id | Disease_id |
---------------------------
| 1 | 1 |
---------------------------
| 1 | 2 |
---------------------------
This would mean that poor Peter Jones has cancer as well as leucemia. You can now query your patient table like so:
SELECT patients.patient_name, diseases.disease_name
FROM (diseases INNER JOIN ill_patients ON diseases.disease_id = ill_patients.disease_id) INNER JOIN patients ON ill_patients.patient_id = patients.patient_id;
This gives you all the patients with their respective diseaes.
Patients:
patient_id, name, phone
Disease:
disease_id, name, description
Patient_Disease:
patient_id, disease_id
Example:
patient_id, name , phone
1 , 'jhon', '555-1234'
disease_id, name , description
1 , 'Cancer' , 'Cancer is the uncontrolled development of cells.'
2 , 'Diabetes', 'Diabetes is a disease that occurs when your blood glucose is too high.'
patient_id, disease_id
1 , 1
1 , 2
Then you can do
SELECT p.name, d.name, d.description
FROM patients p
JOIN patient_disease pd
ON p.patient_id = pd.patient_id
JOIN diseases d
ON pd.disease_id = d.disease_id

MS access 2007 - checklist options(multiple) to be stored in a column of the database

I have a situation like - the customer form in MS access 2007 have list of documents provided by customers. The list is in the checklist format. Assuming there are 6 documents under the checklist. So if the one or more checklists are selected, all the selected list should be saved in the database column named "Documents_Provided". So in order to achieve this scenario what should I have to do. How should my database field "Documents_provided" should be declared and what do I have to write in VBA code.
As per your Question heading suggests "Multiple to be stored in a Column of the database" is a very bad table design, it breaks one of the rules of Fundamentals of Database Design, Data should be atomic.
The system you should be having is a One to Many, between the Customer and Document table. The Customer table will normally have the basic customer information; one side of the relationship, and the Documents table will have all the documents that pertain to each Customer; many side of the relationship. In Addition you will have another table Document Category that will say what are all the documents that needs/can have for each customer. So sample data in your table will be something like,
tbl_Customers
`````````````
ID | customerName | customerArea
----+-------------------+------------------
1 | Paul | Bournemouth
2 | Eugin | Bristol
3 | Francis | London
tbl_DocumentsCategory
`````````````````````
ID | DocumentName
----+---------------------------
1 | Address Proof
2 | Photo ID
3 | Employer Certificate
tbl_CustomersDocument
`````````````````````
ID | CustomerID | DocumentID
----+---------------+--------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 3
6 | 3 | 2
So when you need to get the list of Documents each Customer has, you simply JOIN the two tables to get the right information. This is the standard and efficient way to organize the data. I hope this helps, and you stick to this.

Inserting data into many-to-many relationship table

I'm trying to build a database with multiple tables for a study/research. This is the first time I'm designing database of this magnitude; the database grows by 100-200 records a day, and so far I have the data since 2010. Out of all the data, Generic Sequence Number, Product Name and the Strength of a drug (prescription) is slightly bothering me. This is what I have done so far:
Generic Seq number is unique to the strength of drug (product name). So, I have a table that contains id, generic seq no, and strength. Another table is for prod_id and product name. Each Generic seq number may have one or more product name, and each product name may have different generic seq number based on the strength. So, I set it up as many-to-many relationship. I created another table for this relationship that contains rx_id, drug_id, and prod_id. Since many patients may be prescribed for the same drug, the drug_id and prod_id may repeat several times in the rx_table.
My first question is, is this design appropriate?
How should I insert the data into rx_table? Should I create new record every time for new data even if the drug_id and prod_id already exist in the rx_table, or should I look for the rx_id where the drug_id and prod_id sequence exist and insert the rx_id into the other main table (not shown) which contains other data.
Or is this question too vague?
Thank you for your help.
I don't know what exactly is your Generic Sequence Number so i'll just use a real life drug example. From your description i think it's pretty similar to your application. Lets say you have Paracetamol as an agent. Then your Generic Sequence Number table would be something like
drug_id | generic_seq_no | strength
--------+--------------------+----------
1 | Paracetamol-100 | 100
2 | Paracetamol-250 | 250
3 | Paracetamol-500 | 500
Your product table would contain the names of the trademarks:
prod_id | prod_name
----------+------------
1 | Tylenol
2 | Captin
3 | Panadol
the rx_table contains the combinations of trademark name, agent and strength:
rx_id | drug_id | prod_id
-------+----------+----------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
6 | 3 | 2
7 | 3 | 3
So e.g. the first row would be Tylenol, containing 100 mg of Paracetamol. Now you have what can be prescribed by a doctor and that's what you already did so far. So as i said your approach is fine.
Now you need (or have?) another table with all your patients
patient_id | firstname | lastname
-----------+-----------+-----------
1 | John | Doe
2 | Jane | Doe
In the end, you must link your trademark/agent/strength combination to the patients. Since one patient may get different drugs and multiple patients may get the same drug you need another many-to-many-relation, let's call it prescription
prescription_id | patient_id | rx_id
----------------+------------+------
1 | 1 | 1
2 | 1 | 3
3 | 2 | 4
This means John Doe will get Tylenol and Panadol containing 100 mg Paracetamol each. Jane Doe will receive Tylenol with 250 mg Paracetamol. I think the table you will be inserting the most is the prescription table in this model.

Query relations within the same table

I have a table of entities that can have many-to-many relations to each other, using a second junction table. At a first glance, the design may seem flawed and suggests a separate table for each type of entity. The thing is, that the entities are generic, and completely user-defined. They may also be connected completely ad-hoc and each entity can have an unlimited number of connections.
Here is a simplified example of my tables:
Entities
------------
Entity | Id | Type
-------------------
Event | 1 | Request
Stroll | 2 | Activity
Dinner | 3 | Activity
Angela | 4 | Person
Anders | 5 | Person
Michael | 6 | Person
Junctions
----------------
Left | Right
----------------
1 | 2 // Connect Request -> Stroll
2 | 4 // Connect Stroll -> Angela
1 | 3 // Connect Request -> Dinner
3 | 5 // Connect Dinner -> Anders
3 | 6 // Connect Dinner -> Michael
Now to my question:
I would like to perfom queries from the view-point of different entities. Lets say I would like to look at Requests and see what Activities they have, and any Persons attending each activity. I would like to get a result like this:
Request | Activity | Person
-----------------------------
Event | Stroll | Angela
| Dinner | Anders
| Michael
I would also, for example, like to be able to flip the coin and look at Persons And see what Requests they attend, like this:
Person | Request
-----------------
Angela | Event
Anders | Event
Michael | Event
How can i write queries to achieve results like this, and is it even possible with the current structure? I have spent much time on googling and trials with no luck, and I am very grateful for any help.
Here is an SQLFiddle
That's how you do it
SELECT e1.Entity Request,
e2.Entity Activity,
e3.Entity Person
FROM Junctions j1
JOIN Junctions j2
ON j1.`Right` = j2.`Left`
JOIN Entities e1
ON j1.`Left` = e1.Id
JOIN Entities e2
ON j1.`Right` = e2.Id
JOIN Entities e3
ON j2.`Right` = e3.Id;
SQLFiddle
To help you understand - at first I joined Junctions on itself like that:
SELECT j1.`Left` Request,
j1.`Right` Activity,
j2.`Right` Person
FROM Junctions j1
JOIN Junctions j2
ON j1.`Right` = j2.`Left`;
And then joined to the Entity, as you can see, to replace the Ids. One time for each type.
But, nevertheless, I still think that this architecture is horrible, and it needs to be redesigned.