20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction to <strong>SQL</strong><br />

A perfectly valid question to ask is, “Why have different data types?” Why not just treat everything as<br />

text? The main reason is efficiency. The amount of storage space and the speed of access improve when<br />

the database knows what sort of data it’s dealing with. For example, the number 243787452 can be<br />

stored in as little as 4 bytes of computer memory. Storing the same number as text takes 9 bytes of memory<br />

for the character data.<br />

In addition, the data type determines what the RDBMS expects users to do with the data. If you have<br />

numerical data, then 123 + 123 calculates as addition with the answer being 246. If it were text data,<br />

the RDBMS would interpret the plus sign as meaning that you want to join the two character strings<br />

together to form 123123.<br />

So what are the various data types available? Unfortunately, data type varies among RDBMSs. Added to<br />

this conundrum is the problem that while the ANSI <strong>SQL</strong> standards such as <strong>SQL</strong>-92, <strong>SQL</strong>-99, and <strong>SQL</strong>-<br />

2003 define standards for data types, they are far from fully and universally implemented by the various<br />

RDBMS manufacturers. However, all is not lost. There’s enough support of the standards for the purposes<br />

of this book. Once you have a handle on the basic ANSI <strong>SQL</strong> data types, researching the data types<br />

that your particular RDBMS uses is fairly easy. You can then use them in addition to the data types<br />

examined here.<br />

The following table contains a subset of the more commonly used ANSI <strong>SQL</strong> data types and the name of<br />

the same data type in other RDBMSs such as <strong>SQL</strong> Server, IBM DB2, and so on.<br />

ANSI <strong>SQL</strong> MS Access <strong>SQL</strong> Server 2000 IBM DB2 My<strong>SQL</strong> Oracle 10<br />

Character char char char char char<br />

Character varying varchar varchar varchar varchar varchar<br />

National character char nchar graphic char nchar<br />

National character varchar nvarchar vargraphic varchar nvarchar<br />

varying<br />

Integer number (long int int int int<br />

integer)<br />

Smallint number smallint smallint smallint smallint<br />

(integer)<br />

Real number real real real real<br />

(double)<br />

Decimal number decimal decimal decimal decimal<br />

(decimal)<br />

Date date datetime date date date<br />

Time time datetime time time date<br />

19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!