Lab 6 SQL DML (INSERT, UPDATE, DELETE, SELECT INTRODUCTION)
Lab 6 SQL DML (INSERT, UPDATE, DELETE, SELECT INTRODUCTION)
Contents
1. Introduction 3
4. Concept Map 3
4.1. Inserting Data using INSERT INTO Statement 4
4.1.1. Inserting single record 4
4.1.2. Inserting multiple records 4
4.1.3. Inserting Single Record with Specific Columns 4
4.1.4. Inserting Multiple Record with Specific Columns 5
4.1. Modifying Data using UPDATE Statement 5
4.2. Deleting Rows using DELETE FROM Statement 6
5.1. Reading the data from database using SELECT statement. 6
7. Practice Tasks 10
7.1. Practice Task 1 [Expected time = 15mins] 10
7.2. Out comes 11
9. Evaluation criteria 11
10.1. Books 11
Text Book 11
10.2. Slides 11
11. REFERENCES: 12
The oracle_db database should now contain the data that is in the Oracle.sql file. 13
1. Introduction
As we have briefly discussed some of the DML operation in lab 4, now in this lab we will
discussed the DML in detail. You will learn about the INSERT, UPDATE, DELETE and
SELECT INTRODUCTION. INSERT statement is used to insert records in the database,
UPDATE statement is used to update records of tables. Delete statement is used to delete records
from tables. Throughout this lab you will learn how to use these statements.
a) Text Book: Java: Text Book: Database Systems, A practical approach to design,
implementation and management by Thomas Connolly, Carolyn Begg, Addison
Wesley , Fifth Edition,
1. Read URL:
i. https://www.studytonight.com/dbms/
4. Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
afterwards. The examples in the following sections are based on “Oracle.db” database.
All of the operation is performed on Oracle database. First we will import this database then we
have used it. The database is available on data server. The procedure of Import and export are
given in Appendix.
Department of Computer Science, Page 3
C.U.S.T.
Lab 6: SQL DML (INSERT, UPDATE, DELETE, SELECT INTRODUCTION)
You need to list the values in the same order in which the columns are defined in the CREATE
TABLE, separated by commas. For columns of string data type (CHAR, VARCHAR), enclosed
the value with a pair of single quotes (or double quotes). For columns of numeric data type (INT,
DECIMAL, FLOAT, DOUBLE), simply place the number.
For Examples:
To insert a record in a jobs table, job table contain following attribute:
• Job id (varchar(50))
• Job title (varchar(50))
• Min Salary (int)
• Max Salary (int)
For Example:
INSERT INTO jobs (job_id,job_title,min_salory)
VALUES (‘C_PROG’, ‘C_Sharp Programmar‘, 2000),
VALUES (‘J_PROG’, ‘Java Programmer‘, 3000);
Note:
• A column defined with NOT NULL constraint and no DEFAULT constraint, must have a
value i.e. it cannot be NULL.
For Examples:
To modify max_salory of all jobs to “25000”, the following statement can be written:
Department of Computer Science, Page 5
C.U.S.T.
Lab 6: SQL DML (INSERT, UPDATE, DELETE, SELECT INTRODUCTION)
To modify min_salory to 5000 and max_salory to 10000, against all jobs who’s job_id is
IT_PROG, the following statement can be written:
ALTER Command is used to add, delete, modify
the attributes of the relations (tables) in the
UPDATE jobs database. UPDATE Command is used to update
SET min_salary = 5000, max_salary = 10,000 existing records in a database.
WHERE job_id = “IT_PROG”;
For Examples:
To delete all jobs the following statement can be written:
For example,
To select all rows from the table jobs for the columns job_id, and job_title
mysql> SELECT name, price FROM products;
job_id job_title
AC_ACCOUNT Public Accountant
AC_MGR Accounting Manager
5.2.Task-2
Describe the difference between where and having clause.
CAUTION: If the WHERE clause is omitted in the UPDATE statement, ALL ROWS will be
updated. Hence, it is a good practice to issue a SELECT query, using the same criteria, to check
the result set before issuing the UPDATE. This also applies to the DELETE statement in the
following section.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. For practice task you will use the given schema.
When you finish them, put these tasks in the following folder:
\\fs\assignments$
Figure 7: HR Database
7.2.Out comes
After completing this lab, student will be able to insert, update, and delete data from a database.
They will also be able to create complex queries involving sorting, getting distinct records, and
limiting the number of records to be fetched.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1. Books
Text Book
Database Systems, A practical approach to design, implementation and management by Thomas
Connolly, Carolyn Begg, Addison Wesley , Fifth Edition,
10.2. Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\fs\lectures$\
11. REFERENCES:
Appendix
11.2. Exporting and Importing databases
A database, created on MySQL server can be exported and imported to the same or other server.
Database is exported to an SQL dump file. SQL dump file contains database structure as well as
the data. This helps in the following:
• Taking and restoring backup.
• Moving the database to another server.
For Example,
MySQLDUMP -u root -p oracle_db > Oracle.sql
For Example,
MySQL -u root -p oracle_db < Oracle.sql
The oracle_db database should now contain the data that is in the Oracle.sql file.