sqlchar和varchar数据类型两者区别对比
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了sqlchar和varchar数据类型两者区别对比,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3529字,纯文字阅读大概需要6分钟。
内容图文
![sqlchar和varchar数据类型两者区别对比](/upload/InfoBanner/zyjiaocheng/544/dcb5cb77edd0496e95339d570ea5f560.jpg)
本文章介绍mssql server中char和varchar数据性能对比分析,详细的利用实例来说明两者的关系,有需要了解的朋友可以参考一下。
1. char
固定长度,最长n个字符。
2. varchar
最大长度为n的可变字符串。
(n为某一整数,不同,最大长度n不同)
如果我们将长度设为10:char(10), varchar(10), 然后我们将值设为: ‘apple’。
Char(10) 存储的值会是:’apple ’.
Varchar(10) 存储的值会是:’apple’.
Char 和 Varchar 不能存储 Unicode 字符。
数据类型 | 可以存储 unicode | 固定长度 |
---|---|---|
char | 不 | 是 |
nchar | 是 | 是 |
varchar | 不 | 不 |
nvarchar | 是 | 不 |
char和varchar区别:
varchar必char节省空间,但在效率上比char稍微差一些。
说varchar比char节省空间,是因为varchar是可变字符串,比如:用varchar(5)存储字符串“abc”,只占用3个字节的存储空间,而用char(5)存储,则占用5个字节(“abc ”)。
说varchar比char效率稍差,是因为,当修改varchar数据时,可能因为数据长度不同,导致数据迁移(即:多余I/O)。其中,oracle对此多余I/O描述的表达是:“行迁移”(Row Migration)。
看个实例
本次测试的SQL SERVER 2000 的char 与 varchar。
测试分三组,每次增加插入的行数,脚本如下:
代码如下 | |
CREATE TABLE [dbo].[testchar] ( [a] [int] IDENTITY (1, 1) NOT NULL , [b] [char] (200) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[testvarchar] ( [a] [int] IDENTITY (1, 1) NOT NULL , [b] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO --truncate table testvarchar --truncate table testchar declare @i as int set @i=0 while @i<50000 begin insert into testvarchar values(cast(@i as varchar(200))) set @i=@i+1 if @i=50000 break end declare @i as int set @i=0 while @i<50000 begin insert into testchar values(cast(@i as char(200))) set @i=@i+1 if @i=50000 break end |
实验结果:
插入行数 数据类型 Cpu duration 50000 varchar 2359 21203 50000 char 2344 22250 100000 varchar 4156 44500 100000 char 4172 44186 2000000 varchar 8907 89093 2000000 char 9188 96530
两表存储空间比较
代码如下 | |
Testchar:73.94M |
从国外网站找到一则实例
Fixed-length (char) or variable-length (varchar) character data types.
char[(n)]
Fixed-length non-Unicode character data with length of n bytes. n must be a value from 1 through 8,000. Storage size is n bytes. The SQL-92 synonym for char is character.
varchar[(n)]
Variable-length non-Unicode character data with length of n bytes. n must be a value from 1 through 8,000. Storage size is the actual length in bytes of the data entered, not n bytes. The data entered can be 0 characters in length. The SQL-92 synonyms for varchar are char varying or character varying.
Remarks
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
Objects using char or varchar are assigned the default collation of the database, unless a specific collation is assigned using the COLLATE clause. The collation controls the code page used to store the character data.
Sites supporting multiple languages should consider using the Unicode nchar or nvarchar data types to minimize character conversion issues. If you use char or varchar:
Use char when the data values in a column are expected to be consistently close to the same size.
Use varchar when the data values in a column are expected to vary considerably in size.
If SET ANSI_PADDING is OFF when CREATE TABLE or ALTER TABLE is executed, a char column defined as NULL is handled as varchar.
When the collation code page uses double-byte characters, the storage size is still n bytes. Depending on the character string, the storage size of n bytes may be less than n characters.
内容总结
以上是互联网集市为您收集整理的sqlchar和varchar数据类型两者区别对比全部内容,希望文章能够帮你解决sqlchar和varchar数据类型两者区别对比所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。