Help me to condense redundant code in exercise 5: "Insert"

I’d like to know if there’s a way to avoid typing"

INSERT INTO celebs (id, name, age)

for each of the celebrities to be added to the table. My first impulse is to type something like this:

INSERT INTO celebs (id, name, age)
VALUES (2, ‘Beyonce Knowles’, 33),
VALUES (3, ‘Jeremy Lin’, 26),
VALUES (4, ‘Taylor Swift’, 26);

The commas are intended to separate multiple entries under the same clause/command. Needless to say, this didn’t work-- hence my posting here.

Just trying to follow the principle of “Do Not Repeat Yourself” with regard to my code. Thanks!

Good policy!

Have a look at this:

Thanks to your link I think I managed to remove the redundant code:

INSERT celebs (id, name, age)
VALUES (1, ‘Justin Bieber’, 21),
(2, ‘Beyonce Knowles’, 33),
(3, ‘Jeremy Lin’, 26),
(4, ‘Taylor Swift’, 26);

SELECT * FROM celebs;

I just removed the clause, “VALUES” from lines 3, 4, and 5. Does this look right to you?

Yes it does. Did it work when you tried it?

I had to reset the exercise for some reason. But once I did that, the code worked properly! Thanks for the alley-oop!

1 Like