Web Programming Step by Step

Appendix A
Database Design

Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.

Valid XHTML 1.1 Valid CSS!

Database design principles (Appendix A)

First database design

student_grades
nameemailcoursegrade
Bartbart@fox.comComputer Science 142B-
Bartbart@fox.comComputer Science 143C
Milhousemilhouse@fox.comComputer Science 142B+
Lisalisa@fox.comComputer Science 143A+
Lisalisa@fox.comComputer Science 190MA+
Ralphralph@fox.comInformatics 100D+

Second database design

students
idnameemail
123Bartbart@fox.com
456Milhousemilhouse@fox.com
888Lisalisa@fox.com
404Ralphralph@fox.com
courses
idname
10001Computer Science 142
10002Computer Science 143
10003Computer Science 190M
10004Informatics 100
grades
student_idcourse_idgrade
12310001B-
12310002C
45610001B+
88810002A+
88810003A+
40410004D+

Related tables and keys

students
idnameemail
123Bartbart@fox.com
456Milhousemilhouse@fox.com
888Lisalisa@fox.com
404Ralphralph@fox.com
courses
idname
10001Computer Science 142
10002Computer Science 143
10003Computer Science 190M
10004Informatics 100
grades
student_idcourse_idgrade
12310001B-
12310002C
45610001B+
88810002A+
88810003A+
40410004D+

Design question

students
idnameemail
123Bartbart@fox.com
456Milhousemilhouse@fox.com
888Lisalisa@fox.com
404Ralphralph@fox.com
courses
idname
10001Computer Science 142
10002Computer Science 143
10003Computer Science 190M
10004Informatics 100
grades
student_idcourse_idgrade
12310001B-
12310002C
45610001B+
88810002A+
88810003A+
40410004D+

Design answer

teachers
idname
1234Krabappel
5678Hoover
9012Stepp
courses
idnameteacher_id
10001Computer Science 1421234
10002Computer Science 1435678
10003Computer Science 190M9012
10004Informatics 1001234

Entities and relationships

ER diagram

The SQL CREATE TABLE statement (A.1.2)

CREATE TABLE name (
    columnName type constraints,
    ...
    columnName type constraints
);
CREATE TABLE students (
    sid INTEGER UNSIGNED NOT NULL PRIMARY KEY,
    name VARCHAR(20),
    email VARCHAR(32)
);

SQL data types