Oracle Database Quick Start
Oracle Database Quick Start
Overview
Purpose
In this tutorial, you learn about Oracle Database and how to perform simple operations on tables.
Time to Complete
45 minutes to complete the prerequisites (Note: The time to download Oracle Database software to your local machine depends on the network. Therefore, it is not included in the time specified to complete the prerequisites.)
1 hour to complete this Oracle by Example (OBE)
Introduction
In general, a database is a collection of data treated as a unit. A database management system (DBMS) stores, manages and retrieves a large amount of data in a multi-user environment so that many users can access the same data
concurrently.
Oracle Database is a robust object relational database that provides efficient and effective solutions for database users such as delivering high performance, protecting users from unauthorized access, and enabling fast failure
recovery.
The following is a list of hardware and software needed to install Oracle Database:
Prerequisites
Complete the OBE entitled Installing Oracle Database Software and Creating a Database.
Complete the OBE entitled Getting Started with Oracle Enterprise Manager Express.
If you have problems connecting to the listener, complete the OBE entitled Using the Listener Control Utility to Manage the Listener.
A database schema is a collection of metadata that describes the relationship between the data in a database. A schema can be simply described as the "layout" of a database or the blueprint that outlines how data is organized into
tables.
Schema objects are database objects that contain data or that govern or perform operations on data. By definition, each schema object belongs to a specific schema. The following are commonly used schema objects:
Tables: Basic units of data storage in an Oracle database. Here, data is stored in rows and columns. You define a table with a table name and a set of columns.
Indexes: Performance-tuning methods for allowing faster retrieval of records.
Views: Representations of SQL statements that are stored in memory so that they can be reused.
The Human Resources (HR) schema is part of the Oracle Sample Schemas that you can install with Oracle Database. The following is the entity-relationship diagram of the HR schema:
The REGIONS table contains rows that represent a region such as the Americas or Asia.
The COUNTRIES table contains rows for countries, each of which is associated with a region.
The LOCATIONS table contains the specific addresses for the offices, warehouses, or production sites of a company in a particular country.
The DEPARTMENTS table contains details about the departments in which employees work. Each department may have a relationship representing the department manager in the EMPLOYEES table.
The EMPLOYEES table contains details about each employee who works in a department. Some employees may not be assigned to a department.
The JOBS table contains the job types that an employee can hold.
The JOB_HISTORY table contains an employee's job history.
In this section, you connect to Oracle Database by using the SQL*Plus utility, and you unlock the HR schema.
1. Open a terminal window and execute the oraenv command to set the environment variables.
. oraenv
3. By default, the HR schema is locked. Execute the following statement to unlock the HR schema.
CONNECT hr/hr;
5. The DESCRIBE command provides a description of a specified table or view. The description for tables and views contains the following information:
Column names
Whether null values are allowed (NULL or NOT NULL) for each column
Data type of columns, such as DATE, NUMBER, VARCHAR2
Precision of columns, such as VARCHAR2(50)
Execute the following command to view the description of the EMPLOYEES table:
DESCRIBE EMPLOYEES;
In this section, you execute the SELECT statement to query tables in the HR schema. You also use the ORDER BY and WHERE clauses within the SELECT statement to sort and restrict data in the result set.
Querying Tables
In this section, you execute the SELECT statement to retrieve data from tables and views. You can select rows and columns that you want to return in the output. In its simplest form, a SELECT statement must contain the following:
1. You can display all columns of data in a table by entering an asterisk (*) after the SELECT keyword. Execute the following statement to view all rows and columns in the DEPARTMENTS table:
SELECT *
FROM departments;
2. You can display specific columns of data in a table by specifying the column names in the SELECT statement. Execute the following statement to view the JOB_ID and JOB_TITLE columns in the JOBS table:
Restricting Data
In this section, you use the WHERE clause to restrict the rows that are returned from the SELECT query. A WHERE clause contains a condition that must be met. It directly follows the FROM clause. If the condition is true, the row that
meets the condition is returned.
1. Modify the SELECT statement. Execute the following query to restrict the number of rows to DEPARTMENT_ID 60:
SELECT *
FROM departments
WHERE department_id=60;
Sorting Data
In this section, you use the ORDER BY clause to sort the rows that are retrieved from the SELECT statement. You specify the column based on the rows that must be sorted. You also specify the ASC keyword to display rows in
ascending order (default), and you specify the DESC keyword to display rows in descending order.
1. Execute the following SELECT statement to retrieve the LAST_NAME, JOB_ID, and HIRE_DATE columns of employees who belong to the SA_REP job ID. Sort the rows in ascending order based on the HIRE_DATE column.
2. Modify the SELECT statement to display rows in descending order. Use the DESC keyword.
Creating a Schema
In this section, you create a schema named ONLINE_SHOPPE. This schema portrays an online store that operates with a customer base and commodities. Information about customers is stored in the CUSTOMERS table, information abo
commodities is stored in the COMMODITIES table and order details are stored in the ORDERS table.
Creating a User
Database administrators perform many tasks. One of their more common tasks is creating database users and assigning them unique usernames. After users log in to the database with their username and password, they can
issue database SQL statements to create objects, query objects, and manage the database.
Creating a user is a way to create a schema. In this section, you execute the CREATE USER statement to create and configure a database user.
1. Execute the following statements to connect to the database as an administrator and create a user named ONLINE_SHOPPE.
Assigning Privileges
When multiple users access database objects, you can control the authorization of the objects with privileges. Privileges control whether a user can modify an object that is owned by another user. They are granted or revoked
either by:
System privilege: The right to perform a particular action on any object, such as, tables, views and indexes. Only the instance administrator or a user with the ADMIN privilege can assign or revoke system privileges.
Object privilege: The right to perform a particular action on an object or to access another user's object. An object's owner has all object privileges for that object and can assign object privileges for that object to other
database users.
System privileges:
Create a table, a view, or an index that is owned by any user in the database
Alter a table, a view, or an index in the database
Drop a table, a view, or an index in the database
Object privileges:
You use the GRANT statement to assign privileges to users and roles. To assign privileges, you must have been assigned either the ADMIN OPTION or the GRANT ANY PRIVILEGE system privilege.
1. When you create a user with the CREATE USER statement, the user's privilege domain is empty by default. The administrator assigns privileges to the user based on the tasks that the user may perform in the future. In
this tutorial, the ONLINE_SHOPPE user establishes a session, creates a table, and writes DML statements against tables. Execute the following statements to assign the required privileges to the ONLINE_SHOPPE user:
Creating Tables
Before creating tables in the ONLINE_SHOPPE schema, you should understand the concepts of tables and integrity constraints.
Table: Basic unit of data storage in a database. Within a table, data is stored in rows and columns. You define a table with a table name, a set of columns, a data type, and a width.
Integrity constraints: Rules for columns in a table. You specify these rules to enforce data integrity within the columns for which they are defined. Basic constraints on Oracle Database include the following:
In this section, you execute the CREATE TABLE statement to create tables.
Perform the following steps to create the CUSTOMERS, COMMODITIES, and ORDERS tables in the schema.
1. Connect to the ONLINE_SHOPPE schema and create the CUSTOMERS table with the CUSTOMER_ID column as the primary key.
CONNECT online_shoppe/online;
2. Create the COMMODITIES table with the COMMODITY_ID column as the primary key and the UNIT_PRICE column as a non-null column.
1. Inserting data: You execute the INSERT statement to add rows of data to a database table.
Execute the following statements to insert data into the CUSTOMERS, COMMODITIES, and ORDERS tables.
2. Modifying data: You use the UPDATE statement to modify rows of data in a database table. Execute the following statement to change the unit price of the DVD player from $109 to $129:
3. Deleting data: You use the DELETE statement to delete rows of data from a database table. Execute the following statement to delete the first record in the ORDERS table:
1. Execute the COMMIT statement to save the data manipulation transactions that you performed in the previous section.
COMMIT;
2. Execute the following statements to delete the row whose order ID is R002 and to query the ORDERS table to ensure that the record was deleted.
3. Execute the following statements to undo deletion of the row whose order ID is R002 and to query the table to display the records:.
ROLLBACK;
SELECT * FROM orders;
Note: You cannot undo transactions after you save them permanently with the COMMIT statement.
Removing Tables
In this section, you execute the DROP TABLE statement to remove a table and its data from the database.
An error message is displayed because of the referential integrity constraint on the CUSTOMER_ID column.
2. Include the CASCADE CONSTRAINTS clause to remove the table and its referential integrity constraints..
Revoking Privileges
In this section, you execute the REVOKE statement to revoke user and role system privileges. To revoke a system privilege or a role, you must be assigned the privilege with the ADMIN OPTION.
1. Connect to the database as the SYS user and revoke the CREATE SESSION privilege for ONLINE_SHOPPE.
CONNECT online_shoppe/online;
You cannot connect because you do not have the CREATE SESSION privilege.
Summary
Resources
Credits