Why does it tell me that insert has more expressions than destination columns?

0

I hope you can help me I'm new to this ...

CREATE DATABASE mundial
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    LC_COLLATE = 'Spanish_Spain.1252'
    LC_CTYPE = 'Spanish_Spain.1252'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;


CREATE SEQUENCE usuario_seq;    
    CREATE TABLE usuarios(
    id_usuario SMALLINT PRIMARY KEY DEFAULT NEXTVAL('usuario_seq'),
    usuario VARCHAR(20) NOT NULL,
    contrasena VARCHAR(20) NOT NULL,
    tipo VARCHAR(20) NOT NULL,
    estado BOOLEAN
);

INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('admin','123','administrador',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('AMLO','135','administrador',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('cisco','456','invitado',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('hansolo','789','invitado',TRUE);


CREATE TABLE jugador(
   id_jugador SMALLINT PRIMARY KEY,
   nombre VARCHAR(40) NOT NULL,
   apellidop VARCHAR(40) NOT NULL,
   apellidom VARCHAR(40) NOT NULL,
   fecha_nac DATE NOT NULL,
   id_posicion VARCHAR(10) NOT NULL
);

This is the first error

  

ERROR: the null value for the "player_id" column violates the   restriction not null       DETAIL: The row that fails contains (null, Cristiano, Ronaldo, two saints, 1985-02-05, front).

INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Cristiano','Ronaldo','dos santos','05/02/1985','delantero');
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Francisco Guillermo','Ochoa','Magaña','13/07/1985','portero');
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Neymar','Da silva','Santos','05/02/1995','medio');
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Sergio','Ramos','Garcia','30/03/1986','defensa');
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Arturo','Vidal','Pardo','22/05/1987','defensa');

CREATE TABLE posicion(
    id_posicion VARCHAR(10) PRIMARY KEY,
    des_posicion VARCHAR(50) NOT NULL
);
  

ERROR: INSERT has more expressions than target columns       LINE 1: ... SERT INTO position (des_posicion) VALUES ('goalkeeper', 'defense', ...

INSERT INTO posicion(des_posicion) VALUES('portero','defensa','medio','delantero');
    
asked by Stephen75 New Digital World 08.08.2018 в 17:26
source

2 answers

0

You have several errors in your data model .

I list them here as I have seen and at the end of I propose a correction.

  • Declare the auto-incremental primary keys as SERIAL , that way you will delegate the sequences to the handler and you will not have to worry about anything else.
  • In the jugador table there should be a relation with the id_posicion column in the posiciones table, which can also be auto-incremental and not varchar .
  • In the INSERT INTO of the table jugador you must use the numeric value of the position, not the varchar. That can bring you many mistakes.
  • In the same table jugadores you are entering the dates of birth in the wrong format. You must use the Año-Mes-Dia format whenever you are going to save data in the database, otherwise you will be exposed to erroneous values.
  • Table posicion must be created first than jugador .
  • In the posicion table, you must indicate the% UNIQUE restriction for the des_posicion column
  • In the jugador table you should indicate that the id_posicion column is a foreign key.

The data model would then look like this:

CREATE TABLE usuarios(
    id_usuario SERIAL PRIMARY KEY,
    usuario VARCHAR(20) NOT NULL,
    contrasena VARCHAR(20) NOT NULL,
    tipo VARCHAR(20) NOT NULL,
    estado BOOLEAN
);

INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('admin','123','administrador',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('AMLO','135','administrador',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('cisco','456','invitado',TRUE);
INSERT INTO usuarios(usuario, contrasena, tipo, estado) VALUES('hansolo','789','invitado',TRUE);

SELECT * FROM usuarios;

CREATE TABLE posicion(
    id_posicion SERIAL PRIMARY KEY,
    des_posicion VARCHAR(50) NOT NULL UNIQUE
);


INSERT INTO posicion(des_posicion) VALUES('portero'),('defensa'),('medio'),('delantero');

CREATE TABLE jugador(
    id_jugador SERIAL PRIMARY KEY,
    nombre VARCHAR(40) NOT NULL,
    apellidop VARCHAR(40) NOT NULL,
    apellidom VARCHAR(40) NOT NULL,
    fecha_nac DATE NOT NULL,
    id_posicion INT NOT NULL,
    FOREIGN KEY (id_posicion) REFERENCES posicion (id_posicion)

);


INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Cristiano','Ronaldo','dos santos','1985/02/05',4);
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Francisco Guillermo','Ochoa','Magaña','1985/07/13',1);
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Neymar','Da silva','Santos','1995/02/05',3);
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Sergio','Ramos','Garcia','1986/03/30',2);
INSERT INTO jugador(nombre, apellidop, apellidom, fecha_nac, id_posicion) VALUES('Arturo','Vidal','Pardo','1987/05/22',2);

And to relate positions and players you would do something like this:

SELECT 
    j.id_jugador, 
    j.nombre,
    j.apellidop,
    j.apellidom,
    j.fecha_nac,
    p.des_posicion
FROM jugador j 
    INNER JOIN posicion p ON j.id_posicion=p.id_posicion;

Getting:

id_jugador  nombre      apellidop   apellidom   fecha_nac   des_posicion
----------------------------------------------------------------------------
1           Cristiano   Ronaldo     dos santos  05.02.1985  delantero
2           Francisco   Ochoa       Magaña      13.07.1985  portero
3           Neymar      Da silva    Santos      05.02.1995  medio
4           Sergio      Ramos       Garcia      30.03.1986  defensa
5           Arturo      Vidal       Pardo       22.05.1987  defensa

Test data

Here you can see a DEMONSTRATION and test the data.

    
answered by 08.08.2018 / 18:21
source
1

First of all, I think the alerts that Postgresql sends you are very clear. In the first case, as the column id_jugador is PK, and as it does not have a sequence created it will be generated as null, since you do not give any value in the insert. You will have to create a sequence for this column instead of giving it the type SMALLINT you define it as serial .

In the second case the table posicion has only 2 columns, and in the insert you are giving only 1 which is the description, but in the VALUES you deliver 4 values.

If your intention was to add as a single value it should be like this

INSERT INTO posicion(des_posicion) VALUES('portero'); INSERT INTO posicion(des_posicion) VALUES('defensa'); INSERT INTO posicion(des_posicion) VALUES('medio') INSERT INTO posicion(des_posicion) VALUES('delantero');

    
answered by 08.08.2018 в 17:40