Click to See Complete Forum and Search --> : SQL Syntax


chestertb
03-16-2004, 05:29 PM
Hi all,

I was wondering whether I have missed something the SQL tutorials...

The syntax for INSERT seems to be

INSERT into filename (field1, field2 etc)
VALUES ('x','y' etc)

The syntax for UPDATE seems to be

UPDATE file SET field1 = 'x',
field2 = 'y'
WHERE somecondition

That just seems clumsy, to have two different structures for what are essentially variations of the same task.

Can I use something like this?

INSERT into filename SET field1 = 'x',
field2 = 'y'

(it returned a syntax error)

It would make the query much easier to read.

Cheers
CTB

wingman8
03-19-2004, 01:18 PM
I've been using SQL for a few years, and as far as I know you can't use the update syntax for inserts or vise versa. Any variation on that would probably not conform to current ANSI SQL standards and would only be supported on a specific DB system. Which is just to say that it may depend on your DB package (MySQL, PostgreSQL, Oracle, etc).

You can, though, leave out the field list on an insert as long as you are inserting into all fields and you order the values the same as they are in the table definition.

buntine
03-19-2004, 01:26 PM
The SQL INSERT statement does not do the same thing as the UPDATE statement.

INSERT will insert a new record into the database whilst UPDATE will update an existing record. Inserting and updating are not the same thing.


INSERT into filename SET field1 = 'x',
field2 = 'y'

This will return an error because you are mixing two SQL statements. SET cannot be used in conjunction with INSERT.

Regards.

chestertb
03-19-2004, 04:03 PM
Thanks guys.
It was just a thought.
I'll stick to the rules.

Cheers
CTB