Hi.

I'm using php and mysql for a system. I have some questions on INSERT query.

When we are using INSERT, in our query....

i. Is it necessary to include each field included in the table we are
going to insert value with (though some of the field maybe do not
inserted with value) ?

ii. Have to follow the sequence of the fields in database table?

Thanks in advance.:)

*ps: And, I'm sorry if I post this at the wrong place

Recommended Answers

All 5 Replies

when using teh insert statement, the fields that do allow nulls have to be listed in teh statement, with teh correspoding values. So if all the fields in the db were designed to accept a value, then they have to be listed in the statement.

They do not have to be in the same order they are in the database.

Hi
I m making web page and for that i am using mysql and php. In tha i enter data and that is to be stoored in mysql database. I m having a problem that i want add,delete,previous,next,cancel and update button in that. But can't make all buttons work differently.
I want all buttons performing different function.
Pls guide me...
Thanx in advance 2 great mind who'll help..
sry if i posted this in wrong position.

Member Avatar for diafol

@sumit007: yes you did post this in the wrong place - you just hijacked somebody else's thread. Start your own.

Back to the thread
For fields, no you don't have to include every one in INSERT statements. You can rely on the default value of the field when you set up the table. However, do take care with this.

For example if you had a users table with an avatar and status fields, you could leave these to the default values (NULL - no avatar yet and 0 - meaning registered but not confirmed). Actually setting these to NULL and 0 in the initial INSERT query is superfluous (well, IMO).

I'm no expert, but I find the 'SET' method of using INSERT staements useful because I don't have to worry about placing the right value in the right order compared to the fields (if using 'VALUES' method).

e.g.

INSERT INTO mytable SET field1='value1', field9='value9', field6='value6'

You can also use

INSERT INTO my_table(field4, field3, field1, field2) VALUES(2, 2, 5, 1)

The order won't matter here.

Thanks to everyone here. I 'll repair my database as yo guys said (put null as initiate value for those to-be-left-blank field) and hope that there will be no problem. ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.