Skip to content
Advertisement

Problem when trying to create a table on SQL DEVELOPER [closed]

I was trying to create a table on SQL Developer, and I had the error called “Missing left parenthesis”, which I think it can be almost anything.

The code is this, and the 3 tables take the same error:

CREATE TABLE cliente (dni VARCHAR2(10), CONSTRAINT dni_pk PRIMARY KEY,
                     nombre_cli VARCHAR2 (50), CONSTRAINT nombre_cli_nn NOT NULL,
                     apellidos VARCHAR2 (50),
                     direccion VARCHAR2 (50),
                     f_nac DATE);

CREATE TABLE producto (cod_prod NUMBER (10), CONSTRAINT cod_prod_pk PRIMARY KEY,
                       nom_prod VARCHAR2 (50), CONSTRAINT nom_prod_nn NOT NULL,
                       precio VARCHAR2 (50));

CREATE TABLE compra (dni_cli NUMBER (10),
                      cod_prod NUMBER (10), CONSTRAINT cod_prod_pk PRIMARY KEY, 
                      cantidad NUMBER (10),
                       CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY(cod_prod) REFERENCES producto(cod_prod),
                       CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY(cod_cli) REFERENCES cliente(dni_cli));

I have tried a lot of things and none has worked. It is probably something silly, so I would be grateful for any help.

Advertisement

Answer

Update: The issues were that the primary key constraint expects you to specify the columnname. I.e. `PRIMARY KEY (column_name). You also used the wrong column names in the Foreign Key constraint declaration. Lastly, there was a difference in column types between the key columns in the 2 initial tables and the referencing table, and you reused the same name for the cod_prod_pk constraint.

CREATE TABLE cliente (
                     dni VARCHAR2(10) NOT NULL,
                     nombre_cli VARCHAR2(50) NOT NULL,
                     apellidos VARCHAR2(50),
                     direccion VARCHAR2(50),
                     f_nac DATE,
                     CONSTRAINT dni_pk PRIMARY KEY (dni)
);

CREATE TABLE producto (cod_prod NUMBER (10) NOT NULL,
                       nom_prod VARCHAR2 (50), 
                       precio VARCHAR2 (50)
                       , CONSTRAINT cod_prod_pk PRIMARY KEY (cod_prod));

CREATE TABLE compra ( dni_cli VARCHAR2(50),
                      cod_prod NUMBER(10) NOT NULL,
                      cantidad NUMBER (10),
                       CONSTRAINT cod_prod_pk2 PRIMARY KEY(cod_prod), 
                       CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY (cod_prod) REFERENCES producto(cod_prod),
                       CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY  (dni_cli) REFERENCES cliente(dni));

Fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement