CS 683 Emerging Technologies Fall Semester, 2005 Doc 24 Rails Database Nov 22, 2005 Copyright ©, All rights reserved. 2005 SDSU & Roger Whitney, 5500 Campanile.

Slides:



Advertisements
Similar presentations
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Advertisements

Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 25 Rails Transactions, Sessions, Filters Nov 29, 2005 Copyright ©, All rights reserved SDSU.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 21 Rails Intro Nov 10, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 23 Rails Model Example Nov 17, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
1 Introduction to Web Application Introduction to Data Base.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 22 Rails Basic Action Pack & Active Record Nov 15, 2005 Copyright ©, All rights reserved SDSU.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
INTERNET APPLICATION DEVELOPMENT For More visit:
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Final Exam Guide PHP NOTE: PHP CODE WILL BE BLUE, HTML IS BLACK EXAMPLE
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
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:
SQL, Data Storage Technologies, and Web-Data Integration Week 2.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
M1G Introduction to Database Development 2. Creating a Database.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
Relational Database. Database Management System (DBMS)
Web Application Development Technology เทคโนโลยีสำหรับการพัฒนาโปรแกรม ประยุกต์เว็บ Suntorn Witosurapot Phone: or
Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. Adding a new field 1Right click the table name and select design view 2Type the field information at the end.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 19 Ruby Regexp, Expression, Exceptions, Modules Nov 8, 2005 Copyright ©, All rights reserved
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
CS499 Project #3 XML mySQL Test Generation Members Erica Wade Kevin Hardison Sameer Patwa Yi Lu.
Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
1 Active Records. 2 What’s Active Records? O-R Mapping layer To make database access almost a non-issue Relies heavily on convention over configuration.
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
 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.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 26 Rails Miscellaneous Dec 1, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
Class11 Introduction to relational databases and MySQL MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Ruby on Rails & Databases. Active Record Active Record in Rails CRUD & Other Stuff Mapping Cardinalities Migrations Demo.
IST 220 – Intro to DB Lecture 4 Database Design thru ER Modeling.
Ch 3. Working with Tables and Views. Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes.
Working with MySQL A290/A590, Fall /07/2014.
Microsoft Access CS 110 Fall Entity Relationship Model Entities Entities Principal data object about which information is to be collectedPrincipal.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Advanced Migration By Aye Mon Tun.  To change database schema in consistent and easy way  In ruby code Migration? 11/25/2013 2Web Application Engineering.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
CS320 Web and Internet Programming SQL and MySQL
Data Definition and Data Types
Data Definition and Data Types
BookStore App as a console app
Ruby on Rails Model of MVC
Agile Web Development with Ruby and Rails
Model for Student Table
Intro to Relational Databases
CS3220 Web and Internet Programming SQL and MySQL
Model for Student Table
Chapter 4 Introduction to MySQL.
Data.
CS3220 Web and Internet Programming SQL and MySQL
SQL NOT NULL Constraint
JDBC II IS
Presentation transcript:

