quick and dirty databases
DESCRIPTION
A quick intro to database design and SQLTRANSCRIPT
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
tablea bunch of tables make a
databasea bunch of databases make a
DB system
Designing a (Relational) DB
1.Entities and Entity Relationships (ERD)
2.Normalizing
3.Detailing
4.Cleanup
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
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
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
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
Normalizing
Normalizing is basically removing duplication
• merge mostly similar tables
• split out common field sets
• split out conceptually distinct field sets
Normalizing – Merge Tables
• tables have very similar field sets• entities are both / all examples of a more
general concept
1. generalize the table name
2. add a type field
3. change foreign keys in other tables to
include the type
Normalizing – Split Out Field Sets
• same list of field repeated in different tables
• group of fields describes a distinct concept
1. create a new table for that new entity2. add a foreign key to the new table in each of the
original tables
Normalizing – Split Out Concepts
• a table has multiple sub-parts– common field name prefix is one indicator
• data would be repeated for many records
1. create a new table for those fields
2. add a foreign key to the original
table
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)
ERD Cleanup
• add timestamps (created_at,
updated_at)
• review relationships, fix as needed
• review fields, add any missing
characteristics
ERD is DONE!
Now you get to implement it...
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/
SQL concepts (manipulation)
1. Databases are a big pile of information
2. Identify which piece of info you want to manipulate
3. State how you want to manipulate it– retrieve it– edit it– delete it– add it
Writing SQL
build it backwards....
1. what you want to do – select, update, delete, or insert
2. where it is– the table and row(s)
3. the specific parts– the fields
SQL Conventions
• SQL keywords in upper case
• Your info (tables, fields, etc) in lower case
• Indent each section of the statement
• Use multiple lines
SQL SELECT (most common)
SELECTfields
FROMtables
WHEREconditions
GROUP BYnon-aggregate fields
ORDER BYfields
3. the SELECT clause – which fields
1. the FROM clause – which tables
2. the WHERE clause – which rows
SQL SELECT Example
SELECTe.name
FROMcwarren_equipment AS e,cwarren_location AS l
WHEREe.location_id=l.location_id
AND l.name=‘Lab 54’;
SQL SELECT Example
SELECTe.name
FROMcwarren_equipment AS eJOIN cwarren_location AS l ON
e.location_id=l.location_idWHERE
l.name=‘Lab 54’;
SQL UPDATE
UPDATE TABLE tableSET
field1=newval1,field2=newval2,...
WHEREconditions
SQL INSERT
INSERT INTO tableVALUES (v1,v2,v3,...)
NOTE: use the special value NULL for id columns
INSERT INTO tableVALUES (NULL,v1,v2,v3,...)
SQL DELETE
DELETE FROM tableWHERE
conditions
NOTE: DANGEROUS!
SQL Gotchas
• quote text values• do NOT quote number values• always specify table links• separate using commas
• FORMAT WELL!!
SQL Manual
http://dev.mysql.com/doc/refman/5.0/en/