Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Intro to Databases (Rails Girls PHL)
Search
Ernesto Tagwerker
December 03, 2016
Programming
0
73
Intro to Databases (Rails Girls PHL)
A quick intro to databases for Rails Girls PHL.
Ernesto Tagwerker
December 03, 2016
Tweet
Share
More Decks by Ernesto Tagwerker
See All by Ernesto Tagwerker
Stuck in the Tar Pit at Sin City Ruby '24
etagwerker
1
280
Lightning Talk: Escaping the Tar Pit
etagwerker
1
120
Fortify Rails Webinar
etagwerker
0
2.3k
Here Be Dragons: The Hidden Gems of Technical Debt
etagwerker
0
220
Lessons Learned from Open Source
etagwerker
0
90
Upgrading Rails: The Dual-Boot Way
etagwerker
1
470
Ruby 3.0 & Rails 6.1
etagwerker
0
180
RubyMem: The Leaky Gems Database for Bundler at Ruby Kaigi Takeout 2020
etagwerker
0
250
Escaping The Tar Pit at NYC.rb
etagwerker
0
110
Other Decks in Programming
See All in Programming
KotlinConf 2025 現地で感じたServer-Side Kotlin
n_takehata
1
230
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
260
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
250
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.3k
C++20 射影変換
faithandbrave
0
530
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
310
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
150
GoのGenericsによるslice操作との付き合い方
syumai
3
690
NPOでのDevinの活用
codeforeveryone
0
350
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
860
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
100
プロダクト志向ってなんなんだろうね
righttouch
PRO
0
160
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Designing for Performance
lara
609
69k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Unsuck your backbone
ammeep
671
58k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Code Reviewing Like a Champion
maltzj
524
40k
Why You Should Never Use an ORM
jnunemaker
PRO
57
9.4k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Transcript
Intro to Databases Rails Bridge Philadelphia December 2016
Ernesto Tagwerker @etagwerker
Spreadsheets
None
From spreadsheets to databases
None
None
None
Primary Keys
Primary Keys
None
Relational Databases
Relations (Tables)
Relational Database Management Systems (DBMS)
MySQL Postgres SQLite* MS SQL Server and more … DBMS
Alternatives
Databases in Rails
$ rails new --help # database # (options: mysql/oracle/postgresql/sqlite3/ frontbase/ibm_db/sqlserver/jdbcmysql/
jdbcsqlite3/jdbcpostgresql/jdbc) $ rails new polls —database=postgresql $ rails new polls # defaults to sqlite3
Where is the database?
SQL (for data) • Create • Retrieve • Update •
Delete
Rails Migrations # db/migrate/20161124163416_create_topics.rb class CreateTopics < ActiveRecord::Migration def change
create_table :topics do |t| t.string :title t.text :description t.timestamps null: false end end end
Rails Migrations $ rake db:migrate == 20161124163416 CreateTopics: migrating =====================================
-- create_table(:topics) -> 0.0496s == 20161124163416 CreateTopics: migrated (0.0497s) ============================
Rails Migrations CREATE TABLE `topics` ( `id` int(11) NOT NULL
AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `description` text, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) )
SQL (for table definitions) • Create table • Modify table
• Drop table
Models # app/models/vote.rb class Vote < ActiveRecord::Base belongs_to :user belongs_to
:topic end # app/models/topic.rb class Topic < ActiveRecord::Base has_many :votes end # app/models/user.rb class User < ActiveRecord::Base has_many :votes end
Models (create) $ rails console > Topic.create(title: 'Intro to Database',
description: 'This is awesome!') SQL (1.4ms) INSERT INTO `topics` (`title`, `description`, `created_at`, `updated_at`) VALUES ('Intro to Database', 'This is awesome!', '2016-11-24 16:57:13', '2016-11-24 16:57:13') => #<Topic id: 1, title: "Intro to Database", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 16:57:13">
Models (retrieve) > topic = Topic.find 1 Topic Load (1.6ms)
SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1 => #<Topic id: 1, title: "Intro to Database", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 16:57:13">
None
Models (update) > topic.title = "Intro to Databases" => "Intro
to Databases" > topic.save SQL (2.3ms) UPDATE `topics` SET `title` = 'Intro to Databases', `updated_at` = '2016-11-24 17:00:47' WHERE `topics`.`id` = 1 => true
Models (destroy) > topic.delete SQL (10.0ms) DELETE FROM `topics` WHERE
`topics`.`id` = 1 => #<Topic id: 1, title: "Intro to Databases", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 17:00:47">
Models (count) > Topic.count (2.1ms) SELECT COUNT(*) FROM `topics` =>
0
How to use models
Topic.find 123 Topic.first Topic.last Topic.count To interact with tables
topic = Topic.find 123 topic.title = “New title” topic.save To
modify records
Thank you!
Questions?