1. Trang chủ
  2. » Công Nghệ Thông Tin

SQL VISUAL QUICKSTART GUIDE- P10 docx

10 290 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 176,02 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Character String TypesUse character string data types to represent text.. A string stored in a column defined as CHARACTERlength can have up to length characters, where length is an inte

Trang 1

Character String Types

Use character string data types to represent

text A character string, or just string, has

these characteristics:

◆ It’s an ordered sequence of zero or more

characters

◆ Its length can be fixed or varying

◆ It’s case sensitive (‘A’comes before ‘a’ when sorted)

◆ In SQL statements, a string is surrounded

by single quotes

It’s one of the types listed in Table 3.4.

Table 3.4

Character String Types

T y p e D e s c r i p t i o n

CHARACTER Represents a fixed number of characters A string stored in a column defined as

CHARACTER(length) can have up to length characters, where length is an integer greater than or equal to 1; the maximum length depends on the DBMS When you

the DBMS pads the end of the string with spaces to create a string that has exactly

length characters A CHARACTER(6) string ‘Jack’ is stored as ‘Jack ‘ , for

CHARACTER VARYING Represents a variable number of characters A string stored in a column defined as

CHARACTER VARYING(length) can have up to length characters, where length is

an integer greater than or equal to 1; the maximum length depends on the DBMS.

CHARACTER VARYING(length)column, the DBMS stores the string as is and doesn’t

NATIONAL CHARACTER This data type is the same as CHARACTER except that it holds standardized multibyte

characters or Unicode characters (see the sidebar in this section) In SQL statements,

NATIONAL CHARACTER strings are written like CHARACTER strings but have an N in

NATIONAL CHARACTER VARYING This data type is the same as CHARACTER VARYING except that it holds standardized

CHARACTER VARYING , NATIONAL CHAR VARYING , and NCHAR VARYING are synonyms.

OBJECT are synonyms.

NCLOB The national character large object ( NCLOB ) type is the same as CLOB except that

CHARACTER ) NCLOB , NCHAR LARGE OBJECT , and NATIONAL CHARACTER LARGE OBJECT are synonyms.

Trang 2

✔ Tips

■ Two consecutive single quotes represent one single-quote character in a string

Type ‘don’’t’to represent don’t, for

example A double-quote character (“)

is a separate character and doesn’t need this special treatment

■ The length of a string is an integer

between 0 and length, inclusive A string

with no characters—’’(two single quotes with no intervening space)—is

called an empty string or a zero-length

string An empty string is considered to

be a VARCHARof length zero

■ DBMSs often can sort and manipulate fixed-length strings faster than variable-length ones

■ Keep character columns as short as possible rather than giving them “room

to grow” in the future Shorter columns sort and group faster than longer ones

■ SQL:1999 introduced CLOBandNCLOBto the SQL language (but most DBMSs already had similar data types by then)

Table 3.5 lists character-string

and similar types for the DBMSs See the DBMS documentation for size limits and usage restrictions

Oracle treats empty strings as nulls; see

“Nulls” later in this chapter

In MySQLANSI_QUOTESmode, string lit-erals can be quoted only with single quotes; a string quoted with double quotes is interpreted as an identifier

Unicode

Computers store characters (letters,

digits, punctuation, control characters,

and other symbols) internally by

assign-ing them numeric values An encodassign-ing

determines the mapping of characters

to numeric values; different languages

and computer operating systems use

many different native encodings Standard

U.S.-English strings use ASCII encoding,

which assigns values to 128 (27) different

characters—not much, and not even

enough to hold all the Latin characters

used in modern European languages,

much less all the Chinese ideographs

Unicode is a single character set that

represents the characters of almost all

the world’s written languages Unicode

can encode up to about 4.3 billion (232)

characters (using UTF-32 encoding)

The Unicode Consortium develops and

maintains the Unicode standard The

actual Unicode mappings are available

in the latest online or printed edition

of The Unicode Standard, available at

www.unicode.org

Table 3.5

DBMS Character String Types

D B M S T y p e s

nvarchar , ntext

clob , nclob , long

DB2 char , varchar , long varchar , clob ,

dbclob , graphic , vargraphic ,

long vargraphic

national varchar , tinytext , text ,

