data and database

Post on 22-Jan-2018

135 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Data and Database

A Quick Approach to Rails Data Core

Mohammad Saleh Mahdizadeh

Designed by Mohammad Saleh Mahdizadeh

What to do with DATA?

▪ Paper-base:

– Forms (Files) => One page for every RECORD

▪ Folders => Collection of FORMS

– Lists => ROW and COLUMNS

Every one of ANYTHING

Designed by Mohammad Saleh Mahdizadeh

Digital-base: Tables!

▪ Columns

▪ Rows

▪ id (Identification)– Uniqueness

▪ Data Types– integer, float, string, date,

datetime, boolean, text, …

– Multimedia?!

▪ null Values

id name birthday sex icdl

1 Jack 1758/3/10 m

2 Jill 1758/3/10 f

3 Cinderella 1890/2/18 f false

4 Robin 1992/11/13 f true

7 Walter 1976/5/5 m false

11 Elsa f false

13 Anna f true

14 Walter (Jr.) 1994/7/29 m true

Designed by Mohammad Saleh Mahdizadeh

… Relational Database

id name birthday sex icdl

1 Jack 1758/3/10 m

2 Jill 1758/3/10 f

3 Cinderella 1890/2/18 f false

4 Robin 1992/11/13 f true

7 Walter 1976/5/5 m false

11 Elsa f false

13 Anna f true

14 Walter (Jr.) 1994/7/29 m true

id name start_time duration points

1 Database 8:00 90 2

2 MVC 10:30 60 4

3 Conventions 17:00 75 1

4 UI 14:00 120 3

5 Architecture 12:00 60 6

6 Deploy 45 1.5

id name degree age

1 Rachel BSC 24

2 Ross PHD 27

3 Phoebe BSC 29

4 Joey 25

23 Chandler MSC 23

24 Monica BSC 28

students

courses

teachers

Database

Designed by Mohammad Saleh Mahdizadeh

Find Needle in Haystack!

▪ all

▪ find( ID )

▪ where( HASH )

▪ find_by( COLUMN: VALUE )

▪ order( COLUMN: :desc )

▪ order( COLUMN: :asc )

▪ first

{ :name1 => value1, :name2 => value2, :name3 => value3, … }

{ name1: value1, name2: value2, name3: value3, … }

1

2

Designed by Mohammad Saleh Mahdizadeh

Let’s think about it …

Fact1: How to save which teacher offers which course?

Designed by Mohammad Saleh Mahdizadeh

And then there was Relations …

id name start_time duration points teacher_id

1 Database 8:00 90 2 23

2 MVC 10:30 60 4 2

3 Conventions 17:00 75 1

4 UI 14:00 120 3 1

5 Architecture 12:00 60 6 3

6 Deploy 45 1.5 3

id name degree age

1 Rachel BCS 24

2 Ross PHD 27

3 Phoebe BCS 29

4 Joey 25

23 Chandler MCS 23

24 Monica BCS 28

courses teachers

Foreign Key

One to many relation

1∞

Designed by Mohammad Saleh Mahdizadeh

Again, let’s think about it …

Fact2: How to save which student takes which course?

Designed by Mohammad Saleh Mahdizadeh

Does foreign key can be helpful?

id name start_time duration points teacher_id

1 Database 8:00 90 2 23

2 MVC 10:30 60 4 2

3 Conventions 17:00 75 1

4 UI 14:00 120 3 1

5 Architecture 12:00 60 6 3

6 Deploy 45 1.5 3

coursesstudents

Many to many relation

id name birthday sex icdl

1 Jack 1758/3/10 m

2 Jill 1758/3/10 f

3 Cinderella 1890/2/18 f false

4 Robin 1992/11/13 f true

7 Walter 1976/5/5 m false

11 Elsa f false

13 Anna f true

14 Walter (Jr.) 1994/7/29 m true

Designed by Mohammad Saleh Mahdizadeh

Break into one to many relations

id name start_time duration points teacher_id

1 Database 8:00 90 2 23

2 MVC 10:30 60 4 2

3 Conventions 17:00 75 1

4 UI 14:00 120 3 1

5 Architecture 12:00 60 6 3

6 Deploy 45 1.5 3

coursesstudents

Many to many relation

id name birthday sex icdl

1 Jack 1758/3/10 m

2 Jill 1758/3/10 f

3 Cinderella 1890/2/18 f false

4 Robin 1992/11/13 f true

7 Walter 1976/5/5 m false

11 Elsa f false

13 Anna f true

14 Walter (Jr.) 1994/7/29 m true

id student_id course_id

1 3 2

2 3 6

3 13 1

4 7 1

5 3 4

courses_students

∞∞ 11

Designed by Mohammad Saleh Mahdizadeh

Final Data Model

id name start_timeduratio

npoints

teacher_id

1 Database 8:00 90 2 23

2 MVC 10:30 60 4 2

3Conventions

17:00 75 1

4 UI 14:00 120 3 1

5Architecture

12:00 60 6 3

6 Deploy 45 1.5 3

coursesstudents

id name birthday sex icdl

1 Jack 1758/3/10 m

2 Jill 1758/3/10 f

3 Cinderella 1890/2/18 f false

4 Robin 1992/11/13 f true

7 Walter 1976/5/5 m false

11 Elsa f false

13 Anna f true

14 Walter (Jr.) 1994/7/29 m true

id student_id course_id

1 3 2

2 3 6

3 13 1

4 7 1

5 3 4

courses_students

id name degree age

1 Rachel BCS 24

2 Ross PHD 27

3 Phoebe BCS 29

4 Joey 25

23 Chandler MCS 23

24 Monica BCS 28

teachers

Designed by Mohammad Saleh Mahdizadeh

1

11

∞ ∞

has_many courses_students

belongs_to student

has_many courses_students

belongs_to course

has_many courses

belongs_to teacher

has_many courses through courses_students

has_many students through courses_students

… Somehow a class diagram!

courses

- id (integer)

- name (string)

- start_time (datetime)

- duration (integer)

- points (float)

- teacher_id (integer)

students

- id (integer)

- name (string)

- birthday (date)

- sex (string)

- icdl (boolean)

courses_students

- student_id (integer)

- course_id (integer)

teachers

- id (integer)

- name (string)

- degree (string)

- age (integer)Designed by Mohammad Saleh Mahdizadeh

1

11

∞ ∞

∞∞

Remember,Just take it easy and simple as you can!

Thanks for your attention :)

Designed by Mohammad Saleh Mahdizadeh

top related