Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

TAFILA TECHNICAL UNIVERSITY

Faculty of Engineering
Department of Communication and Computer Engineering

Database Systems Lab


Lab 3
Data Modification Operations

Objectives:
- Introduce the data manipulation capabilities of SQL
o Inserting, deleting, updating data.

Introduction:
This lab introduces the DML part of SQL. Data manipulation operations can be classified
into two categories:
1) Data modification operations: These are the operations that change or modify the
database state, which in turn can be divided into three types:
a. Data insertion: In this operation a whole new record or row is inserted into a
table.
b. Data deletion: In this operation a whole record or row is deleted from a table.
c. Data updating: In this operation the value of an attribute in a row of a certain
table is changed (updated).
2) Data retrieval operations: These are the operations used to read or retrieve data from
the database, which are usually called queries (the plural for query). Of course, retrieving
data does not change the database state.

In this lab, we will work with the data modification commands; “insert”, “delete”, and
“update”. All of these commands are DML commands and cannot be used to modify the
database structure. That is, they cannot be used to add or remove tables or constraints.

Inserting data into a relation in SQL:


A table or a relation is identified by its name, and is described by a list of attributes. For
example, in R(A1, A2, A3, …, An) the table name is “R” and the attributes in the table
are A1, A2, …, An. This information should be known by the user to be able to insert
new tuples in the state of relation R.

There are two ways to insert a new row or tuple in a table. The first one is using the full
version of the “insert” command, in which your command should conform to the
following format:
INSERT INTO <table_name>(A, B, C, …, X) VALUES (a, b, c, …, x);

In this version you need to specify the attribute names first (A, B, C, …, X), then specify
the value for each one of these attributes (a, b, c, …, x). In this case, the order in which
you specify the attributes that will be assigned values can be different from the order of
the attributes in the relation schema. Moreover, you need to make sure that the assigned
values should also follow the order you specified.
For example, for a relation R(A, B, C) you can write the following insert command:
INSERT INTO R(C, B, A) VALUES(c, b, a);

Note that if you use the full version of the insert command, you can choose to enter
values for some of the relation attributes and let the remaining attributes take their default
values. This is not allowed if you use the shortened version of the insert command. The
shortened version assumes that you will conform to the ordering of attributes in the
relation schema. Therefore, you do not need to specify the attributes first, and you can
start specifying value directly, but you need to specify a value for every attribute in the
relation. The command format for the shortened version is as follows:
INSET INTO <table_name> VALUES(a1, a2, a3, …, an);

Updating data in SQL:


The insert operation is used to enter new data records into a relation. However, the update
operation is used to modify on values of attributes in existing records in the relation. The
update operation modifies the value of some attributes in one or more tuples.

The general format for the update command is as follows:


UPDATE <table_name> SET <attribute_name>=<new_value>,
<attribute_name>=<new_value>, … WHERE <update_condition>;

If the “WHERE” clause is omitted then the update takes effect on all the tuples in the
relation. Otherwise, only the tuples that satisfy the condition will be updated.

Deleting data from a relation:


The delete operation is used to remove tuples from a relation. The format for the delete
operation is as follows:
DELETE FROM <table_name> WHERE <delete_condition>;

Similar to the update operation, if the where clause is omitted then all the tuples in the
table are deleted (note that the table is still there, it’s just empty). Otherwise, only the
tuples that satisfy the condition will be deleted.
Exercise:
1) Create the table named EMPLOYEE shown below:
• Fname, Lname, and Address are of type “varchar”. Choose the lengths properly.
• Minit, and Sex are of type “char”.
• SSN is “char(9)”, Bdate is “date”, and DNO is “int”.
• Salary is “decimal(10,2)”.
2) Use the insert command to fill the table with the data shown below
• Insert 4 tuples using the full version of the insert command, and write two of the
commands in you report.
• Insert the remaining 4 tuples using the shortened version of the insert command, and
write two of the commands in your report.

Employee
Fname Minit Lname Ssn Bdate Address Sex Salary Dno
john B smith 123456789 1950-01-09 731 fondren, houston, tx M 30000.00 5
franklin T wong 333445555 1955-12-08 638 vos, houston, tx M 40000.00 5
joyce A english 453453453 1972-07-31 5631 rice, houston, tx F 25000.00 5
ramesh K narayan 666884444 1962-09-15 975 fire oak, humble, tx M 38000.00 5
james E borg 888665555 1937-11-10 450 stone, houston, tx M 55000.00 1
jennifer S wallace 987654321 1941-06-20 291 berry, bellaire, tx F 43000.00 4
ahmad V jabbar 987987987 1969-03-29 980 dallas, houston, tx M 25000.00 4
alicia J zelaya 999887777 1968-01-19 3321 castle, spring, tx F 25000.00 4

3) Give a raise of 10% to all employees.


4) Give a raise of 1000 to all employees with “i” in their first names.

5) Ahmad and Joyce moved to work at department number 3, update the table
accordingly.
6) Delete the rows for employees in department 4.
7) Delete all rows in the table.

Submission:
You are required to submit a report that includes the following:
1) Comparison between the full version and the shortened version of the insert command.
What are the advantages and disadvantages of each?
2) The answers to the steps in the exercise above.
3) Conclusions: what did you learn in this lab?

You might also like