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?
To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.
INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.
To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With