CS 683 Emerging Technologies Fall Semester, 2005 Doc 24 Rails Database Nov 22, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA USA. OpenContent ( license defines the copyright on this document.

2 References Agile Web Development with Rails, Thomas & Hanson, The Pragmatic Bookshelf, 2005 Active Record Documentation,

3 Databases MySQL Open source Later versions support Transactions PostgreSQL Open source Now runs on Windows CREATE TABLE faculty ( name CHAR(20) NOT NULL, faculty_id SERIAL PRIMARY KEY ); CREATE TABLE faculty ( name CHAR(20) NOT NULL, faculty_id INTEGER AUTO_INCREMENT PRIMARY KEY ); create database lectureExamples;

4 Converting Types SQL TypeRuby Type int, integerFixnum decimal, numericFloat interval, dateDate clob, blob, textString float, doubleFloat char, varchar, stringString datetime, timeTime boolean special handling needed

5 Normalization First Normal Form Keep attributes single valued Put collections in separate table idTitleAuthor1Author2 1Fooyoume idname 1you 2me idtitle 1Foo Books Authors book_idauthor_id Join Table

6 Normalization Second Normal Form All non-key attributes must be fully dependent on the entire primary key Don't repeat data idtitlepublisher 1Fooyou 2Baryou idname 1you idtitlepublisher_id 1Foo1 2Bar1 Books Publishers

7 Table Relationships invoices id order_id orders id name class Invoice < ActiveRecord::Base belongs_to:order end class Order < ActiveRecord::Base has_one:invoice end One-to-One line_items id order_id orders id name class LineItem < ActiveRecord::Base belongs_to:order end class Order < ActiveRecord::Base has_one:line_items end One-to-Many

8 Table Relationships categories id name categories_products category_id product_id products id name class Category < ActiveRecord::Base has_and_belongs_to:products end class Product < ActiveRecord::Base has_and_belongs_to:categories end Many-to-Many

9 One-to-Many idtitleauthordatepublisher_id 1 Agile Web Development with Rails Thomas, Dave Programming RubyThomas, Dave Web Component Development with Zope 3 Weitershausen idname 1The Pragmatic Bookshelf 2Springer * 1 CREATE TABLE `books` ( `id` int(11) NOT NULL auto_increment, `title` varchar(100) NOT NULL, `author` varchar(50) NOT NULL, `date` date default ' ', `publisher_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) CREATE TABLE `publishers` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) constraint fk_publisherforeign key (publisher_id) references publishers(id) books publishers

10 One-to-Many idtitleauthordatepublisher_id 1 Agile Web Development with Rails Thomas, Dave Programming RubyThomas, Dave Web Component Development with Zope 3 Weitershausen idname 1The Pragmatic Bookshelf 2Springer * 1 books publishers require "rubygems" require_gem "activerecord" ActiveRecord::Base.establish_connection( :adapter => "mysql", :host => "localhost", :database => "cs683BookStore_development", :username => "whitney") class Book < ActiveRecord::Base belongs_to :publisher end class Publisher < ActiveRecord::Base has_many :books end springer = Publisher.create(:name => 'Springer') ruby = Book.new ruby.title = 'Web Component Development with Zope 3' ruby.author = 'Weitershausen' ruby.date = Time.now ruby.publisher = springer ruby.save from_database = Book.find_by_author('Weitershausen') puts from_database.publisher.name

11 Added Methods class Book < ActiveRecord::Base belongs_to :publisher end Methods Added to Book publisher(force_reload=false) publisher=(object) build_product(attributes={}) create_product(attribute={}) class Publisher < ActiveRecord::Base has_many :books end Methods Added to Publisher books(force_reload=false) books<<aBook books.push(book1,...) books.delete(book1, book2,...) books.clear books.find(options...) books.build(attributes={}) books.create(attributes={}) books.size books.emply?

12 Automatic Saving - One Way springer = Publisher.find_by_name('Springer') networking = Book.new networking.title = 'Fundametantal Networking in Java' networking.date = Time.now springer.books<<networking Book networking is now saved in the database addison = Publisher.new addison.name = 'Addison-Wesley' ruby = Book.find_by_title('Programming Ruby') ruby.publisher = addison Publisher addison is not saved ruby not updated addison = Publisher.new addison.name = 'Addison-Wesley' ruby = Book.find_by_title('Programming Ruby') ruby.publisher = addison ruby.save Now addison is saved in the database ruby is updated

13 Changing the Defaults class Publisher < ActiveRecord::Base has_many :books, :class => 'PaperBook', :forgeign_key => 'bk_id', :dependent => :destroy end

14 Many-to-Many idtitledatepublisher_id 1 Agile Web Development with Rails Programming Ruby Web Component Development with Zope idname 1The Pragmatic Bookshelf 2Springer * 1 books publishers idfirst_namelast_name 1DaveThomas 2DavidHansson 3PhilippWeiterhausen authors book_idauthor_id authors_books CREATE TABLE `authors_books` ( `author_id` int(11) NOT NULL, `book_id` int(11) NOT NULL, PRIMARY KEY (`author_id`,`book_id`) ) TYPE=MyISAM CREATE TABLE `authors` ( `id` int(11) NOT NULL auto_increment, `first_name` varchar(30) NOT NULL, `last_name` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM

15 Many-to-Many books id title date publisher_id authors_books book_id author_id publishers id name authors id first_name last_name require "rubygems" require_gem "activerecord" ActiveRecord::Base.establish_connection( etc) class Book < ActiveRecord::Base belongs_to :publisher has_and_belongs_to_many :authors def to_s "#{title} by #{authors.collect {|a| a.to_s + ' '}}" end class Publisher < ActiveRecord::Base has_many :book end class Author < ActiveRecord::Base has_and_belongs_to_many :books def to_s "#{first_name} #{last_name}" end thomas = Author.find_by_last_name('Thomas') thomas.books.each {|book| puts book} rails = Book.find(1)#two authors rails.authors.each {|author| puts author} ruby = Book.find(2)#one author ruby.authors.each {|author| puts author}

16 Added Methods class Author < ActiveRecord::Base has_and_belongs_to_many :books end Methods Added to Author books(force_reload=false) books<<aBook books.push_with_attributes(book1,...) books.delete(book1, book2,...) books=objects books.clear books.empty? books.size books.find(id)

17 Concurrent Edits Thread 1Thread 2 a = Book.find(1) b = Book.find(1) b.title = "Bat" b.save a.title = "Foo" a.save

18 Optimistic Locking Version each row Add integer column 'lock_version' of zeros to table Check version each update Throw StaleObjectError if version changes before you update Thread 1Thread 2 a = Book.find(1) b = Book.find(1) b.title = "Bat" b.save a.title = "Foo" a.save #now throws StaleObjectError CREATE TABLE `books` ( `id` int(11) NOT NULL auto_increment, `title` varchar(100) NOT NULL, `date` date default ' ', `publisher_id` int(11) NOT NULL, `lock_version` int(11) default '0', PRIMARY KEY (`id`) )

19 reload Thread 1Thread2Output a = Book.find(1) puts a.title Agile Web Development b = Book.find(1) b.title = "Foo" b.save puts b.titleFoo puts a.titleAgile Web Development a.reload puts a.titleFoo

20 Detecting when a save fails b = Book.find(1) b.title = "Foo" saved = b.save if saved puts 'Successful save' else puts 'No save' end begin a = Book.find(1) a.title = "Bar" a.save! rescue RecordInvalid puts 'No Save' end