SlideShare a Scribd company logo
Databases and the MVC Model
http://flic.kr/p/ar4nLn
How do we enable our Rails apps
to CRUD persistent data?
Create, Read, Update, Delete
The Big Question
Server
Router Controller
View
Ye Olde
Internet
Browser
Model
DB
Migrations
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
Migrations
Today’s focus
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
Migrations
Rails uses
a DBMS
Database (DB): Organized collection of data
Database Management System (DBMS):
Controls the creation, maintenance, and
use of a DB
Examples:
PostgreSQL
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
Migrations
Model classes use
the database
Migrations set up
the database
Model
DB
Migrations
• DBs and DBMSs
• Rails Object–Relational Mapping
Outline
Model
DB
Migrations
• DBs and DBMSs
• Rails Object–Relational Mapping
Outline
Two key aspects of a DBMS
• Database model: How DB is structured and used
– Examples: Relational, Object-Oriented, Hierarchical
• Query language: Types of questions you can ask
– Examples: SQL, XQuery
Relational model + SQL is most common
and used by Rails
Relational Model Concepts
http://en.wikipedia.org/wiki/File:Relational_model_concepts.png
Example Tables
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
id publisher_name
1 Bobbs Merrill
2 Random House
authors
publishers
How Apps Use DBs
• Pre-deployment of app:
– Create (empty) tables
• Rails migrations do this
– Maybe “seed” with data
• At runtime of app:
– CRUD table rows
• Rails model classes do this
• NOTE: Tables/columns don’t change
CRUD-to-SQL Mapping
CRUD Operation SQL Statement
Create INSERT
Read (Retrieve) SELECT
Update (Modify) UPDATE
Delete (Destroy) DELETE
For complete MySQL documentation, see: http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-data-manipulation.html
Example SELECT Queries
• SELECT * FROM authors
• SELECT * FROM authors WHERE year_born > 1910
• SELECT * FROM authors WHERE last_name REGEXP '[a-r]*'
• SELECT * FROM authors WHERE last_name REGEXP '[a-r]*'
ORDER BY last_name ASC
For complete MySQL documentation, see http://dev.mysql.com/doc/refman/5.5/en/select.html,
http://dev.mysql.com/doc/refman/5.5/en/pattern-matching.html
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
Model
DB
Migrations
• DBs and DBMSs
• Rails Object–Relational Mapping
Outline
Rails Object-Relational Mapping (ORM)
• You make object-oriented classes
• Rails handles the SQL/DBMS
Model
DB
Quick Intro to UML Class Diagrams
• I use UML class diagrams a lot
• Rationale: Easier to see “big picture” relationships
than in code
• Example model class:
first_name : string
last_name : string
year_born : integer
Author
Box = class
Class name
Attributes w/ types
Classes Represent Sets of Possible Objects
• Class Diagram: • Object Diagram
first_name : string
last_name : string
year_born : integer
Author
first_name = “Ayn”
last_name = “Rand”
year_born = 1905
rand : Author
first_name = “Peter”
last_name = “Benchley”
year_born = 1940
: Author
Class
Attribute values
Object name
Example Mapping from Class to Table
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year_born : integer
Author
You get ID
for free
What do objects map to?
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name = “Ayn”
last_name = “Rand”
year_born = 1905
: Author
first_name = “Peter”
last_name = “Benchley”
year_born = 1940
: Author
What do objects map to?
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name = “Ayn”
last_name = “Rand”
year_born = 1905
: Author
first_name = “Peter”
last_name = “Benchley”
year_born = 1940
: Author
Rows!
id first_name last_name year_born
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year_born : integer
Author
Model
DB
Migrations
See the Demos-n-Deets for
How This ORM Is Applied in
Rails Model Programming
Summary
• Rails Model Components
• DBs and DBMSs
• Rails DB Migrations
• Rails ORM
http://flic.kr/p/aCLor3

More Related Content

Similar to model.pptx

Dbms Lec Uog 02
Dbms Lec Uog 02Dbms Lec Uog 02
Dbms Lec Uog 02smelltulip
 
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
EdwinJacob5
 
