Oracle9i Has Several Types of Tablespaces
Total Page:16
File Type:pdf, Size:1020Kb
THE DATA DEFINITION LANGUAGE Tablespaces Oracle9i has several types of tablespaces. These are: 1. Data tablespaces - Contains data and the objects that control the data (i.e. indexes, synonyms). 2. System tablespaces - Contains information Oracle needs to control itself. 3. Temporary tablespaces - Contains temporary information. For example, the space is used if Oracle needs temporary disk space for sorting a large set of rows. 4. Tool tablespaces - Contains space required for Oracle tools. 5. Rollback tablespaces - Contains information such as uncommitted database changes and enables Oracle’s internal mechanisms for read consistency. 6. User tablespaces - Default tablespace assigned to the user. 1 Creating a Tablespace There are several components to the Create Tablespace command: ? Datafile - Contains the name of the file used by the tablespace. ? Autoextend clause - Enables or disables the automatic extension of the tablespace. Off disables this feature. On enables the feature. The next keyword specifies the amount of disk space to allocate when an extension occurs. Unlimited sets no limit. The Maxsize keyword specifies the maximum amount of space to allocate. ? Size - Determines the maximum size of the file used for the tablespace. Two setting symbols are used. These are K and M. Each increment of K increases the file or tablespace size by 1024 bytes. Each increment of M increases the file or tablespace size by 1,048,576 bytes. ? Initial extent - The amount of disk space initially set-aside for the tablespace. An extent is a portion of disk space that is reserved for the tablespace. This disk space is unavailable to any other application until it is released. 2 ? Next extent - The amount of additional disk space that is reserved for the tablespace each time the existing extents are filled. ? Minextents - This option enables the developer to set aside additional extents at the time of creation. ? Maxextents - This option sets a limit to the number of extents a tablespace can use. ? Pct Increase - This option is a growth factor for extents. For example, if it is set at 25, Oracle will add 25 percent to each additional extent. Creating a tablespace SQL> create tablespace realistic_employees datafile 'real.tab' size 4 m 2 default storage (initial 25 k next 25 k 3 minextents 10 maxextents 50) 4 autoextend on next 50k maxsize 100k; Tablespace created. SQL> End Listing 3 The Alter Tablespace command is used to change tablespace parameters. Some of these parameters are: Add datafile - This option is used to add a file to the tablespace. The option uses the same format as used in the create command (‘filename’ [size k or m] [reuse]). The Reuse keyword when used in conjunction with the Size keyword causes Oracle to create the file if it does not exist and to modify the file if it exists. If the Size keyword is not used, Oracle will destroy the contents of any existing file with the name and replace it with the new file. Specifying the Size option alone causes the file to be created if it doesn’t exist and launch an error message if it already exists. Rename datafile - This option is used to change the name of an existing datafile. The keyword does not actually change the name of the file but associates a new name with the file. The syntax of this command is: “rename datafile filename to filename”. Resize - This option can be used to change the size of an existing datafile. The syntax of the options is: “alter database realistic_employees ‘real.tab’ resize 6m”. 4 Autoextend - Changes the size of the datafiles as needed. It does this in increments of Next size to the Maximum of Maxsiz. Using the alter tablespace command to increase the size of the tablespace by adding a data file SQL> ALTER TABLESPACE REALISTIC_EMPLOYEES 2 ADD DATAFILE 'REAL1.TAB' SIZE 8 M; TABLESPACE ALTERED. SQL> END LISTING Eliminating the “realistic_employee” tablespace SQL> DROP TABLESPACE REALISTIC_EMPLOYEES; TABLESPACE DROPPED. SQL> END LISTING 5 Creating and modifying tables Tables are created using the Create Table command. This command has a number of components and options. These are: ? Table name - Determines the table’s name. The name must start with an alpha character. The name may contain letters, numbers, and underscores. It must be in one continuous string of not more than 30 characters. ? Column names - Names of the columns (entity attributes) contained in the table. This component has the same naming restrictions as the table name. The entire set of column definitions must be enclosed by parentheses. ? Column data type - The column definition contains the data type, scale (length), and precision of each column. Valid data types are listed in Table 2.2. ? Constraints - Constraints are options that are used to maintain the integrity to the database. These will be discussed later in this chapter. (optional setting). 6 ? Tablespace - This setting determines the tablespace in which to place the table. If this setting is omitted, the table will be created in the default tablespace assigned to user that executes the Create Table command. It is normal practice to assign a default tablespace to a User account when it is created. This tablespace is the default tablespace for the user. If the user has not been assigned a tablespace and this option is omitted from the Create Table command, the System tablespace will be used. Using the System tablespace for data tables is highly discouraged. (optional setting). ? Partitions - The table data can be divided into numerous partitions. These can be within one or more tablespaces. The specifications to partition a table are placed in the Create Table command. (optional setting) 7 Syntax rules for the Create Table command is: ? Parentheses must enclose the entire table specification. The Create Table keywords and the name of the table are not included in the parentheses. ? A comma completes each column definition. ? Each column name must be unique to the table. ? Oracle reserved words cannot be used as column names. ? A semi-colon ends the command. Creating the “Consultant” table SQL> CREATE TABLE CONSULTANT 2 (EMPLOYEE_ID NUMBER(5,0), 3 FIRST_NAME VARCHAR2(15), 4 LAST_NAME VARCHAR2(25), 5 SPECIALTY VARCHAR2(30)); TABLE CREATED. SQL> END LISTING 8 The following table contains acceptable data types Integer Same as the Number data type. Values will be whole numbers. Columns defined with this format will not accept decimal digits. Integer(n) Specifies an integer data type of the length n. Long Defines a character data type up to 65,535 characters. Only one Long column may be defined per table. This type of column may not be used in subqueries, functions, expressions, Where clauses, or indexes. Long Raw The same as Long, except it contains raw binary data. Long Varchar The same data type as Long. Mislabel Four-byte representation of a secure operating system label. 9 Number Defines a numeric data type with space for 40 digits, space for a sign, and a decimal point. The numbers may be expressed in two manners. The first is with numbers from 0 to 9, the signs + and -, and a decimal point. The second manner is in scientific notation (1.951E4 as 19510). Number(n) Defines a Number column that contains the number of digits equal to the value in n. The maximum number of digits is 105. Number(n, d) Defines a Number column that contains an overall size equal to n and contains the number of decimal positions specified by d. A format specification of number(3,2) cannot contain a number greater than 9.99. N THE SAME DATA TYPE AS NUMBER. U M B E R ( * ) S THE SAME DATA TYPE AS NUMBER. M A L L I N T R DEFINES A COLUMN THAT A CONTAINS RAW BINARY DATA W WITH A LENGTH SPECIFIED TO THE ( VALUE N. N ) R DEFINES A COLUMN AS A BINARY A FORMAT FOR SECURE OPERATING W SYSTEM LABEL. M I S 10 Partitioning Oracle allows the developer to partition the table data. This means the data can reside in one or more tablespaces. This has some advantages especially for large databases with a lot of activity: ? Reduces downtime for scheduled maintenance. ? Reduces downtime caused by data failures. ? Decreases I/O. ? Can increase performance of some queries by limiting the amount of data that is read. ? Increases performance by the reduction of contention for disk arms. Creating a “consultant” table with a partition option SQL> create table consultant 2 (employee_id number, 3 first_name varchar2(15), 4 last_name varchar2(25), 5 specialty varchar2(30)) partition by range (employee_id) (partition t1 value less than (250) tablespace realistic_employee1, partition t2 value less than (500) tablespace realistic_employee2, partition t3 value less than (750) tablespace realistic_employee3, partition t4 value less than (maxvalue) tablespace realistic_employee4); Table created. SQL> End listing 11 Copying a table Using the Create/Select command to copy a table SQL> create table consultant_copy 2 as select * from consultant; Table created. SQL> End Listing This technique only creates a new table and populates the table with data. It does not duplicate the indexes and integrity constraints that were placed on the original table. These must be created independently of the command. The data in the new table also does not stay in sync with the original tables. MAINTAINING THE INTEGRITY OF THE DATABASE Some of these functions are: ? Ensure the uniqueness of the primary key. ? Ensure children records in related tables have a parent record. ? Delete children records when the parent record is deleted. ? Ensure columns always contain a value. ? Ensure a column contains a value within a specific range. ? Ensure a default value is placed in a column. 12 The Check Constraint Using a Check constraint with a table definition SQL> drop table consultant; Table dropped.