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

Lecture Four_DML with tables_

The document provides an overview of Data Manipulation Language (DML) operations, including INSERT, UPDATE, SELECT, and DELETE statements for managing data in databases. It includes examples of creating tables, inserting rows, updating existing data, and deleting rows while highlighting the importance of integrity constraints. Additionally, it outlines exercises for creating tables and adding primary and foreign key constraints.

Uploaded by

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

Lecture Four_DML with tables_

The document provides an overview of Data Manipulation Language (DML) operations, including INSERT, UPDATE, SELECT, and DELETE statements for managing data in databases. It includes examples of creating tables, inserting rows, updating existing data, and deleting rows while highlighting the importance of integrity constraints. Additionally, it outlines exercises for creating tables and adding primary and foreign key constraints.

Uploaded by

kbjoash
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

LECTURE FOUR

DML with table

02/05/25 DML with tables 1


DML Overview
 This is a language that provides a set of operations to
support the basic data manipulation operations on the
data held in the database.

 There are many types of DML statements, these include;

 INSERT – to add data/new rows to a table

 UPDATE – to make changes to existing data/rows


in a table

02/05/25 DML with tables 3.2


DML Overview cont’d
 SELECT – to query data in the database (could involve
one table or many tables to be covered in another learning unit)

 DELETE – to remove data from a table/remove existing


rows from a table

Therefore some of the operations performed are insertion,


modification, retrieval and deletion.

02/05/25 DML with tables 3.3


Example to be used
 Create a table called emp_table with following attributes:

02/05/25 DML with tables 3.4


Statement for creating emp_table
create table emp_table (empId varchar(20)
constraint emp_empId_nn not null constraint
emp_empid_pk primary key, fName varchar(15),
lName varchar(15) constraint emp_lName_nn not
null, dob date);

02/05/25 DML with tables 3.5


The INSERT operation
 This is used to insert new rows in the relation or table.

 The general syntax of as insert statement is;


INSERT INTO table [(column [,, column….])]
VALUES (value [,value….]);

 Note:
 This statement will insert one row at a time.
 Enclose date and character values in single quotes.
 The list of values is by default in the order that of columns in the
relation.

02/05/25 DML with tables 3.6


The Insert Operation
Two primary ways of adding data to a table:
i. Using the VALUES clause in an INSERT INTO statement [only
one row is added]
INSERT INTO emp_table(dob, empid, fName, lName)
VALUES ('22-jan-88','93/3/3', 'sarah', 'naka');

ii. Using a subquery in an insert into statement [many rows can be


added] this is the same as copying rows from another table.
INSERT INTO emp_table (empid, fname, lname, dob)
SELECT employee_id,first_name, last_name, hire_date
FROM employees
WHERE lower(last_name) like '%k%';

02/05/25 DML with tables 3.7


The Insert Operation Cont’d
 In either case (i.e. using values clause or subquery) – the
order and number of columns listed must match the order
and number of values to be inserted.

 If a column list is omitted then ensure that all values are


listed in the default order of the columns in the table.
Example:
INSERT INTO employees
VALUES (‘998’, 'rich', 'maya', '02-feb-98');

02/05/25 DML with tables 3.8


The Insert Operation Cont’d
 Inserting rows with null values
 When using the implicit method; specifying the columns you are
interesting in, eliminate the columns names of those that you are not
interested in.
Example: INSERT INTO departments (department_id,
department_name )
VALUES ( 30, ‘Purchasing’);

 When using the explicit method; one must specify the NULL
keyword for the columns whose values you are not entering
Example: INSERT INTO departments
VALUES ( 200, ‘Accounts’, NULL, NULL);
02/05/25 DML with tables 3.9
The Insert Operation Cont’d
 Known values must be supplied for columns that are
defined as ‘not null’.
Example:
INSERT INTO employees (employee_id, last_name, hire_date)
VALUES (903,'Okello', '22-jan-88');

 Why does the statement below fail?


INSERT INTO employees (employee_id, first_name, hire_date)
VALUES (311,'Smith', '22-Oct-03');

02/05/25 DML with tables 3.10


