Beruflich Dokumente
Kultur Dokumente
Additional information
http://technet.oracle.com - for developers and administrators, available for:
Download of Oracle products Download and viewing of product documentation
http://www.mini.pw.edu.pl/~maczewsk/oracle
Presentations from the lecture Lab tasks
Grades
Each lab will start with a test
test duration: 15 90 minutes depending on the task each test will be graded in the scale 0 10 points at the end one worst result will be excluded
Final exam:
exam duration: 90 minutes will take place in the computer room
Lecture plan
Part 1 basic database administration:
creating database objects: users, tables, indexes, tablespaces, datafiles etc. managing database objects networking, connecting to remote databases programming Oracle PL/SQL language
History
1978 Oracle V1, never officially released 1980 Oracle V2 released, written in assembly language 1994 Oracle 7 for PC released 1998 Support for Linux platform 1999 Oracle 8i with Java integration 2001 Oracle 9i 2004 Oracle 10g 2007 Oracle 11g
Oracle database
The best database for large database systems:
most reliable in terms of data safety highest availability scalable good internationalization support
Oracle shortcomings:
price expensive speed not the fastest database available amount of resources consumed
Data safety
Many features that ensure safety of data:
multiplexing of important database files writing changes to data files and to special log files. Log files are used to recover database after software or hardware failure archiving of log files. Archived log files can be written to backup location(s) protection from human errors:
possibility to view data from some point in time in the pas possibility to use backup and perform point in time recovery
Availability
Very high availability:
online backups without shutting down the database most administrative tasks can be performed while the database is running and available for users, for example: analyzing, rebuilding indexes, moving data disk failure: it is possible to turn off and recover only the part of the database that was affected by the failure, the rest of the database can continue running parameters that ensure that recovery after failure will take no more than the specified time standby database second database that can be opened if the main database fails
Scalability
Oracle can handle unlimited amount of data:
no limit on number of rows, number of tables etc. no limit on total size of the database
Internationalization
Database supports multiple languages/locales:
sorting of text can be done in client language order date, time and currency is client dependent character set conversion database running in utf8 or iso8859-2 will convert data for clients using windows-1250
Oracle editions
Express edition:
Simple installation and administration, some features are not available (for example: Java Virtual Machine is disabled)
Standard edition:
Most frequently used features are available
Enterprise edition:
All features, very expensive
Resources consumed
Oracle consumes large amounts of resources:
new, empty database at least 200MB, usually over 1GB at least 256MB RAM
Single database
Many other database systems (Postgres, MSSQL) can have multiple databases opened by a single database engine
each application can use separate database for its data
Data storage
Many other database systems (Postgres, MySQL) store table data in a separate file or group of files Oracle stores table data in a tablespace. Multiple tables are normally stored in the same tablespace. Tablespace can be stored in one or more data files (physical disk files)
Transactions
Oracle executes every statement in a transaction
there is no command to start a transaction (like BEGIN TRANSACTION) transaction is started automatically with a first statement after COMMIT or ROLLBACK usually transaction must be finished manually with COMMIT, exceptions:
it is possible to turn on AUTOCOMMIT (by default: disabled) some statements commit the transaction automatically: all CREATE, ALTER, DROP and TRUNCATE statements
Transaction example
-- user connects to the database DELETE FROM table1; -- new transaction created INSERT INTO table1 VALUES (1, some text); INSERT INTO table1 VALUES (2, some text); CREATE TABLE table2 ( id NUMBER PRIMARY KEY, text VARCHAR2(256) ); ROLLBACK; -- table1 contains 2 rows, -- the rollback statement rolled -- back empty transaction
Constraint rule that must be satisfied by each table row. Constraint can be:
PRIMARY KEY constraint NOT NULL constraint UNIQUE constraint FOREIGN KEY constraint (relation) CHECK constraint
Trigger action performed when user makes changes to database data Session single connection to the database. One user can open multiple sessions to the database Transaction set of operations that are visible as single (atomic) operation on the database. Transaction belongs to a single open database session.
Connect string:
determines location of the database, if not specified local database
Connect mode specified for administrative connections (database startup and shutdown)
Networking
Connecting to Oracle
Connection to Oracle is possible:
in local mode when connecting to local database only via network when connecting to some other database
The listener
Oracle Listener is separate from the database In Windows it starts as service OraclexxxxTNSListener One listener can handle connections for multiple databases Listener configuration determines how clients will connect to the database. By default listener:
uses TCP/IP listens on port 1521 default Oracle port
Listener configuration
Listener configuration is in
$ORACLE_HOME/network/admin/listener.ora
Example configuration:
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = host_name)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0)) ) )
Other commands:
lsnrctl stop stops the listener lsnrctl status shows list of services that this listener supports
Naming configuration
When connecting to database using SQLPlus you specify:
user name password database name
When the database name is empty connection to the local database When the database name is not empty lookup of the database name. By default database names are looked up in tnsnames.ora file
tnsnames.ora
Location:
$ORACLE_HOME/network/admin/tnsnames.ora
tnsnames.ora - example
Typical entry in the tnsnames.ora file:
TEST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )
tnsnames.ora - example
Entries in tnsnames.ora enable:
specifying multiple addresses for a connection (connect time failover) request load balancing between the list of available addresses
TEST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = other_host) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )
tnsnames.ora - example
Load balancing example
TEST = (DESCRIPTION = (ADDRESS_LIST = (LOAD_BALANCE=on) (FAILOVER=off) (ADDRESS = (PROTOCOL = TCP)(HOST = name_or_ip) (PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = other_host) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = database_instance_name) ) )
Database administration
Administrative privileges
Database administration requires strong privileges:
SYSDBA the most powerful database privilege, enables database startup, shutdown and access to all data in the database SYSOPER enables database startup and shutdown without access to database data DBA role (a group of privileges) giving user full administrative access, but without the right to start and shut down the database
Administrator accounts
Each Oracle database has two accounts:
SYS owner of the administrative objects, created with DBA role, has SYSDBA and SYSOPER privileges SYSTEM automatically created with DBA role
SYS and SYSTEM users should not be used to create any tables
Perform STARTUP and SHUTDOWN operations ALTER DATABASE: open, mount, back up, or change character set CREATE DATABASE DROP DATABASE CREATE SPFILE ALTER DATABASE ARCHIVELOG ALTER DATABASE RECOVER Includes the RESTRICTED SESSION privilege SYSDBA lets you look at any users data
Database shutdown
SHUTDOWN NORMAL waits for all users to disconnect, not recommended can take a long time SHUTDOWN TRANSACTIONAL waits for all users to finish their transactions SHUTDOWN IMMEDIATE rolls back all uncommitted transactions, closes all sessions, closes the database and shuts down the instance SHUTDOWN ABORT aborts the instance, does not close the database properly. Requires automatic recovery after startup.
Database startup
Database startup involves 3 steps:
starting the instance mounting the database opening the database opened database is available for use
STARTUP executes those 3 steps When performing administrative actions, those steps can be performed one by one:
STARTUP NOMOUNT only start the instance, useful when creating the database STARTUP MOUNT start the instance and mount the database (open database control files)
Database startup
Example startup (rarely used):
STARTUP NOMOUNT ALTER DATABASE MOUNT ALTER DATABASE OPEN
System views
Oracle provides many system views that provide information about the database:
v$session all open sessions v$datafile all data files v$logfile all log files v$database general information about the database v$instance general information about the instance
Example scenario
The database cannot start, error message is that file number 4 is corrupt. Recovery:
STARTUP MOUNT SELECT name FROM v$datafile WHERE file# = 4
fix the problem with the file, e.g. restore it from backup recover the database
ALTER DATABASE OPEN
Restricted mode
Sometimes the administrator wants to execute some actions without users being able to log in In order to do that, activate the restricted mode, only users with RESTRICTED SESSION privilege will be able to log in:
STARTUP RESTRICTED starts up in restricted
mode
ALTER SYSTEM ENABLE RESTRICTED SESSION ALTER SYSTEM DISABLE RESTRICTED SESSION