Download Lab 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

Serializability wikipedia , lookup

Microsoft Access wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

IMDb wikipedia , lookup

Btrieve wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Oracle Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Concurrency control wikipedia , lookup

PL/SQL wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

ContactPoint wikipedia , lookup

Transcript
CISB314 DATABASE 2
LAB 2
LAB 2
Getting Started with DB2
In this lab, you will be introduced to instances, databases, DB2 commands and DB2 server
connection. This lab consists of five parts:




Initial Steps
Working with DB2 Databases
Creating SQL scripts
Handling Errors
1. Initial Steps
To complete this lab, you will need VMware image and VMware Player 2.x or VMware Player
Workstation or later.
a) To start the VMware image, double-click on VMware Player icon on the desktop.
b) Select Open a Virtual Machine. Browse through the D: drive, go to this path: DB2
Training\03_VMWare\DB2v97AA_20110520_new. Then, double click DB2 Express C
9.7 32-bit.
NLAG
1
CISB314 DATABASE 2
LAB 2
c) Select DB2 Express-C 9.7 32-bit and click Play virtual machine button.
d) At the login prompt, login with following credentials:
Username: db2inst1
Password: password
e) Open the terminal window by right-clicking on the Desktop and choosing the Open
Terminal item.
NLAG
2
CISB314 DATABASE 2
LAB 2
2. Working with DB2 Databases
a) To get started with DB2, ensure that the DB2 Database Manager has been started by
issuing following command at the prompt:
db2inst1@db2rules:-> db2start
You should get:
SQL1063N DB2START processing was successful
Or
SQL1026N The database manager is already active
b) Throughout the lab, the SAMPLE database will be used to explore the features of DB2.
To create the SAMPLE database we need to first remove the existing SAMPLE database
by issuing the following command:
db2inst1@db2rules:-> db2 drop db sample
You should get:
DB20000I The DROP DATABASE command completed successfully
c) To generate the SAMPLE database, issue this command:
db2inst1@db2rules:-> db2sampl
This process will take some time, you should get the following:
Creating database “SAMPLE”…
Connecting to database “SAMPLE”…
Creating tables and data in schema “SAMPLE”…
Creating tables with XML columns and XML data in schema
“DB2INST1”…
‘db2sampl’ processing complete.
NLAG
3
CISB314 DATABASE 2
LAB 2
d) Before working with a database, a user of application program must establish a
connection with that database. You connect to databases using the CONNECT statement.
There are SEVERAL WAYS you can connect to a database:
i.
If the database you want to connect is local, and you simply want to connect to it
using the default user ID, issue the command:
db2inst1@db2rules:-> db2 connect to sample
You should get:
Database Connection Information
Database server
SQL authorization
Local database alias
ii.
= DB2/LINUX 9.7.4
= DB2INST1
= SAMPLE
OR you can also use following command:
db2inst1@db2rules:->
db2
db2inst1 using password
iii.
connect
to
sample
user
OR you can also have DB2 prompt you for the password using following
command:
db2inst1@db2rules:->
db2inst1
db2
connect
to
sample
user
e) Let’s take a look at the tablespaces that DB2 created by default when we issued the create
the SAMPLE database statement by issuing following command:
db2inst1@db2rules:-> db2 list tablespaces
NLAG
4
CISB314 DATABASE 2
LAB 2
You should get:
Table space SYSCATSPACE contains the System Catalog tables. It basically contains
system information that should not be modified or deleted; otherwise the database will
not work correctly.
Table space TEMPSPACE1 is used by DB2 when it needs additional space to perform
some operations such as sorts.
Table space USERSPACE1 is normally used to store user database tables if there is no
table space specified when creating a table.
f) Let’s try to view a table from the SAMPLE database. Issue following SQL statement:
db2inst1@db2rules:-> db2 “select
* from department”
You should get:
NLAG
5
CISB314 DATABASE 2
LAB 2
g) Next, create a table named tbl1 and insert values into it:
db2inst1@db2rules:->
db2
“create
varchar(30), phone varchar(20))”
table
tbl1(name
db2inst1@db2rules:->
db2
“insert
into
values(‘Tom’,’123456789’),(‘Mary’,’987654321’)”
tbl1
You should get a message saying:
DB20000I The SQL command completed successfully
for each statement.
h) Then, view the table:
db2inst1@db2rules:-> db2 “select
* from tbl1”
You should get:
i) Because we do not need this table in SAMPLE database, remove table tbl1 using this
command:
db2inst1@db2rules:-> db2 drop table tbl1
You should get a message saying:
DB20000I The SQL command completed successfully
j) Anytime that you need to terminate the connection to the database, you can issue the
TERMINATE command:
db2inst1@db2rules:-> db2 terminate
You should get a message saying:
DB20000I The TERMINATE command completed successfully
NLAG
6
CISB314 DATABASE 2
LAB 2
3. Creating SQL Scripts
You can also save all the SQL commands as a file in SQL Scripts.
a) To create the file, press the menu at the bottom left corner of the screen, and select More
Applications.
b) Scroll down and select gedit text editor, located under Tools.
NLAG
7
CISB314 DATABASE 2
LAB 2
c) Type the following SQL commands in the text editor and save it as myscript1.db2:
Note: Put semicolon (;) to terminate each statement.
d) Then, issue following command at the prompt:
db2inst1@db2rules:-> db2 –tvf myscript1.db2 –z
myscript1.log
You will get the same output as in Section 2g – 2j.
NLAG
8
CISB314 DATABASE 2
LAB 2
e) The SQL commands in previous example use semicolon as statement terminator. If you
use different symbol, for example the exclamation mark (!), a different DB2 command
must be issued to run the file.
Locate and edit myscript1.db2 and put exclamation mark at the end of every statement:
The file is located at db2inst1’s Home folder on the Desktop.
Next, issue this command at the prompt:
db2inst1@db2rules:-> db2 –td! –v -f myscript1.db2 –z
myscript1.log
You will get the same output as in Section 2g – 2j.
f) Delete myscript1.db2 and myscript1.log files from db2inst1’s Home folder as we no
longer need it.
NLAG
9
CISB314 DATABASE 2
LAB 2
a) Terminate the connection to the database:
db2inst1@db2rules:-> db2 terminate
b) To stop the DB2 Database Manager, issue:
db2inst1@db2rules:-> db2stop
c) And issue the following to clean up the command window:
db2inst1@db2rules:-> clear
NLAG
10
CISB314 DATABASE 2
LAB 2
Try It Yourself: Practice DB2 Commands
1. Start the DB2 Database Manager.
2. Using command window:
a) Create a new database named DB101.
b) Establish a connection to the database.
c) Create a table named zipcodes that has three columns, as shown below:
Column
zip
city
state
Type
varchar(5)
varchar(30)
varchar(20)
d) Insert two records in the table.
Zip
City
State
59200 Bangsar Kuala Lumpur
43000 Kajang
Selangor
e) View the content of table zipcodes.
f) Remove table zipcodes from database DB101.
3. Using text editor:
a) Create a SQL script named db101script.db2.
b) Repeat Step 1c – 1f.
Use “#” as the statement terminator.
c) Run the script from DB2 Command Window.
4. Terminate connection to the database DB101.
5.
Remove it from the system directory.
6. View the content of the system directory to make sure DB101 is removed.
7. Stop DB2 Database Manager and clean up the command window.
8. Delete db101script.db2 and db101script.log files from db2inst1’s Home folder.
NLAG
11