Showing posts with label Oracle DB. Show all posts
Showing posts with label Oracle DB. Show all posts

Sunday, September 16, 2018

Sample Oracle Database for Learning SQL

1- Introduction

LearningSQL is a small database, used as an example in the instructions of learning SQL on the website of  o7planning. There are 3 version on Databases:
  • Oracle
  • MySQL
  • SQLServer.
In this document, I will instruct you to create this database on Oracle.
This database is used in the documentation to learn SQL (Oracle) at:

2- Download Script

Download script at:
Direct Mediafire
Download Download
With Oracle, you just need to care about the file:
  • LearningSQL-Oracle-Script.sql

3- Run Script

There are two ways to create SCHEMA: LearningSQL
  1. Using SQL Plus
  2. Using visual tools.

3.1- Create SCHEMA LearningSQL using SQL Plus

Run 2 commands:

1
2
3
4
5
6
-- Create Schema named learningsql,  password 1234
Create user learningsql identified by 1234;
 
-- For learning purposes:
-- Grant highest privilege DBA (DB Admin) for the newly created user.
grant dba to learningsql;
Connect to the newly created SCHEMA:

1
2
3
4
-- Connect to Schema
-- Syntax: connect /@
 
connect learningsql/1234@db11g;

Run Script:


1
2
3
-- Run Script file in SQL Plus
 
@F:\LEARNING_SQL\LearningSQL-Oracle-Script.sql
Script run successfully:

3.2- Create SCHEMA LearningSQL using visual tool

On the visual tools (Here I use PL/SQL Developer), connected to the user with DBA authority (for example: system).
See more:
Execute script to create schema: learningsql.

1
2
3
4
5
6
7
8
-- Create schema named learningsql
 
Create user learningsql identified by 1234;
 
-- For learning purposes:
-- Grant highest privilege DBA (DB Admin) for the newly created user.
 
grant dba to learningsql;
Connect to Schema you just created:
Copy content of LearningSQL-Oracle-Script.sql into PL/SQL Developer and execute.
Create Schema success.

4- Overview LearningSQL Database

LearningSQL is a small database simulating the data of a bank:
Table Name Description
ACCOUNT The table saves the bank accounts. Each of customer can register multiple accounts each of which corresponds to a product type provided by the bank.
(See more: PRODUCT)
ACC_TRANSACTION The table saves the transaction history between the bank and a certain account.
BRANCH The branch of bank.
BUSSINESS  
CUSTOMER The table of customers
DEPARTMENT The department table of bank
EMPLOYEE The employee table of bank
OFFICER  
PRODUCT The products and services of bank, such as:
  • Savings account
  • The business lines of credit
  • The loans to small business
  • .....
PRODUCT_TYPE The types of products and services, such as:
  • Bank acounts
  • Loans to individual and business
  • The provider of insurance.

5- The structure of the tables


5.1- ACCOUNT

5.2- ACC_TRANSACTION

5.3- BRANCH

5.4- BUSINESS

5.5- CUSTOMER

5.6- DEPARTMENT

5.7- EMPLOYEE

5.8- INDIVIDUAL

5.9- OFFICER

5.10- PRODUCT

5.11- PRODUCT_TYPE

Installing and configuring Oracle Database 11g



1- Introduction

This document is based on:
  • Oracle 11g R2 (Release 2)  (64 bit)

  • Window 7 (64 bit)

-
-
See more:
  • SQL Tutorial for Beginners with Oracle

2- Download Oracle 11g


Oracle allows you to freely download. You can access the home page of Oracle to download this software.
Please check whether your operating system is 32bit bit or 64bit to download exactly.
After finishing downloading, you have 2 zip files. Decompress them in the same folder.

3- Installing Oracle 11g on Windows


First, we need to understand the structure of Oracle a bit:
The structure of Oracle includes:
  1. Oracle software
  2. And Database
Each Database contains many SCHEMA, and the name of Schema is simultaneously the User name to log in that Schema. A Schema contains a system of tables, views, functions, procedures, packages, etc.

