quick and dirty databases

26
Quick and Dirty Databases (and SQL) Chris Warren [email protected] x4323 Jesup 312

Upload: cwarren

Post on 01-Nov-2014

887 views

Category:

Education


1 download

DESCRIPTION

A quick intro to database design and SQL

TRANSCRIPT

Page 1: Quick And Dirty Databases

Quick and Dirty Databases(and SQL)

Chris [email protected] Jesup 312

Page 2: Quick And Dirty Databases

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

Page 3: Quick And Dirty Databases

Designing a (Relational) DB

1.Entities and Entity Relationships (ERD)

2.Normalizing

3.Detailing

4.Cleanup

Page 4: Quick And Dirty Databases

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

Page 5: Quick And Dirty Databases

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

Page 6: Quick And Dirty Databases

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

Page 7: Quick And Dirty Databases

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

Page 8: Quick And Dirty Databases

Normalizing

Normalizing is basically removing duplication

• merge mostly similar tables

• split out common field sets

• split out conceptually distinct field sets

Page 9: Quick And Dirty Databases

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

Page 10: Quick And Dirty Databases

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

Page 11: Quick And Dirty Databases

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

Page 12: Quick And Dirty Databases

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)

Page 13: Quick And Dirty Databases

ERD Cleanup

• add timestamps (created_at,

updated_at)

• review relationships, fix as needed

• review fields, add any missing

characteristics

Page 14: Quick And Dirty Databases

ERD is DONE!

Now you get to implement it...

Page 15: Quick And Dirty Databases

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/

Page 16: Quick And Dirty Databases

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

Page 17: Quick And Dirty Databases

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

Page 18: Quick And Dirty Databases

SQL Conventions

• SQL keywords in upper case

• Your info (tables, fields, etc) in lower case

• Indent each section of the statement

• Use multiple lines

Page 19: Quick And Dirty Databases

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

Page 20: Quick And Dirty Databases

SQL SELECT Example

SELECTe.name

FROMcwarren_equipment AS e,cwarren_location AS l

WHEREe.location_id=l.location_id

AND l.name=‘Lab 54’;

Page 21: Quick And Dirty Databases

SQL SELECT Example

SELECTe.name

FROMcwarren_equipment AS eJOIN cwarren_location AS l ON

e.location_id=l.location_idWHERE

l.name=‘Lab 54’;

Page 22: Quick And Dirty Databases

SQL UPDATE

UPDATE TABLE tableSET

field1=newval1,field2=newval2,...

WHEREconditions

Page 23: Quick And Dirty Databases

SQL INSERT

INSERT INTO tableVALUES (v1,v2,v3,...)

NOTE: use the special value NULL for id columns

INSERT INTO tableVALUES (NULL,v1,v2,v3,...)

Page 24: Quick And Dirty Databases

SQL DELETE

DELETE FROM tableWHERE

conditions

NOTE: DANGEROUS!

Page 25: Quick And Dirty Databases

SQL Gotchas

• quote text values• do NOT quote number values• always specify table links• separate using commas

• FORMAT WELL!!