Matt
Matt

Reputation: 1531

inserting values into specific rows with SQL

I Have a table of bike details and I want to add different prices to different bike types using SQL queries, but what I have gives me a syntax error:

INSERT INTO bike (full_day)
VALUES (10)
WHERE bike_type = 'mens_hybrid';

What is wrong with this code?

Upvotes: 24

Views: 128869

Answers (1)

ruakh
ruakh

Reputation: 183381

An INSERT statement is only for creating completely new records, not for populating data into existing ones. What you need is an UPDATE statement, which updates an existing record:

UPDATE bike SET full_day = 10 WHERE bike_type = 'mens_hybrid';

(Note: below is an earlier version of this answer, from before it was clear to me what the original poster was trying to do. I'm leaving it here because multiple people who originally answered this question did not seem to notice the problem with writing INSERT ... VALUES (...) WHERE, which makes me think that this explanation might be useful to some people coming across this question.)


That statement doesn't make sense. An INSERT can't take a WHERE clause, because WHERE refers to existing records, and an INSERT creates new ones.

(To forestall potential confusion: there exists such a thing as INSERT INTO ... SELECT ..., where the records to insert are determined by a SELECT query rather than by a VALUES expression, and in that case the SELECT query can, of course, have a WHERE clause. But in no case does the WHERE clause belong to the INSERT statement directly.)

Upvotes: 68

Related Questions