Академический Документы
Профессиональный Документы
Культура Документы
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.
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:
In other databases you have to create a sequence and use the nextval() to specify
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
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
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:
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.
it will create a sequence named 'test_user_id_seq'; if this name already exists the entire CREATE
TABLE statement will fail.