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