In SQL Server char, the difference between varchar, text and nchar, nvarchar, ntext of (turn)

When many developers and database design often do not have much to consider char, varchar type, some did not even pay attention, because memory prices become cheaper, and forget some of the basic principles of design theory and the beginning of this makes me think of today's young people, waved his hand one yuan slipped away from his hands, in fact, I think whether it is a man Ye Hao, Ye Hao to do the development, grasp of detail will determine a lot of things. Of course there are some people did not even find out their differences, will pick any one. Here I would like for them to do a simple analysis, of course, if there is something wrong I hope you advice. 

1, CHAR . CHAR fixed-length data storage is convenient, high efficiency index on CHAR field level, such as the definition char (10), then regardless of your data storage has reached 10 bytes, it must take up 10 bytes of space, inadequate automatic filling a space, so when reading may be used more than once trim (). 

2,  VARCHAR . Storing variable-length data, but not CHAR high storage efficiency. If the value of a field may not be a fixed length, we only know that it can not be more than 10 characters, it is defined as VARCHAR (10) is the best buy. The actual length of VARCHAR type is its actual length value +1. Why "+1" mean? This is a byte used to store how much the actual length. From space considerations, a suitable use varchar; from the viewpoint of efficiency, with a suitable char, the key is to find the trade-off is the actual situation. 

3, TEXT . Unicode text data stored in the non-variable length, the maximum length of 2 ^ 31-1 (2,147,483,647) characters. 

4,  NCHAR, NVARCHAR, NTEXT. Judging from the name of these three three kinds of more than a "N" than the previous. It represents the type of data storage is Unicode character. We know the characters, English characters only need one byte memory is sufficient, but many Chinese characters, require two bytes of storage, is likely to cause confusion when English and Chinese characters exist, Unicode character set the character set is to solve this incompatibility the problems that arise, with all its characters are two bytes, ie the English characters is represented by two bytes. length nchar, nvarchar is between 1 and 4000. And char, varchar comparison, nchar, nvarchar then store up to 4000 characters, whether English or Chinese characters; and char, varchar can store up to 8,000 English, 4,000 Chinese characters. It can be seen using the nchar, nvarchar data types do not worry when character input is English or Chinese characters, more convenient, but when some of the English memory loss amount. 

So, in general, if it contains Chinese characters, with nchar / nvarchar, if plain English and digital, with char / Var- char

I put their differences summarized: 

CHAR, NCHAR Fixed-length, speed, accounting for a large space, the need to deal with 
VARCHAR , NVARCHAR, TEXT variable length, small space, slow, without having to deal with 
NCHAR, NVARCHAR, NTEXT handle Unicode code

The second:

Previously only known text and image are likely to be out of the SQL Server data types, but the exact cause is not clear, today's reading, I noticed the difference between text and varchar (max) and nvarchar (max), primarily restrictions on operators , text can only be a function of the following effects:

 

function Statement

DATALENGTH

READTEXT

PATINDEX

SET TEXTSIZE

SUBSTRING

UPDATETEXT

TEXTPTR

WRITETEXT

TEXTVALID

 

 

For Liezi, if the "text" data type of the column is text, then it will not be used "=" "left ()" and other operations, such as the following example:

Creating tables, populate the data:

if exists (select * from sysobjects where id = OBJECT_ID('[asdf]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)

DROP TABLE [asdf]

CREATE TABLE [asdf] (

[inttest] [int] IDENTITY (1, 1) NOT NULL ,

[text] [text] NULL ,

[varcharmax] varchar(max) NULL )

ALTER TABLE [asdf] WITH NOCHECK ADD CONSTRAINT [PK_asdf] PRIMARY KEY NONCLUSTERED ( [inttest] )

SET IDENTITY_INSERT [asdf] ON

INSERT [asdf] ( [inttest] , [text] , [varcharmax] ) VALUES ( 1 , '1111111' , '1111111' )

SET IDENTITY_INSERT [asdf] OFF

Run the query:

A query:
the SELECT [text]

      ,[varcharmax]

FROM [testDB].[dbo].[asdf]

where

[text] ='11111' AND

[varcharmax] = '1111111'


You receive the following error message:

Message 402, Level 16, State 1, Line 1

Varchar and text data types are not compatible equal to operator.

Query two:
the SELECT [text]

      ,[varcharmax]

FROM [testDB].[dbo].[asdf]

where

[varcharmax] = '1111111'


Run successfully

In the above versions MS SQL2005 and added a large value data type (varchar (max), nvarchar (max), varbinary (max)). Data type can store a large value up to 2 ^ 30-1 bytes of data.

These smaller data types and data types in behavior  VARCHAR , nvarchar  and  varbinary  same.

Microsoft's argument is that before using this data type instead of text , ntext  and  image  data types, correspondence between them:

varchar(max)-------text;

nvarchar(max)-----ntext;

varbinary(max)----image.

 

With the large-value data types after, when the value of large data operations more flexible than before. For example: Before text can not be used 'like', it does not have these problems after varchar (max), and because varchar (n) on the same varchar (max) in the behavior, it is possible to be used both in varcahr used in the varchar (max).

In addition, this also supports the inserted and deleted tables large-value data type column references on the use AFTER triggers. text will not work, in short, followed by a large-value data types, I was "back does not hurt, and legs do not ache, breath can also be on the sixth floor." But so what, it quickly with a large value types.


October 16, 2014 11:34:19

SQL Server database data types stored learning to master is not too good, I think I was the author says young people before now, looked a little information on the author to say to sum:

1. The correspondence between the type of data:

varchar(max)-------text;

nvarchar(max)-----ntext;

varbinary(max)----image.

2. Now try not to text, ntext, image type to store the data

3. From the space considerations, with varchar appropriate (in fact or try to use varchar, after all, now is the era of big data); from the viewpoint of efficiency, with char appropriate, the key is to find trade-offs based on the actual situation.

4. Use the nchar, nvarchar data types do not worry when character input is English or Chinese characters, more convenient, but when some of the English memory loss amount. So, in general, if it contains Chinese characters, with nchar / nvarchar, if plain English and digital, with char / varchar

5.

Support multi-language sites should consider using the Unicode nchar or nvarchar data types to minimize character conversion issues.

If you want the data values ​​in the column close to the same size, use char.

If the desired size of the data values ​​in columns are significantly different, use varchar.

If you want the column size of all data items close to the same, use the nchar.

If you want a big difference in the size of the column items, use nvarchar.

If the CREATE TABLE or ALTER TABLE SET ANSI_PADDING is OFF, a char column defined as NULL will be treated as varchar.

6.

1) If the amount of data is very large, but also 100% certain length and save just ansi characters, char 
2) can determine the length does not necessarily mean ansi or character, then use nchar; 
3) indeterminate length, and you want to use to query index, then use nvarchar type it, they will be set to 400; 
4) does not, then there's really nothing query, use nvarchar (4000)

7.

CHAR, NCHAR Fixed-length, speed, large space, to be addressed 
VARCHAR, NVARCHAR, TEXT variable length, small space, slow, without having to deal with 
NCHAR, NVARCHAR, NTEXT handle Unicode code

 

Transfer from https://www.cnblogs.com/smilefortoday/p/4028421.html

Guess you like

Origin www.cnblogs.com/adspark/p/11326240.html