An Introduction to Class::DBI - Primary Keys 21

Portably Generating new primary key values

  • Databases handle primary keys differently:

    • Mysql lets you declare a column to be 'autoincrementing'

          CREATE TABLE customer (
              id             INTEGER     NOT NULL AUTO_INCREMENT PRIMARY KEY,
              name           VARCHAR(32) NOT NULL,
              address        VARCHAR(64) NOT NULL,
              email          VARCHAR(64)
          );
      
    • With Postgres you typically declare a sequence and use that.

          CREATE TABLE customer (
              id             INTEGER     NOT NULL PRIMARY KEY,
              name           VARCHAR(32) NOT NULL,
              address        VARCHAR(64) NOT NULL,
              email          VARCHAR(64)
          );
      
          CREATE SEQUENCE customer_id_seq start 1;
      

 

YAPC::Canada << Previous | Index | Next >>
Copyright © 2003 Michael Graham