Summary: in this tutorial, you will learn how to create a new database in Db2 and load the sample database into the database server.
Step 1. Launch the Db2 command window
Step 2. Create the Books database
From the DB2 command window, type
db2 command, you will see the following command line:
CREATE DATABASE statement to create the
db2 => CREATE DATABASE books
It will take a while to create the database. Once, you see the following message:
DB20000I The CREATE DATABASE command completed successfully.
It means you have successfully created a new database.
To list all databases in the current instance, you use the
list database directory command:
db2 => list database directory
Here is the output:
System Database Directory Number of entries in the directory = 2 Database 1 entry: Database alias = BOOKS Database name = BOOKS Local database directory = C: Database release level = 14.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number = Database 2 entry: Database alias = SAMPLE Database name = SAMPLE Local database directory = C: Database release level = 14.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =
There are two databases. The
SAMPLE database that we created after installing DB2 database server and the
Books database that we have created.
Step 3. Download Books sample database script files
Click the following link to download the
Books sample database script. You must extract the zip file into a directory e.g.,
There are three files:
- create.sql for creating tables
- data.sql for inserting data into the tables
- drop.sql for deleting all tables
Step 4. Load Books sample database
First, use quit command to exit the session:
db2 => quit
you are now back to the BIN directory.
Next, connect to the
books database using the
> db2 connect to books user db2admin using password
Note that you must replace the password to your password that you provided during installing the Db2 database server.
Then, use the following command to run the
create.sql script to create tables:
> db2 -stvf d:\bookdb\create.sql
Verify if all commands completed successfully.
After that, use the following command to execute the data.sql script to insert data into the tables:
> db2 -stvf d:\bookdb\data.sql
It will take a while to complete all commands in the file.
Finally, use the following command to double check if data is loaded successfully:
>db2 select count(*) author_count from authors
If you see the following output, then congratulation, you have successfully created the books database:
AUTHOR_COUNT ------------ 1388 1 record(s) selected.
In this tutorial, you have learned how to create the
Books sample database and execute the SQL script files to create tables and insert data into them.