Database design

The first real step will be to think about what kind of data we need to store and then design a database schema to efficiently store that data. To keep things simple (but realistic), let's set our specification as follows:

  • The address book should keep track of multiple addresses for a person
  • Each person can have a first and last name
  • Each address can have a street address, a phone number, and an e-mail address

Translated into a relational database schema, that means we'll have a people table that will assign a unique identifier to each firstname and lastname pair.

Then we'll have an addresses table that will allow each person to have multiple addresses, each consisting of a unique ID (so it's easy to reference later), a location ("Office", "Home"), a free-form postal address (so we don't have to worry about the complexity of having a city, country, state, postal code, and so on), a phone number, and an e-mail address. Each row will have a column called person, which will be the ID of the person that "owns" this address (called a foreign key in SQL parlance). We'll also make each field in the addresses table optional (except for the location), so that we can have locations like "Mobile" for storing a mobile phone number (without a postal address or e-mail).

Now we just have to express this as SQL, and type it into SQLite as follows:

$ sqlite3 tmp/database
sqlite> CREATE TABLE people (id INTEGER NOT NULL PRIMARY KEY, firstname
VARCHAR(50) NOT NULL, lastname VARCHAR(50) NOT NULL);
sqlite> CREATE TABLE addresses (id INTEGER NOT NULL PRIMARY KEY, person INTEGER NOT NULL, location VARCHAR(20), postal TEXT, phone VARCHAR(20), email VARCHAR(100));

Let's also add some sample data, so that we can create a "view" page and actually see some data show up as follows:

sqlite> INSERT INTO people VALUES(NULL, 'Your', 'Name'),
sqlite> INSERT INTO addresses VALUES(NULL, 1, 'Home', '123 Green St.',
'123-456-7890', '[email protected]'),
sqlite> INSERT INTO addresses VALUES(NULL, 1, 'Work', '42 Work St.',
'987-654-3210', '[email protected]'),
sqlite> .quit

With the schema configured, let's link the database to a Catalyst Model using the following command:

$ perl script/addressbook_create.pl model AddressDB DBIC::Schema AddressBook::Schema::AddressDB create=static dbi:SQLite:database

This will create a Model called AddressDB, and it will also create a Schema/ AddressDB subdirectory that contains the definition of our database schema in DBIx::Class's (DBIC) format. This schema will be pre-populated with the schema we just created with the sqlite3 utility. Keeping the schema in this format will allow us to deploy to any database system that DBIx::Class supports by simply running the deploy function in your schema. It will also let us explicitly specify any relations between the tables, so that we can access the data from inside our program in a natural way. This will also allow functionality, like automatically deleting a person's addresses when we delete that person. At this stage, you can do a quick sanity check and see if the contents of the file lib/AddressBook/Schema/AddressDB.pm have the following:

__PACKAGE__->config(
schema_class => 'AddressBook::Schema::AddressDB',
connect_info => [
'dbi:SQLite:database',
],
);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.61