A Guide to Oracle9i 1


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

A Guide to Oracle9i 1

  1. 1. Client/Server Databases and the Oracle9 i Relational Database
  2. 2. Relational Database Terms <ul><li>Entity : an object about which you want to store data </li></ul><ul><li>Relationships : links that show how different records are related </li></ul><ul><li>Key Fields : establish relationships among records in different tables </li></ul><ul><li>Five main types of key fields: </li></ul><ul><ul><li>primary keys </li></ul></ul><ul><ul><li>candidate keys </li></ul></ul><ul><ul><li>surrogate keys </li></ul></ul><ul><ul><li>foreign keys </li></ul></ul><ul><ul><li>composite keys </li></ul></ul>
  3. 3. Primary Keys <ul><li>Primary key </li></ul><ul><ul><li>Value must be unique for each record </li></ul></ul><ul><ul><li>Serves to identify the record </li></ul></ul><ul><ul><li>Present in every record </li></ul></ul><ul><ul><li>Can’t be NULL </li></ul></ul><ul><ul><li>Should be numeric </li></ul></ul>
  4. 4. Candidate Keys <ul><li>Candidate key </li></ul><ul><ul><li>Any field that could be used as the primary key </li></ul></ul><ul><ul><li>Should be a unique, unchanging numeric field </li></ul></ul>
  5. 5. Surrogate Keys <ul><li>Surrogate key : created to be the record’s primary key identifier when no suitable primary key exists </li></ul><ul><li>Surrogate key has no real relationship to the record to which it is assigned, other than to identify the record uniquely </li></ul><ul><li>Developers configure the database to generate surrogate key values automatically </li></ul><ul><li>In an Oracle database, you can automatically generate surrogate key values using a sequence </li></ul><ul><li>Surrogate keys are always numerical fields, because the database generates surrogate key values automatically by incrementing the previous value by one </li></ul>
  6. 6. Foreign Keys <ul><li>Foreign key : a field in a table that is a primary key in another table </li></ul><ul><li>Foreign key creates a relationship between the two tables </li></ul><ul><li>Foreign key value must exist in the table where it is a primary key </li></ul>
  7. 7. Composite Keys <ul><li>Composite key: a unique key that you create by combining two or more fields </li></ul><ul><li>Usually comprised of fields that are primary keys in other tables </li></ul>
  8. 8. Personal Database Management Systems <ul><li>DBMS and database applications run on the same workstation and appear to the user as a single integrated application </li></ul><ul><li>Personal databases used primarily for creating single-user database applications </li></ul><ul><li>Support small multiuser database applications by storing the database application files on a file server instead of on a single user’s workstation and then transmitting the files or the parts of files containing the desired data to various users across a network </li></ul><ul><li>Database developers should use a personal database only for non-mission-critical applications </li></ul>
  9. 9. Client/Server Database Management Systems <ul><li>Client/server database </li></ul><ul><ul><li>Takes advantage of distributed processing and networked computers by distributing processing across multiple computers </li></ul></ul><ul><ul><li>DBMS server process runs on one workstation, and the database applications run on separate client workstations across the network </li></ul></ul><ul><ul><li>Preferred for database applications that retrieve and manipulate small amounts of data from databases containing large numbers of records because they minimize network traffic and improve response times </li></ul></ul><ul><li>Organizations generally use a client/server database if the database will have more than 10 simultaneous users and if the database is mission critical </li></ul>
  10. 10. Client/Server Database Architecture
  11. 11. The Oracle9 i Client/Server Database <ul><li>Oracle9 i is the latest release of Oracle Corporation’s relational database </li></ul><ul><li>All Oracle server- and client-side programs use Oracle Net, a utility that enables the network communication between the client and the server </li></ul>
  12. 12. Client-Side Utilities <ul><li>SQL*Plus </li></ul><ul><ul><li>for creating and testing command-line SQL queries and executing PL/SQL procedural programs </li></ul></ul><ul><li>Oracle9 i Developer Suite </li></ul><ul><ul><li>for developing database applications including the following Developer tools: </li></ul></ul><ul><li>Forms Builder </li></ul><ul><ul><li>for creating custom user applications </li></ul></ul><ul><li>Reports Builder </li></ul><ul><ul><li>for creating reports for displaying, printing, and distributing summary data </li></ul></ul><ul><li>Enterprise Manager </li></ul><ul><ul><li>for performing database administration tasks such as creating new user accounts and configuring how the DBMS stores and manages data </li></ul></ul>
  13. 13. Design Principles <ul><li>To avoid creating tables that contain redundant data, group related items that describe a single entity together in a common table </li></ul><ul><li>Do not create tables that duplicate values many times in different rows </li></ul><ul><li>When creating a database and inserting data values, you must specify the data type for each column </li></ul><ul><li>Recall that primary key fields should use a number data type to avoid typographical, punctuation, and case variation errors </li></ul>
  14. 14. The Clearwater Traders Sales Order Database <ul><li>Clearwater Traders </li></ul><ul><li>Markets a line of clothing and sporting goods via mail-order catalogs </li></ul><ul><li>Accepts customer orders via telephone, mail, and fax </li></ul><ul><li>Wants to begin accepting orders using its Web site </li></ul><ul><li>Has decided to offer 24-hour customer order service </li></ul><ul><li>Existing microcomputer-based database system cannot handle current transaction volume </li></ul><ul><li>Managers concerned that the current database does not have the failure-handling and recovery capabilities needed for an ordering system that cannot tolerate failures or downtime </li></ul>
  15. 15. Clearwater Traders Data Requirements <ul><li>Customer name, address, daytime and evening telephone numbers, user names, and passwords </li></ul><ul><li>Order date, payment method (check or credit card), order source (catalog description or Web site), and associated item numbers, sizes, colors, and quantities ordered </li></ul><ul><li>Item descriptions and photo images, as well as item categories (women’s clothing, outdoor gear, and so on), prices, and quantities on hand. Many clothing items are available in multiple sizes and colors. Sometimes the same item has different prices depending on the item size </li></ul><ul><li>Information about incoming product shipments </li></ul>
  16. 16. Clearwater Traders Table Relationships
  17. 17. The Northwoods University Student Registration Database <ul><li>Northwoods University </li></ul><ul><li>Decided to replace its aging mainframe-based student registration system with a more modern client/server database system </li></ul><ul><li>School officials want students to be able to retrieve course availability information, register for courses, and print transcripts using personal computers located in the student computer labs </li></ul>
  18. 18. The Northwoods University Student Registration Database (cont) <ul><li>Faculty members must be able to retrieve student course lists, drop and add students, and record course grades </li></ul><ul><li>Faculty members must also be able to view records for the students they advise </li></ul><ul><li>Security is a prime concern, so student and course records must be protected by password access </li></ul>
  19. 19. Northwoods University Data Requirements <ul><li>Student name, address, telephone number, class (freshman, sophomore, junior, or senior), date of birth, PIN (personal identification number), and advisor ID </li></ul><ul><li>Course call number (such as MIS 101), course name, credits, location, duration, maximum enrollment, instructor, and term offered </li></ul><ul><li>Instructor name, office location, telephone number, rank, and PIN </li></ul><ul><li>Student enrollment and grade information </li></ul>
  20. 20. Northwoods University Table Relationships
  21. 21. The Software Experts Project Management Database <ul><li>Software Experts </li></ul><ul><li>Consulting firm that creates custom software applications </li></ul><ul><li>Managers determine the number and the skill sets of the consultants that a given project requires </li></ul><ul><li>Managers locate available consultants with the necessary skills </li></ul><ul><li>One consultant is named project manager </li></ul><ul><li>When completed, the project manager evaluates all the other consultants and all the consultants evaluate the project manager </li></ul><ul><li>A project can be subdivided into multiple subprojects </li></ul>
  22. 22. Software Experts Requirements <ul><li>Consultant information, including name, address, city, state, ZIP code, phone number, and e-mail address </li></ul><ul><li>Descriptions of consultant skills </li></ul><ul><li>Client information, including the client name, client contact name, and contact phone number </li></ul>
  23. 23. Software Experts Requirements <ul><li>Project information, including the project name, the client associated with the project, associated subprojects, the project manager, and required skill sets </li></ul><ul><li>Dates that a consultant started and finished working on a specific project and total days that the consultant spent working on the project </li></ul><ul><li>Consultant evaluation information, including the date the evaluation was completed, who performed the evaluation, and the evaluation score and comments </li></ul>
  24. 24. Software Experts Table Relationships
  25. 25. Introduction to SQL <ul><li>Structured Query Language (SQL) : the standard query language for relational databases </li></ul><ul><li>Data definition language (DDL) </li></ul><ul><ul><li>Create new database objects </li></ul></ul><ul><ul><li>Modify or delete existing objects. </li></ul></ul><ul><li>Data manipulation language (DML) </li></ul><ul><ul><li>Insert, update, delete, and view database data . </li></ul></ul>
  26. 26. Oracle9 i User Accounts <ul><li>User account - identified by a unique username and password </li></ul><ul><li>User schema - all of the objects that the user creates and stores in the database </li></ul><ul><li>Object owner has privileges to perform all possible actions on an object </li></ul>
  27. 27. Defining Oracle9 i Database Tables <ul><li>To create a new table specify: </li></ul><ul><ul><li>Table name </li></ul></ul><ul><ul><li>Name of each data field </li></ul></ul><ul><ul><li>Data type and size of each data field </li></ul></ul><ul><li>Constraints : restrictions on the data values that a field can store </li></ul><ul><li>Oracle Naming Standard: </li></ul><ul><ul><li>Series of rules Oracle Corporation established for naming all database objects </li></ul></ul><ul><ul><li>Objects must be from 1 to 30 characters long </li></ul></ul><ul><ul><li>Can contain letters, numbers, and the special symbols $, _, and # </li></ul></ul><ul><ul><li>Must begin with a character </li></ul></ul>
  28. 28. Creating a Table <ul><li>CREATE TABLE tablename </li></ul><ul><li>(fieldname1data_type, </li></ul><ul><li>(fieldname2 data_type, </li></ul><ul><li>… ) </li></ul>
  29. 29. Oracle9 i Data Types <ul><li>Data type : specifies the kind of data that a field stores </li></ul><ul><li>Assigning a data type provides a means for error checking </li></ul><ul><li>Data types enable the DBMS to use storage space more efficiently by internally storing different types of data in different ways </li></ul>
  30. 30. Character Data Types <ul><li>VARCHAR2 </li></ul><ul><ul><li>Stores variable-length character data up to a maximum of 4,000 characters </li></ul></ul><ul><ul><li>Values in different records can have a different number of characters </li></ul></ul><ul><ul><li>fieldname VARCHAR2(maximum_size) </li></ul></ul>
  31. 31. Character Data Types <ul><li>CHAR </li></ul><ul><ul><li>Fixed-length character data up to a maximum size of 2,000 characters </li></ul></ul><ul><ul><li>Data values for different records all have the same number of characters </li></ul></ul><ul><ul><li>DBMS adds trailing blank spaces to the end of the entry to make the entry fill the maximum_size value </li></ul></ul><ul><ul><li>Data longer than maximum_size causes an error </li></ul></ul><ul><ul><li>fieldname CHAR[(maximum_size)] </li></ul></ul>
  32. 32. Character Data Types <ul><li>NVARCHAR2 and NCHAR </li></ul><ul><ul><li>Analogous to VARCHAR2 and CHAR but use Unicode rather than ASCII </li></ul></ul><ul><ul><li>Used to hold character data in languages other than English </li></ul></ul>
  33. 33. Number Data Types <ul><li>Stores negative, positive, fixed, and floating point numbers between 10–130 and 10125, with precision up to 38 decimal places </li></ul><ul><li>General Syntax: fieldname NUMBER [([precision,] [scale])] </li></ul><ul><li>Integer: fieldname NUMBER(precision) </li></ul><ul><li>Fixed point: fieldname NUMBER[([precision],[scale])] </li></ul><ul><li>Floating point: fieldname NUMBER </li></ul>
  34. 34. Date and Time Data Types <ul><li>DATE </li></ul><ul><ul><li>Dates from December 31, 4712 BC to December 31, 4712 AD </li></ul></ul><ul><ul><li>Default format DD-MON-YY </li></ul></ul><ul><ul><li>Default time format HH:MI:SS A.M. </li></ul></ul><ul><ul><li>fieldname DATE </li></ul></ul><ul><li>TIMESTAMP </li></ul><ul><ul><li>Similar to DATE but stores fractional seconds </li></ul></ul><ul><ul><li>fieldname TIMESTAMP (fractional_seconds_precision) </li></ul></ul>
  35. 35. Date and Time Data Types <ul><li>INTERVAL YEAR TO MONTH </li></ul><ul><ul><li>Time interval, expressed in years and months </li></ul></ul><ul><ul><li>+02-11 specifies a positive time interval of 2 years and 11 months </li></ul></ul><ul><ul><li>fieldname INTERVAL YEAR[(year_precision)] TO MONTH </li></ul></ul><ul><li>INTERVAL DAY TO SECOND </li></ul><ul><ul><li>Time interval, expressed in days, hours, minutes, and seconds </li></ul></ul><ul><ul><li>-04 03:20:32.00: 4 days, 3 hours, 20 minutes, and 32 seconds </li></ul></ul><ul><ul><li>fieldname INTERVAL DAY[(leading_precision)] TO SECOND[(fractional_seconds_precision)] </li></ul></ul>
  36. 36. Large Object (LOB) Data Types
  37. 37. Constraints <ul><li>Rules that restrict the data values that you can enter into a field in a database table </li></ul><ul><li>Integrity constraints : define primary and foreign keys </li></ul><ul><li>Value constraints: define specific data values or data ranges that must be inserted into columns and whether values must be unique or not NULL </li></ul><ul><li>Table constraint: restricts the data value with respect to all other values in the table </li></ul><ul><li>Field constraint: limits the value that can be placed in a specific field, irrespective of values that exist in other table records </li></ul>
  38. 38. Oracle Constraint Naming Convention <ul><li>tablename _ fieldname _ constraintID </li></ul>
  39. 39. Integrity Constraints <ul><li>Define primary key fields </li></ul><ul><li>Specify foreign keys and their corresponding table and column references </li></ul><ul><li>Specify composite keys </li></ul>
  40. 40. Value Constraints <ul><li>Check conditions: field value must be a specific value or fall within a range of values </li></ul><ul><li>NOT NULL constraints: specify whether a field value can be NULL </li></ul><ul><li>Default constraints: specify that a field has a default value that the DBMS automatically inserts for every record, unless the user specifies an alternate value </li></ul><ul><li>Unique constraints: specify that a field must have a unique value for every table record </li></ul>
  41. 41. Creating Database Tables Using SQL*Plus <ul><li>Type SQL commands at the SQL prompt </li></ul><ul><li>End each command with a semicolon (;) </li></ul><ul><li>Not case sensitive </li></ul>
  42. 42. Log On to SQL*Plus
  43. 43. SQL*Plus Program Window
  44. 44. Create a Table
  45. 45. Table Creation Sequence <ul><li>When creating tables with foreign key references, create referenced tables first </li></ul><ul><li>Always create tables without foreign keys before those with foreign keys </li></ul>
  46. 46. Using Notepad <ul><li>Useful to use Notepad to edit sql commands </li></ul><ul><ul><li>Commands can be edited without retyping </li></ul></ul><ul><ul><li>Commands can be saved </li></ul></ul><ul><ul><li>Saving multiple sql commands in a file creates a script </li></ul></ul>
  47. 47. Errors <ul><li>When an error occurs error information is displayed: </li></ul><ul><ul><li>Line number </li></ul></ul><ul><ul><li>Position on line </li></ul></ul><ul><ul><li>Error code </li></ul></ul><ul><ul><li>Description of error </li></ul></ul><ul><li>Error codes </li></ul><ul><ul><li>3 letter prefix (I.e. ORA) </li></ul></ul><ul><ul><li>5 digit code </li></ul></ul><ul><ul><li>More information on errors can be found at http://otn.oracle.com </li></ul></ul>
  48. 48. Exiting SQL*Plus <ul><li>Three ways to exit SQL*Plus: </li></ul><ul><ul><li>Type exit at the SQL prompt </li></ul></ul><ul><ul><li> lick File on the menu bar, and then click Exit </li></ul></ul><ul><ul><li>Click the Close button on the program window title bar </li></ul></ul><ul><li>Database session ends when SQL*Plus exits </li></ul>
  49. 49. Create Table with Foreign Key Constraint
  50. 50. Viewing Information About Tables <ul><li>describe tablename : displays column names and data types </li></ul><ul><li>Data dictionary : tables that contain information about the structure of the database. </li></ul><ul><ul><li>USER: shows the objects in the current user’s schema </li></ul></ul><ul><ul><li>ALL: shows both objects in the current user’s schema and objects that the user has privileges to manipulate </li></ul></ul><ul><ul><li>DBA: allows users who are database administrators to view information about all database objects </li></ul></ul>
  51. 51. Viewing Tables in the Database
  52. 52. Viewing Constraints on One Table
  53. 53. Modifying and Deleting Database Tables <ul><li>Modify existing database tables by: </li></ul><ul><ul><li>Changing the name of a table </li></ul></ul><ul><ul><li>Adding new columns </li></ul></ul><ul><ul><li>Deleting columns that are no longer needed </li></ul></ul><ul><ul><li>Changing the data type or maximum size of an existing column </li></ul></ul><ul><li>Unrestricted action: some specifications can always be modified </li></ul><ul><li>Restricted action: specifications modified only in certain situations </li></ul>
  54. 54. Unrestricted Action
  55. 55. Restricted Actions
  56. 56. Deleting and Renaming Tables <ul><li>To delete: </li></ul><ul><ul><li>Drop table [tablename] </li></ul></ul><ul><ul><li>Use with caution </li></ul></ul><ul><ul><li>To delete foreign key constraints, add “ cascade constraints ” </li></ul></ul><ul><li>To rename: </li></ul><ul><ul><li>Rename old_tablename to new_tablename </li></ul></ul><ul><ul><li>DBMS automatically transfers to the new table integrity constraints, indexes, and privileges that referenced the old table. </li></ul></ul>
  57. 57. Adding Fields to Existing Tables <ul><li>To add a field: </li></ul><ul><ul><li>ALTER TABLE tablename ADD(fieldname data_declaration constraints); </li></ul></ul>
  58. 58. Modifying Existing Field Data Definitions <ul><li>Can only change datatype to compatible data type (i.e. varchar2 to char) </li></ul><ul><li>ALTER tablename MODIFY(fieldname new_data_declaration); </li></ul>
  59. 59. Deleting a Field <ul><li>ALTER TABLE tablename DROP COLUMN fieldname; </li></ul><ul><li>Can be used to rename a field – first drop, then add </li></ul>
  60. 60. Adding and Deleting Constraints <ul><li>Add a constraint: ALTER TABLE tablename ADD CONSTRAINT constraint_name constraint_definition ; </li></ul><ul><li>Remove a constraint: ALTER TABLE tablename DROP CONSTRAINT constraint_name ; </li></ul>
  61. 61. Enabling and Disabling Constraints <ul><li>When modifying a database it can be useful to disable constraints </li></ul><ul><li>Constraints are enabled by default </li></ul><ul><li>To disable a constraint: ALTER TABLE tablename DISABLE CONSTRAINT constraint_name; </li></ul><ul><li>To enable a constraint: ALTER TABLE tablename ENABLE CONSTRAINT constraint_name; </li></ul>
  62. 62. Summary <ul><li>SQL commands include: </li></ul><ul><ul><li>Data description language (DDL) commands: create, modify, Deleted database objects </li></ul></ul><ul><ul><li>Data manipulation language (DML) commands: insert, update, delete, view database data </li></ul></ul><ul><li>To create a table: </li></ul><ul><ul><li>specify the table name, the name of each data field, and the data type and size of each data field </li></ul></ul><ul><li>Data types ensure correct data values </li></ul><ul><li>Constraints restrict values of database fields </li></ul><ul><li>SQL*Plus commands are not case sensitive </li></ul>
  63. 63. Summary <ul><li>Errors include line number, position, error code </li></ul><ul><li>Use DESCRIBE command to display a table’s fieldnames and data types </li></ul><ul><li>Tables can be modified or deleted but some changes are restricted </li></ul>