Painted Shadows - Not Allowed

Slides:



Advertisements
Similar presentations
 To provide you with an overview of the aspects that make up a relational database.  This includes: › Tables › Records › Fields › Data types › Keys.
Advertisements

CSE 190: Internet E-Commerce Lecture 10: Data Tier.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21.
Database Design. The process of developing database structures from user requirements for data a structured methodology Structured Methodology - a number.
Introduction to Access Chapter 13 pages 1-4. What is a database??? Related information is stored in databases  All SC student information is stored in.
Planning & Creating a Database By Ms. Naira Microsoft Access.
NORMALIZATION. What is Normalization  The process of effectively organizing data in a database  Two goals  To eliminate redundant data  Ensure data.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
COMP 430 Intro. to Database Systems MongoDB. What is MongoDB? “Humongous” DB NoSQL, no schemas DB Lots of similarities with SQL RDBMs, but with more flexibility.
Standards and Conventions
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core * aka ASP.NET 5 before.
MVC Controllers TestsMigrations Ye Olde Internet Model DB Server Router View Browser Today’s focus Controller.
Databases and the MVC Model
Chengyu Sun California State University, Los Angeles
Y.-H. Chen International College Ming-Chuan University Fall, 2004
Database Design.
Chapter 1 Introduction.
SQL Relational Database Project
Security: Exploits & Countermeasures
Security: Exploits & Countermeasures
Database Programming in Java
MIS2502: Data Analytics SQL – Putting Information Into a Database
Basic Database Design COSC 2328 – Web Programming.
Chapter 2: Relational Model
 2012 Pearson Education, Inc. All rights reserved.
Cameron Blashka| Informer Implementation Specialist
Data Definition and Data Types
LINQ to DATABASE-2.
Introduction to Database Systems
Design a Relational Database Identify Database Purpose
MIS2502: Data Analytics SQL – Putting Information Into a Database
Databases and Information Management
ISC440: Web Programming 2 Server-side Scripting PHP 3
Structured Query Language (SQL) William Klingelsmith
Agile Web Development with Ruby and Rails
Exploring Microsoft Access 2003
Chapter 22 - SQL, MySQL, DBI and ADO
MIS2502: Data Analytics Converting ERDs to Schemas
What is a Database? A collection of data organized in a manner that allows access, retrieval, and use of that data.
LINQ to DATABASE-2.
02 - The Relational Database Model
MIS2502: Data Analytics SQL – Putting Information Into a Database
Databases and Information Management
MIS2502: Data Analytics SQL – Putting Information Into a Database
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Introduction To Databases GCSE Computer Science
Advanced Database Concepts: Reports & Views
Introduction To Databases GCSE Computer Science
Databases and the MVC Model
Databases and the MVC Model
Security: Exploits & Countermeasures
PHP and MySQL.
MVC Controllers.
MVC Controllers.
Security: Exploits & Countermeasures
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
Databases and the MVC Model
Security: Exploits & Countermeasures
Painted Shadows - Not Allowed
CS3220 Web and Internet Programming SQL and MySQL
MVC Controllers.
Databases and the MVC Model
MIS2502: Data Analytics Relational Data Modeling 3
Security: Attacks & Countermeasures
Presentation transcript:

https://flic.kr/p/dxGXD Painted Shadows - Not Allowed Taken from Alexander Calder's mobile room at the National Gallery of Art, Washington DC. MVC Model Associations

MVC Model Review What key capability do Rails model classes provide? Browser MVC Model Review What key capability do Rails model classes provide? Ye Olde Internet DB Server View Router Controller Model

MVC Model Review What key capability do Rails model classes provide? Browser MVC Model Review What key capability do Rails model classes provide? Ye Olde Internet CRUD persistent data DB Server View Router Controller Model

MVC Model Review How do you create Rails model classes? Browser MVC Model Review How do you create Rails model classes? Ye Olde Internet DB Server View Router Controller Model

MVC Model Review How do you create Rails model classes? Browser MVC Model Review How do you create Rails model classes? Ye Olde Internet Generate DB migrations and model classes with “rails g model …” then customize DB Server View Router Controller Model

MVC Model Review What purpose do migrations serve? Ye Olde Internet Browser MVC Model Review What purpose do migrations serve? Ye Olde Internet DB Server View Router Controller Model

MVC Model Review What purpose do migrations serve? Ye Olde Internet Browser MVC Model Review What purpose do migrations serve? Ye Olde Internet Setup tables in DB via, e.g., “rake db:migrate:reset” command DB Server View Router Controller Model

MVC Model Review What DB table would go with this model class? Database Table: first_name : string last_name : string year : integer Author ???

MVC Model Review What DB table would go with this model class? Database Table: authors first_name : string last_name : string year : integer Author id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940

Recall Rails ORM (Object-Relational Mapping) MVC Model Review What DB table would go with this model class? Model Class: Database Table: authors first_name : string last_name : string year : integer Author Recall Rails ORM (Object-Relational Mapping) id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940

What Author methods does Rails provide “for free”? MVC Model Review What Author methods does Rails provide “for free”? Model Class: first_name : string last_name : string year : integer Author

What Author methods does Rails provide “for free”? MVC Model Review What Author methods does Rails provide “for free”? Model Class: Many methods: Author.create/Author.create! Author.find Author.find_by Author.all Getters/Setters for attributes Author#first_name Author#first_name= Author#save/Author#save! Author#destroy Author#valid? first_name : string last_name : string year : integer Author

What customizations might you add to Author? MVC Model Review What customizations might you add to Author? Model Class: first_name : string last_name : string year : integer Author

What customizations might you add to Author? MVC Model Review What customizations might you add to Author? Model Class: Only customization covered so far is data validation first_name : string last_name : string year : integer Author

