2009-08-17 13 views
54

No puedo entender el error de sintaxis al crear una clave compuesta. Puede ser un error lógico, porque he probado muchas variedades.Postgres: ¿Cómo hacer las teclas compuestas?

¿Cómo se crean las claves compuestas en Postgres?

CREATE TABLE tags 
    (
       (question_id, tag_id) NOT NULL, 
       question_id INTEGER NOT NULL, 
       tag_id SERIAL NOT NULL, 
       tag1 VARCHAR(20), 
       tag2 VARCHAR(20), 
       tag3 VARCHAR(20), 
       PRIMARY KEY(question_id, tag_id), 
       CONSTRAINT no_duplicate_tag UNIQUE (question_id, tag_id) 
    ); 
    ERROR: syntax error at or near "(" 
    LINE 3:    (question_id, tag_id) NOT NULL, 
         ^

Respuesta

80

Su especificación compuesto PRIMARY KEY ya hace lo que quiere. Omitir la línea que te está dando un error de sintaxis, y omitir la redundancia CONSTRAINT (ya implícita), también:

CREATE TABLE tags 
     (
       question_id INTEGER NOT NULL, 
       tag_id SERIAL NOT NULL, 
       tag1 VARCHAR(20), 
       tag2 VARCHAR(20), 
       tag3 VARCHAR(20), 
       PRIMARY KEY(question_id, tag_id) 
    ); 

NOTICE: CREATE TABLE will create implicit sequence "tags_tag_id_seq" for serial column "tags.tag_id" 
    NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "tags_pkey" for table "tags" 
    CREATE TABLE 
    pg=> \d tags 
             Table "public.tags" 
     Column |   Type   |      Modifiers  
    -------------+-----------------------+------------------------------------------------------- 
    question_id | integer    | not null 
    tag_id  | integer    | not null default nextval('tags_tag_id_seq'::regclass) 
    tag1  | character varying(20) | 
    tag2  | character varying(20) | 
    tag3  | character varying(20) | 
    Indexes: 
     "tags_pkey" PRIMARY KEY, btree (question_id, tag_id) 
+0

¿Cómo implementar un contraint como "RESTRICCIÓN DE no_duplicate_refences de referencia único tag_id a (etiqueta 1, etiqueta 2, etiqueta 3)"? –

+1

@Masi, no creo que entienda lo suficiente de lo que está tratando de modelar aquí, y, para ser sincero, las columnas 'tag1' a' tag3' me sugieren que podría tener más refinamientos de diseño para hacer. Tal vez una pregunta por separado, con una descripción en lenguaje natural de su modelo y algunos registros de ejemplo, ayude. – pilcrow

9

El error que está recibiendo es en la línea 3. es decir, no está en

CONSTRAINT no_duplicate_tag UNIQUE (question_id, tag_id) 

pero antes:

CREATE TABLE tags 
    (
       (question_id, tag_id) NOT NULL, 

he absolutamente ninguna idea de por qué te he puesto ahí - ¿cuál es el propósito? ¿Cuál es la lógica?

De todos modos. La definición correcta de la tabla es como pilcrow mostró.

Y si desea agregar único en la etiqueta 1, etiqueta 2, etiqueta 3 (que suena muy sospechoso), la sintaxis es:

CREATE TABLE tags (
    question_id INTEGER NOT NULL, 
    tag_id SERIAL NOT NULL, 
    tag1 VARCHAR(20), 
    tag2 VARCHAR(20), 
    tag3 VARCHAR(20), 
    PRIMARY KEY(question_id, tag_id), 
    UNIQUE (tag1, tag2, tag3) 
); 

o, si se quiere tener la restricción con nombre según su deseo :

CREATE TABLE tags (
    question_id INTEGER NOT NULL, 
    tag_id SERIAL NOT NULL, 
    tag1 VARCHAR(20), 
    tag2 VARCHAR(20), 
    tag3 VARCHAR(20), 
    PRIMARY KEY(question_id, tag_id), 
    CONSTRAINT some_name UNIQUE (tag1, tag2, tag3) 
); 
Cuestiones relacionadas