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 trialJohn Grillo
30,241 PointsSQL, 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
jcorum
71,830 PointsYes, 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)
Boban Talevski
24,793 PointsJust 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");
Vijeth Krishna
1,381 PointsThanks for the great explanation
Tyler Brandt
4,729 PointsHey 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
5,972 PointsHey 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
4,698 PointsFor 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);
Daniel Phillips
26,940 PointsI'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");
patrick wollner
1,821 Pointswriting 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
John Grillo
30,241 PointsJohn Grillo
30,241 Pointsyes, the editor accepted this and it's how i passed but i wanted to confirm that it was considred acceptable practice to do so.