0% found this document useful (0 votes)
7 views

Lab Chapter 6

Uploaded by

Tegbaru Tamene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Chapter 6

Uploaded by

Tegbaru Tamene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER SIX

6. INSERT, SELECT UPDATE AND DELETE STATEMENTS


6.1. Insert Statement
INSERT statement is used to insert a single or a multiple record in a table within the database.
We can insert single or multiple records using a single query in MySQL. There are two ways to
insert data in a table:
1. Insert into statement: By specifying column names and without specifying column names.
2. Insert into select statement
The SQL INSERT INTO command is used to insert data in MySQL table.
Syntax: INSERT INTO table_name (field1, field2,...fieldN ) VALUES(value1,value2,...valueN);
Field name is optional. If you want to specify partial values, field name is mandatory.
Syntax for all fields: INSERT INTO table_name VALUES ( value1, value2,...valueN );
If you have to store all the field values, either specify all field name or don't specify any field.
6.1.1. Insert for All Fields
Example: INSERT INTO Employee VALUES (7, 'Dawit', 40000); OR
Example: INSERT INTO Employee (ID, name, salary) VALUES (7, 'Dawit', 40000);
6.1.2. Insert for Partial Fields
In such case, it is mandatory to specify field names.
Example: INSERT INTO emp(id,name) VALUES (8, 'Daniel');
6.1.3. Inserting Multiple Records
Many times developers ask that is it possible to insert multiple rows into a single table in a single
statement. Currently developers have to write multiple insert statement when they insert values
in a table. It is not only boring, also time consuming.
Here, we are going to insert record in the "Student" table of "WKU" database.
Example: INSERT INTO Student (ID, FirstName, Lastname) VALUES (1, 'Aster', 'Tomas'), (2,
'Abel', 'Adam'), (3, 'Solomon', 'Dawit');

6.2. SELECT Statement


The SELECT statement is used to select data from a database. The data returned is stored in a
result table, called the result-set.
SELECT Syntax: SELECT column1, column2, ... FROM table_name;

1
Prepared by: Worku Muluye
Here, column1, column2, ... are the field names of the table you want to select data from. If you
want to select all the fields available in the table, use the following syntax: SELECT * FROM
table_name;
6.2.1. SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a
table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values. The SELECT DISTINCT statement is used to return only distinct
(different) values.
SELECT DISTINCT Syntax: SELECT DISTINCT column1, column2, ... FROM table_name;
6.3. Update Statement
UPDATE statement is used to update data of the table within the database. It is used when you
need to modify the table. The UPDATE statement used to modify the data that is already in the
database. Which rows is to be update, it is decided by a WHERE clause.
Syntax: UPDATE table-name SET [column-name1=value1, …, column-nameN= valueN]
[WHERE condition];
Example: UPDATE student SET FirstName = 'Abebe' WHERE StudentID = '3'
6.3.1. Updating Multiple Fields
If you are going to update multiple fields, you should separate each field assignment with a
comma. SQL UPDATE statement for multiple fields:
Updating Single Column:
Example: UPDATE student SET StudentID = 001 WHERE FirstName = 'Abebe';
This SQL UPDATE example would update the StudentID to '001' in the student table where
FirstName is 'Abebe'.
Updating Multiple Columns:
To update more than one column with a single update statement:
Example: UPDATE students SET FirstName = 'Hana', City = 'Dessie' WHERE LastName = 'Tomas';
Examples: UPDATE student SET FirstName = 'Abebe', LastName = 'Yonas', City=’Bahir Dar’
WHERE StudentID = '3';
This SQL UPDATE statement will change the FirstName 'Hana' and City to 'Dessie' where the
LastName 'Tomas'.

2
Prepared by: Worku Muluye
6.3.2. Update Date
If you want to update a date & time field in SQL, you should use the following query. let's see
the syntax of SQL update date.
Syntax: UPDATE table SET Column-Name = 'YYYY-MM-DD HH:MM:SS' WHERE Id = value;
First, we take a table in which we want to update date and time fields. If you want to change the
first row which id is 1 then you should write the following syntax:
Example: UPDATE table SET EndDate = '2014-03-16 00:00:00.000' WHERE Id = 1
Note: you should always remember that SQL must attach default 00:00:00.000 automatically.

6.4. Delete Statement


The DELETE statement is used to delete rows from a table. Generally, DELETE statement
removes one or more records from a table.
Syntax: DELETE FROM table-name [WHERE condition];
Here table-name is the table which has to be deleted. If you want to remove a specific row from
a table, you should use WHERE condition. The WHERE clause in DELETE statement is optional
here. But if you do not specify the WHERE condition it will remove all the rows from the table.
Syntax: DELETE FROM table-name;
There is not used DELETE statement to delete the database. But, there is used DROP statement
to delete the database. Let's see an "employee" table.
EMPLOYEE
Emp-Name Address Salary
Abebe Addis Ababa 15000
Dawit Adama 18000
Tomas Bahir Dar 20000
Daniel Hawassa 25000
6.4.1. Delete Partial Rows
Let us take an example of Employee. Original table:
EMPLOYEE
EmpID Emp-Name Address Salary
001 Abebe Addis Ababa 15000
002 Dawit Adama 18000
003 Tomas Bahir Dar 20000
004 Daniel Hawassa 25000
If you want to delete an Employee with EmpID 003 from the Employee table, then the SQL
DELETE query should be like this:
DELETE FROM Employee WHERE EmpID >= 003;

3
Prepared by: Worku Muluye
EMPLOYEE
EmpID Emp-Name Address Salary
001 Abebe Addis Ababa 15000
002 Dawit Adama 18000
Resulting table after SQL DELETE query:
6.4.2. Delete All Rows
The statement SQL DELETE ALL ROWS is used to delete all rows from the table. If you want
to delete all the rows from student table the query would be like,
DELETE FROM Employee;
Resulting table after using this query: Select * from Employee;
EmpID Emp-Name Address Salary

Review Questions
1. How to delete the data in the table?
2. What is the difference between delete and truncate statement?
3. How to delete a row from the table?
4. How to delete all the rows of a table?
5. How to delete the view from the database?
6. How to use delete statement with inner join?
7. How can we delete parent table records?
8. What is the difference between select, insert, update and delete statements?
9. Update tables by using join operator?

4
Prepared by: Worku Muluye

You might also like