Compound key in database schema I'm trying to define a compound key for a custom database table.

My first insert goes through OK but the following inserts fail because the PID value is the same.

What I'm trying to achieve is a state where the uniqueness of each insert is determined by a combination of the PID, QID and UID values of each insertion.

The following is my schema in my install script:

function module_schema() {

  $schema = array();

  $schema['module_records'] = array(
    'description' =&gt; '',
    'fields' =&gt; array(
      'pid' =&gt; array(
        'description' =&gt; 'PID',
        'type' =&gt; 'int',
        'unsigned' =&gt; TRUE,
        'not null' =&gt; TRUE,
      ),
      'uid' =&gt; array(
        'description' =&gt; 'User ID',
        'type' =&gt; 'int',
        'unsigned' =&gt; TRUE,
        'not null' =&gt; TRUE,
      ),
      'qid' =&gt; array(
        'description' =&gt; 'QID',
        'type' =&gt; 'int',
        'unsigned' =&gt; TRUE,
        'not null' =&gt; TRUE,
      ),
      'first' =&gt; array(
        'description' =&gt; 'First option',
        'type' =&gt; 'text',
        'length' =&gt; 255,
        'not null' =&gt; FALSE,
      ),
      'second' =&gt; array(
        'description' =&gt; 'Second option',
        'type' =&gt; 'text',
        'length' =&gt; 255,
        'not null' =&gt; FALSE,
      ),
    ),
    'primary key' =&gt; array('pid'),
    'indexes' =&gt; array(
      'uid' =&gt; array('uid'),
      'qid' =&gt; array('qid'),
    );

  return $schema;
}
You may define 'type' => 'serial' if you want to auto increment your primary key.