Semantic web
Semantic webSemantic web
Semantic web
Ronit Mathur
 
Longwell final ppt
Longwell final pptLongwell final ppt
Longwell final ppt
Kuldeep Singh
 
Intro to the semantic web (for libraries)
Intro to the semantic web (for libraries) Intro to the semantic web (for libraries)
Intro to the semantic web (for libraries)
robin fay
 
Multi-Model Data Query Languages and Processing Paradigms
Multi-Model Data Query Languages and Processing ParadigmsMulti-Model Data Query Languages and Processing Paradigms
Multi-Model Data Query Languages and Processing Paradigms
Jiaheng Lu
 
DBMS & Data Models - In Introduction
DBMS & Data Models - In IntroductionDBMS & Data Models - In Introduction
DBMS & Data Models - In Introduction
Rajeev Srivastava
 
NO SQL Databases, Big Data and the cloud
NO SQL Databases, Big Data and the cloudNO SQL Databases, Big Data and the cloud
NO SQL Databases, Big Data and the cloud
Manu Cohen-Yashar
 
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
IDERA Software
 
Dublin Core Metadata Tutorial.ppt
Dublin Core Metadata Tutorial.pptDublin Core Metadata Tutorial.ppt
Dublin Core Metadata Tutorial.ppt
Bharath Abbareddy
 
Structural Metadata in RDF (IS575)
Structural Metadata in RDF (IS575)Structural Metadata in RDF (IS575)
Structural Metadata in RDF (IS575)
Robert Sanderson
 
9 relational database concepts
9 relational database concepts9 relational database concepts
9 relational database conceptsKumar
 
oracle
oracle oracle
Semantic framework for web scraping.
Semantic framework for web scraping.Semantic framework for web scraping.
Semantic framework for web scraping.
Shyjal Raazi
 
Graph database in sv meetup
Graph database in sv meetupGraph database in sv meetup
Graph database in sv meetup
Joshua Bae
 
RDBMS
RDBMSRDBMS
Entity framework
Entity frameworkEntity framework
Entity framework
Mehdi jannati
 
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
Drew Madelung
 
Echoes Project
Echoes ProjectEchoes Project

Similar to model.pptx (20)

Dbms Lec Uog 02
Dbms Lec Uog 02Dbms Lec Uog 02
Dbms Lec Uog 02
 
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
RELATIONAL MODEL OF DATABASES AND OTHER CONCEPTS OF DATABASES​
 
Semantic web
Semantic webSemantic web
Semantic web
 
Longwell final ppt
Longwell final pptLongwell final ppt
Longwell final ppt
 
Intro to the semantic web (for libraries)
Intro to the semantic web (for libraries) Intro to the semantic web (for libraries)
Intro to the semantic web (for libraries)
 
Multi-Model Data Query Languages and Processing Paradigms
Multi-Model Data Query Languages and Processing ParadigmsMulti-Model Data Query Languages and Processing Paradigms
Multi-Model Data Query Languages and Processing Paradigms
 
DBMS & Data Models - In Introduction
DBMS & Data Models - In IntroductionDBMS & Data Models - In Introduction
DBMS & Data Models - In Introduction
 
NO SQL Databases, Big Data and the cloud
NO SQL Databases, Big Data and the cloudNO SQL Databases, Big Data and the cloud
NO SQL Databases, Big Data and the cloud
 
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
Geek Sync | Data in the Cloud: Understanding Amazon Database Services with Vi...
 
Dublin Core Metadata Tutorial.ppt
Dublin Core Metadata Tutorial.pptDublin Core Metadata Tutorial.ppt
Dublin Core Metadata Tutorial.ppt
 
Structural Metadata in RDF (IS575)
Structural Metadata in RDF (IS575)Structural Metadata in RDF (IS575)
Structural Metadata in RDF (IS575)
 
9 relational database concepts
9 relational database concepts9 relational database concepts
9 relational database concepts
 
oracle
oracle oracle
oracle
 
