字段ID:primary和auto_increment.need要填充? [关闭]

If I have a mysql table made in this way:

CREATE TABLE users(
    id int( 11 ) NOT NULL AUTO_INCREMENT ,
    name varchar( 80 ) NOT NULL ,
    surname varchar( 80 ) NOT NULL ,
    address varchar( 80 ) NOT NULL ,
    birth_place varchar( 50 ) NOT NULL ,
    province varchar( 5 ) NOT NULL ,
    birth_date date NOT NULL ,
    sex enum( 'male', 'female' ) NOT NULL DEFAULT 'male',
    treatment enum( 'politrauma', 'cardiologico', 'neurologico' ) NOT NULL DEFAULT 'politrauma',
  PRIMARY KEY ( id )
) ENGINE = MYISAM DEFAULT CHARSET = utf8

and I execute a query to insert data like so:

"INSERT INTO users (name, 
                    surname, 
                    address, 
                    birth_place,
                    province,
                    dt,
                    sex,
                    treatment) value (:name
                                      :surname,
                                      :address,
                                                        :birth_place,
                                                        :province,
                                                        :dt,
                                                        :sex,
                                                        :treatment)"

without fill id in mysql table.this can be cause of error?mysql will fill automatically id with a int value?

as a guess you want:

ALTER TABLE `XXX` CHANGE `id` `id` INT( 11 ) NOT NULL AUTO_INCREMENT ;

primary key defines the attribute so that it is unique for the tuple (row) in the table.

That is the primary keys (you can have multiple primary keys) must be chosen so that combined (on the current row) they can never be the same on any different row.

auto_increment defines that the value in that column is automatically created (increased) when the row is inserted into the table. An attribute does not have to be a primary key to use auto_increment.

So if you want the value to automatically increase you must use auto_increment, otherwize it will be set to null.

You have to add AUTO_INCREMENT attribute after id int(11), otherwise, this will not fill it automatically.