Thursday, July 9, 2020

HOW TO INSERT, SELECT, UPDATE AND DELETE RECORDS IN SQL SERVER

First, we will create a new table i.e. Employee with columns as "ID", "EMP_NAME", "EMP_CODE", "DEPARTMENT", "SALARY", "CREATED_DATE_TIME", "IS_DELETED". Please see the below screenshot.


The table Employee has been created. Now we will do a operations i.e.
a) INSERT A NEW RECORD IN EMPLOYEE TABLE
b) FETCH RECORDS FROM EMPLOYEE TABLE BY SELECT
c) UPDATE AN EXISTING RECORD IN EMPLOYEE TABLE
d) DELETE A RECORD FROM EMPLOYEE TABLE

a) INSERT NEW RECORD IN EMPLOYEE TABLE: We are inserting a new record into the table by using insert command. The syntax is as follows:

INSERT INTO EMPLOYEE(EMP_NAME,EMP_CODE,DEPARTMENT,SALARY,CREATED_DATE_TIME) VALUES('RAMAN SACHDEVA','EMP_001','DEVELOPMENT',10000,GETDATE()) 
This query is used into the below screenshot. Please have a look into this.



The 1 row afftected. It means one record has been inserted in Employee table.

b) FETCH RECORDS FROM TABLE BY SELECT: After inserting a new record, you will get or fetch a record from Employee table by using Select command. The syntax is as follows:

SELECT * FROM EMPLOYEE



 c) UPDATE AN EXISTING RECORDING IN EMPLOYEE TABLE: When record has been inserted into the table then we have to do some updation for some records. In this case, we are using the Update command to update an existing record. The syntax is as follows:

UPDATE EMPLOYEE SET EMP_NAME = 'RMN SACHDEVA',  WHERE ID = 1



d) DELETE A RECORD FROM EMPLOYEE TABLE: We can delete a record from table by using Delete command. Also we can delete a record from table in two ways:
1) We can delete a record from table as soft deleting process or updating only a IS_DELETED column by using update command. The syntax is:

UPDATE EMPLOYEE SET IS_DELETED = 1 WHERE ID = 1



You will see into the above screenshot that the IS_DELETED column has been updated with 1. 1 means soft delete from table. We can active same record by updated value 0 in IS_DELETED column.

2) We can delete a record permanently from table by using delete command. The syntax is:
DELETE FROM EMPLOYEE WHERE ID = 1



You will see into the above screenshot that an existing records has been deleted permanently from Employee table.

                                   - - - - - - Cheers, Happy to Help! - - - - - -



               

No comments:

Post a Comment

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...