[spring 2016] cs-217/224: database systems lab-1 manual · pdf file[spring 2016] cs-217/224:...
TRANSCRIPT
[Spring 2016] CS-217/224: Database Systems
Lab-1 Manual Introduction to the Oracle Database 10g and SQL
V1.0 4/12/2016
GIFT Department of
Computing Science
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 1
Introduction to Lab-1
This lab introduces students to the Oracle 10g object-relational database management system.
The lab discusses the basic operations of working with the Oracle Database 10g using the SQL
Plus command shell. The fundamental concepts of using Structured Query Language (SQL) are
also introduced.
The main topics of this lab include:
1. Introduction to the Oracle Database 10g
2. System Development Life Cycle
3. Data Storage on Different Media
4. Definition of a Relational Database
5. Relational Database Terminology
6. Starting a Database Session
7. Communicating with an RDBMS Using SQL
8. SQL Statements
9. Configuring and Connecting to an Oracle Server using SQL+
10. Creating & Finding Tables in a Database using SQL+
11. Displaying the Table Data
12. Displaying the Structure of Tables
13. Practice SQL Statements
This lab resembles a walkthrough and guides students by giving them hands-on practice of
applying the various operations of working with the Oracle database. This lab also contains
various exercises of using the SQL SELECT statement for fetching data from tables.
Objectives of this Lab
At the end of this lab, students should be able to:
Understand the process of configuring a client to connect to the Oracle database
Understand the usage of SQL Plus command shell for working with the Oracle database
Understand how to import a simple database schema into the user tablespace using SQL
script file
Understand the purpose and the various types of SQL languages
Understand how to use the SQL SELECT statement for listing tables and simple data
fetching from tables
Understand how to list the structure of tables
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 2
1. Introduction to the Oracle Database 10g
Oracle Database 10g is designed
to store and manage enterprise
information. Oracle Database
10g cuts management costs and
provides a high quality of
service. Reduced configuration
and management requirements
and automatic SQL tuning have
significantly reduced the cost of
maintaining the environment.
Oracle Database 10g contributes
to the grid infrastructure
products of the Oracle 10g
release. Grid computing is all
about computing as a utility. If
you are a client, you need not
know where your data resides
and which computer stores it. You should be able to request information or computation on your
data and have it delivered to you.
Oracle Database 10g manages all your data. This is not just the object relational data that you
expect an enterprise database to manage. It can also be unstructured data such as:
Spreadsheets
Word documents
PowerPoint presentations
XML
Multimedia data types like MP3, graphics, video, and more
The data does not even have to be in the database. Oracle Database 10g has services through
which you can store metadata about information stored in file systems. You can use the database
server to manage and serve information wherever it is located.
2. System Development Life Cycle
From concept to production, you can develop a database by using the system-development life
cycle, which contains multiple stages of development. This top-down, systematic approach to
database development transforms business information requirements into an operational
database.
Strategy and Analysis Phase
Study and analyze the business requirements. Interview users and managers to identify the
information requirements. Incorporate the enterprise and application mission statements as
well as any future system specifications.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 3
Build models of the
system. Transfer the
business narrative into a
graphical representation
of business information
needs and rules.
Confirm and refine the
model with the analysts
and experts.
Design Phase
Design the database based
on the model developed in
the strategy and analysis
phase.
Build and Documentation Phase
Build the prototype system. Write and execute the commands to create the tables and
supporting objects for the database.
Develop user documentation, help text, and operations manuals to support the use and
operation of the system.
Transition Phase
Refine the prototype. Move an application into production with user-acceptance testing,
conversion of existing data, and parallel operations. Make any modifications required.
Production Phase
Roll out the system to the users. Operate the production system. Monitor its performance, and
enhance and refine the system.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 4
3. Data Storage on Different Media
Every organization has some information needs. A library keeps a list of members, books, due
dates, and fines. A company needs to save information about employees, departments, and
salaries. These pieces of information are called data.
Organizations can store data on various media and in different formats, such as a hard-copy
document in a filing cabinet or data stored in electronic spreadsheets or in databases.
A database is an organized collection of information.
To manage databases, you need a database management system (DBMS). A DBMS is a program
that stores, retrieves, and modifies data in databases on request. There are four main types of
databases: hierarchical, network, relational, and (most recently) object relational.
4. Definition of a Relational Database
A relational database uses relations or two-dimensional tables to store information.
For example, you might want to store information about all the employees in your company. In a
relational database, you create several tables to store different pieces of information about your
employees, such as an employee table, a department table, and a salary table.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 5
5. Relational Database Terminology
A relational database
can contain one or
many tables. A table
is the basic storage
structure of an
RDBMS. A table
holds all the data
necessary about
something in the real
world, such as
employees, invoices,
or customers.
The diagram shows
the contents of the
EMPLOYEES table or
relation. The numbers
indicate the following:
1. A single row (or
tuple) representing all
data required for a
particular employee.
Each row in a table should be identified by a primary key, which permits no duplicate rows. The
order of rows is insignificant; specify the row order when the data is retrieved.
2. A column or attribute containing the employee number. The employee number identifies a
unique employee in the EMPLOYEES table. In this example, the employee number column is
designated as the primary key. A primary key must contain a value, and the value must be
unique.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 6
3. A column that is not a key value. A column represents one kind of data in a table; in this
example, the data is the salaries of all the employees. Column order is insignificant when storing
data; specify the column order when the data is retrieved.
4. A column containing the department number, which is also a foreign key. A foreign key is a
column that defines how tables relate to each other. A foreign key refers to a primary key or a
unique key in the same table or in another table. In the example, DEPARTMENT_ID uniquely
identifies a department in the DEPARTMENTS table.
5. A field can be found at the intersection of a row and a column. There can be only one value in
it.
6. A field may have no value in it. This is called a null value. In the EMPLOYEES table, only
those employees who have the role of sales representative have a value in the
COMMISSION_PCT (commission) field.
6. Starting a Database Session
Oracle uses a client/server model of communication. An Oracle Database server is continually
running, waiting for client requests. The server processes the request and returns the result to the
client.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 7
7. Communicating with an RDBMS Using SQL
Using SQL, you can communicate with the Oracle server. SQL has the following advantages:
Efficient
Easy to learn and use
Functionally complete (With SQL, you can define, retrieve, and manipulate data in the
tables.)
8. SQL Statements
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 8
9. Configuring and Connecting to an Oracle Server using SQL+
The SQL+ application (Command Shell) allows for the creation of databases and other objects,
as well as to interact with an Oracle Database Server. The SQL+ program is a simple console
application that provides a command line interface to the Oracle Database system. You enter text
commands in the SQL+ program to view and manipulate data.
For our lab, we will be connecting with a database named “cs217db” using SQL+ as follows:
1. Perform a configuration sequence as directed by the instructor to establish a connection with
the Oracle Database Server. This is done via the Oracle Network Assistant wizard.
Note: Carefully observe and note this process. You will need to perform this when working
in the labs.
2. Launch the SQL+ application from the Application Development Menu under the Oracle
home menu
3. Enter the User ID, Password, and Host String as specified by the instructor
Note: The Host String refers to the database name, i.e., cs217db for this course
4. If the credentials are correct, then you will get the SQL prompt, meaning that you have
successfully connected to the Oracle Database
5. We are now ready to issue SQL commands to the Oracle Database
10. Creating & Finding Tables in a Database using SQL+
Every user created in the Oracle Database has a predefined tablespace for storing tables and
other objects. You can easily see the tables in a tablespace using SQL+ by using the SQL
SELECT statement as follows:
SELECT * FROM tab;
Here, tab, or TAB is a special data dictionary table having the details of the tables owned by a
user. Observer the output of the above command. We will see the SELECT statement in detail in
the later labs. Don’t forget to terminate the SQL statements with the semi-colon.
For the purpose of this and coming labs, we will create a simple schema having 4 tables and
populate the tables using some data by using the “Sample.sql” file. This is simple SQL script
file containing SQL statements for creating and populating our sample tables.
First, copy this file to the root of your Z: drive, and then issue the following command at the
SQL+ prompt:
@z:\Sample.sql
This assumes that you have placed the file at the root of your Z-drive.
Now, again issue a
SELECT * FROM tab;
command to list the tables created. Carefully, observe the output.
Don’t worry about the content of this script file. You will learn about all the SQL statements
involved in later labs.
Lab-1 Manual 2016
Spring 2016 CS-217: Database Systems (Lab) Page 9
11. Displaying the Table Data
There are now four tables, named EMP, DEPT, SALGRADE, and BONUS in your tablespace as
indicated by the last SELECT statement. To list the table data, use the SELECT statement as
follows:
SELECT * FROM emp;
SELECT * FROM dept;
SELECT * FROM bonus;
SELECT * FROM salgrade;
What output do you observe from the above commands, especially when SELECTing the
BONUS table?
12. Displaying the Structure of Tables
You can easily view the details of a table structure by using the DESCRIBE statement. Try out
the following:
DESCRIBE emp;
Or,
DESC emp;
What output do you see here? Do you see any data types being listed? Can you relate this to the
structure of a C++ class? What do you think about the NOT NULL constraint? Why it is listed
here?
Now, go ahead and explore the structure of the other three tables.
You can now take rest as you have done a lot in this first lab!
13. Practice SELECT Statements
You are expected to practice what you have learned in this lab exercise. You are expected to be
able to perform the following:
Establishing a connection with the Oracle Database by configuring the Network
Assistant Wizard
Create the sample schema by calling a SQL script file
List the tables and table data
Finally, list the table structures