feature | daily | high gear | we::blog | contact      
 
Search
Feature / 16 Oct 2000 / MySQL types and primary key
Web Search
Anyone can submit a news item, but only members can comment on them! New users, sign up here.
 
  danchan Login  
  Nickname  
   
  Password  
   
  Remember  
   

New openlog - the unbearable lightness of blog - plus free weblogging with comments, easy syndication and no ads!

Get [danchan] for your PDA!

Every daily news item!

10 latest comments

  Archive  
  2001 December
2001 November
2001 October
2001 September
2001 August
2001 July
2001 June
2001 May
2001 April
2001 March
2001 February
2001 January
2000 December
2000 November
2000 October
2000 September
2000 August
2000 July
2000 June
 

MySQL types and primary key

We haven't talked about the primary key directive in the create table command.

create table users
(
    ...
    primary key    (user_id),
    ...
)

This is an instruction to the MySQL "secretary" to file things by user_id. The constraint imposed on a primary key is that each row must have a unique value for the key. Technically, MySQL creates a B-tree to make lookup of a specific row by user_id fast. So this

select * from users where user_id = 2;

is faster than

select * from users where favorite_number = 945;

Is MySQL limited to small data types? Only if you think 4 gigabytes is small. That's what the LongBlob and LongText types can hold.

Let's say we want to create a message system. A simple example of a message row is created with:

create table messages
(
    id           int auto_increment not null,
    user_id      int not null,
    posting_date datetime not null,
    comment_body text
    primary key  (id)
)

This introduces two new SQL types: datetime and text.

The datetime column data is structured like so, "YYYY-MM-DD hh:mm:ss". This way, datetimes can be ASCII sorted into chronological order. To us, it's a string, that's the format we give to MySQL and the format we get out, but internally, it's an 8 byte bitstream.

The text type holds up to 64Kb of data, more than enough for a message.

The user_id column, that's the relational part of Relational Database Management System (RDBMS). The user_id in our example references the id column of the users table. This way we're relating the message to the user who is the author. The table is the structural foundation but this concept of referencing is the functional basis for RDBMS.

Here's an example message row (the message_body can be much longer):

+----+---------+---------------------+--------------+
| id | user_id |        posting_date | message_body |
+----+---------+---------------------+--------------+
|  1 |       3 | 2000-10-10 10:00:00 |      Wassup! |
+----+---------+---------------------+--------------+

Let's say we have a voting system, where users can vote on whether or not the message was worth reading. We'd create a table like this:

create table message_votes
(
    message_id  int not null,
    user_id     int not null,
    vote        enum('good', 'bad') not null,
    primary key (message_id, user_id)
);

In this example, the vote column can contain either the value 'good' or the value 'bad'.

The primary key directive specifies two columns to "file" by. Since primary keys are by definition unique, (message_id, user_id) as a value pair must be unique. This imposes the constraint that each user can only vote on a specific message once.

Also, in this example, MySQL "files" message votes "sorting" by message_id first then by user_id. Which means lookups like:

select * from message_votes where message_id = 3;

are going to be faster than

select * from message_votes where user_id = 2;

But the fastest way to lookup a message vote is:

select * from message_votes where 
    message_id = 3 and user_id = 2;

Now we're ready to use Perl DBI to interface with MySQL.

next 4. Using Perl DBI to interface to MySQL
page 1, 2, 3, 4
 
0 comments
 
  DAYPOP  
Searching the Living Web
Daypop indexes news sites and weblogs every day to give you the latest relevant information.

Weblogging

Super-customizable weblogging with comments
Add comments to your web site or create a weblog of your own!

Web Caching
Accelerate your website!
Improve your website's responsiveness by preloading your site content into a user's browser cache.

Contents
1
Introduction to MySQL (using Perl DBI)
2
Simple MySQL Commands
3
MySQL types and primary key
4
Using Perl DBI to interface to MySQL
Questions? Comments? Send all mail to: dc@danchan.com