Semantic framework for web scraping.
Semantic framework for web scraping.Semantic framework for web scraping.
Semantic framework for web scraping.
 
Graph database in sv meetup
Graph database in sv meetupGraph database in sv meetup
Graph database in sv meetup
 
Linked data and voyager
Linked data and voyagerLinked data and voyager
Linked data and voyager
 
RDBMS
RDBMSRDBMS
RDBMS
 
Entity framework
Entity frameworkEntity framework
Entity framework
 
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
Essentials for the SharePoint Power User - SharePoint Engage Raleigh 2017
 
Echoes Project
Echoes ProjectEchoes Project
Echoes Project
 

model.pptx

  • 1. Databases and the MVC Model http://flic.kr/p/ar4nLn
  • 2. How do we enable our Rails apps to CRUD persistent data? Create, Read, Update, Delete The Big Question
  • 6. Database (DB): Organized collection of data Database Management System (DBMS): Controls the creation, maintenance, and use of a DB Examples: PostgreSQL
  • 7. Ye Olde Internet Server Router Controller View Browser Model DB Migrations Model classes use the database Migrations set up the database
  • 8. Model DB Migrations • DBs and DBMSs • Rails Object–Relational Mapping Outline
  • 9. Model DB Migrations • DBs and DBMSs • Rails Object–Relational Mapping Outline
  • 10. Two key aspects of a DBMS • Database model: How DB is structured and used – Examples: Relational, Object-Oriented, Hierarchical • Query language: Types of questions you can ask – Examples: SQL, XQuery Relational model + SQL is most common and used by Rails
  • 12. Example Tables id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 id publisher_name 1 Bobbs Merrill 2 Random House authors publishers
  • 13. How Apps Use DBs • Pre-deployment of app: – Create (empty) tables • Rails migrations do this – Maybe “seed” with data • At runtime of app: – CRUD table rows • Rails model classes do this • NOTE: Tables/columns don’t change
  • 14. CRUD-to-SQL Mapping CRUD Operation SQL Statement Create INSERT Read (Retrieve) SELECT Update (Modify) UPDATE Delete (Destroy) DELETE For complete MySQL documentation, see: http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-data-manipulation.html
  • 15. Example SELECT Queries • SELECT * FROM authors • SELECT * FROM authors WHERE year_born > 1910 • SELECT * FROM authors WHERE last_name REGEXP '[a-r]*' • SELECT * FROM authors WHERE last_name REGEXP '[a-r]*' ORDER BY last_name ASC For complete MySQL documentation, see http://dev.mysql.com/doc/refman/5.5/en/select.html, http://dev.mysql.com/doc/refman/5.5/en/pattern-matching.html id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 authors
  • 16. Model DB Migrations • DBs and DBMSs • Rails Object–Relational Mapping Outline
  • 17. Rails Object-Relational Mapping (ORM) • You make object-oriented classes • Rails handles the SQL/DBMS Model DB
  • 18. Quick Intro to UML Class Diagrams • I use UML class diagrams a lot • Rationale: Easier to see “big picture” relationships than in code • Example model class: first_name : string last_name : string year_born : integer Author Box = class Class name Attributes w/ types
  • 19. Classes Represent Sets of Possible Objects • Class Diagram: • Object Diagram first_name : string last_name : string year_born : integer Author first_name = “Ayn” last_name = “Rand” year_born = 1905 rand : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author Class Attribute values Object name
  • 20. Example Mapping from Class to Table id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 authors first_name : string last_name : string year_born : integer Author You get ID for free
  • 21. What do objects map to? id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 authors first_name = “Ayn” last_name = “Rand” year_born = 1905 : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author
  • 22. What do objects map to? id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 authors first_name = “Ayn” last_name = “Rand” year_born = 1905 : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author Rows!
  • 23. id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 authors first_name : string last_name : string year_born : integer Author Model DB Migrations See the Demos-n-Deets for How This ORM Is Applied in Rails Model Programming
  • 24. Summary • Rails Model Components • DBs and DBMSs • Rails DB Migrations • Rails ORM http://flic.kr/p/aCLor3