Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Modifying Data with SQL Adding Data to a Database Review and Practice

John Grillo
John Grillo
30,241 Points

SQL, NULL's and auto-increment -- is typing 'null' necessary?

My question is straightforward. In the review, Chalkley says that using the NULL statement can trigger auto-incrementing columns. My question is, do you have to? Maybe it's different in SQLite, but I recall being able to just INSERT INTO the columns that didn't auto-increment, omitting the ID column in his example and it handles that automatically [since it inserts into the next row].

So do I have to write NULL into those insert statements, or will it auto-increment if I omit that column altogether?

5 Answers

Yes, you can omit the id column from the field list and NULL from the values list. The editor accepted this:

INSERT INTO products (name, description, price) VALUES ("x", "y", 23)
John Grillo
John Grillo
30,241 Points

yes, the editor accepted this and it's how i passed but i wanted to confirm that it was considred acceptable practice to do so.

Boban Talevski
Boban Talevski
24,793 Points

Just to add that in case you don't specify the column names, then you need to enter NULL for the id column.

So while this passes ok

INSERT INTO actors (name) VALUES ("John Smith");

This doesn't

INSERT INTO actors VALUES ("John Smith");

and you would have to use NULL like this

INSERT INTO actors VALUES (NULL, "John Smith");

Thanks for the great explanation

Hey John,

In an example on W3schools.com, they state that you do not have to specify a value for the "ID" column if it is auto-incrementing.

Code example from them below.

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen');
fady mohamed
fady mohamed
5,972 Points

Hey Tyler, I know it's been a year, but thought i'd mention for anyone reading this. In that example the column names have been specified and that is why you can omit the ID column name and value as it would be taken as NULL. In the case that column names are not specified you must enter a value for all the columns and if no value is to be entered then NULL must be used. At least that is what I have come to understand.

David Corrales
David Corrales
4,698 Points

For some reason this isn't working, would someone be able to find out why?

INSERT INTO movies (id, title, year_released, genre) VALUES (NULL, E.T. the Extra Terrestrial, 1982, Sci-Fi);

I'm not sure if your quotes got stripped by the comment parser, but if you don't have quotes around your strings, that will cause a problem.

INSERT INTO movies (id, title, year_released, genre) VALUES (NULL, "E.T. the Extra Terrestrial", 1982, "Sci-Fi");

writing the id-value sure is optional yet usefull if you want to insert test data with high ids , or are manually fiddeling around with the values inside the database