Insert, Update, Delete and Select Operation in SQL Server Database

How to Insert, Update, Delete and Select operation in MS SQL Server Database table. Here we will see a syntax with simple example in MS SQL.
In today's article I will show you a simple tutorial with example how you can perform Insert, Update, Delete and select operation in SQL server database. So here I will show you with the help of query to perform crud operation. So, let's connect the Ms SQL server database. Let's create a table, we will perform all insert update delete and select operation in this table only. 

SQL table

Insert

Insert is used for adding or insert the new record to the database table. This we can use to insert single or multiple or bulk insert of records into the database table. 

Syntax

 INSERT INTO [dbo].[TableName]
           ([Column_1]
           ,[Column_2]
           ,[Column_3])
     VALUES
           (<Column_1, DataType,>
           ,<Column_2, DataType,>
           ,<Column_3, DataType,>) 
The above query shows the syntax for SQL insert statement. In this Insert is the keyword for to determine the statement is written for what purpose, and the table name which describe in which table we need to insert the values. Here Column name and the with Values keyword we have defined the column value as per datatype.

Example

Now let's check the example query to insert the record in Database table. Here we will take a table name as "EmpMaster". Now let's check the query to insert the record in database table. 
 INSERT INTO [dbo].[EmpMaster]
           ([Employee_Name]
           ,[Department]
           ,[Salary])
     VALUES
           ('Rakesh'
           ,'dev'
           ,'25000000') 
Above query will insert a single record in "EmpMaster" table. Now let's run the query and check the output.

SQL table record

Update

Update is used for updating the table record as per given condition by using where clause. If we don't user any condition while updating the data at that time query will update all the record against the column value which we are going to update. 

Syntax

 UPDATE [dbo].[TableName]
   SET [Column_1] = <Column_1, datatype,>
      ,[Column_2] = <Column_2, datatype,>
      ,[Column_3] = <Column_3, datatype,>
 WHERE <Search Conditions,,> 
The above query shows the syntax for the update query used in SQL to update the data in a specified table we per given condition. Here we have used "Update" keyword with table name and Set keyword to define the column name and its value which we are going to update, and at the end where clause which hold the condition under which the record will update. 

Example

Now let's check the example query to update the record in Database table. Here we will take a table name as "EmpMaster". Now let's check the query to update the record in database table. Here we will not update value for all columns. Fo updating all column values you need to add the column value after SET keyword.  After WHERE we will put condition with value which describe which value need to be updated.
 UPDATE [dbo].[EmpMaster]
   SET [Employee_Name] = 'Soham'
 WHERE Id=1 
Here in above query, I am going to change the or update the Employee name. Now lets update the record and check the output.

Update Record in SQL

Delete

Delete command is used to remove or delete the record or records from a database table. In Delete command we can delete record by putting the condition. Delete command does not actually delete the record from the database table it just removes the reference of the data.

Syntax

 DELETE FROM [dbo].[TableName]
      WHERE <Search Conditions,,> 
The above query represents the syntax for delete command from a table. To delete a record from a table we will use DELETE command and a where class to delete the record by given condition.

Example

Now let's check and example to delete record from database. Here we will check two set of examples, in first example we will check the try to delete the record without any condition and in second we will add a condition and try to delete the record.  For this we will add two or more records in table.

SQL Table Records


With Condition:

Here we will check the query to delete the record as per given condition. 
 DELETE FROM [dbo].[EmpMaster]
      WHERE Id=1 
Now let's execute the query and check the output. After executing the query, we are able to get only one record and the record having Id=1 is no longer available in the table. 

Table record

Without Condition:
Here we will provide an example which will delete all records from the table. 
 DELETE FROM [dbo].[EmpMaster] 
The above query will delete all the records from the database table. Now lets run the query and check the output.

No Record Table

After executing the query there is no record in the table. 

Select

To select the record or get the record from the SQL data base we use SELECT command. Select command can use to get record with or without condition. To select the record from SQL table we can user wildcard "*" to select all the records and if we want some specific field record, we can specify the column name. Now let's check the syntax for SELECT. 

Syntax

 SELECT [Column_1]
      ,[Column_2]
      ,[Column_3]
      ,[Column_4]
  FROM [dbo].[TableName] 
In above I have defined the column name this will return the record of the specified column name of the table. If we want to get all column without writing all column names, we can simply user wildcard "*". Now let's check example for select.

Example

Select all records
 SELECT [Id]
      ,[Employee_Name]
      ,[Department]
      ,[Salary]
  FROM [dbo].[EmpMaster] 
In above query I have defined the column name. This will return only the specified column name from the table.  Now let's run the query to check the output.

Select all

Select specific records
 SELECT [Id]
      ,[Employee_Name]
      ,[Department]
      ,[Salary]
  FROM [dbo].[EmpMaster]
  Where Id=4 
The above query will return the record which I have Id as 4. Now let's run the query and check the output.

Select Condition Record


Select Using Wild Card
 SELECT *
  FROM [dbo].[EmpMaster] 
In above instead of defining the column name I have used "*" wild card to get all the columns record.  Now let's run and check the output.

Wild Card in Select

Post a Comment