Databases and the MVC Model

Slides:



Advertisements
Similar presentations
Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Advertisements

CSC 2720 Building Web Applications Database and SQL.
Financial Information Management How do I talk to a DBMS? SQL In one hour.
Information storage: Introduction of database 10/7/2004 Xiangming Mu.
1 Intro to Info Tech Database Management Systems Copyright 2003 by Janson Industries This presentation can be viewed on line at:
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
THE WEBMASTERS: SENG + WAVERING.  On account of construction, we will be having class in room 1248 next week.
CS453: Databases and State in Web Applications (Part 2) Prof. Tom Horton.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Introduction to Database Programming with Python Gary Stewart
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
MVC Controllers TestsMigrations Ye Olde Internet Model DB Server Router View Browser Today’s focus Controller.
Databases Stefano Grazioli.
CompSci 280 S Introduction to Software Development
Web Systems & Technologies
Chapter 1 Introduction.
Roles in the Database Environment
CS320 Web and Internet Programming SQL and MySQL
Database Programming in Java
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
Information Systems Today: Managing in the Digital World
CS1222 Using Relational Databases and SQL
ADO.NET Entity Framework
Tools for Memory: Database Management Systems
CIS 336 Competitive Success/snaptutorial.com
CIS 336 Education for Service-- snaptutorial.com.
CIS 336 Teaching Effectively-- snaptutorial.com
ISC440: Web Programming 2 Server-side Scripting PHP 3
“Introduction To Database and SQL”
Structured Query Language (SQL) William Klingelsmith
System And Application Software
Database.
CS1222 Using Relational Databases and SQL
Chapter 22 - SQL, MySQL, DBI and ADO
PHP and MySQL.
Structured Query Language
Data Model.
Database Design Hacettepe University
Tutorial 6 PHP & MySQL Li Xu
CS1222 Using Relational Databases and SQL
Painted Shadows - Not Allowed
CS1222 Using Relational Databases and SQL
CS3220 Web and Internet Programming SQL and MySQL
Databases and the MVC Model
Databases and the MVC Model
Database Management Systems
MVC Controllers.
MVC Controllers.
Databases and the MVC Model
Chapter 3 Database Management
Painted Shadows - Not Allowed
CS3220 Web and Internet Programming SQL and MySQL
MVC Controllers.
CS1222 Using Relational Databases and SQL
Databases and the MVC Model
DATABASE Purpose of database
Database SQL.
JDBC II IS
Lecuter-1.
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
CS1222 Using Relational Databases and SQL
Presentation transcript:

Databases and the MVC Model http://flic.kr/p/ar4nLn Databases and the MVC Model

Ye Olde Internet Browser Migrations DB Server View Router Controller Model

Today’s focus Ye Olde Internet Browser Migrations DB Server View Router Controller Model

Rails uses a DBMS Ye Olde Internet Browser Migrations DB Server View Router Controller Model

Database (DB): Organized collection of data Database Management System (DBMS): Controls the creation, maintenance, and use of a DB E.g.: MySQL, Postgres http://flic.kr/p/ar4nLn

Migrations set up the database Browser Migrations set up the database Migrations Ye Olde Internet DB Model classes use the database Server View Router Controller Model

Outline How DBs/DBMSs work Rails Object–Relational Mapping Migrations How DBs/DBMSs work Rails Object–Relational Mapping How to create model classes How migrations work How to use model classes DB Model

Outline How DBs/DBMSs work Rails Object–Relational Mapping Migrations How DBs/DBMSs work Rails Object–Relational Mapping How to create model classes How migrations work How to use model classes DB Model

Why use a DBMS? Data independence: Applications need not be concerned with how data is stored or accessed Provides a lot of functionality that would be silly to implement yourself: Sharing (network) Customizable security Integrity

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 authors publishers id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 publishers id publisher_name 1 Bobbs Merrill 2 Random House

How DBs are used by apps Pre-deployment of app: At runtime 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 authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940

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 authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 SELECT * FROM authors SELECT id, last_name 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

Outline How DBs/DBMSs work Rails Object–Relational Mapping Migrations How DBs/DBMSs work Rails Object–Relational Mapping How to create model classes How migrations work How to use model classes DB Model

Rails Object-Relational Mapping (ORM) You make object-oriented classes Rails handles the SQL/DBMS DB Model

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: Box = class Class name Attributes w/ types first_name : string last_name : string year_born : integer Author

Classes Represent Sets of Possible Objects Class Diagram: Object Diagram Class Attribute values Object name first_name = “Ayn” last_name = “Rand” year_born = 1905 rand : Author first_name : string last_name : string year_born : integer Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author

Example Mapping from Class to Table first_name : string last_name : string year_born : integer Author You get ID for free authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940

What do objects map to? authors first_name = “Ayn” last_name = “Rand” year_born = 1905 : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940

What do objects map to? Rows! authors first_name = “Ayn” last_name = “Rand” year_born = 1905 : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940

Outline How DBs/DBMSs work Rails Object–Relational Mapping Migrations How DBs/DBMSs work Rails Object–Relational Mapping How to create model classes How migrations work How to use model classes DB Model

How to make a model class Run Rails command: $ rails g model Author first_name:string last_name:string year_born:integer What it generates: Model class: used by controller classes app/models/author.rb class Author DB migration class: used to setup tables in DB db/migrate/20141030033652_create_authors.rb Test class: used to test your model class test/models/author_test.rb Demo time!

Appendix

Class Diagram (table + columns) Relational DB Table authors id first_name last_name year_born 1 Ayn Rand 1905 2 Peter Benchley 1940 Class Diagram (table + columns) first_name : string last_name : string year_born : integer Author Object Diagram (rows) first_name = “Ayn” last_name = “Rand” year_born = 1905 : Author first_name = “Peter” last_name = “Benchley” year_born = 1940 : Author

Ye Olde Internet Browser Migrations Tests DB Server View Router Controller Model

Ye Olde Internet Browser Migrations Tests DB Server View Router Controller Model