-2

As part of my school project, I'm supposed to design/make a database where one can store/update/retrieve yearly data about international trade and transportation. To begin with, I isolated a small part of the database in order to start small.

  • Firstly I tried to design a diagram that would store the number of passengers (not individual passengers) that embarked/disembarked on/off ships in each port of every country every year and how many local and foreign passengers there were (I don't need those two to interact).

    1st diagram

    (Ignore the Passengers on the top.) and the inwards_outwards entity would give me a table in the database that would look like this:

    table of 1st diagram

  • Secondly I tried to design the diagram of a table where I could store Origin-Destination data (e.g. of the passengers that arrived in (or left from ) a country, how many came from (went to) each other country etc.

    For instance in 2011, from England 20 passengers flew to France, 10 to Germany, etc. and in 2011, in England arrived 23 from France, 19 from Germany, etc.

    2nd diagram

    and the od_hellas entity would give me a table like this:

    table of 2nd diagram

Questions:

  1. Do the above look OK to you?

  2. Is there a more efficient way to store yearly data?

  3. Is what I'm trying to make doable in the context of a project? Any advice in general?

1 Answers1

0

You can do this with three tables as shown below. If you want to add data about Passengers then you would need a fourth table "Passenger"

The value in your "Numbers" column can be calculated from the base data by using SQL COUNT something like this:

SELECT COUNT(passengerNr)
FROM Departure
WHERE portCode = "EL_OGRPIR";

To get the data by year, you just add something like [AND date = "2011"] (depends on how you choose to store your date data.)

If my solution helps, please click on the vote icon.

Here is the logical view of the tables.

Here is the logical view of the tables

Here is the SQL DDL that you would use to generate the tables in a database. (e.g. you could cut and paste this SQL into the "New Query" panel in SQL Server Management Studio.)

CREATE SCHEMA Trade
GO

CREATE TABLE Trade.Port
(
    portCode nchar(15) NOT NULL,
    countryCode nchar(2) NOT NULL,
    portName nchar(50) NOT NULL,
    type nchar(10) CHECK (type IN (N'SeaPort', N'AirPort', N'LandBorder')) NOT NULL,
    CONSTRAINT Port_PK PRIMARY KEY(portCode)
)
GO


CREATE TABLE Trade.Departure
(
    passengerNr int NOT NULL,
    portCode nchar(15) NOT NULL,
    "date" datetime NOT NULL,
    isInternational bit,
    CONSTRAINT Departure_PK PRIMARY KEY(passengerNr, portCode)
)
GO


CREATE TABLE Trade.Arrival
(
    passengerNr int NOT NULL,
    portCode nchar(15) NOT NULL,
    "date" datetime NOT NULL,
    isInternational bit,
    CONSTRAINT Arrival_PK PRIMARY KEY(passengerNr, portCode)
)
GO


ALTER TABLE Trade.Departure ADD CONSTRAINT Departure_FK FOREIGN KEY (portCode) REFERENCES Trade.Port (portCode) ON DELETE NO ACTION ON UPDATE NO ACTION
GO


ALTER TABLE Trade.Arrival ADD CONSTRAINT Arrival_FK FOREIGN KEY (portCode) REFERENCES Trade.Port (portCode) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
Ken Evans
  • 362
  • 1
  • 11
  • Two more questions. 1) I suppose the PK passengerNr is just an autogenerated number and could be whatever and as large/long as it has to be right ? 2) Lets say that for Croatia and Cyprus I don't have number for each port but total the total traffic for each country. Can a primary key (in this case port_id) be Null if I want to (I see that in the code you write NOT NULL, can I just not include that? 3) Could you explain the "IsInternational: boolean" a bit? I'm a complete amateur at this. – Still Closed May 15 '19 at 10:26
  • The limitations of Stack Overflow mean that my answer is in several parts (1) You would have to invent your own way to assign a value to "passengerNr". You could use an autogenerated number as long as you understand the limitations of autogenerated numbers. (like what happens if you delete one) (2) A primary key cannot, under any circumstances, have a value of NULL. – Ken Evans May 15 '19 at 11:57
  • From your description, I assumed that you already had a list of ports and their related codes such as EL_OGRPIR. If you don't have a unique code for every port, you will have to create one. For example, you could use a composite primary key "Country + Port Name" Just to be clear: You cannot have a value of "NULL" in a column that is designated as the primary key. Secondly, the use of NULLS for any column is usually an indication of a sloppy database schema design. – Ken Evans May 15 '19 at 11:59
  • I always design my schemas to the standard of what is called "Fifth Normal Form" (so they never have nulls.) I used "IsInternational: boolean" to provide a simple "yes/no" way to differentiate "Domestic" traffic from "International" traffic. (If it is not international then it is domestic). – Ken Evans May 15 '19 at 12:00
  • By the way, I have been using formal methods for data modeling for over 25 years. I use a method called "object-role modeling" (ORM) and a tool called NORMA that automatically generates each schema (and the SQL DDL) in fifth normal form. ORM is far better than the ER approach. – Ken Evans May 15 '19 at 12:06
  • @StillClosed I'm happy to answer your questions. If my answers have helped you, please vote for my answers. – Ken Evans May 15 '19 at 14:40