mediumtext , longtext

Trang 3

Binary Large Object Type

Use the binary large object (BLOB) data

type to store binary data A BLOB has these

characteristics:

◆ The type name is BLOBorBINARY LARGE

OBJECT

◆ Unlike a CLOB, which stores a long

character string, a BLOB stores a long

sequence of bytes The two data types

are incompatible

◆ BLOBs are used mainly to store large

amounts of multimedia data (graphics,

photos, audio, or video, for example),

sci-entific data (MRI images or climate maps),

or technical data (engineering drawings)

◆ BLOBs can’t be used as keys or in indexes

and support far fewer functions and

opera-tions than do other types BLOBs can be

compared for only equality (=) or inequality

(<>) because it makes no sense for a BLOB

to be “less than” another BLOB You also

can’t use BLOBs with DISTINCT, in GROUP BY

orORDER BYclauses, or in column functions

◆ In host languages, BLOBs are referenced

with a unique locator (pointer) value,

avoiding the overhead of transferring

entire BLOBs across a client/server

network

✔ Tips

■ DBMSs don’t attempt to interpret BLOBs;

their meaning is up to the application

A binary string literal is given in

hexadec-imal, or hex (base 16), format The

hexa-decimal system uses the digits 0 through 9

and the letters A through F (uppercase or

lowercase) One hex character is equivalent

to 4 bits In SQL statements, hex strings

have an Xin front of the first quote, with

no intervening space The hex stringX’4B’

corresponds to the bits 01001011or the

bit stringB’01001011’, for example

Table 3.6 DBMS BLOB Types

D B M S T y p e s

blob , mediumblob , longblob

■ SQL:1999 introduced BLOBto the SQL language (but most DBMSs already had similar data types by then)

Table 3.6 lists BLOB and

simi-lar types for the DBMSs See the DBMS documentation for size limits and usage restrictions

Trang 4

Exact Numeric Types

Use exact numeric data types to represent

exact numerical values An exact numerical

value has these characteristics:

◆ It can be a negative, zero, or positive

number

◆ It’s an integer or a decimal number

An integer is a whole number expressed

without a decimal point: –42, 0, 62262

A decimal number has digits to the

right of the decimal point: –22.06, 0.0,

0.0003, 12.34

◆ It has a fixed precision and scale The

precision is the number of significant

digits used to express the number; it’s the total number of digits both to the right and to the left of the decimal point

The scale is the number of digits to the

right of the decimal point Obviously, the scale can’t exceed the precision To rep-resent a whole number, set the scale equal to zero See the Tips in this section for some examples

It’s one of the types listed in Table 3.7.

Table 3.7

Exact Numeric Types

T y p e D e s c r i p t i o n

NUMERIC Represents a decimal number, stored in a column defined as NUMERIC(precision [,scale]) precision is

greater than or equal to 1; the maximum precision depends on the DBMS scale is a value from 0 to precision.

DECIMAL This data type is similar to NUMERIC , and some DBMSs define them equivalently The difference is that the

INTEGER Represents an integer The minimum and maximum values that can be stored in an INTEGER column depend

SMALLINT This data type is the same as INTEGER except that it might hold a smaller range of values, depending on the

BIGINT This data type is the same as INTEGER except that it might hold a larger range of values, depending on the

Trang 5

✔ Tips

Table 3.8 shows how the number 123.89

is stored for different precision and

scale values

■ Don’t enclose a numeric literal in quotes

■ Store numbers as strings if the numbers

are not involved in arithmetic calculations

Store telephone numbers, zip codes, and

U.S Social Security numbers as strings,

for example This technique can save

space and prevent data loss: If you store

the zip code ‘02116’as a number instead

of as a string, you’ll lose the leading zero

■ Calculations involving only integers are

much faster than those involving decimal

and floating-point numbers

Table 3.9 lists exact-numeric

and similar types for the DBMSs

See the DBMS documentation for size

limits and usage restrictions DBMSs

often accept type names that they don’t

implement, converting them to suitable,

supported types; Oracle converts INTto

NUMBER(32), for example

DBMSs usually implement SMALLINTas

16-bit values (–32,768 through 32,767),

INTEGERas 32-bit values (–2,147,483,648

through 2,147,483,647), and BIGINTas

