Thursday, June 18, 2015

Inserting data into SQL table

Go through Create table script in SQL post to know about how to create the table.

In order to to insert the data into table we will use INSERT command.
Now, we are going to insert data into Employee table that we have created in Create table script in SQL post.

There are alternative ways to insert data into table:
Alternate1:
  INSERT INTO dbo.Employee 
  (
[EmpID]
,[FirstName]
,[LastName]
,[Salary]
  )
  VALUES 
  (
1
,'Anil'
,'Kumar'
,10000
  )
This is the best way to insert data into our table.
Because with this script we can identify which data we are inserting into which column.
We can also change the order of columns in insert script and can insert data accordingly.
Alternative2:
  INSERT INTO dbo.Employee 
  VALUES
  (
1
,'Anil'
,'Kumar'
,10000
  )
In this way we should know what is the ordering of columns inside the table in advance. 
As per the ordering of the columns it will insert the data into table.
Alternative3:
INSERT INTO dbo.Employee 
SELECT 
[EmpID]
,[FirstName]
,[LastName]
,[Salary] 

FROM dbo.Employee 

Inserting multiple rows at a time:
If you want to insert multiple values at a time them you can do it as shown below:
INSERT INTO dbo.Employee 
  VALUES
  ('1', 'Anil', 'Kumar', 10000)
  ,('2', 'Suresh', 'T', 10000)
  ,('3', 'Kumar', 'Anil', 10000)

In this way, you are selecting all the data available in Employee table and then reinserting it into Employee table.
Here i have taken the example of selecting data from Employee table but you can select from any table and insert it into Employee table.

If any column in Nullable then you can choose to ignore that column in your script, it will automatically insert NULL values in those columns but we should definitely insert some data into NOT NULL columns.

Labels: , , , , , , , , ,

Tuesday, June 16, 2015

Introduction to SQL

SQL:

SQL - Structured Query Language, is the language to query the database.

SQL is similar to English
 
You might be thinking that it is difficult to learn SQL.
But if you know how to speak English you can easily learn SQL.

Before learning how to write SQL, first we will try to convert our question into English.

Let's say, “Identify all the employees whose salary is greater than $10,000”

Identify – SELECT
all the employees – EmployeeID
Salary greater than $10,000 – Salary > 10000

Now we will try to convert our question into SQL query.

After combining all keywords into single statement:
SELECT EmployeeID Salary > 10000

Now only thing left for us is, where can we get this information that we can tell to SQL by using FROM clause.

After adding the clause our query becomes:

SELECT
        EmployeeID
FROM
       Employee
WHERE
       Salary > 10000

Here WHERE clause is used to provide the filtering criteria, and we need only those whose salary greater than 10000

Hope you understood how simple it is to write a query in SQL.

In next post we will try out different queries to get the information related to employees.


Labels: , , ,