Limitation so far: Insular model classes/tables Database Table: authors first_name : string last_name : string year : integer Author id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940

Imagine an author-profile class Model Class: Database Table: authors first_name : string last_name : string year : integer Author id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles birthplace : string bio : text awards : text AuthorProfile id birthplace bio awards 23 Saint Petersburg Rand was born… Prometheus Award, … 42 New York City He was the son of… Shark Conservation, …

What if you want inter-class relationships? first_name : string last_name : string year : integer Author ??? birthplace : string bio : text awards : text AuthorProfile For example, you might want to say:

Requires inter-table references authors id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id birthplace bio awards 23 Saint Petersburg Rand was born… Prometheus Award, … 42 New York City He was the son of… Shark Conservation, …

Rails Relationship Support has one / belongs to one has many / belongs to one has and belongs to many join table Easiest to learn, but narrow in focus Most complex, but also most flexible

Rails Relationship Support has one / belongs to one has many / belongs to one has and belongs to many join table

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Class Association

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Association Name Triangle indicates reading direction (i.e., Author has AuthorProfile)

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Role Names Like attribute/method name Enables this method call: @author.profile.birthplace

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Multiplicities

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Multiplicities Each Author has 1 AuthorProfile

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Multiplicities Each Author has 1 AuthorProfile Each AuthorProfile is had by 1 Author

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Multiplicities Each Author has 1 AuthorProfile Each AuthorProfile is had by 1 Author More Multiplicities: A B * has Each A has 0 or more Bs 0..* 1..* Each A has 1 or more Bs

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Associated DB Tables authors id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Associated DB Tables authors references id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Primary versus Foreign Keys Primary Key: Uniquely identifies each record in table authors id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Primary versus Foreign Keys Primary Key: Uniquely identifies each record in table Foreign Key: Field in table A such that the field is a primary key in one other table B authors id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Primary versus Foreign Keys Primary Key: Uniquely identifies each record in table Foreign Key: Field in table A such that the field is a primary key in one other table B authors id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Goal: Associate Each Author with an AuthorProfile Generate AuthorProfile class (no references) Generate migration to add references Customize model class by adding association Add seed data for AuthorProfile In “show” view, show profile data

Rails Relationship Support has one / belongs to one has many / belongs to one has and belongs to many join table

Has Many / Belongs to One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile title : string year : integer summary : text Book How to model authorship? (assume 1 author per book)

Has Many / Belongs to One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile has 1 author * book title : string year : integer summary : text Book How to model authorship? (assume 1 author per book)

Has Many / Belongs to One Example What should the ORM be? (hint: authors table unchanged) first_name : string last_name : string year : integer Author authors has 1 author * book id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 title : string year : integer summary : text Book

Has Many / Belongs to One Example What should the ORM be? (hint: authors table unchanged) first_name : string last_name : string year : integer Author authors has 1 author * book id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 title : string year : integer summary : text Book books id title year summary author_id 72 The Fountainhead 1943 Individualistic architect… 1 98 Atlas Shrugged 1957 Dystopian USA… 99 Jaws 1974 Shark!... 2

Has Many / Belongs to One Example What should the ORM be? (hint: authors table unchanged) first_name : string last_name : string year : integer Author authors has 1 author * book id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 title : string year : integer summary : text Book books id title year summary author_id 72 The Fountainhead 1943 Individualistic architect… 1 98 Atlas Shrugged 1957 Dystopian USA… 99 Jaws 1974 Shark!... 2

Has Many / Belongs to One Example Rails generates “books” method that returns list of books first_name : string last_name : string year : integer Author authors has 1 author * book id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 title : string year : integer summary : text Book books id title year summary author_id 72 The Fountainhead 1943 Individualistic architect… 1 98 Atlas Shrugged 1957 Dystopian USA… 99 Jaws 1974 Shark!... 2

Goal: Associate Each Author with Books Authored Generate Books class (no references) Generate migration to add references Customize model classes by adding association Add seed data for Books In “show” view, show books data

Rails Relationship Support has one / belongs to one has many / belongs to one has and belongs to many join table

Join Table Example From http://guides.rubyonrails.org/v4.2.0/association_basics.html

Join Table Example Join table From http://guides.rubyonrails.org/v4.2.0/association_basics.html

References (aka foreign keys) Join Table Example References (aka foreign keys) From http://guides.rubyonrails.org/v4.2.0/association_basics.html

Join Table Example: Model Class “through” enables this sort of thing: From http://guides.rubyonrails.org/v4.2.0/association_basics.html

Rails Relationship Support has one / belongs to one has many / belongs to one has and belongs to many join table For more info, see… Rails Guide Chapter: http://guides.rubyonrails.org/v4.2.4/association_basics.html API Documentation: http://api.rubyonrails.org/v4.2.4/classes/ActiveRecord/Associations/ClassMethods.html

Appendix

Has One / Belongs To One Example first_name : string last_name : string year : integer Author birthplace : string bio : text awards : text AuthorProfile has 1 author profile Associated DB Tables authors references id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 author_profiles id author _id birthplace bio awards 23 1 Saint Petersburg Rand was born… Prometheus Award, … 42 2 New York City He was the son of… Shark Conservation, …

Has Many / Belongs to One Example first_name : string last_name : string year : integer Author authors has 1 author * book id first_name last_name year 1 Ayn Rand 1905 2 Peter Benchley 1940 references title : string year : integer summary : text Book books id title year summary author_id 72 The Fountainhead 1943 Individualistic architect… 1 98 Atlas Shrugged 1957 Dystopian USA… 99 Jaws 1974 Shark!... 2