64-bit values (quintillions) SQL:2003

introduced BIGINTto the SQL language

(but most DBMSs already had a similar

data type by then)

Table 3.8 Precision and Scale Examples for 123.89

S p e c i f i e d A s S t o r e d A s

NUMERIC(5) 124 NUMERIC(5,0) 124 NUMERIC(5,1) 123.9 NUMERIC(5,2) 123.89 NUMERIC(4,0) 124 NUMERIC(4,1) 123.9 NUMERIC(4,2) Exceeds precision

NUMERIC(2,0) Exceeds precision

Table 3.9 DBMS Exact Numeric Types

D B M S T y p e s

decimal , numeric

DB2 smallint , integer , bigint , decimal

bigint , decimal

Trang 6

Approximate

Numeric Types

Use approximate numeric data types to

represent approximate numerical values

An approximate numerical value has these

characteristics:

◆ It can be a negative, zero, or positive

number

◆ It’s considered to be an approximation

of a floating-point (real) number

◆ It typically is used to represent the very

small or very large quantities common

in technical, scientific, statistical, and

financial calculations

◆ It’s expressed in scientific notation A

num-ber in scientific notation is written as a

decimal number multiplied by an (integer)

power of 10 An uppercase E is the

exponen-tiation symbol: 2.5E2 = 2.5 ✕102= 250, for

example The mantissa is the portion that

expresses the significant digits (2.5 here),

and the exponent is the power of 10 (2 here).

The mantissa and exponent each can have

a sign: –2.5E–2 = –2.5 ✕10–2= –0.025

◆ It has a fixed precision but no explicit scale (The sign and magnitude of the exponent determine the scale intrinsically.) The precision is the number of (binary) bits used to store the mantissa To con-vert from binary to decimal precision, multiply the precision by 0.30103 To convert from decimal to binary precision, multiply the decimal precision by 3.32193 For example, 24 bits yields 7 digits of precision, and 53 bits yields 15 digits

of precision

It’s one of the types listed in Table 3.10.

✔ Tips

■ Don’t enclose a numeric literal in quotes

Table 3.11 lists approximate

numeric and similar types for the DBMSs See the DBMS documenta-tion for size limits and usage restricdocumenta-tions DBMSs often accept type names that they don’t implement, converting them to

suitable, supported types; PostgreSQL

converts floattodouble precision,

Table 3.10

Approximate Numeric Types

T y p e D e s c r i p t i o n

FLOAT Represents a floating-point number, stored in a column defined as FLOAT(precision) precision is

greater than or equal to 1 and expressed as the number of bits (not the number of digits); the maximum

precision depends on the DBMS.

DOUBLE PRECISION This data type is the same as FLOAT except that the DBMS defines the precision, which must be greater

Table 3.11

DBMS Approximate Numeric Types

Trang 7

Boolean Type

Use the Boolean data type to store truth

val-ues A Boolean value has these characteristics:

◆ The type name is BOOLEAN

◆ The truth values are True, False, and

Unknown, represented by the Boolean

literals TRUE,FALSE, and UNKNOWN Truth

values are described in “Combining and

Negating Conditions with AND,OR, and

NOT” in Chapter 4

◆ A null is equivalent to the Unknown

truth value and, in practice, usually is

used instead of Unknown (most DBMS

Boolean types don’t accept the literal

UNKNOWN) See “Nulls” later in this chapter

✔ Tips

■ Don’t enclose a Boolean literal in quotes

■ SQL:1999 introduced BOOLEANto the SQL

language

Table 3.12 lists Boolean and

similar types for the DBMSs

See the DBMS documentation for size

limits and usage restrictions Where

no Boolean type is available, I give the

data type for storing single bits or small

integers SQL programmers often use

these numeric values to represent truth

values, where zero means false, 1 (or any

nonzero number) means true, and null

means unknown

Table 3.12 DBMS Boolean Types

D B M S T y p e s

Trang 8

Datetime Types

Use datetime data types to represent the

date and time of day Datetime values have

these characteristics:

◆ They’re specified with respect to UTC,

or Universal Coordinated Time (formerly

called Greenwich Mean Time or GMT)

The SQL standard requires that every

SQL session have a default offset from

UTC that is used for the duration of the

