Search Java Programs

Friday, February 26, 2010

Understanding the LONG (Oracle) or TEXT (MS-SQL Server) Data Type

If you need to store a large amount of text data in a table, you may run into the problem of needing to store a character string larger than the maximum number or characters allowed for the CHARACTER (or VARCHAR) data type. Suppose, for example, that you had a HUMAN_RESOURCES table and one of the columns was RESUME. If you are using MS-SQL Server as your DBMS, you could store only the first 4,000 characters of the resume in the RESUME column of the HUMAN_RESOURCES table. Fortunately, Microsoft has the TEXT data type which, like Oracle's LONG data type, lets you store character strings of up to 2,147,483,647 characters. (If you are storing text strings in Unicode using columns of type NTEXT, you can store only 1,073,741,823 characters. Each Unicode character takes 2 bytes of storage, so you can store only half as many of them.)

It would be wasteful to preallocate 2GB of disk space for each column you declare as type TEXT. As such, MS-SQL Server preallocates only a small portion (8K) of the maximum TEXT space and allocates the remainder in 8K (8,192 byte) increments as you need it. As such, when it is ready to save character 8,193 of a TEXT string to disk, the DBMS allocates another block (page) of 8,192 bytes and creates a link from the page holding the previous 8,192 bytes to the page holding the next 8,192 bytes.

Once the DBMS stores the data in the TEXT column to disk, the entire TEXT block is logically contiguous. This is to say that the DBMS "sees" the TEXT block as one huge character string, even if the individual 8K blocks (pages) that make up the TEXT block are not physically contiguous. As such, you can display the entire contents of a TEXT column using a single SELECT statement such as:

 SELECT resume FROM human_resources

if, for example, HUMAN_RESOURCES were a table defined as:

 CREATE TABLE human_resources
(id INTEGER,
name VARCHAR(25),
department_code TINYINT,
data_of_hire DATE,
resume TEXT)

Note

The actual number of characters of TEXT data displayed by the SELECT statement is limited by the value of the Global Variable @@Textsize. If you don't change the value of @@Textsize, MS-SQL Server limits the number of TEXT characters displayed to 64K (64,512) by default.


Chapter 1 - SQL Tips and Techniques

Chapter 1 - SQL Tips and Techniques

Chapter 1: Understanding SQL Basics and Creating Database Files

Understanding Flat Files - SQL Tips and Techniques

Understanding the Hierarchical Database Model - SQL Tips and Techniques


Understanding the Network Database Model

Understanding the Relational Database Model

Understanding Codd's 12-Rule Relational Database Definition

Understanding Terms Used to Define an SQL Database

Understanding the Components of a Table

Understanding Table Names

Understanding Column Names

Understanding Views

Understanding Schemas

Understanding the SQL System Catalog

Understanding Domains

Understanding Constraints

Understanding the History of SQL

Understanding the Difference Between SQL and a Programming Language

Understanding Data Definition Language (DDL)

Understanding Data Manipulation Language (DML)

Understanding Data Control Language (DCL)

Understanding SQL Numeric Integer Data Types

Understanding SQL Character Data Types

Understanding the Advantages of Using the VARCHAR Data Type

Understanding the LONG (Oracle) or TEXT (MS-SQL Server) Data TypeUnderstanding the MS-SQL Server IMAGE Data Type

Understanding Standard SQL Datetime Data Types and the DATETIME Data Type

Understanding the SQL BIT Data Type

Understanding Constants

Understanding the MS-SQL Server ISNULL() Function

Understanding the MS-SQL Server IDENTITY Property

Understanding Assertions

Understanding the SQL DBMS Client/Server Model

Understanding the Structure of SQL Statements

Understanding SQL Keywords

Using the MS-SQL Server Query Analyzer to Execute SQL Statements

Using the CREATE DATABASE Statement to Create an MS-SQL Server Database and Transaction Log

Using the ED Command Within ISQL to Edit SQL Statements

Using the MS-SQL Server ISQL to Execute SQL Statements from the Command Line or Statements Stored in an ASCII File

Using the MS-SQL Server Enterprise Manager to Create a Database and Transaction Log

Using DROP DATABASE to Erase an MS-SQL Server Database and Transaction Log

Understanding How to Size MS-SQL Server Databases and Transaction Logs

Understanding the MS-SQL Server TempDB Database

Thursday, February 25, 2010

Understanding the Advantages of Using the VARCHAR Data Type

If you have a text column where the number of characters you want to store varies from to row, use a variable-length character string to save disk space. Suppose, for example, that you define an order table as follows:
CREATE TABLE order_table
   (customer_number      INTEGER,
    delivery_date        DATE,
    item_number          SMALLINT,
    quantity             SMALLINT,
    special_instructions CHAR(1000))
By using a fixed CHARACTER type, the DBMS will make the SPECIAL_INSTRUCTIONS column in every row 1,000 characters in length, even if you enter SPECIAL_INSTRUCTION strings for only a few items. As you learned in Tip 23, "Understanding SQL Character Data Types," the DBMS adds blanks to the end of a fixed-length character string if you insert a string with less than the number of characters you define as the string's length—in this case, 1,000 characters. Therefore, if you have one item that requires special instructions in a 10,000-row table, you will waste 9.9MB of disk spaces because the system will store 1,000 blank characters in each of the 9,999 rows that don't have any special instructions.
If on the other hand, you were to create the same ORDER_TABLE using the SQL statement
CREATE TABLE order_table
  (customer_number      INTEGER,
   delivery_date        DATE,
   item_number          SMALLINT,
   quantity             SMALLINT,
   special_instructions VARCHAR(1000))
the DBMS would not add blanks to the character string you insert in the SPECIAL_INSTRUCTIONS column. Thus, for the current example, where only 1 row has SPECIAL_INSTRUCTIONS, your 10,0000-row table will be 9,999,000 bytes (9MB) smaller than the table with identical data whose SPECIAL_INSTRUCTIONS column is declared as a fixed-length character type of 1,000 bytes.
The variable-length data types are:
  • VARCHAR
  • CHAR VARYING
  • CHARACTER VARYING
  • NCHAR VARYING
  • NATIONAL CHAR VARYING
  • NATIONAL CHARACTER VARYING
Review Tip 23 for additional information on how to declare a column using each of these data types.

Website Design by Mayuri Multimedia