If you recall from an earlier tutorial, we learned about the perl Object-Relational Mapping module called Rose::DB. The next step is to actually create the Perl objects that will represent our database tables. This requires a little shift in the way you think about relational database design, or maybe you're already doing this - it's important to think of each table as an object rather than just a collection of data.
As an example, let's create a simple database table in MySQL that will hold a list of Contacts for a simple address book. We'll call the table contacts and the object itself will be the singular version - contact. I'm assuming here that you already know how to create a simple database, mine is called rose_db_test for this tutorial. If you're looking for further information on MySQL, you might take a look at the PHP/MySQL Guide's How To Create a Database in MySQL Tutorial. Let's create a simple table on our rose_db_test database now and insert some dummy data:
CREATE TABLE `rose_db_test`.`contacts` (That gives us a few contacts in the database for us to work with, and they should be user ID's 1 & 2 respectively depending on the status of your auto_increment column.
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL
);
INSERT INTO `rose_db`.`contacts` (
`id` ,
`name` ,
`email`
)
VALUES (
NULL , 'Larry', 'larry@thestooges.com'
), (
NULL , 'Curly', 'curly@thestooges.com'
);
Next we'll take a look at actually setting up the Rose::DB Object to access the data!
