Professional Documents
Culture Documents
db2 Udb
db2 Udb
References
SC26-8966 SQL Reference
Notes:
Database managers are applications that maintain our data. Each database manager
provides a set of automated services. We will discuss three of the more common
services.
A column in a table can have many characteristics assigned to it. We will discuss
three of the characteristics that are assigned to all columns.
The three keys are: unique key, primary key, and foreign key.
Notes:
We work with tables of data everyday. A phone book is a table of data. Each page
has the same columns (name, address and phone number) just different rows. Unlike
tables of data in a relational database manager, phone books are in alphabetical
sequence.
Figure 1-3. Find Phone Number of Larry Jones From Sales (CF031015)
Notes:
If there are multiple rows (more than one Larry Jones from Sales) that meet our
search criteria we must return multiple phone numbers.
Notes:
Phone books are updated and issued periodically
Corrections made to one copy of the book are not distributed to the other copies
around the company.
Notes:
Database managers provide many services. These are but a few.
Logging
Security
Optimization
Locking
Recovery
Data Integrity
Figure 1-6. Some of the Integrated Services DB2 UDB Provides (CF031030)
Notes:
The first 'C' in the word concurrency is pronounced with a k sound. You can use the
acronym CIS (pronounced kiss) to help you remember these services (the kissing
services).
Notes:
Concurrency is the database manager's ability to allow multiple users access to a
table's data, at the same time.
Notes:
A database manager ensures that while a row is in the process of being changed, no
other application can access the row until the change process is completed.
The database manager also ensures that if an application is changing a set of rows,
all rows in the set will be changed, or none of the rows will be changed.
Notes:
Authorizations are given with GRANT SQL statements.
Each time an SQL statement is executed the database manager (DBM) will check the
authorization catalog tables to ensure that the ID executing the statement is
authorized to do so.
Notes:
The database management system we are working with in class is DB2 UDB.
DB2 UDB is a relational database management system (RDBMS). The word relation is
a synonym for the word table. A relational database management system is a
database manager that manages data perceived to be stored in a table format, that is,
columns and rows.
Notes:
Talking to DB2 UDB in English, French or German, and so forth, would be too
cumbersome. A more concise language is needed.
Notes:
SQL is a non-navigational language. With SQL, we tell DB2 UDB what to do, not how
to do it.
Notes:
Every table must be given a name. Each of the table's columns must be assigned
three attributes:
Name
Data type / length
Null Characteristic
Notes:
Table and column names must begin with an alpha character. An alpha character is
in the set (A-Z, $, @, #). The remaining characters can be alpha, numeric (digits 0-9),
and the underscore (_). Table and column names can be from 1 to at least 18
characters long. Newer releases of DB2 UDB support longer table and column
names.
Notes:
Dates, times and timestamps are stored internally as numeric values but must be
treated as character strings in an SQL statement.
Notes:
A null characteristic tells DB2 UDB whether to allow a column to be marked as "The
value for this column is unknown".
Notes:
This CREATE statement tells DB2 UDB that:
The owner of the table is to be COMPANY
The simple name of the table is to be DIRECTORY.
The name of the first column will be LASTNAME
Only character data is to be allowed in the first column.
All values in the first column will be stored with 15 characters.
The first column must always have a value.
The name of the second column will be FIRSTNAME.
Only character data is to be allowed in the second column.
If a first name is not available for a row DB2 UDB is to mark it null (unknown)
The name of the third column will be address
Only character data is to be allowed in the third column.
If an address is not available for a given row, DB2 UDB is to fill the ADDRESS
column with spaces.
Notes:
A table can have any number of unique keys, but only one defined primary key.
Notes:
Primary keys can be defined in the column description area of a CREATE TABLE
statement.
Notes:
Foreign keys are designed into our tables to define relationships between rows.
Notes:
Foreign keys can be defined in the column description area of a CREATE TABLE
statement.
In the table Cities, we are telling DB2 UDB that the COUNTRY_CODE column is a
foreign key and that it contains values from the primary key of the COUNTRY table.
Notes:
A city is related to its country by the common values in the COUNTRY_CODE
columns. Should we need to produce a report with information about a city and the
country in which it is located, we can find a city's country by searching for a matching
COUNTRY_CODE.
Notes:
Each employee is assigned to a department. We can find information about an
employee's department by relating the employee's row to his or her department row.
This is done by looking for matching values in the DEP columns of the EMPL and
DEPT tables.
Notes:
User must be authorized to logon to the system.
Application manager may have its own security.
Application manager loads the program into memory, and so forth.
Application manager passes userid to DB2 UDB.
DB2 UDB verifies that userid is authorized to do what it's trying to do.
DB2 UDB returns messages (and perhaps data) to the application.
Figure 1-25. How DB2 UDB Handles an SQL "Change" Request (CF031125)
Notes:
User logs on.
User's ID is passed to DB2 UDB.
User's request is passed to DB2 UDB.
DB2 UDB verifies that user is permitted to update the salary column.
DB2 UDB locks the data that will be changed.
DB2 UDB reads the data that will be changed into its buffers.
Notes:
DB2 UDB writes a 'before update' copy of the data to the log.
DB2 UDB updates the row.
DB2 UDB writes an 'after update' copy of the data to the log.
DB2 UDB writes a commit to the log.
DB2 UDB writes the data to the table (writes may be batched and may not actually
occur at this time).
DB2 UDB releases the lock on the data.
DB2 UDB returns a message to the user/application as to the success or failure of
the SQL statement.
Notes:
System programmer and/or the SYSADM does the physical install.
The SYSADM creates the system level objects (databases).
The SYSADM grants other IDs appropriate privileges (DBADM, CREATETAB, and
so forth.
SYSADM monitors the overall DB2 UDB system performance.
Notes:
Responsible for building and maintaining the objects within the database.
Grants to other IDs privileges to access and/or change objects within the database.
In many shops the Database Administrator (DBA - the person(s) with DBADM
authority) perform the production binds (prepare application programs to work with
DB2 UDB).
DBADM is responsible for the running of most DB2 UDB utilities.
Notes:
Programmers may be involved with creating test environments for new
applications.
Programmers prototype SQL (write and test the SQL before it is used in
production).
Programmers are concerned with the efficiency of their SQL. Explain is a DB2
UDB service that 'tells' us how DB2 UDB is going to execute our SQL.
Programmers physically embed SQL in host language programs.
Programmers prepare (precompile and bind) their programs for execution.
Notes:
Anyone executing an SQL statement is an end-user.
SQL can be in applications end-users run.
The SQL within the applications may be changing data, adding data, removing data
or reading data.
The end-user may construct (write) their own queries that change data, add data,
remove data, or read data.
Notes:
Operators have the authority to run many of the utilities that the DBA and SYSADMS
run. Operators take some of the maintenance burden off the shoulders of the
SYSADMs and DBAs. Operators do not have data access authority (they can't look at
the data, unless specifically authorized to do so).
Checkpoint
Notes:
A table format implies columns and rows.
Relational database managers understand SQL.
Every column has a column name, a data type assigned to it, and a null
characteristic.
The kissing services are Concurrency, Integrity, Security.
Integer, smallint, and decimal are numeric data types. Char and varchar are
character data types. Date, time, and timestamp are special data types designed
to store dates, times, and timestamp values.
Unique key is a column or set of columns whose values are unique. A table can
have any number of unique keys.
A primary key is one of the unique keys that is defined to the relational database
manager. A table can have only one defined primary key.
A foreign key is a column or set of columns that contain values from some table's
unique key.