Professional Documents
Culture Documents
INFS1603 Lab Manual (v1.11)
INFS1603 Lab Manual (v1.11)
Lab Manual
Document Update:
References:
The following referencing materials were used to create this Oracle Lab Manual:
File Name:
UNSW SISTM Oracle Lab Manual v1.11
Copyright:
This Oracle Lab Manual is created for the Information Systems courses, offered by
the School of Information Systems, Technology and Management, the University
of New South Wales, Sydney, Australia.
ii
UNSW
SISTM
Lab Manual
Table of Contents
Table of Contents ...................................................................................................................................... iii
Table of Figures ....................................................................................................................................... viii
Table of Tables ......................................................................................................................................... xii
Introduction to Oracle and Relational Database ................................................................................... 13
History of Oracle ................................................................................................................................... 13
History of SQL ...................................................................................................................................... 13
SQL Standards ....................................................................................................................................... 13
Oracle 11g ............................................................................................................................................. 14
Information Systems Courses ................................................................................................................ 15
Data Modelling ...................................................................................................................................... 15
Relational Database ............................................................................................................................... 16
A Table .................................................................................................................................................. 16
Oracle SQL Statements.......................................................................................................................... 17
Lab/Tutorial Arrangement ..................................................................................................................... 18
Introduction to Oracle SQL Developer .................................................................................................. 19
SQL Developer Layout and Functionality ............................................................................................. 20
Connecting to a Database ...................................................................................................................... 21
Instructions to Connect to a Database ............................................................................................... 21
Reference .......................................................................................................................................... 22
Open a Database ............................................................................................................................... 22
Executing SQL Statements in Oracle SQL Developer .......................................................................... 23
Summary................................................................................................................................................ 24
Lab Week 2 Basic SQL SELECT statements ....................................................................................... 25
2.1 Objectives ........................................................................................................................................ 25
2.1.1 Reference ................................................................................................................................. 25
2.1.2 Database .................................................................................................................................. 25
2.2 SELECT Statement .......................................................................................................................... 25
2.2.1 Selecting All Data from a Table .............................................................................................. 25
2.2.1.1 Example: Selecting All Data from a Table ....................................................................... 25
2.3 Selecting a Column or Columns from a Table ................................................................................. 26
2.3.1.1 Example: Selecting One Column Only ............................................................................. 26
2.3.1.2 Example: Selecting More Than One Column ................................................................... 26
2.3.2 Viewing Table Structure .......................................................................................................... 27
2.3.2.1 Example: Find the Column Names of a Table .................................................................. 27
2.3.3 Null Values .............................................................................................................................. 27
Difference between CHAR(n) and VARCHAR2(n) ........................................................................... 28
2.4 SELECT <Column Alias> Statement .............................................................................................. 29
2.4.1.1 Example: -- Demonstrate Four Common Alias Methods.................................................. 29
2.5 SELECT DISTINCT Statement....................................................................................................... 30
2.5.1.1 Example: Using Distinct ................................................................................................... 30
2.5.2 Use DISTINCT for Two or More Columns ............................................................................. 31
2.6 SELECT <Concatenation> and <Literal Strings> Statement .......................................................... 32
2.6.1.1 Example: Using Concatenation and Literal Strings .......................................................... 32
2.6.1.2 Example: Single Quotation Mark ..................................................................................... 33
2.7 Arithmetic Operations ..................................................................................................................... 34
2.7.1.1 Example: Arithmetic Operation ........................................................................................ 34
Lab Week 3 Restricting Rows and Sorting Data .................................................................................. 35
3.1 Objectives ........................................................................................................................................ 35
3.1.1 Reference ................................................................................................................................. 35
3.1.2 Database .................................................................................................................................. 35
3.2 SELECT <WHERE clause> Statement ........................................................................................... 35
3.2.1 Numeric, Character Strings and Dates ..................................................................................... 35
3.2.1.1 Example: Numeric Values ................................................................................................ 35
3.2.1.2 Example: Character Strings .............................................................................................. 36
3.2.1.3 Example: Date Values ...................................................................................................... 36
3.3 Logical Operators ............................................................................................................................ 37
iii
UNSW
SISTM
Lab Manual
iv
UNSW
SISTM
Lab Manual
v
UNSW
SISTM
Lab Manual
vi
UNSW
SISTM
Lab Manual
Appendix C Instructions to connect to Oracle SQL Developer Data Modeler ................................. 142
vii
UNSW
SISTM
Lab Manual
Table of Figures
viii
UNSW
SISTM
Lab Manual
ix
UNSW
SISTM
Lab Manual
x
UNSW
SISTM
Lab Manual
xi
UNSW
SISTM
Lab Manual
Table of Tables
Table 2-1 Data Types ...................................................................................................... 28
Table 2-2 Arithmetic Operations .................................................................................... 34
Table 3-1 Logical Operators ........................................................................................... 37
Table 3-2 Mathematical Comparison Operators ............................................................. 38
Table 3-3 Other Comparison Operators .......................................................................... 38
Table 3-4 Evaluation Order ............................................................................................. 48
Table 4-1 Type of Function............................................................................................. 51
Table 4-2 Case Conversion Functions ............................................................................ 51
Table 4-3 Character Manipulation Functions ................................................................. 52
Table 4-4 Table of Common Number Functions ............................................................ 53
Table 4-5 Table of Common Other Functions ................................................................ 56
Table 5-1 Definition of Algebra Operators ..................................................................... 62
Table 5-2 Desription of the Set Operators ...................................................................... 77
Table 6-1 Group 13 and 14 ............................................................................................. 81
Table 6-2 Average Mark for all students ........................................................................ 82
Table 6-3 Average at Group Level.................................................................................. 82
Table 6-4 Finding the average mark with WHERE clause and HAVING clause .......... 83
Table 6-5 Using WHERE Clause and HAVING Clause ................................................ 83
Table 7-1 Table of Types of Constraint .......................................................................... 99
Table 9-1 Five common types of Subquery .................................................................. 120
Table 9-2 ALL and ANY Operator Combinations ....................................................... 122
Table 10-1 Four Types of Views .................................................................................. 131
xii
UNSW
SISTM
Lab Manual
History of SQL
Dr. E. F. Codd published the paper, “A Relational Model of Data for Large Shared Data
Banks," in June 1970 in the Association of Computer Machinery (ACM) journal,
Communications of the ACM. Codd’s model is now accepted as the definitive model for
relational database management systems (RDBMS). The language, Structured English
Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd’s
model. SEQUEL later became SQL (still pronounced “sequel”). In 1979, Relational
Software, Inc. (now Oracle) introduced the first commercially available implementation
of SQL. Today, SQL is accepted as the standard RDBMS language.
SQL Standards
Oracle strives to comply with industry-accepted standards and participates actively in
SQL standards committees. Industry-accepted committees are the American National
Standards Institute (ANSI) and the International Organization for Standardization (ISO),
which is affiliated with the International Electrotechnical Commission (IEC). Both
ANSI and the ISO/IEC have accepted SQL as the standard language for relational
databases. When a new SQL standard is simultaneously published by these
organisations, the names of the standards conform to conventions used by the
organisation, but the standards are technically identical.
The latest SQL standard was adopted in July 2003 and is often called SQL:2003 (revised
in 2006). Oracle 11g is conformed to the Core SQL:2003 standards (see
http://docs.oracle.com/cd/B28359_01/server.111/b28286/ap_standard_sql003.htm#i771
9). The latest standard is SQL:2008.
13
UNSW
SISTM
Lab Manual
Oracle 11g
Today, organisations need to support multiple terabytes of information for users who
demand fast and secure access to business applications round the clock. Oracle database
is widely used in many large corporations in Australia. The database systems must be
reliable and must be able to recover quickly in the event of any kind of failure.
Oracle Database 11g is designed along the following feature areas to help organisations
manage infrastructure grids easily and deliver high-quality service:
Manageability: By using some of the change assurance, management automation,
and fault diagnostics features, the database administrators (DBAs) can increase their
productivity, reduce costs, minimise errors, and maximise quality of service. Some
of the useful features that promote better management are Database Replay facility,
the SQL Performance Analyzer, and the Automatic SQL Tuning facility.
High availability: By using the high availability features, you can reduce the risk of
down time and data loss. These features improve online operations and enable faster
database upgrades.
Performance: By using capabilities such as SecureFiles, compression for online
transaction processing (OLTP), Real Application Clusters (RAC) optimisations,
Result Caches, and so on, you can greatly improve the performance of your
database. Oracle Database 11g enables organisations to manage large, scalable,
transactional, and data warehousing systems that deliver fast data access using low-
cost modular storage.
Security: Oracle Database 11g helps organisations protect their information with
unique secure configurations, data encryption and masking, and sophisticated
auditing capabilities. It delivers a secure and scalable platform for reliable and fast
access to all types of information by using the industry-standard interfaces.
Information integration: Oracle Database 11g has many features to better integrate
data throughout the enterprise. It also supports advanced information life-cycle
management capabilities. This helps you manage the changing data in your database.
14
UNSW
SISTM
Lab Manual
Data Modelling
The following steps are taken to develop from a conceptual model to implementation of
a database:
You will use Entity Relationship (ER) modelling technique to model the data based on a
conceptual model. Tables will be normalised and created in a database using the ER
diagrams created from the ER modelling.
Oracle SQL Developer Data Modeler is a data modeling and database design tool, which
allows you first to create an ER model, and then use this model to create tables in a
database. One of the premium features of SQL Data Modeler is allowing reverse-
engineering of an ER model, i.e. an ER model can be built from a list of selected tables.
This allows a database developer to check the linkages and constraints of tables in an
existing data model.
15
UNSW
SISTM
Lab Manual
Relational Database
Oracle is a relational database as defined by Codd. A relational database uses relations
or two-dimensional tables to store data as shown in the following diagram:
A Table
A relational database contains one or many tables. A table is the basic storage structure
of an RDBMS (Relational Database Management System). A table is made up of rows
and columns. The below diagram shows the contents of an employee table:
16
UNSW
SISTM
Lab Manual
3. A column represents one kind of data in a table; in this example, the data is the
salaries of employees.
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 this example,
department_id uniquely identifies a department in the departments table.
5. A field or data value can be found at the intersection of a row and a column. Each
individual field can only have one value.
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. The others have a null value.
17
UNSW
SISTM
Lab Manual
Lab/Tutorial Arrangement
(1) The tutor will start by demonstrating and discussing the lab materials.
(2) The SQL script created for the demonstration can be downloaded from
Blackboard. You are encouraged to run the script yourself to ensure you
understand the concept. You are also encouraged to experiment and make
changes to the script yourself - this will help you to understand the SQL
statements. If you have any questions, you can ask your tutor.
(3) The lab exercises will help you to develop your SQL knowledge and skills.
You are not expected to finish all the questions in the lab but you are expected
to finish the lab exercises as part of your 10-hour workload for the course.
18
UNSW
SISTM
Lab Manual
The most important regions of the SQL Developer window are highlighted:
1. The list of available database connections. Please note 1a. indicates which database
the SQL commands you have entered will be executed in.
2. The SQL worksheet where SQL commands are entered.
3. The execute button, which allows you to run the SQL commands you have entered.
4. The results panels which show you the results of your SQL commands in different
formats.
19
UNSW
SISTM
Lab Manual
SQL commands need to be run within the context of a specific database. The following
section describes the layout and functionality of the SQL Developer application in more
detail, the process for connecting to a database and the process for executing SQL
commands.
Apart from the standard options of File, Edit and Help, the other options on the Menus
as shown in above are summarised as:
(a) View: Contains options that affect what is displayed in the Oracle SQL
Developer interface
(b) Navigate: Contains options for navigating to various panes and for executing
subprograms
(c) Run: Contains the Run File and Execution Profile options that are relevant when
a function or procedure is being executed, debugged or profiled
(d) Versioning: Provides integrated support for the following versioning and source
control systems such as: Concurrent Versions System (CVS) and Subversion
(e) Tools: Invokes SQL Developer tools such as SQL*Plus, Preferences, and SQL
Worksheet
Some of the common options on the Menu bar can also be executed using short-cut key
(for example, ‘Ctrl-C’ to copy) or by clicking an icon on the menu.
20
UNSW
SISTM
Lab Manual
The icons on the SQL Worksheet menu bar in Figure I-6 are used to execute and
manipulate the SQL statement(s) that you have entered into the SQL worksheet.
Note the number associated with each icon. The functionality of each button, using the
number as the key, is described:
1. Execute Statement (Ctrl-Enter): Execute the statement where the cursor is
located in the ‘Enter SQL Statement’ box
2. Run Script (F5): Execute all statements in the ‘Enter SQL Statement’ box by
using the Script Runner
3. Autotrace (F6): Generate trace information for the statement
4. Execute Explain Plan (F10): Generate the execution plan, which you can see
by clicking the Explain tab.
5. Commit (F11): Write any changes to the database and ends the transaction
6. Rollback (F12): Discard any changes to the database, without writing them to
the database, and ends the transaction.
7. Unshared SQL Worksheet
8. Change Case: Change word to Uppercase, Lowercase or InitCap
9. Clear (Ctrl-D): Erase the statement or statements in the ‘Enter SQL Statement’
box
10. SQL History (F8): Display a dialog box with information about SQL
statements that you have executed
Connecting to a Database
There are two sample databases which you will use in the lab:
(i) Human Resources (HR) database (provided by Oracle) and
(ii) JustLeeBooks database (provided by Casteel1).
The lab materials are written based on the HR database, and the lab and homework
exercises are based on the JustLeeBooks database. You can find the data structures of
these two databases in Appendix A.
1
Casteel, Joan, “Oracle 11g: SQL, 2010, Cengage Learning.
21
UNSW
SISTM
Lab Manual
Reference
Apart from the instructions given in this manual, you can also find an overview of
Oracle SQL Developer in Casteel Appendix B, and full user’s guide in Oracle SQL
Developer User’s Guide (which you can download from Blackboard or Oracle website).
Open a Database
For the purposes of this example we will be connecting to the HR database, but the same
procedure is used to connect to other databases. Click on HR database icon highlighted
in Figure I-7 and SQL Developer will try to connect you to the HR database. It will ask
you for a password, which will be provided to you. The other database you will use for
the lab exercises is JustLeeBooks database. The tables for JustLeeBooks are also listed
in HR database and all the tables begin with ‘ZJLB_’.
Once you have connected to the database, you can get more details about the database
by clicking the ‘+’ button. Figure I-8 shows we have drilled down to the region table.
22
UNSW
SISTM
Lab Manual
After you have connected to the HR database, for example, you can enter the SQL
statements in the SQL Worksheet as shown in Figure I-10.
Once you are satisfied with the SQL statements. In Step 3, you can execute the
statements by either clicking the Execute Statement icon (or Ctrl-Enter) or Run Script
icon (or F5). The differences between the two outputs are shown in Figure I-11. On the
left hand side, clicking Execute Statement icon will generate a Spreadsheet like output –
a query result - whereas on the right hand side, Run Script icon will generate a plain text
output – a script output – and, moreover, you can save the script output.
23
UNSW
SISTM
Lab Manual
Figure I-11 Differences Between the Execute Statement and Run Script Output
You can save your SQL statements to a new or an existing file by clicking one of the
two icons on the menu bar as shown in Figure I-12. The file type you select must be
‘.sql’.
Summary
In summary, there are four basic steps to run SQL statements in Oracle SQL Developer
as shown in Figure I-4:
Step 1: Connect to the desired database, in the diagram the HR database is selected.
Please note 1a will show the current database you are connected to.
Step 2: Enter your SQL statement.
Step 3: Click on the Run Script icon to execute your SQL statement.
Step 4: The results of your executed SQL statement.
24
UNSW
SISTM
Lab Manual
2.1.1 Reference
Casteel Chapter 2
2.1.2 Database
The database used in this lab is HR database.
25
UNSW
SISTM
Lab Manual
FROM table_name;
26
UNSW
SISTM
Lab Manual
(a) Name
Column names of a table.
(b) NULL values
Null is a special value that is used to represent “value unknown” or “value
inappropriate”. Null is not same as a space (‘’) or a zero (0).
The question is “why do we need a NULL value?”
For example, last summer, one student turned up to his examination but did not
write anything. In this case, the student was awarded a mark of zero (0). On the
other hand, a student did not turn up to his examination. No mark was awarded
for his examination, i.e. the examination mark column will have a Null value.
27
UNSW
SISTM
Lab Manual
Depending on which output option is selected in the Oracle SQL Developer, Null value
is presented differently. In the Query Output (see diagram below), you will see the literal
‘(null)’ value for employee King. On the other hand, a blank appears when Script
Output option is selected.
28
UNSW
SISTM
Lab Manual
The first two column alias methods give the columns a new column heading. The use of
AS keyword is optional. However, you cannot have a space between the words of the
alias in the first two methods.
By default, the headings appear in uppercase. If the alias contains a space, a special
character (e.g. ‘$’) or you want the heading appears as upper and lowercase (e.g. ‘First
Name’), then you need to enclose with double quotes (“”) as shown in the third column
alias method. The last common alias is using the AS keyword and double quotes.
29
UNSW
SISTM
Lab Manual
FROM table_name;
Note: The keyword UNIQUE is same as DISTINCT will return the same results.
Figure 2-6 Not using Distinct in the SELECT Figure 2-7 Using DISTINCT in the SELECT
Statement Statement
30
UNSW
SISTM
Lab Manual
DEPARTMENT_ID
-------------
100
30
…
10
12 rows selected
MANAGER_ID
----------
100
123
…
146
103
122
19 rows selected
DEPARTMENT_ID MANAGER_ID
---------------------- ----------------------
60 103
80 146
…
20 201
110 101
28 rows selected
31
UNSW
SISTM
Lab Manual
32
UNSW
SISTM
Lab Manual
33
UNSW
SISTM
Lab Manual
Operator Order of
Execution
Multiplication * 1
Division / 2
Addition + 3
Subtraction - 4
34
UNSW
SISTM
Lab Manual
3.1.1 Reference
Casteel Chapter 8
3.1.2 Database
The database used in this lab is HR database.
35
UNSW
SISTM
Lab Manual
36
UNSW
SISTM
Lab Manual
37
UNSW
SISTM
Lab Manual
Now, we demonstrate examples using some of the operators in Table 3-1, Table 3-2 and
Table 3-3.
38
UNSW
SISTM
Lab Manual
When to use the operator ‘greater than’ (>) or ‘greater than or equal to’ (>=) depends on
how you want to evaluate the values. Figure 3-5 and Figure 3-6 show different values
are retrieved based on different operators. 10,000 and 11,000 were retrieved in Figure
3-6 but not in Figure 3-5. Alternatively, you can use BETWEEN ... AND ... operator.
Values that are specified using the BETWEEN ... AND ... operator are considered as
inclusive – thus, the results in Figure 3-6 and Figure 3-7 should be same.
39
UNSW
SISTM
Lab Manual
40
UNSW
SISTM
Lab Manual
41
UNSW
SISTM
Lab Manual
When comparison is done between two values, the values are compared using ASCII
codes (see http://www.asciitable.com). ASCII stands for American Standard Code for
Information Interchange. ASCII code represents a number which the computer or
software could translate.
In Figure 3-10, postal_code ‘M5V 2L7’, ‘YSW 9T2’, and ‘OX9 9ZB’ are among the
selected records. This is because the ASCII decimal value for the number nine is 57,
and, as an example, the ASCII decimal value for the letter ‘Y’ is 89 - since 89 is greater
than 57, the records are selected as shown in Figure 3-10.
Figure 3-10 Demonstrate how the Values are selected based on the ASCII Code
You can select a range of character strings as shown in Figure 3-11. Alternatively, you
can use the BETWEEN ... AND ... operator (see Figure 3-12) but you must specify the
lower limit first. Otherwise, no record is retrieved as shown in Figure 3-13.
42
UNSW
SISTM
Lab Manual
Figure 3-12 Select a range of Character String Values using BETWEEN ... AND ...
Figure 3-13 Using BETWEEN ... AND ... operator But the Lower Limit is not First
Figure 3-14 demonstrates the values for the character strings are case sensitive – ‘King’
is different from ‘KING’:
43
UNSW
SISTM
Lab Manual
44
UNSW
SISTM
Lab Manual
There are 107 employees but the two SELECT statements in Figure 3-17 only retrieved
106 employees - so one employee is missing? You can find the missing employee by
testing a NULL condition because Jennie does not have a department_id (see Figure
3-18):
45
UNSW
SISTM
Lab Manual
Figure 3-20 shows how the position of wildcard characters can change the results.
46
UNSW
SISTM
Lab Manual
Figure 3-21 shows an example of pattern matching on numbers and date values.
47
UNSW
SISTM
Lab Manual
Evaluation Order
1 Arithmetic (*, /, +, -) operators
2 Concatenation (||) operator
3 Comparison (<, >, =) conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition
SELECT <column_name>
FROM <table_name>
ORDER BY <column_name> | [1, 2,…];
The following queries will give you the same results – either sort by using the column or
the order number of the column in the SELECT list:
-- Sorted by column
SELECT employee_id, last_name, first_name
FROM employees
ORDER BY last_name;
/or/
48
UNSW
SISTM
Lab Manual
You are not recommended to use a column number when sorting because it might create
a different result when you add or delete a column in the future without changing the
column number. However, you will find automatic code generator such as in Oracle
APEX uses column number in the ORDER BY clause.
You can even sort a column that is not on the SELECT list. In the example below, the
manager_id column is not listed in the SELECT clause:
49
UNSW
SISTM
Lab Manual
By default, the NULL values will appear If you want to have the NULL values to appear
at the end of the list (Figure 3-24): at the beginning of the list, you have to use
NULLS FIRST in the ORDER BY clause
(Figure 3-25):
50
UNSW
SISTM
Lab Manual
4.1.1 Reference
Casteel Chapter 10
4.1.2 Database
The database used in this lab is HR database.
51
UNSW
SISTM
Lab Manual
52
UNSW
SISTM
Lab Manual
Note: in Oracle SQL, the position of a string starts from one (1) and not zero (0).
Function Purpose
ROUND The Round function is used to round numeric fields to the stated precision.
TRUNC The TRUNC function is used to round numeric fields to the stated precision.
MOD The MOD (modulus) function returns the remainder of a division operation.
ABS The ABS (absolute) function returns a positive numeric value.
POWER The POWER function is used to calculate a statistical power.
53
UNSW
SISTM
Lab Manual
54
UNSW
SISTM
Lab Manual
55
UNSW
SISTM
Lab Manual
NVL You may want to substitute a value for any NULLs encountered. Figure 4-6
For instance, a number multiplied by a NULL value is NULL and
this may not be what you want. This function is used to address a
column when it has a NULL value. For instance, you might want
to substitute a NULL value with zero.
NVL2 This function takes three parameters: x, y and z. The function will Figure 4-7
return y if x is not NULL. If x is NULL z will be returned instead.
NULLIF This function is used to compare two values for equality – if the Figure 4-8
two values are equal, then it will return a NULL value.
TO_CHAR You will use this function frequently to format values. For Figure 4-9
instance, this function can be used to convert dates and numbers to
a formatted character string.
SOUNDEX SOUNDEX function is to search for value based on the Figure 4-10
pronunciation of words rather than search for value based on an
exact character string.
56
UNSW
SISTM
Lab Manual
57
UNSW
SISTM
Lab Manual
58
UNSW
SISTM
Lab Manual
SELECT column_name, …
CASE
WHEN (condition 1) THEN <value 1>
WHEN (condition 2) THEN <value 2>
ELSE <default value>
END column_alias
FROM table_name
[WHERE …]
59
UNSW
SISTM
Lab Manual
60
UNSW
SISTM
Lab Manual
5.1 Objectives
This week, you will learn how to join two or more tables. You will be able to identify a
Cartesian join; create equality and non-equality joins; create a self-join; and distinguish
an inner join from an outer join. In addition, the theoretical principles behind the
development of SQL statements using the relational algebra from Codd’s original paper
will be discussed.
5.1.1 Reference
Casteel Chapter 9
5.1.2 Database
The database used in this lab is HR database.
2
Date, C. J., 1995, An Introduction to Database Systems, 6th Edition, Addison-Wesley Publishing, USA, p. 141
61
UNSW
SISTM
Lab Manual
Table 5-1 Definition of Algebra Operators
Operator Definition Example
Union Union returns a relation consisting of all tuples appearing in A
either or both of the relations, i.e., select all rows from both s1 A∪B
tables (A ∪ B). s2 s1
A s2
B s3
B s3 s4
s4
Intersect Intersect returns a relation consisting of all tuples appearing in A
both of the relations, i.e. select only the common rows from s1
both tables. s2 A ∩ B
A s2
B
B s3
s2
Difference Difference returns a relation consisting of all tuples appearing A
in the first and not the second of two specified relations, i.e. s1 A- B
select rows that are in the first table but not in the second table. s2 s1
Note: A – B is different from B – A.
B B- A
A (A-B) A (B-A)
s3 s3
B B
s2
62
UNSW
SISTM
Lab Manual
Operator Definition Example
Restrict Restrict returns a relation consisting of all tuples from a A
specified relation that satisfy a specified condition, i.e. rows s1 WHERE A = 's2'
are selected based on a condition(s) using a WHERE clause.
s2 s2
Project It returns a relation consisting of all tuples that remain as col A col B SELECT col A
(sub)tuples in a specified relation after specified attributes have a1 b1 a1
been eliminated, i.e. select a column(s) from a table. a2 b2 a2
Cartesian It returns a relation consisting of all possible tuples that are a A
Product combination of two tuples, one from each of two specified s1 A TIMES B
relations, i.e. rows in the first table TIMES rows in the second s2 s1 b1
table.
s1 b2
B s2 b1
b1 s2 b2
b2
63
UNSW
SISTM
Lab Manual
Operator Definition Example
Natural Natural join returns a relation consisting of all possible tuples A
Join that are a combination of two tuples, one specified from each c1 c2
of two specified relations, such that the two tuples contributing a1 b1
to any given combination have a common value for the
common attributes(s) of the two relations. a2 b2 A JOIN B
c1 c2 c3
a1 b1 c1
B a2 b2 c2
c1 c3
a1 c1
a2 c2
Divide Takes two relations, one binary and one unary, and returns a A B
relation consisting of all values of one attribute of the binary c1 c2 c2
relation that match (in the other attribute) all values in the a1 b1 b1
unary relation.
a1 b2 b3
a1 b3
a1 b4 A Divided by B
a2 b1 c1
a3 b1 a1
a3 b2 a3
a3 b3
64
UNSW
SISTM
Lab Manual
Now, you should have a better understanding of the theoretical background behind the
development of SQL statements of what you have already learned, and what you will
learn in the next few chapters.
Normally, when people see a join such as the one shown in Figure 5-1, they will
assume it is incorrect. If you want to use a Cartesian join, it is recommended that you
use the CROSS JOIN clause to indicate to other people your intention:
65
UNSW
SISTM
Lab Manual
If, for example, the department_id column is in both employees and departments tables,
you can refer department_id in the employees table as ‘employees.department_id’ and
department_id in the departments table as ‘departments.department_id’. Your SELECT
statement will look like this:
SELECT employees.last_name,
employees.department_id,
departments.department_name
FROM employees,
departments
WHERE employees.department_id = departments.department_id;
Alternatively, instead of using the full table name, you can define aliases for your
tables. It is a good practice to identify all the tables using aliases. In this way, you can
associate the columns with the tables. It will also help to improve the performance
when running the query - the table alias give a table a shorter name - it keeps the SQL
code smaller, more readable and uses less memory. Using alias for the above example,
your new SELECT statement will look like this:
66
UNSW
SISTM
Lab Manual
On the other hand, you will get an error if the condition statement is not entered in the
JOIN statement. Thus, this will force you to enter a condition:
67
UNSW
SISTM
Lab Manual
Using NATURAL JOIN in Figure 5-2 is fine because there is only one common
column, namely location_id, between the two tables. However, when there are two or
more columns with the same name in both tables, Oracle will select and link the tables
using one of the common columns. This automatic linking may not produce the join
you after, Oracle may not choose the common column you intended to link the two
tables.
68
UNSW
SISTM
Lab Manual
For example, if you want to link employees and departments table using NATURAL
JOIN, Oracle will use manager_id instead of department_id to link the two tables even
when department_id is a foreign key in the employees table and primary key in the
departments table (see Figure 5-3):
69
UNSW
SISTM
Lab Manual
You should not use any prefix alias in the USING clause because department_id is
common to both tables otherwise an error message will appear. For example, in Figure
5-5, department_id does not have the alias ‘d’ links to the column.
70
UNSW
SISTM
Lab Manual
71
UNSW
SISTM
Lab Manual
You can join three or more tables using JOIN … ON clause as shown in Figure 5-7.
You can still get the same result using JOIN … USING clause as shown in Figure 5-8.
Figure 5-8 Using JOIN ... USING ... for Three tables
72
UNSW
SISTM
Lab Manual
73
UNSW
SISTM
Lab Manual
5.8 Self-Joins
Self-join is to link a table to itself - for example, link employees table to employees
table. Thus, it requires the use of table aliases and use of a column qualifier.
74
UNSW
SISTM
Lab Manual
75
UNSW
SISTM
Lab Manual
76
UNSW
SISTM
Lab Manual
5.10.1 UNION
The UNION operator returns all the rows from multiple tables and eliminates any
duplicate rows to both queries.
77
UNSW
SISTM
Lab Manual
The number of columns in the SELCT statement determines the number of rows to be
retrieved. For instance, if we get rid job_id and department_id columns, then the result
is different for employee_id 101 (see Figure 5-14).
5.10.3 INTERSECT
The INTERSECT operator returns only those records with the same values in the
selected columns in both tables. For instance, you want to find employees who went
back to one of their previous jobs (Figure 5-15). However, in Figure 5-16, it shows that
employee 200 (Jennifer Whalen) went back to her old job but works in a different
department.
78
UNSW
SISTM
Lab Manual
79
UNSW
SISTM
Lab Manual
5.10.4 MINUS
The MINUS operator returns all the rows selected by the first query but not in the
second query. For example, Figure 5-17 shows employees have not changed their job
since joining the company.
80
UNSW
SISTM
Lab Manual
6.1 Objectives
This week, you will learn group functions, also known as multiple-row functions.
These functions will return one result per group. The multiple-row functions covered
include SUM, AVG, MIN and MAX. You will learn to use the COUNT function to
return records containing non-NULL values, or records containing NULL values. We
will also cover the GROUP BY and HAVING clauses.
In this course, we will not cover the topic on multidimensional analysis – GROUPING
SETS, CUBE, and ROLLUP functions.
6.1.1 Reference
Casteel Chapter 11
6.1.2 Database
The database used in this lab is HR database.
G14A 14 1.00
G14B 14 3.00
G14C 14 5.00
G14D 14 7.00
81
UNSW
SISTM
Lab Manual
If you want to find an average mark for all students, you just have to average all the
marks (average mark is 4.50) as shown in Table 6-2.
Table 6-2 Average Mark for all students
Name Marks
G13A 2.00
G13B 4.00
G13C 6.00
G13D 8.00
G14A 1.00
G14B 3.00
G14C 5.00
G14D 7.00
Average: 4.50
If you want find out the average mark for each of the groups, namely Group 13 and 14,
you have to use the GROUP BY clause. The GROUP BY clause will report the group
average at the level as specified. In this case, the group average for Group 13 and 14
are shown in Table 6-3.
Table 6-3 Average at Group Level
Group By
Name Group Marks Average
G13A 13 2.00
G13B 13 4.00
G13C 13 6.00
G13D 13 8.00
Group 13 5.00
G14A 14 1.00
G14B 14 3.00
G14C 14 5.00
G14D 14 7.00
Group 14 4.00
If you want to select the data, you can use the WHERE clause. However, if you want to
select the data at the group level, you have to use the HAVING clause. We now go
through the WHERE clause, HAVING clause, and using both the WHERE and
HAVING clauses.
If you only want to find an average mark for student’s individual mark >= 5, the rows
for marks >= 5 will be selected first, and then the marks for these selected rows will be
82
UNSW
SISTM
Lab Manual
averaged. In Table 6-4, he marks for G13A, G13B, G14A and G14B are not selected
because they are less than 5. On the other hand, if you are using the HAVING clause
(you must use GROUP BY when using HAVING clause), the marks will be averaged
at the group level, and groups with an average mark greater than 5 are then selected, In
this case, only Group 13 is selected as shown in Table 6-4.
Table 6-4 Finding the average mark with WHERE clause and HAVING clause
When using both the WHERE and HAVING clauses, the order of selection is as
follows. First, using Table 6-5 as an example, WHERE clause is applied, so only rows
with marks >= 5 are selected. The average mark for Group 13 and 14 is 7.00 and 6.00
respectively. Second, HAVING clause is applied for average mark greater than 7.00,
and in this case, only Group 13 is selected.
Table 6-5 Using WHERE Clause and HAVING Clause
83
UNSW
SISTM
Lab Manual
SUM function is used to calculate the total amount of a column for a group of
rows
AVG function is used to calculate the average values of a column
MIN function returns the smallest value
MAX function returns the largest value
84
UNSW
SISTM
Lab Manual
Note: there are other group functions such as STDDEV and VARIANCE which you
can find in the Casteel.
6.3.3 COUNT
COUNT function serves two purposes:
Count non-NULL values and
Count total records, including those with NULL values
85
UNSW
SISTM
Lab Manual
86
UNSW
SISTM
Lab Manual
87
UNSW
SISTM
Lab Manual
You will get an error if you do not include group functions such as AVG() in the
HAVING clause. HAVING clause is associated with GROUP BY clause, and you must
have a GROUP BY clause if you want to use HAVING. Thus, HAVING clause is to
test the condition at the group level.
When sorting a column or columns, you must use ORDER BY. GROUP BY does not
guarantee the result set to be sorted as you might want.
88
UNSW
SISTM
Lab Manual
You can also group a number of columns in the GROUP BY clause. In the below
example, Figure 6-11, we group country_id and department_id. Please note there are
seven departments under country US.
89
UNSW
SISTM
Lab Manual
If we apply HAVING clause to the query in Figure 6-11 by selecting only the average
salary greater than $5,000, three of the US departments will be discarded as shown in
Figure 6-12. The HAVING clause applies to the combination of the columns in the
GROUP BY clause.
90
UNSW
SISTM
Lab Manual
7.1 Objectives
So far, you have been using predefined tables. Today, you will learn to create new
tables, and also learn how to use a subquery to create a new table using Data Definition
Language (DDL). You will also learn to add constraints to tables. You will be able to
distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and NOT
NULL constraints and the appropriate use for each constraint. You will gain the
knowledge to create constraints at the column level and table level.
Apart from creating a new table, you will learn to add a column to an existing table;
modify the definition of a column in an existing table; and delete a column from an
existing table. In addition, you will find out how to mark a column as unused and delete
it at a later time; rename a table; and delete a table from the database.
7.1.1 Reference
Casteel Chapter 3 and 4
7.1.2 Database
The database used in this lab is HR database.
Requirements:
You must have a CREATE TABLE privilege
Allow to access to a storage area
Table and column names:
Can contain a maximum 30 characters – no blank spaces
Must begin with a letter
Can contain numbers, underscore (_), and number sign (#)
Must be unique
No reserved words are allowed
Column definition list must be enclosed in parentheses
Datatype must be specified for each column
Maximum of 1,000 columns per table
91
UNSW
SISTM
Lab Manual
In the Oracle SQL Developer, you have to click on the ‘refresh’ option to see your new
table. A data dictionary is a typical component of a DBMS that maintains information
about database objects. You can query the data dictionary to verify all the tables that
exist in your schema. There are options in the Oracle SQL Developer which you can
perform these tasks – your tutor will show you some of the options which you might
useful for your lab exercises:
92
UNSW
SISTM
Lab Manual
TABLE_NAME
------------------------------
REGIONS
LOCATIONS
DEPARTMENTS
JOBS
EMPLOYEES
JOB_HISTORY
DEPARTMENTS_HISTORY
COUNTRIES
8 rows selected
93
UNSW
SISTM
Lab Manual
94
UNSW
SISTM
Lab Manual
95
UNSW
SISTM
Lab Manual
For example,
TABLE_NAME
------------------------------
REGIONS
…
JOB_HISTORY
DEPARTMENTS_HISTORY
EMPLOYEES_HISTORY
COUNTRIES
…
96
UNSW
SISTM
Lab Manual
-- Rename a table...
RENAME employees_history TO employees_hist_2011;
DESCRIBE employees_history
ERROR:
----------------------------------------------
ERROR: object EMPLOYEES_HISTORY does not exist
…
TABLE_NAME
------------------------------
REGIONS
…
JOB_HISTORY
DEPARTMENTS_HISTORY
EMPLOYEES_HIST_2011
COUNTRIES
Oracle 10g has introduced a recycle bin. You can drop a table and their contents, but,
later on, you can recover the table from the recycle bin – it functions like the recycle
bin in Windows.
-- Delete a table
DROP TABLE employees_hist_2011;
97
UNSW
SISTM
Lab Manual
OBJECT_NAME ORIGINAL_NAME
------------------------------ --------------------------------
BIN$0Xud4LLjRL2rhSXQejW1zg==$0 EMPLOYEES_HIST_2011
If you want to recover the table from the recycle bin, you have to use the
FLASHBACK command:
If you want to delete the table permanently, you have to delete the table (again!) in the
recycle bin using the PURGE TABLE command.
Alternatively, you could have deleted the table permanently in the first place – you just
have to add the PURGE keyword to the DROP TABLE statement:
7.9 Constraints
Up to this point, we have been defining how to create, modify and delete a table. Next,
you have to define constraints in your tables. Constraints can be defined as:
Rules used to enforce business rules, practices, and policies
Rules used to ensure accuracy and integrity of data
Constraints can be created at the column level by including the constraint as part of the
column definition or at the table level by defining the constraint after all columns have
been defined. When created at the column level, the column that is the objective of the
constraint is identified first, and then the constraint information is provided.
98
UNSW
SISTM
Lab Manual
All constraints are enforced at the table level and if a data value violates a constraint,
the entire row is rejected. The description for the five constraint types are as follows:
Table 7-1 Table of Types of Constraint
If the constraints are created using the table level approach, the type of constraint is
identified first, and then the column. A constraint can be assigned a name when it is
created; otherwise, the Oracle server will assign a name for the constraint using the
convention format SYS_Cn , e.g. employee_id_pk (see Casteel p.101). When constraint
violations occur, the naming convention for constraint names will help to quickly
identify the specific problem.
Alternatively, if you want to add a constraint after a table is created, you have to use the
ALTER TABLE command:
99
UNSW
SISTM
Lab Manual
If you enter an invalid country_id, then an error message containing the violated
constraint key for country_id is displayed:
One error saving changes to table “HR”.”LOCATIONS_TEMP”:
Row 1: ORA-02291: integrity constraint (HR.LOC_TEMP_C_ID_FK) violated - parent key not
found
ORA-06512: at line 1
A foreign key constraint requires a value to exist in the referenced column of another
table, i.e. enforcing the referential integrity. If you want to ensure that any records
reference the deleted record will also be deleted, you can use ON DELETE CASCADE
option.
For example, if you want to ensure by deleting a country_id, all the records reference
the deleted country_id are also removed, the SQL statement will be:
country_id CHAR(2),
CONSTRAINT loc_temp_c_id_fk
FOREIGN KEY (country_id)
REFERENCES countries (country_id)
ON DELETE CASCADE
However, in practice, you probably would like to check the records first before deleting
them.
100
UNSW
SISTM
Lab Manual
When you create a single column primary key, a composite primary key, a foreign key,
or any other constraints, you have to note the syntax of defining the constraint, and the
position of the constraint in the CREATE statement. You also have to note the position
of the comma (‘,’) used in the CREATE statement.
-- Add a new column location_mnemonic and add a constraint for this column
ALTER TABLE locations_temp
ADD location_mnemonic CHAR(2)
CONSTRAINT loc_temp_l_mn_uk
UNIQUE;
-- Alternatively, you can first add the new column location_mnemonic and then
-- use another statement to add the constraint.
ALTER TABLE locations_temp
ADD location_mnemonic CHAR(2);
ALTER TABLE locations_temp
ADD CONSTRAINT loc_temp_l_mn_uk UNIQUE (location_mnemonic);
101
UNSW
SISTM
Lab Manual
-- Modify the column first_name so it must have a value, i.e. NULL value cannot be accepted.
ALTER TABLE employees_history
MODIFY (first_name CONSTRAINT first_name_nn NOT NULL);
102
UNSW
SISTM
Lab Manual
The check constraint will ensure the condition is satisfied and maintain the consistency
of the data. This checking mechanism is to ensure that all data entered by any means
(via SQLDeveloper, SQLPlus or an application) will pass the check condition. Please
note that when you use user_constriant table, you must quote the name of the table in
UPPER CASE – see Figure 7-8.
103
UNSW
SISTM
Lab Manual
If you try to drop a primary key which is referenced by a foreign key in another table,
an error message is raised indicating that a foreign key reference exists:
Error report:
SQL Error: ORA-02273: this unique/primary key is referenced by some foreign keys
02273. 00000 - “this unique/primary key is referenced by some foreign keys”
*Cause: Self-evident.
*Action: Remove all references to the key before the key is to be dropped.
If you want to continue to delete the primary key, you have to use the CASCADE
option which will also delete the associated foreign key as previously discussed.
104
UNSW
SISTM
Lab Manual
8.1.1 Reference
Casteel Chapter 5 and
Casteel Chapter 6 – Sequences Section (pp. 177 – 188)
8.1.2 Database
The database used in this lab is HR database.
105
UNSW
SISTM
Lab Manual
Figure 8-2 Including and Not including columns in the INSERT statement
106
UNSW
SISTM
Lab Manual
If you want to save what you have already done, you have to use the COMMIT
command. In the Oracle SQL Developer, instead of typing ‘COMMIT;;’, you have the
option to click on the COMMIT icon on the SQL Worksheet bar or use F11 function
key (see Figure 8-4). We will discuss more details on COMMIT command in Section
8.3.
The primary key constraint is case sensitive. For example, if you input the character
strings of a primary key in lower case and upper cases they will be regarded as two
distinct keys. More concretely, as far as the database is concerned, ‘nz’ and ‘NZ’ are
different keys (see Figure 8-5).
107
UNSW
SISTM
Lab Manual
Figure 8-5 Inserting Upper and Lower Text for the Primary Key column
If you do not want to save what you have already done, you can use the ROLLBACK
command by either typing ‘ROLLBACK;;’, or clicking on the rollback icon on the SQL
Worksheet bar, or using the F12 function key (see Figure 8-6). Again, we will discuss
more details on COMMIT command in Section 8.3.
108
UNSW
SISTM
Lab Manual
Therefore, it is important that you plan the order of inserting a row to a table to avoid
violating any of the constraints.
109
UNSW
SISTM
Lab Manual
110
UNSW
SISTM
Lab Manual
UPDATE table_name
SET column_name = new_data_value, …
[WHERE (condition)]
111
UNSW
SISTM
Lab Manual
112
UNSW
SISTM
Lab Manual
Enter the value as requested. The variables are substituted with the entered values
before the command is executed as shown in Figure 8-13.
If you want to enter a value in the WHERE clause, the value of the variable must match
the value of the column – remember values are case-sensitive. For instance, if ‘nz’ is
entered for country_id, a corresponding record with that key will not be found and no
record will be updated – see Figure 8-14.
113
UNSW
SISTM
Lab Manual
114
UNSW
SISTM
Lab Manual
ROLLBACK command: undo all the changes since last transaction control
command.
Implicit COMMIT occurs when DDL command is executed or user properly exits
the system. For example, if you exit the Oracle SQL Developer without updating
your changes, a popup box will ask you for action as shown in Figure 8-16:
Figure 8-16 Exit without Update – a Popup Box prompts for Action
SAVEPOINT name;
ROLLBACK TO name;
115
UNSW
SISTM
Lab Manual
2 rows inserted
1 rows updated
1 rows updated
rolledback
COUNTRY_ID COUNTRY_NAME REGION_ID
---------- ---------------------------------------- ----------------------
FJ fiji 3
NZ All Blacks 3
116
UNSW
SISTM
Lab Manual
rolledback
COUNTRY_ID COUNTRY_NAME REGION_ID
---------- ---------------------------------------- ----------------------
FJ fiji 3
NZ New Zealand 3
To get the next sequence number from the regions id when you insert a new employee
record, you simply use NEXTVAL pseudo column:
INSERT INTO regions (region_id, region_name)
VALUES (region_id_seq.NEXTVAL, ‘Next Sequence Value’);
117
UNSW
SISTM
Lab Manual
SELECT column_name, …
FROM table_name, …
[WHERE condition …]
FOR UPDATE;
118
UNSW
SISTM
Lab Manual
9.1.1 Reference
Casteel Chapter 12
9.1.2 Database
The database used in this lab is HR database.
SELECT
<SubQuery>
FROM
<SubQuery>
JOIN
<SubQuery>
WHERE
<SubQuery>
HAVING
<SubQuery>
ORDER BY
<Column>
119
UNSW
SISTM
Lab Manual
It is also a good practice to give an alias to each of the tables used. This will become
more apparent later in the lab.
120
UNSW
SISTM
Lab Manual
121
UNSW
SISTM
Lab Manual
The next three examples demonstrate the use of IN, ANY and ALL operators
respectively in the parent queries.
122
UNSW
SISTM
Lab Manual
123
UNSW
SISTM
Lab Manual
124
UNSW
SISTM
Lab Manual
125
UNSW
SISTM
Lab Manual
126
UNSW
SISTM
Lab Manual
127
UNSW
SISTM
Lab Manual
We could have used department table in the parent query but we want to demonstrate
the use of the EXISTS clause. If you want to find which location is not currently
assigned to any of the departments, you can use NOT EXISTS instead, i.e. to look for
records that do not exist in the departments table.
There are 23 locations in the locations table, we already know there are 7 locations
currently actively used in the departments table (see Figure 9-11). Therefore, you
should have 16 locations currently not assigned in the departments table and the result
of Figure 9-12 confirms this.
128
UNSW
SISTM
Lab Manual
129
UNSW
SISTM
Lab Manual
In regions_temp_two table, four rows are updated and one row is added as shown in
Figure 9-15. The two temporary tables contain the same records.
130
UNSW
SISTM
Lab Manual
10.1 Objectives
The last topic you will learn is to create and drop a view. We only cover simple and
complex views. You can update date values using a view but we will not cover this
topic in this course.
10.1.1 Reference
Casteel Chapter 13
10.1.2 Database
The database used in this lab is HR database.
You can update a table using a VIEW but, in this course, we will not cover using DML
in a View. Nor we will cover inline and materialized views. We will only cover simple
views, complex views and dropping a view.
131
UNSW
SISTM
Lab Manual
132
UNSW
SISTM
Lab Manual
133
UNSW
SISTM
Lab Manual
134
UNSW
SISTM
Lab Manual
135
UNSW
SISTM
Lab Manual
136
UNSW
SISTM
Lab Manual
137
UNSW
SISTM
Lab Manual
138
UNSW
SISTM
Lab Manual
2. Your Oracle SQL Developer screen should look similar to Figure B-1:
3. Select all Configure File Types associate with SQL Developer as shown in Figure B-
1, and click the OK button.
4. To connect to a database, you need to click the ‘+’ in Connections menu. A form,
similar to Figure B-2, will prompt you to enter fields to connect to a server database:
139
UNSW
SISTM
Lab Manual
5. The fields you have to enter in the form shown in Figure B-2 are as follows:
Connection Name: HR
Username: z (your student number)
Password: (your password – note the password is
case sensitive)
Save Password: If you are running on your own laptop or other devices, you might
want to click this box so you do not have to enter your password
every time you log in.
Hostname: sage.business.unsw.edu.au
Port: 1521
SID: orcl01 (note: the fourth character is ‘l’ for ‘Larry’ not ‘one’)
Once completed, your form should look similar to Figure B-3 (note: substitute the
Username field with your student number):
To test the connection, click the Test button as shown in Figure B-3. You will see an
error if the connection is unsuccessful. Otherwise, click the Connect button to connect
to the server database.
140
UNSW
SISTM
Lab Manual
6. Once connected, you will see two sets of tables as shown in Figure B-4: one set of
tables is used in the Oracle Manual, and the other set is used in your lab exercises.
7. You need to format the dates to the DD-MON-RR. To change the date format, you
need to go the option: Tools > Preferences > Database > NLS, and change the format
to DD-MON-RR as shown:
141
UNSW
SISTM
Lab Manual
Oracle SQL Developer Data Modeler is a data modeling and database design tool. For
instance, you can use the tool to design an Entity Relationship Diagrams (ERD). You
can download the software free from the Oracle website.
You can create a database from scratch using Data Modeler. Alternatively, you can
create an ERD by importing tables of a database you have already created. We will now
demonstrate how to import tables from an existing database to Data Modeler.
First, you have to select the Data Dictionary (or Ctrl-Shift-B) option from the File
menu as shown in Figure C-1:
142
UNSW
SISTM
Lab Manual
Next, you have to choose a database and you might have to enter your username and
password as shown in Figure C-3. In this case, we select the HR database:
143
UNSW
SISTM
Lab Manual
Next, you have to select a schema/database – either student or group account - you
want to use in your data modelling (Figure C-4):
Once you have connected to the schema, you then select the tables you want to model.
As an example, the tables we selected as shown in Figure C-5 are countries,
departments, employees, jobs, job_history and locations:
144
UNSW
SISTM
Lab Manual
Once all the tables are imported successfully, a log similar to Figure C-7 appears, and
clicks on the close button to complete the importing task:
145
UNSW
SISTM
Lab Manual
Data Modeler will automatically attempt to create an ERD based on the tables and
constraints from the schema/database. The ERD shown in Figure C-8 is created based
on the selected six tables:
146