Introduc)on*to*Databases 1 Rela%onal(Databases(with(PostgreSQL - - PowerPoint PPT Presentation

introduc on to databases
SMART_READER_LITE
LIVE PREVIEW

Introduc)on*to*Databases 1 Rela%onal(Databases(with(PostgreSQL - - PowerPoint PPT Presentation

Introduc)on*to*Databases 1 Rela%onal(Databases(with(PostgreSQL Databases(have(tables(to(classify(data Collec3ons(have: rows :(data(defining(an(en3re(rectod,(e.g.(a(user columns


slide-1
SLIDE 1

Introduc)on*to*Databases

1

slide-2
SLIDE 2

Rela%onal(Databases(with(PostgreSQL

  • Databases(have(tables(to(classify(data
  • Collec3ons(have:
  • rows:(data(defining(an(en3re(rectod,(e.g.(a(user
  • columns:(a<ributes(about(the(record,(e.g.(a(user's(email(and(

birthday

2

slide-3
SLIDE 3

Example(table

email | password | birthday | location

  • jon.miller@nycda.com | penguin | 10/10/1988 | Amsterdam

katie@patie.com | bubbles123 | 12/01/1990 | Los Angeles flurble@wurzle.com | bl0rp | 01/02/1981 | Antarctica

3

slide-4
SLIDE 4

Other&databases

  • MySQL
  • MongoDB
  • HBase

4

slide-5
SLIDE 5

Common%psql%commands

  • psql"is"the"PostgreSQL"command"line"interface

\? /* help: list available commands */ \c my_app /* connect to database 'my_app' */ \dt+ /* list tables */ \d+ fruit /* describe table 'fruit' */ \q /* quit */

5

slide-6
SLIDE 6

PostgresSQL+commands:+create

  • Creates(a(named(table(with(some(informa3on(about(each(record:

create table hats ( name text, material text, height integer, brim boolean );

6

slide-7
SLIDE 7

Exercise

  • Create'the'hats'table'shown'previously.

7

slide-8
SLIDE 8

PostgreSQL+commands:+insert+into

  • Adds%records%into%a%table%with%supplied%informa6on.

insert into hats values ('sun hat', 'straw', 7, true); insert into hats (name, material, height, brim) values ('top hat', 'buckram', 12, true); insert into hats (name, material, height, brim) values ('cloche', 'felt', 6, false), ('chicken', 'bwuk bwuk bwuk', 12, false);

8

slide-9
SLIDE 9

PostgresSQL+commands:+select,+where

  • Retrieves)informa/on)from)a)table,)op/onally)given)condi/ons.

select * from hats; select * from hats where name = 'top hat'; select count(*) from hats;

9

slide-10
SLIDE 10

PostgreSQL+commands:+delete+from

  • Removes(data(from(a(table(that(meet(given(condi5ons.

delete from hats where name = 'chicken';

10

slide-11
SLIDE 11

PostgreSQL+commands:+alter

  • Modify(a(table.

alter table hats add column price integer;

11

slide-12
SLIDE 12

Exercise

  • Add$three$new$hats$to$the$hats$table.
  • Select$all$the$hats$that$are$made$of$felt.

12

slide-13
SLIDE 13

Primary'keys

  • Say%we%needed%a%specific%hat%1%how%would%we%get%it?

13

slide-14
SLIDE 14

Primary'keys'(con/nued)

  • serial:#auto(incremented#integer
  • primary key:#2#constraints:#unique#and#non(null

drop table hats; create table hats ( id serial primary key, name text, material text, height integer, brim boolean );

14

slide-15
SLIDE 15

Exercise

  • Recreate(your(hats(table(with(the(serial primary key(

added.

15

slide-16
SLIDE 16

Adding&to&the&database

  • No$ce'how'the'id'is'set'automagically.

insert into hats (name, material, height, brim) values ('cloche', 'felt', 6, false), ('top hat', 'buckram', 12, true);

16

slide-17
SLIDE 17

Exercise

  • Create'a'users'table'with'three'columns:
  • id'(should'be'a'serial'primary'key)
  • name
  • email

17

slide-18
SLIDE 18

Exercise((con,nued)

  • Add$three$sample$users$to$your$table.

18

slide-19
SLIDE 19

"Rela&onal")Databases

  • If$we$had$a$users$table,$and$each$user$had$a$few$hats,$how$would$

we$relate$these$two$tables$to$each$other?

19

slide-20
SLIDE 20

"Rela&onal")Databases)(con&nued)

  • Add$a$user_id$to$the$hats$table.
  • This$is$called$a$Many4to4one$rela6onship.$(many$hats,$one$user)

alter table hats add column user_id integer;

20

slide-21
SLIDE 21

Constraints

  • If$we$wanted$to$make$sure$no$two$hats$belonged$to$the$same$

user,$what$constraint$would$we$add?

  • If$we$wanted$to$make$sure$the$user_id$was$required,$what$

constraint$would$we$add?

  • Coming$up:$If$we$wanted$to$make$sure$the$user$that$the$

user_id$was$poin=ng$to$actually$existed,$what$constraint$would$ we$use?$

21

slide-22
SLIDE 22

Foreign(keys

alter table hats drop column user_id integer; alter table hats add column user_id integer references users;

22

slide-23
SLIDE 23

Foreign(keys((con.nued)

  • Note&the&error&message:&since&the&user_id&10&does&not&exist,&

PostgreSQL&will&complain.

insert into hats (name, material, height, brim, user_id) values ('bowler', 'velvet', 6, false, 10);

23

slide-24
SLIDE 24

Exercise

  • Insert(two(more(hats(which(reference(valid(users.

24

slide-25
SLIDE 25

Select:'Chaining'Queries

  • What&if&we&wanted&to&get&all&the&hats&that&belonged&to&a&

par4cular&user,&but&we&only&had&his&email?

select * from hats where user_id = (select (id) from users where email = 'josh@gmail.com');

25