How to add data to a table in sql
In this post how to add data to a table in sql , we will discuss about
inserting rows in sql tables. This is a very common question how to add data to a table in sql . Adding data means inserting
rows in sql tables. Before we insert data into tables , We first have to create
tables in sql database.
If
the tables are already created, then before you enter data into a SQL database table, you have to know the name of
the table as well as the various columns of the table. Along with names of the
columns, we you have to be familiar with the data types of the various columns
of the table.
Is
this possible to know the name and data types of various columns of the table,
yes this is quite simple to know the name of the columns and their data types. This can be done by
using desc table command.
Desc
Table Command
The Desc Table command of SQL is to describe the structure of
the table.
For example if we supply
command like this : Desc employee;
This
will display the whole structure of the employee table. It will display the
name of the columns along with column specifications.
When
you came to know the names of various columns and their data types, their
widths and constraints, we can add rows to the table.
While inserting rows into
tables, you have to take care with number of columns and their data types.
|
How to add data to a table in sql |
How to add data to a
table in sql
To to add data to a table in sql, we have
to use insert into command. The use of sql insert statement is quite simple.
sql insert
statement
The sql insert statement is
helpful in adding new rows to a table in sql.
The
General Syntax of sql insert
statement
Insert into <table name> values (value1, value 2,…);
Here table name is the name of the
table in which you wants to add rows.
Suppose the name of the table
is employee, the insert statement will be like this;
Insert
into employee values (101, ‘SAM’);
After execution of the
statement, the following message will be displayed
I
row(s) created.
Here, you have to be care full,
101 will be added to first column of the table and SAM will be added to the
second column of the table.
It is necessary to take care of
data types of while inserting rows in a table.
With one insert into command,
you can add only one row in a table. For Multiple rows , we have to use insert
into command multiple times.
Few more examples of Insert
into Command.
Given a table named employee
with columns ecode->char(4), ename->varchar2(30), ecity->varchar2(30)
Employee Table
Insert into employee values (‘E001’,’SAM’,’Florida’);
Here the first value E001 will
be stored in first column of the table, second value SAM will be stored in
second column of the table and third value Florida will be stored in the third column of the table.
In this case, user must be aware about the sequence of the columns of employee
table. If by mistake he supply insert into command as Insert into employee values
(‘SAM’,’E001’,’Florida’);
In this case employee name will
be stored in employee code column and employee code will be stored in employee
name column.
To avoid such problems, supply
insert into like this:
Insert into employee(ecode, ename, ecity ) values (‘E001’,’SAM’,’Florida’);
|
How to add data to a table in sql
|
This will add a row in database
table, in this case E001 will be stored in ecode, SAM will be stored in ename
and Florida will be stored in ecity column of the employee table.
Main thing here is , you don't
have to remember the sequence of columns in the table. You can supply the above
insert into command as : Insert
into employee (ename, ecode, ecity ) values (‘SAM’,’E001’,’Florida’);
Here you can see, we had
altered the sequence of values, we had written SAM as first value and E001 as
second value, we had also altered the sequence of column names in the above
statement, ename is given first and then ecode.
How to insert multiple rows in SQL Table
To insert multiple rows in a
table, we have to supply
insert into command as
Insert into employee values
(&eocde, &ename,&ecity);
When you supply this command,
the following output will be generate.
Enter Value for Ecode :
Here you have to supply value
for ecode like ‘E001’
After this you will get –
Enter Value for Ename :
Here you have to supply value
for ename like ‘SAM’
After this you will get –
Enter Value for Ecity :
Here you have to supply value
for ecity like ‘Florida’
After this following message
will be generated –
1 row(s) created.
After this sql prompt will
come.
Sql>
Here you have to write run
Sql>run;
Run command of sql executes the
last executable statement stored in Sql buffer. In this case it is :
Insert
into employee values (&eocde, &ename,&ecity);
Again you have to supply new
values, you can repeat this step for as many times as you want.
So this was all about How to add data to a table in sql
Summary
We hope this post How to add data to a table in sql is informative and helpful to you. If you wish you can ask any
question regarding this post. We will appreciate social media share of this
post by you.