INSERT INTO/UPDATE TABLE/DELETE TABLE



In this post We will study how to insert records into table,How to delete and how to update records into table using INSERT , UPDATE and DELETE statement. All the below queries are performed in SQL server 2014.

Syntax


INSERT INTO table_name(column_1,column_2)
VALUES(value_1,value_2)

Example


SELECT * from [dbo].[WinForm_1]


Output



INSERT INTO WinForm_1 (Name,Age)
VALUES ('Raj','21').


Output


Note:Here the column ID is the PRIMARY key with AUTO INCREMENT value . PRIMARY  key is the value used to identify records uniquely. When the new record is inserted in table , the value of the ID Attribute is automatically increased to next value. This is done with the help of AUTO INCREMENT Feature.We will learn more about Keys in next coming posts.
We can also omit the column names when we have to enter values in all columns.

Example


INSERT INTO Winorm_1 values('4','karan','32')




Now we will be studying about updating table/value in table

The update table command updates the one or more values in table.

Syntax

UPDATE table_ name
SET column_1=value_1 where condition




Example

See below table we will be updating name value from Raj to Karan.



UPDATEV[WinForm_1]
SET Name='karan' where ID=4.




Output



The value is changed from Raj to Karan.

UPDATE command should be used very carefully. If by any means the where condition is missed , this command will update all the records in the table.


You can also update multiple values by using below syntax.

UPDATE table_ name
SET column_1=value_1 column_2=value_2 where condition......


Example

UPDATE WinForm_1
SET name ='vk' , age='33' where ID=4



The DELETE statement in SQL is used to delete the records from the SQL table.Please note that there is difference between DELETE and TRUNCATE statement. The DELETE statement deletes the no of records from the Table according to given condition. But the TRUNCATE statement deletes all the records/ rows from the table. The TRUNCATE table cannot be used with  WHERE condition.

Syntax

DELETE FROM table_name
WHERE condition



IMP: While deleting records from table , look always for WHERE condition. The WHERE clause specifies which records should be deleted. If you Omit the WHERE condition , all the records from the table will be deleted.

ID Name

State
1 Anoop

Delhi
2 Ankit Pandey

Pune
3

4
Gaurav Kapoor

Arjit Singh
Delhi

Punjab



Consider Above table.

Example

DELETE from Detail
WHERE ID=3


Executing above query will delete third row from the table.

Also you can use the string in WHERE condition to delete specific record. See the below example.

DELETE from Detail
WHERE Name='Anoop'


Executing above query will delete first row from the table.

Deleting all records from the table.

To delete all the rows from the table , we use the following SQL query.

DELETE * from table_name


Above query will delete all records from the table, but the table structure and the schema will remain the same. 
Another alternative command to delete entire records from table, is the TRUNCATE TABLE command. 

Syntax

TRUNCATE TABLE table_name


Example


TRUNCATE TABLE Details


Executing above commnd will delete all the records from table and table structure and schema will also reset.
INSERT INTO/UPDATE TABLE/DELETE TABLE INSERT INTO/UPDATE TABLE/DELETE TABLE Reviewed by LanguageExpert on August 22, 2017 Rating: 5

No comments