That means that after installing the  Oracle software is complete, we can create one or more Database. Usually you only need 1.
As illustrated below, we install Oracle software, and create two database named Db11g and ORCL respectively. The name of the database is also known as SID.

Setup Oracle:

If you decompress two installing zip files of Oracles in two folders, and begin installing the setup file in the folder 1. During the installment, it will require you to indicate the position of folder 2 for continuing to install.
We will install Oracle software and immediately create a Database named as  db11g (or SID = db11g).
For the studying purpose or for small companies, select:
  • Desktop class
To store many kinds of language (including Vietnamese), you should select: Unicode ( AL32UTF8)
  • Character Set: Unicode (AL32UTF8)
  • Global database name: db11g (You can select another name, this is the name of database, known as SID)
And wait until the installation is complete.
Now the installment of Oracle is successful. Parameters relating to database will be synthesized, and you should remember this information.
In the next step, the installer will create Database named as db11g like the previous step where we named.
This is the summary information after a successful installation:
If Windows shows a warning of blocking the activity of Oracle, you click on  "Allow access" for allowing Oracle to work.

4- Check the installation and install the expansion

After the successful installation, services of Oracle are automatically started. We can access to Service of Windows to check.
There are 7 services of Oracle, but usually three services are enough for the operation:
  • OracleDBConsole
  • OracleOra_home1TNSListener
  • OracleService



4.1- Oracle DB Console

OracleDBConsole Service helps you access to the administration page to manage Database through URL:
Using  OracleDBConsole allows you to see overall how your DB is operating, how resources are used, how users work, and what order is executed,... (In this document, I do not mention it in detail).
If your Oracle is used for the purpose of learning, you can turn off the  OracleDBConsole service for avoiding wasting computer resources unnecessarily. For the best, you turn the starting mode to Manual so that the service does not automatically start whenever the computer starts. You can actively run it if necessary.

4.2- SQL Plus

SQL Plus is a simple Console tool that allows you to execute SQL command. Usually, we work with a more visual tool (It is unavailable when you install Oracle) that you need to install additionally. It can be software provided by a third party.
To have the highest permissions we log into user sys.

Syntax:
  • /@
For example:
  • sys/admin@db11g
To login user sys with the highest privileges (sysdba: System DB Admin)
  • sys/admin@db11g as sysdba
Here I access the system (user) to create an example schema (Not necessarily use DBA privileges)
Create a new  SCHEMA whose name is myschema and password is 1234. In essence, we create a user whose name is myschema, as user and schema are the same name in Oracle.







-- Create user (schema), password 1234
Create user myschema identified by 1234;
-- Grant to myschema.
-- For example: dba: DB Admin.
grant dba to myschema;
Connect to the Schema just created:


connect myschema/1234@db11g;
Try to query a table:
Note: DUAL is a system table, can use in all SCHEMA, can try query it.



-- DUAL is a system table.
Select * from Dual;
Create table:












-- Create table Account
Create table Account (User_Name varchar2(30), Full_Name varchar2(64) ) ;
-- Insert 2 row to Account.
Insert into Account(user_name, full_name) values ('gates', 'Bill Gate');
Insert into Account(user_name, full_name) values ('edison', 'Thomas Edison');
-- Query
Select * from Account;
You should install a visual tool,  SQL Plus is just a basic tool.
You can see the instruction of installing and using a visual tool ( PL/SQL Developer):
  • Installing and configuring PL/SQL Developer

4.3- Create new Database

As we all know, Oracle can create multiple databases.

Each database has a name, the name is usually the same as the SID (System Identify DB).
Database have one or more Schema
We rarely have to add a new Database, if any, we add a new Schema to an available Database. However, during the installation of Oracle, if you don't select to automatically create Database, you can use this function to create a new DB. Besides, if you install Database whose Character Set is unsuitable, you can delete DB and create a new one. 

In case you want to delete available Database:

Crate a Database:

Enter Database name and SID. For the best, SID should be alike Database name.
The installation is simple, you can continue.