DBMS Lab 2 2nd Part DML Statements
DBMS Lab 2 2nd Part DML Statements
(Part 2)
SQL DML Commands
SQL can be divided into four parts: The Data Manipulation Language (DML), the Data
Definition Language (DDL), Data Control Language (DCL) and Transaction control
language (TCL).
The DDL part of SQL permits database tables to be created or deleted. It also define indexes
(keys), specify links between tables, and impose constraints between tables.
5 Tjessem Jakob
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies
which record or records that should be updated. If you omit the WHERE clause, all records
will be updated!
5 Tjessem Jakob
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies
which record or records that should be deleted. If you omit the WHERE clause, all records
will be deleted!
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
Note: Be very careful when deleting records. You cannot undo this statement!
OR
SELECT * FROM table_name
Example
The "Persons" table:
Now we want to select the content of the columns named "LastName" and "FirstName" from
the table above.
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
LastName FirstName
Hansen Christ
Svendson Tove
Pettersen Michael
Tip: The asterisk (*) is a quick way of selecting all columns! The result-set will look like this:
Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select only the distinct values from the column named "City" from the table
above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Example
The "Persons" table:
P_Id LastName FirstName Address City
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
Operator Description
= Equal
IN If you know the exact value you want to return for at least one of the columns
Now we want to select only the persons with the first name equal to "Tove" AND the last
name equal to "Svendson":
We use the following SELECT statement:
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first
name equal to "Christ":
We use the following SELECT statement:
Example
The "Persons" table:
TASK 1:
Create the following table using SQL and using the INSERT INTO command, insert the
following values in the table created.
Ikram 09 DIP
Hassan 10
TASK 3:
Using the DELETE statement, delete the record for the student having name Akram and
Ahsan in the above table. Also delete the record for the course having course code=1001.
TASK 4:
Select distinct values from the above table for the last three columns.
TASK 5:
Sort the above table in descending order by their name.