DBMS Lab # 2 DML
DBMS Lab # 2 DML
SQL can be divided into five groups: The Data Query language (DQL), Data Manipulation
Language (DML), the Data Definition Language (DDL), Data Control Language (DCL) and
Transaction control language (TCL).
DQL statements are used for performing queries on the data within schema objects. It allows
getting the data out of the database to perform operations with it.
DDL statements permit database tables to be created or deleted. It also defines indexes
(keys), specify links between tables, and impose constraints between tables.
DCL includes commands such as GRANT and REVOKE which mainly deal with the rights,
permissions, and other controls of the database system.
COMMIT: Commits a Transaction.
ROLLBACK: Rollbacks a transaction in case of any error occurs.
SAVEPOINT:Sets a savepoint within a transaction.
The second form specifies both the column names and the values to be inserted:
5 Tjessem Jakob
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
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!
SELECT column_name(s)
FROM table_name
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
Pettersen Michael
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
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
Note: In some versions of SQL the <> operator may be written as !=
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:
Now we want to select all the persons from the table above, however, we want to sort the
persons by their last name.
We use the following SELECT statement:
ORDER BY LastName
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 2:
Using the UPDATE statement, update the above table for the following values:
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.