Problem with id AUTO_INCREMENT in SQL

Asked

Viewed 1,657 times

1

I created a table in Mysql Workbench with the following settings:

CREATE TABLE teste (
id INT AUTO_INCREMENT,
nome VARCHAR(50) NOT NULL,
email VARCHAR(40) NOT NULL
);

And when I try to insert data into the table using:

INSERT INTO teste VALUES
('Meu nome', '[email protected]');

Gives error because I do not put the id, because I believed it was not necessary since the id is AUTO_INCREMENT. I tried to put the first INSERT INTO with the id and the others without the id, but it gives the same error. In this case, I am doing something wrong or every time I insert data into the table through INSERT INTO I have to insert the id too?

  • A hint, put tbm to the query of INSERT

  • If you omit column names in Insert yes you should pass a value to the id column

1 answer

3


Omitting the names of the columns of the Insert means that the values will be passed in the same order.

INSERT INTO teste VALUES ('Meu nome', '[email protected]');

This Internet is understood as:

INSERT INTO teste (id, nome, email) VALUES ('Meu nome', '[email protected]');

See what value is specified for id is meu nome logo does not even match the data type (int).

The solution to this is to specify the columns and their values or pass null for the auto increment column.

  • Thank you for the answer! Solved my problem! :)

Browser other questions tagged

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