Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Class 7: Databases

Class 7: Databases

Notes for 6/20/2013

Ian Luke Kane

June 20, 2013
Tweet

More Decks by Ian Luke Kane

Other Decks in Technology

Transcript

  1. What  is  a  Database? A  database  is  an  organized  collec1on

     of  data. It  models  relevant  aspects  of  reality  in  a  way  that   supports  processes  requiring  this  informa1on. Alterna1vely,  a  database  is  a  collec1on  of  tables,  with   related  data. Physically,  database  servers  are  dedicated  computers   that  hold  the  actual  databases  and  run  only  the  DBMS   and  related  soCware.
  2. What  is  Data? Data  are  known  facts  that  can  be

     recorded  and  have   meaning  in  the  user's  environment Alterna1vely,  data  are  values  of  qualita1ve  or   quan1ta1ve  variables,  belonging  to  a  set  of  items. Sets  are  important.
  3. What  is  a  Database  Management   System? A  database  management

     system  (DBMS)  is  a  soCware   system  designed  to  allow  the  defini1on,  crea1on,   querying,  update,  and  administra1on  of  databases. Examples:  MySQL,  PostgreSQL,  SQLite,  MicrosoC  SQL   Server,  MariaDB Because  they  are  so  closely  related,  the  term  "database"   when  used  casually  oCen  refers  to  both  a  DBMS  and  the   data  it  manipulates.
  4. Why  use  them? Databases  are  set  up  so  that  one

     set  of  soCware   programs  provides  all  users  with  access  to  all  the  data. Databases  can  inter-­‐operate  by  using  standards  such  as   SQL,  ODBC  (open  database  connec1vity),  or  JDBC  (Java   database  connec1vity).
  5. Abridged  History  and  Prac:ce SQL/Rela1onal  vs.  Post-­‐Rela1onal  (NoSQL) We'll  be

     mainly  talking  today  about  the  rela1onal   model. Why?  Because  it’s  a  good  place  to  start.  At  least  in  my   opinion.  We  will  men1on  NoSQL  and  how  rela1onal   databases  interact  with  object  oriented  programming.
  6. Rela:onal  DBMS  Func:ons Data  Defini)on Defining  new  data  structures  for

     a  database,  removing  data   structures  from  the  database,  modifying  the  structure  of  exis1ng   data. Update   Inser1ng,  modifying,  and  dele1ng  data. Retrieval Obtaining  informa1on  either  for  end-­‐user  queries  and  reports  or  for   processing  by  applica1ons. Administra)on   Registering  and  monitoring  users,  enforcing  data  security,   monitoring  performance,  maintaining  data  integrity,  dealing  with   concurrency  control,  and  recovering  informa1on  if  the  system  fails.
  7. RDBMS  Terminology Table A  table  is  a  matrix  with  data.

     A  table  in  a  database  looks  like  a   simple  spreadsheet. Column One  column  (data  element)  contains  data  of  one  and  the  same   kind. Row A  row  (=  tuple,  entry  or  record)  is  a  group  of  related  data.
  8. RDBMS  Terminology Primary  Key A  primary  key  is  unique.  A

     key  value  can  not  occur  twice  in  one   table.  With  a  key  you  can  find  at  most  one  row. Foreign  Key A  foreign  key  is  the  linking  pin  between  two  tables. Compound  Key A  compound  key  (composite  key)  is  a  key  that  consists  of   mul1ple  columns,  because  one  column  is  not  sufficiently   unique.
  9. DBMS  Terminology Index An  index  in  a  database  resembles  an

     index  at  the  back  of  a   book. Referen)al  Integrity Referen1al  Integrity  makes  sure  that  a  foreign  key  value  always   points  to  an  exis1ng  row.
  10. Simplified  Rela:onal  Database   Structure Databases  use  a  table  format.

    The  table  is  made  up  of  rows  and  columns. Each  piece  of  informa1on  is  entered  into  a  row,  which   then  creates  a  record  (tuple).
  11. Structured  Query  Language  (SQL) Powerful  ad  hoc  query  language.  SQL

     is  a  special-­‐ purpose  programming  language  designed  for  managing   data  held  in  a  RDBMS. The  scope  of  SQL  includes  data  insert,  query,  update  and   delete,  schema  crea1on  and  modifica1on,  and  data   access  control.  Although  SQL  is  oCen  described  as,  and   to  a  great  extent  is,  a  declara1ve  language  (4GL),  it  also   includes  procedural  elements.
  12. Structured  Query  Language  (SQL) SQL  is  designed  for  a  specific

     purpose:  to  query  data   contained  in  a  rela1onal  database.  SQL  is  a  set-­‐based,   declara1ve  query  language,  not  an  impera1ve  language   like  C  or  BASIC.  However,  there  are  extensions  to   Standard  SQL  which  add  procedural  programming   language  func1onality,  such  as  control-­‐of-­‐flow   constructs.  
  13. SQL  CRUD CREATE Insert  data  into  tables RETRIEVE Select  data

     out  of  tables UPDATE Modify  data  in  a  table DELETE Remove  data  from  a  table
  14. SQL  Standard SQL  was  adopted  as  a  standard  by  the

     American   NaEonal  Standards  InsEtute  (ANSI)  in  1986  as   SQL-­‐86[33]  and  the  InternaEonal  OrganizaEon  for   StandardizaEon  (ISO)  in  1987.  Nowadays  the  standard   is  subject  to  conEnuous  improvement  by  the  Joint   Technical  CommiWee  ISO/IEC  JTC  1,  InformaEon   technology,  SubcommiWee  SC  32,  Data  management   and  interchange  which  affiliate  to  ISO  as  well  as  IEC.  It   is  commonly  denoted  by  the  paWern:  ISO/IEC  9075-­‐ n:yyyy  Part  n:  Etle,  or,  as  a  shortcut,  ISO/IEC  9075.
  15. Database  “Languages”  (SQL) Data  defini1on  language  (DDL) Defines  objects,  data

     types,  and  the  rela1onships  among  them Data  Manipula1on  Language  (DML)   Performs  tasks  such  as  inser1ng,  upda1ng,  or  dele1ng  data   occurrences Query  Language Allows  searching  for  informa1on  and  compu1ng  derived   informa1on
  16. SQL,  DML  and  DDL The  Data  Manipula1on  Language  (DML)  is

     the  subset  of   SQL  used  to  add,  update  and  delete  data.  Some  basic   items  of  DML  are  the  SELECT,  UPDATE,  and  DELETE   statements. The  Data  Defini1on  Language  (DDL)  manages  table  and   index  structure.  The  most  basic  items  of  DDL  are  the   CREATE,  ALTER,  RENAME,  DROP  and  TRUNCATE   statements.
  17. Data  Types Data  can  be  stored  in  a  variety  of

     data  types.  Here  are   some: Character  strings CHARACTER(n)  or  CHAR(n):  fixed-­‐width  n-­‐character  string,   padded  with  spaces  as  needed CHARACTER  VARYING(n)  or  VARCHAR(n):  variable-­‐width  string   with  a  maximum  size  of  n  characters NATIONAL  CHARACTER(n)  or  NCHAR(n):  fixed  width  string   suppor1ng  an  interna1onal  character  set NATIONAL  CHARACTER  VARYING(n)  or  NVARCHAR(n):  variable-­‐ width  NCHAR  string
  18. Data  Types Numbers INTEGER  and  SMALLINT FLOAT,  REAL  and  DOUBLE

     PRECISION NUMERIC(precision,  scale)  or  DECIMAL(precision,  scale) Bits A  two  or  three  way  switch Blobs Possibly  mul1media,  for  instance
  19. Data  Types Date  and  Time DATE:  for  date  values  (e.g.

     2011-­‐05-­‐03) TIME:  for  1me  values  (e.g.  15:51:36).  The  granularity  of  the   1me  value  is  usually  a  1ck  (100  nanoseconds). TIME  WITH  TIME  ZONE  or  TIMETZ:  the  same  as  TIME,  but   including  details  about  the  1me  zone  in  ques1on. TIMESTAMP:  This  is  a  DATE  and  a  TIME  put  together  in  one   variable  (e.g.  2011-­‐05-­‐03  15:51:36). TIMESTAMP  WITH  TIME  ZONE  or  TIMESTAMPTZ:  the  same  as   TIMESTAMP,  but  including  details  about  the  1me  zone  in   ques1on.
  20. NoSQL A  NoSQL  database  provides  a  mechanism  for  storage  

    and  retrieval  of  data  that  uses  looser  consistency   models  than  tradiEonal  relaEonal  databases. MoEvaEons  for  this  approach  include  simplicity  of   design,  horizontal  scaling  and  finer  control  over   availability.   NoSQL  databases  are  o`en  highly  opEmized  key–value   stores  intended  for  simple  retrieval  and  appending   operaEons,  with  the  goal  being  significant   performance  benefits  in  terms  of  latency  and   throughput.
  21. Object  Rela6onal  Mapping  (ORM) A  programming  technique  for  conver1ng  data

     between   incompa1ble  type  systems  in  object-­‐oriented  programming   languages.  This  creates,  in  effect,  a  "virtual  object   database"  that  can  be  used  from  within  the  programming   language. The  heart  of  the  problem  is  transla1ng  the  logical   representa1on  of  the  objects  into  an  atomized  form  that  is   capable  of  being  stored  on  the  database,  while  somehow   preserving  the  proper1es  of  the  objects  and  their   rela1onships  so  that  they  can  be  reloaded  as  an  object   when  needed.