Saturday, 19 November 2011


SQL INSERT Statement

The INSERT Statement is used to add new rows of data to a table.
We can insert data to a table in two ways,
1) Inserting the data directly to a table.

Syntax for SQL INSERT is:

INSERT INTO TABLE_NAME 
[ (col1, col2, col3,...colN)] 
VALUES (value1, value2, value3,...valueN); 
  • col1, col2,...colN -- the names of the columns in the table into which you want to insert data.
While inserting a row, if you are adding value for all the columns of the table you need not specify the column(s) name in the sql query. But you need to make sure the order of the values is in the same order as the columns in the table. The sql insert query will be as follows
INSERT INTO TABLE_NAME 
VALUES (value1, value2, value3,...valueN); 
For Example: If you want to insert a row to the employee table, the query would be like,
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);
NOTE:When adding a row, only the characters or date values should be enclosed with single quotes.
If you are inserting data to all the columns, the column names can be omitted. The above insert statement can also be written as,
INSERT INTO employee 
VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);
Inserting data to a table through a select statement.

Syntax for SQL INSERT is:

INSERT INTO table_name 
[(column1, column2, ... columnN)] 
SELECT column1, column2, ...columnN 
FROM table_name [WHERE condition]; 
For Example: To insert a row into the employee table from a temporary table, the sql insert query would be like,
INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id, emp_name, dept, age, salary, location 
FROM temp_employee;
If you are inserting data to all the columns, the above insert statement can also be written as,
INSERT INTO employee 
SELECT * FROM temp_employee; 
NOTE:We have assumed the temp_employee table has columns emp_id, emp_name, dept, age, salary, location in the above given order and the same datatype.
IMPORTANT NOTE:
1) When adding a new row, you should ensure the datatype of the value and the column matches
2) You follow the integrity constraints, if any, defined for the table.

0 comments:

Post a Comment

TrainingHUB. Powered by Blogger.

Total Pageviews

THE BEST QTP TRAINING INSTITUTE IN HYDERABAD

QTP Training in hyderabad

Sql Tutorial

Popular Posts

Our Facebook Page

TrainingHUB

Followers