Search Postgresql Archives

Re: auto increment within a compound key

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 17, 2006 at 04:33:46PM +0100, Bogdoll, Dieter wrote:
> Is there any way to achieve the same result in Postgresql as the MySQL
> AUTO_INCREMENT does?

Not without an adverse impact on performance.  PostgreSQL's serial
type is just a notational convenience for an integer column that
takes its default value from a sequence; the sequence's value isn't
affected by other columns' values.  You could achieve the effect
you're after with a trigger but you'd have to allow for concurrency.

I see that a table definition such as the one you posted isn't
allowed for InnoDB tables:

mysql> CREATE TABLE foo (
    -> x int AUTO_INCREMENT,
    -> y int,
    -> PRIMARY KEY (y, x)
    -> ) TYPE INNODB;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

It's allowed if you reverse the order of the columns in the primary
key, but then you get the same effect as in PostgreSQL:

mysql> CREATE TABLE foo (
    -> x int AUTO_INCREMENT,
    -> y int,
    -> PRIMARY KEY (x, y)
    -> ) TYPE INNODB;
Query OK, 0 rows affected, 1 warning (0.08 sec)

mysql> INSERT INTO foo (y) VALUES (1), (1), (2);
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM foo;
+---+---+
| x | y |
+---+---+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+---+---+
3 rows in set (0.01 sec)

-- 
Michael Fuhr


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux