Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

At first I have posted this question at http://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.

share|improve this question
add comment

1 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.
share|improve this answer
    
Catcall I really apreciate your feedback thank you! But about your consideration I have a painfull history with Views in MySQL and I discarded that possibility. And this question began when I found problens with MVC applications in CRUD implementation. Basically I copyed some suff from Zend Framework and Yii and start over a new Framework with dbTable concept from Zend with HMVC design, basically presented in Yii by CController's forward. It's a nice handicap for code reuse since all about 80% of screens from my project are CRUD based, but this is a discussion topic for too long. –  Allyson de Paula Apr 7 '13 at 14:01
    
MySQL is the least standards-compliant, client/server dbms out there. When you're trying to do something that works well and easily on PostgreSQL, Oracle, and MySQL, you'll constantly get poked by MySQL's behavior. –  Mike Sherrill 'Cat Recall' Apr 8 '13 at 13:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.