The Update Operation
 The Update operation is used to modify existing rows in a
database.

 The general syntax of an update statement is;


UPDATE table
SET column = value [,column = value,…]
[WHERE condition];

02/05/25 DML with tables 3.11


The Update Operation
 Specific value(s) are modified if you specify the WHERE
clause in the UPDATE statement. This is mainly by the
use of the primary key because any other column name
may cause several rows to be updated unexpectedly .
Example:
UPDATE emp_table
SET lname = 'nakato'
WHERE empid = '93/3/3';

02/05/25 DML with tables 3.12


The Update Operation cont’d
 All rows in the table are modified if you omit the where
clause.
Example:
UPDATE emp_table
SET lname= 'nakato';
 Updating rows with values with that are tied to intergrity
constraints usually returns an error when the constraint is
violated.
 Example when updating a foreign key.

02/05/25 DML with tables 3.13


Updating with the use of a Subquery
 A subquery may be used to update a row or rows in a given
relation
Example:
UPDATE emp_copy
SET job_id='SAL_PER',
salary = (select salary
from employees
where employee_id = 100)
WHERE job_id = 'FI_ACCOUNT';

02/05/25 DML with tables 3.14


The Update Operation cont’d
 Updating multiple columns using a subquery;
 The general syntax is
UPDATE table
SET column = ( SELECT column
FROM table
WHERE condition)
[,
column = ( SELECT column
FROM table
WHERE condition)]
[WHERE condition];

02/05/25 DML with tables 3.15


The Delete Operation
 To be able to remove rows from a table, the DELECT
statement is used.

 The general syntax is;


DELETE [FROM] table
[WHERE condition];

02/05/25 DML with tables 3.16


The Delete Operation
 Specific row(s) are deleted if you specify the WHERE
clause in the DELETE FROM statement.
Example:
DELETE FROM emp_copy
WHERE job_id= ‘SAL_PER’;

 Take care, if a WHERE clause is not specified, then all


rows from the table will be deleted.
Example:
DELETE FROM emp_copy;

02/05/25 DML with tables 3.17


Deleting Rows based on Another Table
 This is usually done using a Subquery
Example:
DELETE FROM emp_copy
WHERE department_id = (SELECT department_id
FROM departments
WHERE location_id=2400);

02/05/25 DML with tables 3.18


Deleting Rows: Integrity constraint Error
 You can not delete a row that contains a primary key that is
used as a foreign key in another table.

 Example:
DELETE FROM departments
WHERE department_id = 60;

Error: child record found violation

02/05/25 DML with tables 3.19


Common Integrity Constraint Errors
 Leaving out values for columns with NOT NULL
constraints.
 Deleting a parent record that has dependencies on it.
Example:
DELETE FROM dept_copy
WHERE department_id = 20;
 Inserting or updating a record with a foreign key value
that does not exist in the referenced primary or unique
key of the parent table. Example:
UPDATE emp_copy
SET department_id = 55
WHERE department_id = 110;
02/05/25 DML with tables 3.20
Exercise

The following tables must be created for purposes of


completing the remaining DML operations:
 Create a table called emp_copy with a data storage that is
the same as that of the employees table.
 Create a table called dept_copy with a data storage that is
the same as that of the departments table.
 Compare the structures for dept_copy and departments.
Why the difference?

02/05/25 DML with tables 3.21


Exercise Cont’d
Primary keys and Foreign key constraints to be added to
the tables created:
 Add a primary key constraint on the emp_copy table.
 Add a primary key constraint on the dept_copy table
 Add a foreign key constraint on the dept_copy table
which references the primary key of the emp_copy table.

02/05/25 DML with tables 3.22


Exercise Cont’d – statements for adding
the constraints
 alter table emp_copy add constraint
emp_copy_employee_id_pk primary key(employee_id);
 alter table dept_copy add constraint
dept_copy_department_id_pk primary
key(department_id);
 alter table emp_copy add constraint
emp_copy_department_id_uk foreign key(department_id)
references dept_copy(department_id);

02/05/25 DML with tables 3.23

You might also like