introduc on to databases
play

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


  1. Introduc)on*to*Databases 1

  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

  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

  4. Other&databases • MySQL • MongoDB • HBase 4

  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

  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

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

  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

  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

  10. PostgreSQL+commands:+delete+from • Removes(data(from(a(table(that(meet(given(condi5ons. delete from hats where name = 'chicken'; 10

  11. PostgreSQL+commands:+alter • Modify(a(table. alter table hats add column price integer; 11

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

  13. Primary'keys • Say%we%needed%a%specific%hat%1%how%would%we%get%it? 13

  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

  15. Exercise • Recreate(your(hats(table(with(the( serial primary key ( added. 15

  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

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

  18. Exercise((con,nued) • Add$three$sample$users$to$your$table. 18

  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

  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

  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

  22. Foreign(keys alter table hats drop column user_id integer; alter table hats add column user_id integer references users; 22

  23. Foreign(keys((con.nued) • Note&the&error&message:&since&the& user_id &10&does&not&exist,& PostgreSQL&will&comp lain. insert into hats (name, material, height, brim, user_id) values ('bowler', 'velvet', 6, false, 10); 23

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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend