Click to See Complete Forum and Search --> : Specify all fields with INSERT?


LibertyOrDeath
July 9th, 2009, 03:22 PM
Hi all,

Apologies if this is a n00b question. I'm learning SQL peripherally from a Python textbook so I don't have much experience of the language. My question is this:

If I'm inserting a new record into a table, do I have to specify all fields with the INSERT statement? For example, say that I have an "employee" table in my database with the columns empid, firstname, lastname and phonenumber. If I want to insert a new record, do I have to specify all columns or can I omit some and not initialise them for that record?

If I were to use the command

"INSERT INTO employee (empid, lastname, phonenumber) VALUES (23, 'Smith', '123-4567')"

, would this throw an error because I haven't specified the firstname column? If not, does the firstname field for that record get initialised to some default value for that data type or would it be set as null and an error is then thrown if I try to access it.

I'm asking this as a general question so I'd also be interested to know whether it differs between different SQL implementations, i.e. some will let you omit columns in an INSERT statement and others won't.

Thanks :)

dannystommen
July 9th, 2009, 05:51 PM
If the column has a constraint that it cannot be null, then it would result in exception. If not, the default value will be inserted.