Quick And Dirty Databases

984 views

Published on

A quick intro to database design and SQL

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
984
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Quick And Dirty Databases

  1. 1. Quick and Dirty Databases(and SQL)<br />Chris Warren<br />cwarren@williams.edu<br />x4323 Jesup 312<br />
  2. 2. Data Organization in Databases<br />field (a.k.a. column)<br />a bunch of fields make a<br />record (a.k.a. row)<br />a bunch of records make a<br />table<br />a bunch of tables make a<br />database<br />a bunch of databases make a<br />DB system<br />
  3. 3. Designing a (Relational) DB<br />Entities and Entity Relationships (ERD)<br />Normalizing<br />Detailing<br />Cleanup<br />
  4. 4. Entities and Entity Relationships<br />Entities (tables)<br />ID (a.k.a. primary key)<br />Entity Attributes (fields)<br />Relationships<br />1-to-1<br />1-to-many or many-to-1<br />many-to-many<br />
  5. 5. Relationships<br />1-to-1<br />usually data is in the same table<br />1-to-many and many-to-1<br />foreign key goes with the many entity<br />many-to-many<br />linking table with 2 foreign keys, one for each side<br />
  6. 6. Create an ERD<br />ERD = Entity Relationship Diagram<br />Draw on paper<br />Entities (tables)<br />Relationships between them<br />indicate type : 1-to-1, 1-to-many, many-to-many<br />
  7. 7. Detail the ERD<br />Finalize table names and list fields<br />table name should be plural<br />primary id is singular + _id<br />add any foreign keys<br />list fields<br />include unit in name<br />add is_ or flag_ or _flag for true/false fields <br />
  8. 8. Normalizing<br />Normalizing is basically removing duplication<br />merge mostly similar tables <br />split out common field sets<br />split out conceptually distinct field sets<br />
  9. 9. Normalizing – Merge Tables<br />tables have very similar field sets<br />entities are both / all examples of a more general concept<br />generalize the table name<br />add a type field<br />change foreign keys in other tables to include the type<br />
  10. 10. Normalizing – Split Out Field Sets<br />same list of field repeated in different tables<br />group of fields describes a distinct concept<br />create a new table for that new entity<br />add a foreign key to the new table in each of the original tables<br />
  11. 11. Normalizing – Split Out Concepts<br />a table has multiple sub-parts<br />common field name prefix is one indicator<br />data would be repeated for many records<br />create a new table for those fields<br />add a foreign key to the original table<br />
  12. 12. ERD Detailing<br />create any grouping tables as needed<br />add data types and descriptions to fields<br />int<br />varchar (string up to 255 characters)<br />text (BIG string)<br />date and/or time<br />boolean (true/false)<br />
  13. 13. ERD Cleanup<br />add timestamps (created_at, updated_at)<br />review relationships, fix as needed<br />review fields, add any missing characteristics<br />
  14. 14. ERD is DONE!<br />Now you get to implement it...<br />
  15. 15. SQL – Structured Query Language<br />used to send commands to a database<br />there’s a core standard, and a lot of vendor-specific minor changes / additions<br />two branches<br />data definition – create tables<br />data manipulation – add, edit, retrieve, delete data<br />http://dev.mysql.com/doc/refman/5.0/en/<br />
  16. 16. SQL concepts (manipulation)<br />Databases are a big pile of information<br />Identify which piece of info you want to manipulate<br />State how you want to manipulate it<br />retrieve it<br />edit it<br />delete it<br />add it<br />
  17. 17. Writing SQL<br />build it backwards....<br />what you want to do <br />select, update, delete, or insert<br />where it is<br />the table and row(s)<br />the specific parts<br />the fields<br />
  18. 18. SQL Conventions<br />SQL keywords in upper case<br />Your info (tables, fields, etc) in lower case<br />Indent each section of the statement<br />Use multiple lines<br />
  19. 19. SQL SELECT (most common)<br />3.the SELECT clause – which fields <br />SELECT<br />fields<br />FROM<br />tables<br />WHERE<br />conditions<br />GROUP BY<br />non-aggregate fields<br />ORDER BY<br />fields<br />1.the FROM clause – which tables <br />2.the WHERE clause – which rows <br />
  20. 20. SQL SELECT Example<br />SELECT<br />e.name<br />FROM<br />cwarren_equipment AS e<br /> ,cwarren_location AS l<br />WHERE<br />e.location_id=l.location_id<br /> AND l.name=‘Lab 54’;<br />
  21. 21. SQL SELECT Example<br />SELECT<br />e.name<br />FROM<br />cwarren_equipment AS e<br /> JOIN cwarren_location AS l ON e.location_id=l.location_id<br />WHERE<br />l.name=‘Lab 54’;<br />
  22. 22. SQL UPDATE<br />UPDATE TABLE table<br />SET <br />field1=newval1<br />,field2=newval2,...<br />WHERE<br />conditions<br />
  23. 23. SQL INSERT<br />INSERT INTO table<br />VALUES (v1,v2,v3,...)<br />NOTE: use the special value NULL for id columns<br />INSERT INTO table<br />VALUES (NULL,v1,v2,v3,...)<br />
  24. 24. SQL DELETE<br />DELETE FROM table<br />WHERE<br />conditions<br />NOTE: DANGEROUS!<br />
  25. 25. SQL Gotchas<br />quote text values<br />do NOT quote number values<br />always specify table links<br />separate using commas<br />FORMAT WELL!!<br />
  26. 26. SQL Manual<br />http://dev.mysql.com/doc/refman/5.0/en/<br />

×