Loading...

Blogger news

Thursday, August 8, 2013

Best Practice for Insert Statement

I have seen many new developers created insertion SQL statement with a quick and not careful way. That was, not including the column of the table.

After testing this worked perfectly, but this would yield error in the future if there are changes to table structure, say, there are more columns added.

So, it is highly recommended to always add the columns of the table in the insertion SQL statement along with the values data.

For example:
Let's assume that we have  data_table with the following structure.
DATA_TABLE
-------------------
IDNUMBER
NAMETEXT
LAST_NAMETEXT


This insertion SQL statement tend to create the problem in the future.
INSERT INTO DATA_TABLE VALUES(1, 'NAME 1', 'LAST NAME 1')
This statement would be OK at first, but when the structure of the table changes by adding ADDRESS column; this will surely cause problem.

DATA_TABLE
-------------------
IDNUMBER
NAMETEXT
LAST_NAMETEXT
ADDRESSTEXT

If we have created the insertion statement as shown below in the beginning, the problem would not happen.
INSERT INTO DATA_TABLE(ID, NAME, LAST_NAME) VALUES(1, 'NAME 1', 'LAST NAME 1')

0 comments:

Post a Comment

About

 
TOP