Inheritance & Polymorphism In Relational Databases solving some common problems in web development
The Problem <ul><li>Books & Televisions </li></ul><ul><li>Both </li></ul><ul><ul><li>SKU </li></ul></ul><ul><ul><li>Title ...
Own Tables <ul><li>Duplication of shared fields </li></ul><ul><ul><li>Gets worse with more products </li></ul></ul><ul><li...
Single table inheritance (STI) <ul><li>No duplication </li></ul><ul><li>Wasted fields </li></ul><ul><ul><li>Gets worse wit...
Class Table Inheritance (CTI)  <ul><li>No duplication </li></ul><ul><li>No wasted fields </li></ul><ul><li>Index shared fi...
Another Problem <ul><li>Tags for Books and Televisions </li></ul><ul><li>Reference Products from Tags </li></ul><ul><li>Wh...
Multiple Tables <ul><li>Duplication of Tag structure </li></ul><ul><li>Need new table for each thing </li></ul><ul><li>Can...
Multiple Relationships <ul><li>Single Tag structure </li></ul><ul><li>Can index all Tags </li></ul><ul><li>Need similar ta...
Class Table Inheritance <ul><li>Generalize Products and Images </li></ul><ul><li>Avoids duplication </li></ul><ul><li>Alte...
Polymorphism <ul><li>Simple </li></ul><ul><ul><li>Just one Tags table to Tag anything </li></ul></ul><ul><li>No changes to...
Upcoming SlideShare
Loading in …5
×

Inheritance & Polymorphism In Relational Databases

4,583
-1

Published on

1 Comment
3 Likes
Statistics
Notes
  • Very nice explanation. If you are interested: review of each implementation (another problem is described) you can find here: http://www.vertabelo.com/blog/inheritance-in-a-relational-database
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,583
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
57
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Inheritance & Polymorphism In Relational Databases

  1. 1. Inheritance & Polymorphism In Relational Databases solving some common problems in web development
  2. 2. The Problem <ul><li>Books & Televisions </li></ul><ul><li>Both </li></ul><ul><ul><li>SKU </li></ul></ul><ul><ul><li>Title </li></ul></ul><ul><ul><li>Description </li></ul></ul><ul><ul><li>Manufacturer </li></ul></ul><ul><li>Books </li></ul><ul><ul><li>ISBN </li></ul></ul><ul><ul><li>Author </li></ul></ul><ul><li>Televisions: </li></ul><ul><ul><li>Screen Size </li></ul></ul><ul><ul><li>Resolution </li></ul></ul>
  3. 3. Own Tables <ul><li>Duplication of shared fields </li></ul><ul><ul><li>Gets worse with more products </li></ul></ul><ul><li>Lose indexing of shared fields </li></ul><ul><ul><li>i.e. SKU unique across products </li></ul></ul>SELECT * FROM books WHERE id = 7 SELECT id, title, ‘book’ AS type FROM books UNION SELECT id, title, ‘television’ AS type FROM televisions
  4. 4. Single table inheritance (STI) <ul><li>No duplication </li></ul><ul><li>Wasted fields </li></ul><ul><ul><li>Gets worse with more products </li></ul></ul><ul><li>Rails built in inheritance </li></ul>SELECT * FROM products WHERE id = 7 SELECT id, title, type FROM products
  5. 5. Class Table Inheritance (CTI) <ul><li>No duplication </li></ul><ul><li>No wasted fields </li></ul><ul><li>Index shared fields </li></ul><ul><li>Referential integrity </li></ul><ul><li>Difficult to enforce books or televisions </li></ul><ul><li>Creating and reading becomes more complex </li></ul>SELECT books.*, products.* FROM books JOIN products ON(products.id = books.product_id) WHERE books.id = 7 SELECT products.id, products.title, books.id, televisions.id FROM products LEFT JOIN books ON(books.product_id = products.id) LEFT JOIN televisions ON(televisions.product_id = products.id)
  6. 6. Another Problem <ul><li>Tags for Books and Televisions </li></ul><ul><li>Reference Products from Tags </li></ul><ul><li>What about tagging other things besides Products as well?... </li></ul>
  7. 7. Multiple Tables <ul><li>Duplication of Tag structure </li></ul><ul><li>Need new table for each thing </li></ul><ul><li>Can’t index across all tags </li></ul>
  8. 8. Multiple Relationships <ul><li>Single Tag structure </li></ul><ul><li>Can index all Tags </li></ul><ul><li>Need similar tables for anything we want to tag </li></ul><ul><li>Need some app logic </li></ul><ul><ul><li>Deleting Product deletes relationship, not Tags themselves </li></ul></ul>
  9. 9. Class Table Inheritance <ul><li>Generalize Products and Images </li></ul><ul><li>Avoids duplication </li></ul><ul><li>Alter existing structures </li></ul><ul><li>Increasing complexity </li></ul><ul><ul><li>Books span 3 tables </li></ul></ul><ul><li>Tightly coupled </li></ul><ul><ul><li>Adding Comments: “TaggableCommentables”? </li></ul></ul>
  10. 10. Polymorphism <ul><li>Simple </li></ul><ul><ul><li>Just one Tags table to Tag anything </li></ul></ul><ul><li>No changes to existing entities </li></ul><ul><li>Can handle tagging of any future entities </li></ul><ul><li>Loses referential integrity </li></ul><ul><ul><li>Need to enforce in application logic </li></ul></ul><ul><li>Comments could be added similarly </li></ul><ul><li>Rails has it built in </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×