Вы находитесь на странице: 1из 3

PostgreSQL

PostgreSQL & PHP Tutorials - PostgreSQL Enum Types

An enum datatype allows only certain values to be entered into a particular field (for example
- 'red', 'blue', 'yellow', 'purple' for favourite colours).

Postgresql doesn't have an enum datatype, but we can emulate it quickly and easily.

Instead of an enum type we can set up a CHECK CONSTRAINT - this tells postgresql to make
sure that the value we are entering is valid.

CREATE TABLE person (


personid int not null primary key,
favourite_colour varchar(255) NOT NULL,
CHECK (favourite_colour IN ('red', 'blue', 'yellow', 'purple'))
);

Now that's done, let's check it works:

test=# insert into person(personid, favourite_colour) values (1, 'red');


INSERT 0 1

Now for something not in the list:

test=# insert into person(personid, favourite_colour) values (2, 'green');


ERROR: new row for relation "person" violates check constraint
"person_favourite_colour_check"

While this works, I don't think it is the best design. First of all, you can move the
check constraint to a the domain level. However, by far the best approach is to create a color
table:

create table color


(
color text primary key
);

insert into color values ('red'); etc

in the referencing table you just do:

create table person


(
personid int not null primary key,
color text references color
);

now you get the benefits of constraint checking but also:


1. can control color list by inserting/deleting records
2. can allow RI to cascade chagnes if you want to change color text
3. have a color table you can query for UI pulldown boxes, etc

mysql to postgres and auto_increment

In other databases you have to create a sequence and use the nextval() to specify

that a field is to be auto incremented. While this may appear to be more

cumbersome than the mysql way - it certainly gives greater control. In postgresql

there is a shortcut; it allows you to define a field as SERIAL and the sequence

is automatically created for you.

thus messageId int(11) NOT NULL auto_increment in mysql becomes

messageId SERIAL in postgres - and that's quite a bit less typing than what you

have to do in mysql.

Another point worth mention about the column definition is that mysql has

several types of integers and with any of them you can define the number of bytes

to be used. Even though postgress has a smallint and bigint in addition to the

standard integer you will rarely need to use anything other than integer and due

to a quirk in type casting if you use smallint your query may not make use of the

indices as you expect.

Another datatype that will cause you trouble is the blob - blobs are called bytea in

postgresql while in mysql you find them in many flavours including mediumblob

and blob
Declare the type SERIAL instead of that long string o crap you gotta use in most databases and it
works fine, like so:

create table test(


name text,
address text,
user_id serial);

And you've got an autoincrementing field named user_id.

I'd echo the recommendation to use SERIAL; just don't forget that the mechanism PosgreSQL uses
to create the serial data type is to create a named sequence.

Given the field specification

create table test(


...
user_id serial

it will create a sequence named 'test_user_id_seq'; if this name already exists the entire CREATE
TABLE statement will fail.

Вам также может понравиться