Mysql – Object-oriented programming CRUD design pattern

MySQLoraclepostgresql

At first I have posted this question at https://cstheory.stackexchange.com/questions/17170/object-oriented-programming-crud-design-pattern – that's because I think it's a subect question, and it can not answered by "yes" or "no" or with a single answer.

I don't know if here it's the better place for this question, but here we go. I have shared a common question for DB designers – imagine the following SELECT:

 SELECT
     P.id,
     P.legalName,
     P.type,
     L.tradeName,
     L.foundation,
     L.EIN,
     N.SSN,
     N.gender,
     N.birthDay,
     A.zipCode,
     A.address,
     A.number,
     A.district,
     A.city,
     A.state
 FROM
     Person as P

 LEFT JOIN
     LegalEntity AS L ON L.person = P.id AND P.type = 'L'

 LEFT JOIN
     NaturalPerson as N ON N.person = P.id AND P.type = 'N'

 LEFT JOIN
     Address as A ON A.person = P.id AND E.legalAddress = 1

The above SELECT has a "maintaining" level relatively high by the following factors:

  • CREATE – When a Person are inserted in the database the next insert will be based on Person.type (L => Legal or N => Natural) that defines if will create a new record on LegalEntity or NaturalPerson with Person last inserted id as the foreign key.
  • READ – To provide a consistent data, we use JOINS with aditional term "AND T.type" for LegalEntity and NaturalPerson to return only relative data to Person.type FLAG. For eg. if Person it's a LegalEntity than we have EIN (Employer Identification Number or Federal Tax Id), if not, it's a NaturalPerson than have SSN (Security Social Number)
  • UPDATE – When you perform an update is needed to test whether there is specialization. This is because a record could originally have been created as type = N (Natural) and later want to upgrade it to Corporate (L = Legal). An INSERT to create original NaturalPerson was made, and now with the exchange of nature type is necessary to exclude logically or physically the NaturalPerson record and then create the new record of LegalEntity.
  • DELETE – WITHOUT aditional complexities.

Imagining all this complexity is also necessary. Now think that be need to run in 3 differente SGDBs (MySQL, Oracle and Postgres) – the quoted example are created in MySQL.

  • What are the best practices that we can adopt in this scenario?
  • Maintenance of data with the triggers assist for the 3 SGDS?
  • Create a CRUD with appropriate treatments implemented via application?

Thank you in advance for your time.

Best Answer

CREATE - When a Person are inserted in the database the next insert will be based on Person.type (L => Legal or N => Natural) that defines if will create a new record on LegalEntity or NaturalPerson with Person last inserted id as the foreign key.

Do all inserts for each "person" in a single transaction based on updatable views, not on base tables. Every dbms exposes a function that will return the last inserted ID number. (For PostgreSQL, this SO answer.)

READ - To provide a consistent data, we use JOINS with aditional term "AND T.type" for LegalEntity and NaturalPerson to return only relative data to Person.type FLAG. For eg. if Person it's a LegalEntity than we have EIN (Employer Identification Number or Federal Tax Id), if not, it's a NaturalPerson than have SSN (Security Social Number)

In the query you posted, you don't do that at all. Instead, you retrieve all the data for every type.

Instead, use two views (see above), one for legal persons and one for natural persons. Resist the urge to join every possible table in the views.

UPDATE - When you perform an update is needed to test whether there is specialization. This is because a record could originally have been created as type = N (Natural) and later want to upgrade it to Corporate (L = Legal). An INSERT to create original NaturalPerson was made, and now with the exchange of nature type is necessary to exclude logically or physically the NaturalPerson record and then create the new record of LegalEntity.

In the real world, you don't "upgrade" a natural person to be a corporation. (A person who "is" a corporation is legally two different things. That's why there are corporations.) In a database, if you run into such a situation, you have to take one of three paths.

  • All the original data is wrong, because you entered data for a person who is not really a person. Delete the bad data, and enter new data for the corporation.
  • All the original data is right; that person is actually a natural person. Reject all the updates that would make that natural person a corporation.
  • All the original data is right, and that person is both a natural person and a corporation. Redesign your schema to accommodate this possibility.