1 / 78

Guide to Oracle 10g

Guide to Oracle 10g. Chapter 2: Creating and Modifying Database Tables. Database Objects. An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views Stored programs Etc. Database Queries.

sheehanj
Télécharger la présentation

Guide to Oracle 10g

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Guide to Oracle 10g Chapter 2: Creating and Modifying Database Tables Guide to Oracle 10g

  2. Database Objects • An Oracle database consists of multiple user accounts • Each user account owns database objects • Tables • Views • Stored programs • Etc. Guide to Oracle 10g

  3. Database Queries • Query: command to perform operation on database object • Create • Modify • View • Delete • Structured Query Language (SQL) • Standard query language for relational databases Guide to Oracle 10g

  4. SQL Command Types • Data Definition Language (DDL) • Used to create and modify the structure of database objects • Data Manipulation Language (DML) • Used to insert, update, delete, and view database data Guide to Oracle 10g

  5. DDL Commands • Used to create and modify the structure of database objects • CREATE • ALTER • DROP • DDL commands execute as soon as they are issued, and do not need to be explicitly saved Guide to Oracle 10g

  6. DML Commands • Used to insert, view, and modify database data • INSERT • UPDATE • DELETE • SELECT • DML commands need to be explicitly saved or rolled back • COMMIT • ROLLBACK Guide to Oracle 10g

  7. User Accounts • Each Oracle database user has a user schema • Area in the database where the user’s database objects are stored • Identified by a unique username and protected by a password • Each user schema is granted specific privileges Guide to Oracle 10g

  8. Types of Database Privileges • System Privileges • Control the operations that the user can perform within the database • Connecting to the database (Create Session), creating new tables, shutting down the database, etc. • Object Privileges • Granted on individual database objects • Controls operations that a user can perform on a specific object (insert data, delete data, etc.) • When you create an object in your user schema, you can then grant object privileges on that object to other database users Guide to Oracle 10g

  9. Break Time: SQL Plus • Oracle SQL command line utility for issuing SQL commands • Starting SQL Plus LOGON to YOUR Oracle Account Guide to Oracle 10g

  10. How to Access Your Oracle Account 1. Click the START button, point to Programs 2. Select Oracle –Oracle10g, then 3. Click Application Development, then 4. Select SQL PLUS User Name: Password: Host string: Guide to Oracle 10g

  11. Creating New User Accounts • Done by DBA • Syntax: CREATE username IDENTIFIED BY password; Guide to Oracle 10g

  12. Oracle Naming Standard • Oracle database objects must adhere to the Oracle Naming Standard • 1 to 30 characters long • Must begin with a character • Can contain characters, numbers, and the symbols $, _, and # Guide to Oracle 10g

  13. Defining Database Tables • To create a table, you must specify: • Table name • Field names • Field data types • Field sizes • Constraints Guide to Oracle 10g

  14. Table and Field Names • Must follow the Oracle Naming Standard • Each table in a user schema must have a unique name within that user schema • Each field in a table must have a unique name within that table Guide to Oracle 10g

  15. Creating a Table CREATE TABLEtablename (fieldname1 data_type, (fieldname2 data_type, …) Guide to Oracle 10g

  16. Oracle Data Types • Data type: specifies type of data stored in a field • Date, character, number. • LONG, RAW, LONG RAW, BLOB • Uses • Error checking • Efficient use of storage space Guide to Oracle 10g

  17. Oracle Character Data Types • VARCHAR2 columnname VARCHAR2(max_size) • Variable-length character strings • Max_size can be between 1 and 4,000 characters • Must specify the size • No trailing blank spaces are added • If more than max_size data is inserted, an error occurs. • Example declaration: student_name VARCHAR2(30) Guide to Oracle 10g

  18. Character Data Types • CHAR columnname CHAR(max_size) • Fixed-length character data • Max_size can be between 1 and 2000 characters • Max_size is optional. Default is 1. • Adds trailing blank spaces to pad width • If more than max_size data is inserted, an error occurs. • Example declaration: student_gender CHAR(2) Guide to Oracle 10g

  19. Character Subtypes • Examples: • VARCHAR2(5) ‘Smith’ or ‘Smi’ • CHAR(5) ‘Smith’ or ‘Smi ’ Guide to Oracle 10g

  20. Question: Which query will possibly generate student information? s_last CHAR(15); SELECT s_last, s_first, s_address FROM student WHERE s_last = ‘Smith’; s_last VARCHAR2(15); SELECT s_last, s_first, s_address FROM student WHERE s_last = ‘Smith’; • What data type should be used if there is any chance that all column spaces will NOT be filled? • Answer: VARCHAR2 Guide to Oracle 10g

  21. Character Data Types • 3. NVARCHAR2 and NCHAR • Analogous to VARCHAR2 and CHAR but use Unicode rather than ASCII • Used to hold character data in languages other than English (Japanese). Guide to Oracle 10g

  22. Number Data Type • NUMBER • stores negative, positive, fixed, and floating point numbers values between 10-130 and 10126 • General declaration format: variable_name NUMBER(precision, scale) Guide to Oracle 10g

  23. NUMBER Data Types • Number type (integer, fixed point, floating point) specified by precision and scale • Precision: total number of digits on either side of the decimal point. It does not include the decimal point itself or any commas or any formatting symbols. • Scale: number of digits to right of decimal point Guide to Oracle 10g

  24. Integer Numbers • Whole number with no digits to right of decimal point • Precision is maximum width • Scale is omitted • Sample declaration: s_age NUMBER (2) Guide to Oracle 10g

  25. Fixed Point Numbers • Contain a specific number of decimal places • Precision is maximum width • Scale is number of decimal places • Sample declaration: item_price NUMBER(5, 2) • 259.99 33.89 (decimal point is not included) Guide to Oracle 10g

  26. Floating Point Numbers • Contain a variable number of decimal places • Precision and scale are omitted • Sample declaration: s_GPA NUMBER Guide to Oracle 10g

  27. Date Data Type • DATE • Stores dates from 1/1/4712 BC to 12/31/4712 AD • Stores both a date and time component • Default date format: DD-MON-YY HH:MI:SS AM • example: 05-JUN-03 12:00:00 AM • Sample declaration: s_dob DATE Guide to Oracle 10g

  28. Specifying Date and Time Values • If no time value is given when a new date is inserted, default value is 12:00:00 AM • If no date value is given when a new time is inserted, default date is first day of current month Guide to Oracle 10g

  29. TIMESTAMP Data Type • The same as Date DT, but it stores also fractional seconds. • Field Timestamp(Fr_Se_Precision) • E.g: ship_dt Timestamp(2) • Fractional Seconds Precision default value is 6 (If omitted). Guide to Oracle 10g

  30. Interval Year to Month Data Type • Field Interval Year(Y_Pr) To Month. • Y_Pr: Year Precision(Default: 6). • E.g: elapsed Interval Year(2) To Month. • Possible Values: +02-11 :add 2 years and 11 months to a known date. -11-4:subtract 11 years and 4 months. Guide to Oracle 10g

  31. Interval Day to Second Data Type • Field Interval Day(D_Pr) To Second(Fr_Se_pr). • D_Pr: Day Precision(Default : 2). • Fr_Se_Pr: Fractional Seconds Precision (Default : 6). • Possible value: -04 03:20:32.00 (Days Hours:Minutes:Seconds.Fractions) Guide to Oracle 10g

  32. Large Object (LOB) Data Types • Binary Large Object (BLOB) • Stores up to 4 GB of binary data • Character Large Object (CLOB) • Stores up to 4 GB of character data • BFILE • Stores a reference to a binary file maintained in the operating system • NCLOB • Character LOB that supports 16-bit character code Guide to Oracle 10g

  33. 6. Large Object (LOB) Data Types Ex: f_image BLOB; Guide to Oracle 10g

  34. Declaring LOB Data Fields • Item size is not specified • Examples: item_image BLOB item_image BFILE Guide to Oracle 10g

  35. Creating a Database Table • Syntax: CREATE TABLE table_name ( fieldname1 datatype, fieldname2 datatype, …); • Example: CREATE TABLE my_students ( s_id NUMBER(6), s_name VARCHAR2(30), s_dob DATE, s_class CHAR(2)); Guide to Oracle 10g

  36. Constraints • Rules that restrict the values that can be inserted into a field • Types of constraints • Integrity: define primary and foreign keys • Value: specify values or ranges of values that can be inserted Guide to Oracle 10g

  37. Constraint Levels • Table constraint • Restricts the value of a field with respect to all other table records • Example: primary key value must be unique for each record • Column constraint • Restricts values in a specific column • Example: values in an S_GENDER field must be ‘M’ or ‘F’ Guide to Oracle 10g

  38. Constraint Names • Internal name used by DBMS to identify the constraint • Each constraint name in a user schema must be unique • If you do not name a constraint, the system will automatically generate an unintuitive name starts with SYS_Cn. n is a numeric value. Guide to Oracle 10g

  39. Constraint Names • Constraint naming convention: tablename_fieldname_constraintID • Constraint ID values: • Primary key: pk • Foreign key: fk • Check condition: cc • Not NULL: nn • Unique: uk • Example constraint name: my_students_s_id_pk Guide to Oracle 10g

  40. Constraint Names 10g too Guide to Oracle 10g

  41. Primary Key Constraints • Table-level • Defining a primary key: CONSTRAINT constraint_name PRIMARY KEY • Example: s_id NUMBER(6) CONSTRAINT student_s_id_pk PRIMARY KEY Guide to Oracle 10g

  42. Primary Key Constraints • Can be defined when field is declared Guide to Oracle 10g

  43. Primary Key Constraints • Can also be defined after all table field definitions are completed Guide to Oracle 10g

  44. Composite Primary Keys • Syntax: CONSTRAINT constraint_name PRIMARY KEY (field1, field2) • Must be defined after fields that compose key are defined Guide to Oracle 10g

  45. Foreign Key Constraints • Table-level • Can only be defined after field is defined as a primary key in another table • Syntax: CONSTRAINT constraint_name REFERENCES primary_key_table_name (field_name) Guide to Oracle 10g

  46. Foreign Key Constraints • Can be defined when field is declared NOTE: faculty TABLE MUST EXIST BEFORE my_students. Guide to Oracle 10g

  47. Foreign Key Constraints • Can also be defined after all table field definitions are completed Guide to Oracle 10g

  48. Value Constraints • Column-level • Restricts data values that can be inserted in a field • In general, avoid value constraints because they make the database very inflexible Guide to Oracle 10g

  49. Types of Value Constraints • Check condition: restricts to specific values • Example: s_gender (M or F) CONSTRAINT my_students_s_gender_cc CHECK (s_gender = ‘M’) OR (s_gender = ‘F’) • Not NULL: specifies that a field cannot be NULL • Example: CONSTRAINT my_students_s_dob_nn NOT NULL Guide to Oracle 10g

  50. Types of Value Constraints • Default: specifies a default value that is inserted automatically unless the user insert an other value • Example: Must be created in the column declaration, NOT a separate command beginning with CONSTRAINT. s_state CHAR(2) DEFAULT ‘WI’ • Unique • Table constraint • Specifies that a non-primary key field must have a unique value CONSTRAINT consultant_c_email_uk UNIQUE (c_email) (Primary key constrain does not allow NULL, but this does) Guide to Oracle 10g

More Related