Bài tập làm quen với hệ quản trị cơ sở dữ liệu Informix chi tiết minh họa từng dòng lệnh trên ubuntu
Trang 1DAY 1
The DB-Access Utiltiy
1 Creating the demonstration database
2 Putting the DB-Access utility to work
2.1 Invoke DB-Access and connect to the stores database.
2.2 To look at the list of tables in the database To look at the columns in the orders table:
Trang 22.3 Running a simple query against the table to view data.
2.4 This switches from editing mode to interactive mode.
Creating Simple Test Data
1 Start the dbaccess utility, and select the stores_demo database.
Trang 32 Create a table called big
3 Insert one row of data into the table:
4 Select the data from the table Which values are assigned to the columns?
Trang 45 Change the insert statement by choosing the Use-Editor option from the menu Change the editor from vi to gedit Insert more data in to the table big by executing the following statement
6 Select the data from the table Which values are assigned to the columns? What is the meaning
of the data type serial(1) ?
- Ý nghĩa của kiểu dữ liệu serial(1): mỗi lần insert trường id sẽ tăng thêm 1 đơn vị
7 Execute the insert statement from exercise 5 again Count the number of rows in the table.
8 Execute the insert statement until you have a table with about 3 million rows.
9 Leave dbaccess and create a file called sumbig.sql which contains the following statement
Trang 510 Execute the SQL script from exercise 9 in batch mode using the command
11 Using the Unix tool time you can also measure the execution time of the select statement:
DAY 2
Module ‘Memory in an DBMS’ Exercises
1 Open a terminal window Restart the DBMS using onmode and oninit to empty thebuffer pool Use the onstat-utility to verify that your DBMS is up and running.
Trang 62 What is the page size of your buffer pool pages?
3 How many pages can be cached in the buffer pool of your DBMS?
4 Use onstat –z to reset the values that are shown to you by the onstat-utility The values reported
by onstat –g buf should now be 0.
Trang 75 Open a second terminal window Start dbaccess Execute the following SQL statement: select sum(id)
Trang 88 Re-run the select-statement from exercise 5.
9 Monitor the number of pages read from disk and the cache-hit rate in the other terminal There should by a significant change Explain the reasons for this change
Trang 9Module ‘Disks in a DBMS’ Exercises
1 Open a terminal window Which dbspaces exist in your DBMS?
Trang 102 Open a second terminal window and start dbaccess Connect to the demo database and create a table friends
3 Insert some rows of data so that both dbspaces are used.
4 Go to the other terminal window and clear the reported numbers using onstat –z Using onstat –D we can monitor how many pages are read from which dbspace Execute the command and convince yourself Since we cleared statistics, everything should by zero.
Trang 115 Switch to your dbaccess terminal Write a select-statement, that only selects
records of friends, whose last name start with a letter between ‘N’ to ‘Z’.
6 Switch to the other window and execute onstat –D What output did you expect?
7 We should avoid caching in the current case We bounce (shut down and restart) the DBMS:
onmode –yuk
shuts the DBMS down
Trang 12restarts it
8 Reset the onstat-results again by using onstat –z
9 Try to execute the following SQL statement:
10 Try to use the internet to resolve this problem.
DAY 3
Module ‘Sequential Reads’ Exercises
1 Create a table of significant size, as you did in the lab ‘Creating Simple Test Data’.
Trang 132 Monitor the current status of the allocated extents using oncheck –pt like we did in the lecture Review the following information
• Number of extents 4
• First extent size 8
• Next extent size 2048
• Number of rows 3263442
3 Read the chapters ‘Extent size doubling’ and ‘Merge of Extents for the Same Table’ in the IBM documentation This should make things clearer.
4 Use the tools that we discussed in the lecture to reorganize the table into one extent.
5 The previous exercises demonstrated, that it is not that simple, to create tables with multiple extents Create and populate a table with three extents.
Trang 14Module ‘Index’ Exercises
1 Copy and paste the following SQL statements to a file called fill.sql:
2 Run the script and measure the time that is used for execution: time dbaccess stores_demo fill.sql
Write down the resulting time.
Trang 153 Edit the SQL script and change the line
4 Create an SQL script access.sql that contains the following statement
select * from big where id=1000000
Bounce the database server, run the script and measure the time that is used for execution Write down the execution time.
5 Open dbaccess and navigate in the menu to Query-Language->Info Choose the table big Go to the Constraints->Primary menu and write down the name of the primary key constraint Go to Query Language and execute following statement alter table big drop constraint <const_name>
Trang 166 Bounce the database server, run the script access.sql again and measure the time that is used for execution Write down the execution time Compare it with result from 4 and explain the difference.
Does it make sense if the DBMS uses the index in the following query?
select sum(number)
from big
where mod(id,2)=0
Explain!
Assume we have an index on the column number:
create index idx1 on big(number)
Does it make sense if the DBMS uses this index in the following query?
select count(id)
from big
where number=0
Explain!
Trang 17DAY 4
Module 1: Overview of IBM Informix Dynamic Server Exercise
Match the term on the left with the correct definition on the right.
_D_ chunk A Physical contiguous pages on a disk that are assigned to a specific
tblspace
_H_ virtual processors B Largestdata from disk.part of shared memory that is dedicated to
holding_A_ extent C Area on disk that holds before-images of pages
_G_ logical log D A unit of disk space that is assigned to a dbspace
_B_ buffer pool E System event in which modified buffers are copied to disk
_E_ checkpoint F Basic unit of storage and I/O in a server system
Trang 18_F_ page G Space on disk that holds transaction information.
_C_ physical log H Processes that make up the database server
_K_ resident portion I Shared-memory component that contains thread and session data. J dbspaces J A logical collection of chunks that forms a pool of disk space used by
tables and databases_I_ virtual portion K Shared-memory component that contains the buffer pool
Module 3 ‘Configuring Client-Server Connectivity’ Exercises
1 Check your environment using the Unix command env
- INFORMIXDIR: /opt/IBM/informix
- ONCONFIG: onconfig.ol_informix1170_1
- INFORMIXSQLHOSTS:/opt/IBM/informix/etc/sqlhosts.ol_informix1170_1
2 Use Nautilus to locate the file referenced by the environment variable ONCONFIG in
the directory INFORMIXDIR/etc and open it in a text editor.
3 Locate the parameter DBSERVERALIASES in the configuration file and add the alias
shm_informix1170 to this parameter Save your configuration file.
4 Locate the file referenced by the environment variable INFORMIXSQLHOSTS Add the appropriate connectivity information in a second line of the file Remember: The syntax of the sqlhosts file requires an entry in the last column of a rows, although it is not needed for the shared memory configuration
Trang 195 To make your new server alias become effective shutdown and restart your database server.
6 Start dbaccess and choose the menu item Connection Choose your new alias shm_informix1170 and press the return key, when asked for the user name The connection should be established.
DAY 5
Module ‘Configuring a Database Server’ Exercises
In this exercise, you will configure and initialize a database server from scratch Since
you already have a configured server on your machine, this exercise will also provide
practice in configuring in a multiple-residency environment Multiple residency is simply
running multiple database servers on a single machine.
1 To reduce load from your virtual machine shutdown your current server instance using
Trang 20INFORMIXDIR/etc using Nautilus and open it in a text editor This is the configuration file of your current pre-configured DBMS Locate the parameter SERVERNUM and write it down Close the file.
4 The environment of your current DBMS is defined in the script.bashrc, which is
automatically executed every time a terminal is opened In your home directory, create a new file yourname.sh to set environment variables for your new server Use the
Trang 215 Go to your terminal window and execute the script using the command
./yourname.sh
The changes to your environment should now show up, if you use the env command.
Trang 226 In your home directory, create a cooked file called rootchunk to be used as the first chunk in your root dbspace Use the
chmod 660
command to make sure that owner, group, and permissions on this file are properly set.
Trang 237 Since we want to configure network connection using TCP we should add an entry to the /etc/services file This file can only be changed by the Unix super user ‘root’.
You can edit the file with root permissions with the following command:
sudo gedit /etc/services
Add a TCP service to the bottom of the file and use new unused port.
Trang 248 In your home directory, create the file sqlhosts.yourname Add a line to the file that configures network connectivity for your new server.
Trang 259 Use Nautilus to locate the $INFORMIXDIR/etc directory Copy the file
onconfig.std to your configuration file onconfig.yourname Open this file using a
text editor.
We have to change some configuration parameters:
Set ROOTPATH to the full path and filename of the rootchunk file you created in
step 5 Do not change the ROOTNAME, ROOTSIZE, or ROOTOFFSET parameters.
Set the MSGPATH configuration parameter to <pathname>/online.log.
Substitute your home directory for the pathname Do not change the CONSOLE
parameter.
Set SERVERNUM to a number that is different from the number you found in 3 Remember: The SERVERNUM is the key to distinguish the old and to new server
instance from each other.
Set DBSERVERNAME to the same value as INFORMIXSERVER (which should be
yourname).
Set the TAPEDEV and LTAPEDEV parameters to /dev/null These parameters
specify the name of the devices to use for backing up the database server and
logical logs, respectively, using the ontape command They will be discussed
further in a later module.
Trang 3010 Issue the following command to initialize the server
oninit -iy Remember: The -i option of the oninit command is used to initialize the disk space for a database instance for the first time Any data that currently resides in your root dbspace is overwritten during this initialization.
11 View the contents of the message log using the onstat -m command Answer the
following questions:
At what time was the disk initialization completed?
When does the first checkpoint occur?
When does your server go to online mode?
When does the building of sysmaster begin?
How many logical logs are used during the building of sysmaster and sysutils?
When does the building of sysmaster complete?
When does the building of sysutils complete?
12 Use dbaccess to connect to your brand new database server Create a database in
this server using the SQL command
create database simpledb
13 Open a second terminal window Check the environment It should be the
environment of the ‘old’ preconfigured server It should still be possible to start the
server and to connect to this server Try it!
DAY 6
Module ‘Transactions’ Exercises
1 Use dbaccess and run the following SQL statement to create a database with logging: create database testdb with log;
Trang 312 Run the following statements in dbaccess while you are connected to your new database:
create table simple(id int);
Trang 323 Close the terminal in which you run dbaccess Open a new terminal and start dbaccess again Connect to testdb and execute
select * from simple
Does the result meet your expectations?
4 Drop the testdb database Since you are not allowed to drop the database you are currently connected to, connect to the sysadmin database by executing the SQL command
database sysadmin
Then execute
drop database testdb;
Trang 33create database testdb
You should now be connected to your new database Execute the statements from exercise 2 again What do you observe? What is default behavior of a database concerning transactions and logging?
Trang 345 We now create a database with a different log mode: Execute the following
statements:
database sysadmin;
drop databse testdb;
create database testdb with log mode ansi
You should now be connected to your new database Run the statements from exercise
2 again What happened?
Trang 356 If we use log mode ANSI we are always in a transaction There is no need for explicit begin work statements Remove all begin work statements and run the script again.
Trang 367 Do not close dbaccess Close the terminal in which dbaccess is running Start a new terminal, run dbaccess, and execute
select * from simple
At first glance the result might appear bizarre Try to find reason for the result.
Module ‘Locking’ Exercises
For this lab exercise you may use any Informix DBMS server that is up and running There are no specific requirements.
1 Start dbaccess and create a database with logging:
create database testdb with log;
Trang 372 Create a simple table:
create table simple(
id int
)
3 Start a transaction and insert a new record: begin work;
insert into simple values( -23);
Do not commit this transaction!
Trang 384 Open a second terminal, start dbaccess and connect to testdb Try to execute the statement
update simple
set id=23
where id=-23
Try to understand the error message Does this meet your expectations?
5 Execute the following statement in terminal 2:
select *
from simple
Trang 39set isolation to dirty read ;
select * from simple where id=-23
Explanation:
Although there is no way to explicitly lock rows inside SQL, there are ways to change the isolation level In the 'dirty read' scenario session 2 sees all records even those which are not yet committed The choice of the isolation level depends on your use case.
The default setting is
set isolation to committed read;
Trang 407 Go back to terminal 1 and commit the transaction Switch to terminal 2, switch the isolation level back to 'committed read' and execute the following command:
begin work;
select count(*) from simple
8 Switch to terminal 1 and execute the following command
insert into simple values(42);
Switch to session 2 and execute the following command again
Trang 41select count(*) from simple
The result is now different from the result in exercise 7.
Trang 429 May be we do not want that the result set of a select statement changes while we are in a transaction The isolation level 'repeatable read' gives you a higher degree of isolation than ‘committed read’: When we use ‘repeatable read’ the result set of select statements inside a transaction is never changed by other sessions:
Commit the transaction you started in exercise 7 in terminal 2 and execute the following commands in terminal 2:
set isolation to repeatable read ;
begin work;
select count(*) from simple
Trang 4310 Switch to session 1 What happens if we now try to insert a new record? Try it! Explanation:
Session 2 puts s-locks on records to prevent changes to the result set It is even
possible that the complete table is locked.
The isolation level ‘repeatable read’ is the highest isolation level that is available in an Informix DBMS, but the high amount of locks is the price we have to pay.