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


#1

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!


#2

Good policy!

Have a look at this:
http://stackoverflow.com/a/452934


#3

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?


#4

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


#5

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