How to work with auto incrementing columns in mySQL
how would开发者_开发知识库 I use the insert into syntax with mysql columns that are auto incrementing?
for example:
INSERT INTO table (auto, firstname, lastname) VALUES ('Dan','Davidson')
would the above code work and fill in the columns firstname and lastname while having auto auto-increment?
You don't need to list the auto-incrementing column in the field list, it will auto-increment regardless:
INSERT INTO table (firstname, lastname) VALUES('Dan', 'Davidson')
No, the code would not work. The auto-increment column should not be inserted to as the value is dynamically generated. Doing the following:
INSERT INTO table (firstname, lastname) VALUES ("fName", "lName")
Will automatically assign a vlue to the "auto" field.
You can just leave that field null.
INSERT INTO table (auto, firstname, lastname) VALUES (null, 'Dan','Davidson')
or just leave the auto incrementing field out.
INSERT INTO table (firstname, lastname) VALUES ('Dan','Davidson')
精彩评论