session; –8 hours is the time-zone offset

of San Francisco, California, for example

◆ The rules of the Gregorian calendar determine how date values are formed

DBMSs reject values that they can’t recognize as dates

◆ Time values are based on a 24-hour

clock, also called military time (use 13:00,

not 1:00 p.m.)

◆ Hyphens (-) separate the parts of a date, and colons (:) separate the parts of a time A space separates a date and time when both are combined

It’s one of the types listed in Table 3.13.

Table 3.13

Datetime Types

T y p e D e s c r i p t i o n

YEAR , MONTH , and DAY —and is formatted yyyy-mm-dd (length 10) ( 2006-03-17 , for

no arguments.

HOUR , MINUTE , and SECOND —and is formatted hh:mm:ss (length 8) ( 22:06:57 , for

of fractional digits and is greater than or equal to zero The maximum precision, which

values for the fields.

TIMESTAMP Represents a combination of DATE and TIME values separated by a space The TIMESTAMP

TIME WITH TIME ZONE This data type is the same as TIME except that it adds a field, TIME_ZONE_OFFSET , to

HOUR TO MINUTE (see the next section) and can contain the values listed in Table 3.14 (on

AT LOCAL , for example) If the AT clause is omitted, all times default to AT LOCAL

TIMESTAMP WITH TIME ZONE This data type is the same as TIMESTAMP except that it adds a field, TIME_ZONE_OFFSET ,

WITH TIME ZONE except that you must include a date ( 2006-03-17 22:06:57 AT TIME ZONE -08:00 , for example).

Trang 9

✔ Tips

■ To get your system time, see “Getting the

Current Date and Time” in Chapter 5

■ You can compare two datetime values if

they have the same fields; see “Filtering

Rows with WHERE” in Chapter 4 See also

“Performing Datetime and Interval

Arithmetic” in Chapter 5

■ TheSECONDfield can accept values up to

61.999 (instead of 59) to allow for the

(rare) insertion of leap seconds into a

particular minute to keep Earth’s clocks

synchronized with sidereal time

■ A datetime literal is a datetime type

name, followed by a space, followed by

a datetime value surrounded by single

quotes—DATE ‘yyyy-mm-dd’,TIME

‘hh:mm:ss’, and TIMESTAMP ‘yyyy-mm-dd

hh:mm:ss’, for example

■ Standard SQL can’t handle B.C.E./B.C

(Before the Common Era/Before Christ)

dates, but your DBMS might be able to

do so

■ Timestamps often are used to mark

events associated with the row in which

they appear In MySQL, for example, a

timestampcolumn is useful for recording

the date and time of an UPDATEoperation

■ The data type TIME WITH TIME ZONE

doesn’t make sense, because real-world

time zones have no meaning unless they’re

associated with a date (because the

time-zone offset varies throughout the year)

Favor TIMESTAMP WITH TIME ZONE

■ See also “Working with Dates” in

Chapter 15

Table 3.14 Datetime Fields

F i e l d V a l i d V a l u e s

SECOND 00 to 61.999 (see the Tips)

TIME_ZONE_OFFSET –12:59 to +13:00

Trang 10

Table 3.15 lists datetime and

similar types for the DBMSs See the DBMS documentation for size limits and usage restrictions

DBMSs allow you to enter date values

in month-day-year, day-month-year, and other formats and time values based on

a 12-hour (a.m./p.m.) clock The format

in which dates and times are displayed can differ from the format in which they’re entered

In Microsoft Access, surround

date-time literals with # characters instead

of quotes and omit the data type name prefix The standard SQL date DATE

‘2006-03-17’is equivalent to the Access date #2006-03-17#, for example

In Microsoft SQL Server, omit the data

type name prefix from datetime literals

The standard SQL date DATE ‘2006-03-17’

is equivalent to the SQL Server date

‘2006-03-17’, for example

In DB2, omit the data type name prefix

from datetime literals The standard SQL date DATE ‘2006-03-17’is equivalent to the DB2 date ‘2006-03-17’, for example

Table 3.15

DBMS Datetime Types

D B M S T y p e s

DB2 date , time , timestamp

timestamp , year

Ngày đăng: 05/07/2014, 05:20

TỪ KHÓA LIÊN QUAN