Insert values in a row with auto-increment

1

I need to know how to insert data in the row of a table in which one of the fields is a self-increment. The table, called 'employees', already has about 30 records and the 'employee code' is the auto-increment column, which goes by the number 30.

I have tried to introduce the following:

insert into empleados values(default, 'perico', 'palotes', 'de dios', 4587, '[email protected]', 'MADR-ES', 3, 'Representante ventas')

Specifying: the first column of the table, being a self-increment, I thought of putting a value "default" but it seems that it does not work, and put a constant '31', which is the next number that should appear I do not think it's the right thing to do.

EDIT: I can not put a null, the column is not null.

It has also occurred to me that it can work if I specify all the columns except this one and I enter their values, but I think it is not the best solution and there must be something else that can be done.

What should I put in the first value of the insert statement so that the auto-increment field is automatically filled in. Is there a keyword for the field to auto-fill?

    
asked by Alvaro Tauroni 03.08.2016 в 10:22
source

3 answers

2

According to the MySQL documentation, it is recommended to insert NULL:

link

  

An integer or floating-point column can have the additional attribute AUTO_INCREMENT. When you insert a value of NULL (recommended) or 0 into an indexed AUTO_INCREMENT column, the column is set to the next sequence value. Typically this is value + 1, where value is the largest value for the column currently in the table. AUTO_INCREMENT sequences begin with 1.

The value "0" works unless you have the NO_AUTO_VALUE_ON_ZERO activated.

It also specifies the documentation that it is not necessary to specify a value for that field when inserting data in the other fields of the row, and that you can insert NULL if the field is NOT NULL or assign a specific value (10, 25, 1000, ...) and the sequence will have the largest value in the column as a new value.

link

  

No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers, unless the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled. If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers. When you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the largest column value.

    
answered by 03.08.2016 / 12:59
source
0

Option 1, pass it a NULL:

insert into empleados values(NULL, 'perico', 'palotes', 'de dios', 4587, '[email protected]', 'MADR-ES', 3, 'Representante ventas')

Option 2, as you comment, you can specify the fields to insert, you can skip the id field since it is as auto-incremental you will not need to worry about it:

insert into empleados (campo1,campo2,campo8) values('perico', 'palotes', 'de dios', 4587, '[email protected]', 'MADR-ES', 3, 'Representante ventas')
    
answered by 03.08.2016 в 10:33
0

When the field is defined as auto-increment in the DB you do not have to indicate it, you just have to pass the fields that are not auto-increment and the safest way to do it is ...

string query = "INSERT INTO Empleados(campo1, campo2, campo3) Values(@c1, @c2, @c3)";
using(SqlCommand cmd = new Sqlcommand(query, conexion)){
    cmd.Parameters.AddWithValue("@c1", 'perico');
    cmd.Parameters.AddWithValue("@c2", 'palotes');
    cmd.Parameters.AddWithValue("@c3", 'de dios');
    cmd.ExcecuteNonQuery();
}
    
answered by 17.10.2016 в 19:23