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

Ben Os
Ben Os
20,008 Points

Is it a bug in the "Modifying Data with SQL" course ?

* We have an eCommerce database and it has a products table. It has the columns id, name, description and price.

Add a new product to the products table. Use any valid values you want. All columns are required. The id column is auto incrementing. *

Here's a link to the quesion


My answer is:

INSERT INTO products (id, name, description, price)
VALUES (2, "sesso", "desco", 10)

I also tried

INSERT INTO products (id, name, description, price)
VALUES ( , "sesso", "desco", 10)

Since it is said in the question that "id" is auto-incremented...

Yet I keep getting the error:

** Was expecting 4 entries in the products table. There's only 3. **

4 Answers

ben, when a field is auto-incrementing you don't include it in INSERT statements. Try this instead:

INSERT INTO products ( name, description, price)
VALUES ( "sesso", "desco", 10)

When this record is inserted the DBMS will automatically add the next id number to the record.

Ben Os
Ben Os
20,008 Points

It worked; I didn't know it should not be included in the column-sequence as well.

Thanks !

chase singhofen
chase singhofen
3,811 Points

forgot to put quotes on names/words

Strange I included an auto-incrementing field in part two of the challenge and it worked.

ben, think of it this way. If it's auto-incrementing you don't want to try to add it yourself.

There is another way to do it, as discussed in the video. You could use NULL

INSERT INTO products ( id, name, description, price)
VALUES (NULL, "sesso", "desco", 10)

but this is not usually considered "best practice".

Andrew did go over this in the video:

Finally, because the ID and the returned_on are NULL, meaning that

5:41 they are absent, we don't have to specify them in our columns or values.

5:53 The database will automatically know that there should be NULL because you didn't

5:58 specify them.

5:59 When we run the statement, we see the final sixth row entered

6:03 with the auto incremented ID and the returned_on as NULL.

Here ID was auto-increment, returned_on wasn't. Happy coding!

Ben Os
Ben Os
20,008 Points

I agree, I thinked that writing it in the column-sequence is BUT NOT in the value sequence is what's needed. My small mistake was vanished when I removed it from the column-sequence as well as you suggested.

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points
INSERT INTO products ( name, description, price)
VALUES ( "asd", "ddsd", 10)

is my impression or there is a bug. the code for this sample is asking you to remove the closing semicolon to mark complete but if you use that anywhere else it will fail

because is not the same : INSERT INTO books (title, author, genre, first_published) VALUES ("The Circle", "Dave Eggers", "Science Fiction", 2013)

to INSERT INTO books (title, author, genre, first_published) VALUES ("The Circle", "Dave Eggers", "Science Fiction", 2013);

semicolon makes a difference best regards