Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TO WRECK YOUR
DATABASE
Josh Berkus PostgreSQL Experts, Inc. For O'Reilly Media, July 2009
REVENGE
guarantee yourself
JOB SECURITY
ensure PROJECT
FAILURE
make sure
NOBODY KNOWS
open a database
BACK
DOOR
force a
REWRITE
of the code
TOTAL
SLOW
DOWN
the application
to a crawl
DEVELOPERS
MISERABLE
10 Ways
to:
Lose Data
Be Insecure
Become Unmaintainable
Stife Changes
and
Make Everyone
Miserable
2. ranDom_naming(s) felds
users
users
is_active ACL
tblUsers
tblUsers
bool_is_active int4ACL
3. No Real Keys
Users
3. No Real Keys
ID
21 47 198 204
first_name last_name
Josh Joshua Josh Josh Berkus Berkus Berkus Berkus
4. No Foreign Keys
users.login Josh Berkus jberkus jerkyboy selena posts.content Josh Berkus What's up? I'm going crazy! www.pornking.com Why? www.whitehouse.com OSCON! It's too much! www.whiteslavery.com www.lolcats.com I told you so ...
5. No Constraints
Users
userID AUTONUMBER PRIMARY KEY frst_name TEXT last_name TEXT login TEXT email TEXT is_active TEXT
5. No Constraints
Users
userID AUTONUMBER PRIMARY KEY frst_name VARCHAR (30) last_name VARCHAR (30) login VARCHAR (20) email VARCHAR (40) is_active BOOLEAN
5. No Constraints
first_name
Josh NULL Mike S
last_name
Berkus NULL Hunt F
email
josh@pgexperts.com kelley@ucb www.pornking.com gavin@sf.gov
login
jberkus k c34521 gavin
password active
jehosaphat NULL c34521 twitter TRUE FALSE TRUE NULL
level
a u I x
6. Non-Atomic Fields
name Josh Berkus SELECT SUBSTR(name,STRPOS(name, ' ')) ... status a WHERE status = 'a' OR status = 'u' ...
6. Non-Atomic Fields
name Josh Berkus SELECT SUBSTR(name,STRPOS(name, ' ')) ... status i WHERE status = ??? ...
6. Non-Atomic Fields
Account Type
5400 active individual 5401 inactive individual 5600 active board individual 5601 inactive board individual 6600 active corporate donor 6601 active corporate board member 6602 inactive corporate donor 6603 inactive corporate board member
7. Magic Numbers
ID = 0
7. Magic Numbers
ID = 0
SELECT case_file, count(1) FROM case_events WHERE ID > 0 AND scheduled = TRUE GROUP BY case_file;
7. Magic Numbers
2008-02-30
7. Magic Numbers
2008-02-30
SELECT user_id, min(date) FROM user_posts WHERE NOT ( extract(month from date) = 2 and extract (day from date) = 30 ) GROUP BY user_id;
7. Magic Numbers
-1, 1, 2, 3, 4, 5, 100
7. Magic Numbers
-1, 1, 2, 3, 4, 5, 100
SELECT household, avg(headcount) FROM house WHERE headcount BETWEEN 0 and 99 GROUP BY household;
7. Magic Numbers
$-0.01
7. Magic Numbers
$-0.01
SELECT * FROM invoices WHERE total <> $0.01 AND sent_on IS NULL;
7. Magic Numbers
8. Polymorphic Fields
Name
Josh Berkus John Dillinger Pearson Clorox
AccountType PrefContact
Individual Board Company Company Email Phone NULL Jackie
ContactInfo
josh@pgexperts.com 415-555-1212 www.pearson.com 510-555-1111 x202
9. EAV (entity-attribute-value)
ID 407 407 407 408 408 408 409 Property Eyes Height Married? Married? Smoker Age Height Setting Brown 73in TRUE FALSE FALSE 37 66in
9. EAV (entity-attribute-value)
Diffcult tasks:
Find out how many men have brown hair and are over 6 feet? Make marital status and age required, but not other things. Apply constraints to feld values.
407
408
409
More Wreckage
OSCON, Monday 8:30 AM Before OSCON, Sunday 19th, all day wiki.postgresql.org/wiki/PgDaySanJose2009 PostgreSQL Experts: www.pgexperts.com blog: it.toolbox.com/blogs/database-soup email: josh@pgexperts.com
This presentation copyright 2009 Josh Berkus, licensed for distribution under the Creative Commons Attribution License.
Me