Code is copied!
Insert, Update & Delete - MySQL
Insertion in table
In SQL,INSERT INTO statement is used for inserting new record in a table in database
Syntax:
INSERT INTO < table name>(column1, column2, column1,.....)
VALUES(< value1>,< value2>,< value3>,.......)
Lets see some examples how we can insert data in table.
INSERT INTO < table name>(column1, column2, column1,.....) VALUES(< value1>,< value2>,< value3>,.......)
Inserting records in a table name Student
These following SQL statement will insert a new record in the 'Student' table with column name as
StudentNo,Class,Name,Game,
Grades,SUPW,Grade2.
For the creation of table named as Student,click on this link given
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(10,7,'Sameer','Cricket','B','Photography','A');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(11,8,'Sonu','Tennis','A','Gardening','C');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(12,7,'Archit','Basket Ball','C','Photography','B');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(13,9,'Sanya','Swimming','D','Cooking','D');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(14,7,'Sam','Hockey','B','Literature','C');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(15,10,'Shinu','Cricket','A','Photography','A');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2) values(10,7,'Sameer','Cricket','B','Photography','A');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(11,8,'Sonu','Tennis','A','Gardening','C');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2)
values(12,7,'Archit','Basket Ball','C','Photography','B');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2) values(13,9,'Sanya','Swimming','D','Cooking','D');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2) values(14,7,'Sam','Hockey','B','Literature','C');
insert into Student(StudentNo,Class,Name,Game,Grades,SUPW,Grade2) values(15,10,'Shinu','Cricket','A','Photography','A');
Inserting records in a table name ClubMembers
These following SQL statement will insert a new record in the 'ClubMembers' table with column name as
Coach_id,Coach_name,Age.
For the creation of table named as club,click on this link given
insert into ClubMembers(Coach_id,Coach_name,Age)
values(1,'Kia',35);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(2,'Samay',30);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(3,'Archita',34);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(4,'Sohan',45);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(5,'Samira',30);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(6,'Shinnay',32);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(7,'Soham',31);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(8,'Sonali',35);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(9,'Shailvika',37);
insert into ClubMembers(Coach_id,Coach_name,Age)
values(10,'Yasho',36);
insert into ClubMembers(Coach_id,Coach_name,Age) values(1,'Kia',35);
insert into ClubMembers(Coach_id,Coach_name,Age) values(2,'Samay',30);
insert into ClubMembers(Coach_id,Coach_name,Age) values(3,'Archita',34);
insert into ClubMembers(Coach_id,Coach_name,Age) values(4,'Sohan',45);
insert into ClubMembers(Coach_id,Coach_name,Age) values(5,'Samira',30);
insert into ClubMembers(Coach_id,Coach_name,Age) values(6,'Shinnay',32);
insert into ClubMembers(Coach_id,Coach_name,Age) values(7,'Soham',31);
insert into ClubMembers(Coach_id,Coach_name,Age) values(8,'Sonali',35);
insert into ClubMembers(Coach_id,Coach_name,Age) values(9,'Shailvika',37);
insert into ClubMembers(Coach_id,Coach_name,Age) values(10,'Yasho',36);
Inserting records in a table name Customer
These following SQL statement will insert a new record in the 'Customer' table with column name as
CustomerID,Name,Location.
For the creation of table named as Customer,click on this link given
insert into Customer(CustomerID,Name,Location)
Values(101,'Shrey','Hazratganj');
insert into Customer(CustomerID,Name,Location)
Values(102,'Rashi','Alambagh');
insert into Customer(CustomerID,Name,Location)
Values(103,'Khushi','Rajajipuram');
insert into Customer(CustomerID,Name,Location)
Values(104,'Abhay','Rajajipuram');
insert into Customer(CustomerID,Name,Location) Values(101,'Shrey','Hazratganj');
insert into Customer(CustomerID,Name,Location) Values(102,'Rashi','Alambagh');
insert into Customer(CustomerID,Name,Location) Values(103,'Khushi','Rajajipuram');
insert into Customer(CustomerID,Name,Location) Values(104,'Abhay','Rajajipuram');
Inserting records in a table name Sales
These following SQL statement will insert a new data in the 'Sales' table with column name as
SalesID,CustomerID,Amount.
For the creation of table named as sales,click on this link given
insert into sales(SalesID,CustomerID,Amount)
Values(1,102,500);
insert into sales(SalesID,CustomerID,Amount)
Values(2,103,1000);
insert into sales(SalesID,CustomerID,Amount)
Values(3,101,900);
insert into sales(SalesID,CustomerID,Amount)
Values(4,104,800);
insert into sales(SalesID,CustomerID,Amount)
Values(5,102,800);
insert into sales(SalesID,CustomerID,Amount) Values(1,102,500);
insert into sales(SalesID,CustomerID,Amount) Values(2,103,1000);
insert into sales(SalesID,CustomerID,Amount) Values(3,101,900);
insert into sales(SalesID,CustomerID,Amount) Values(4,104,800);
insert into sales(SalesID,CustomerID,Amount) Values(5,102,800);
Updating record in table
Here,UPDATE statement is used to update existing records in a table.If we want to update a particular
record,we
use "WHERE" clause along with "UPDATE" statement.
And if we dont use the WHERE Clause, it will affect entire row in a table
Syntax:
UPDATE < table_name>
SET col=val,col1=val1,col=val2,.....
WHERE condition;
Lets see some examples to update records in the following tables.
UPDATE < table_name> SET col=val,col1=val1,col=val2,..... WHERE condition;
Updating records in a table named Student
In the following statement we will update first Student(Studentno) class and game
update Student
set class=11,game="football"
where StudentNo=10;
Now update Student game as hockey and supw as Drawing where StudentNo is 13 and Student name as Sanya
update Student
set Game="Hockey",SUPW="Drawing"
where StudentNo=13 and Name="Sanya";
update Student
set class=11,game="football"
where StudentNo=10;
update Student
set Game="Hockey",SUPW="Drawing"
where StudentNo=13 and Name="Sanya";
Updating records in a table named clubMember
In the following statement we will update coach age as 34 and pay as 4000 where coach id is 7 and coach name
is
Soham
update club
set age=34,pay=4000
where Coach_id=7 and Coach_name="Soham";
update club
set age=34,pay=4000
where Coach_id=7 and Coach_name="Soham";
Deleting records in a table
Here,Delete statement is used to Delete existing records in a table.If we want to delete a particular
record,we
use "WHERE" clause along with "DELETE" statement.
And if we dont use the WHERE clause, it will delete all the records in a table
Syntax:
DELETE < table_name>
[WHERE < predicate>];
DELETE < table_name> [WHERE < predicate>];
Deleting records in a table named Student
In the following statement we will delete the records where StudentNo is 15 and name is "Shinu"
delete from Student where StudentNo=15 and Name="Shinu";
Now to delete the entire records from a table Student we write
delete from Student;
delete from Student where StudentNo=15 and Name="Shinu";
delete from Student;
Deleting records in a table named ClubMembers
In the following statement we will delete the records where name as Sonali and age as 35
delete from clubmembers
where Coach_id=8 and Coach_name="Sonali";
Now to delete the entire records from a table coachmembers we write
delete from clubmembers;
delete from clubmembers
where Coach_id=8 and Coach_name="Sonali";
delete from clubmembers;