Columns do not hit insertion

Asked

Viewed 170 times

0

You’re making a mistake

Column Count doesn’t match value Count at Row 1

Meu código

  • 1

    Can you post the code text? Images is bad for us to respond. Just copy and paste here.

  • You insert data into just three columns and send the database to insert data into four columns, so it doesn’t work! You have to have Insert into (column1, column2) values (valor1, valor2)

  • Show @Renatosilva

3 answers

1

You want to insert 4 values in the table, but the table only has 3 columns.

Something else in the column nome varchar(10) will not contain names with more than 10 letters, so increase this length.

Since the first column is numerical and sequential, just create a column id which automatically increments a value with each new Insert in the table

 CREATE TABLE IF NOT EXISTS `livros` (
  `id` INT NOT NULL,
  `nome` VARCHAR(60) NOT NULL,
  `autor` VARCHAR(60) NOT NULL,
  `codliv` INT(2) NOT NULL,
  PRIMARY KEY (`codliv`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `livros` (`nome`, `autor`, `codliv`) VALUES
('O grande Conflito', 'Ellen White', 1),
('O capital', 'Karl Marx', 1),
('O Manifesto Comunista', 'Karl Marx', 3),
('A Ideologia Alema', 'Karl Marx', 1);

1

You have 3 columns and are trying to insert 4 of them, so the error.

In fact that column codliv it’s weird. If it’s a book code it shouldn’t be repeated, right?

I think what you want is this code to be the primary key, textual descriptions are bad for primary keys since it is difficult to type the same and may even have repetition. The book code should be auto-incremented to ensure uniqueness. And you don’t even need to use it when you insert it, so it’s automatically placed by the database.

Would look like this:

CREATE TABLE IF NOT EXISTS `livros` (
    `codigo` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `nome` VARCHAR(255) NOT NULL,
    `autor` VARCHAR(255) NOT NULL
) ENGINE = MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `livros` (`nome`, `autor`) VALUES
    ('O grande Conflito', 'Ellen White'),
    ('O capital', 'Karl Marx'),
    ('O Manifesto Comunista', 'Karl Marx'),
    ('A Ideologia Alema', 'Karl Marx');

Behold working in the SQL Fiddle. Also put on the Github for future reference.

0

The problem

"Column Count doesn’t match value Count at Row 1"

In Portuguese:

"The column count does not correspond to the value count in row 1"

Solution

INSERT INTO livros (codliv, nome, autor) VALUES (1, 'O grande Conflito', 'Ellen White'), (2, 'O capital', 'Karl Marx'), (3, 'O Manifesto Comunista', 'Karl Marx'), (4, 'A Ideologia Alema', 'Karl Marx');

My suggestion

See by clicking here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.