MYSQL: Querying from tables based upon Len Silverston's "The Data Model Resource Book"

StackOverflow https://stackoverflow.com/questions/19967765

  •  30-07-2022
  •  | 
  •  

Question

I am currently in the process of developing a Database software for a company that I am working with. I based the tables off of Len Silverston's book, as I found it to be an excellent source for information based on data modeling.

Now, you do not need to be acquainted with his book to know the solution to my problem, but I could not think of any other way to word my title.

Suppose I have two tables, Persons and Person_Names:

CREATE TABLE Persons
(
 party_id    INT    PRIMARY KEY,
 birth_date  DATE,
 social      VARCHAR(20)
);

CREATE TABLE Person_Names
(
 party_id    INT,
 name_id     INT,
 person_name VARCHAR(20),

 CONSTRAINT person_names_cpk
    PRIMARY KEY (party_id, name_id)
);

The two tables can be joined by party_id. Also, under Person_Names, name_id = 1 correlates to the person's first name (which is stored in the field person_name) and name_id = 2 is the person's last name.

* EDIT * Someone asked for some data, so I will add some data below:

INSERT INTO Persons VALUES
 (1, '01-01-1981', '111-11-1111'),
 (2, '02-02-1982', '222-22-2222'),
 (3, '03-03-1983', '333-33-3333');

INSERT INTO Person_Names VALUES
 (1, 1, 'Kobe'),
 (1, 2, 'Bryant'),
 (2, 1, 'LeBron'),
 (2, 2, 'James'),
 (3, 1, 'Kevin'),
 (3, 2, 'Durant');

Now that I added those data, how would I query the following?

-----------------------------------------------------------------------
| Party Id  |  First Name  |  Last Name  |  Birthdate  |  Social No.  |
-----------------------------------------------------------------------
|     1     |    Kobe      |   Bryant    |  01-01-1981 |  111-11-1111 | 
|     2     |    LeBron    |   James     |  02-02-1982 |  222-22-2222 |
|     3     |    Kevin     |   Durant    |  03-03-1983 |  333-33-3333 |  
-----------------------------------------------------------------------

Thanks for taking your time to read my question!

Was it helpful?

Solution

Quite easily. I don't know the book, but presumably it contains some material describing table joins and their application in queries such as this one:

SELECT Persons.party_id AS "Party Id",
    firstname.person_name AS "First Name",
    lastname.person_name AS "Last Name",
    Persons.birth_date AS "Birthdate",
    Persons.social AS "Social No."
FROM Persons
    INNER JOIN Person_Names firstname 
        ON Persons.party_id = firstname.party_id
        AND firstname.name_id = 1
    INNER JOIN Person_Names lastname
        ON Persons.party_id = lastname.party_id
        AND lastname.name_id = 2

Be advised that this will return results only for those people who have both a first and a last name defined in your Person_Names table; if one or the other isn't present, the INNER JOINs' ON clause conditions will exclude those rows entirely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top