Home Database Mysql Tutorial SQL数据类型详解

SQL数据类型详解

Jun 07, 2016 pm 05:57 PM

SQL数据类型详解

(1)二进制数据类型

  二进制数据包括 Binary、Varbinary 和 Image
  Binary 数据类型既可以是固定长度的(Binary),也可以是变长度的。
  Binary[(n)] 是 n 位固定的二进制数据。其中,n 的取值范围是从 1 到 8000。其存储窨的大小是 n + 4 个字节。
  Varbinary[(n)] 是 n 位变长度的二进制数据。其中,n 的取值范围是从 1 到 8000。其存储窨的大小是 n + 4个字节,不是n 个字节。
  在 Image 数据类型中存储的数据是以位字符串存储的,不是由 SQL Server 解释的,必须由应用程序来解释。例如,应用程序可以使用BMP、TIEF、GIF 和 JPEG 格式把数据存储在 Image 数据类型中。

(2)字符数据类型

  字符数据的类型包括 Char,Varchar 和 Text
  字符数据是由任何字母、符号和数字任意组合而成的数据。
  Varchar 是变长字符数据,其长度不超过 8KB。Char 是定长字符数据,其长度最多为 8KB。超过 8KB 的ASCII 数据可以使用Text数据类型存储。例如,因为 Html 文档全部都是 ASCII 字符,并且在一般情况下长度超过 8KB,所以这些文档可以 Text 数据类型存储在SQL Server 中。

(3)Unicode 数据类型

  Unicode 数据类型包括 Nchar,Nvarchar 和Ntext
  在 Microsoft SQL Server 中,传统的非 Unicode 数据类型允许使用由特定字符集定义的字符。在 SQL Server安装过程中,允许选择一种字符集。使用 Unicode 数据类型,列中可以存储任何由Unicode 标准定义的字符。在 Unicode 标准中,包括了以各种字符集定义的全部字符。使用Unicode数据类型,所战胜的窨是使用非 Unicode 数据类型所占用的窨大小的两倍。
  在 SQL Server 中,Unicode 数据以 Nchar、Nvarchar 和 Ntext 数据类型存储。使用这种字符类型存储的列可以存储多个字符集中的字符。当列的长度变化时,应该使用Nvarchar 字符类型,这时最多可以存储 4000 个字符。当列的长度固定不变时,应该使用 Nchar 字符类型,同样,这时最多可以存储4000 个字符。当使用 Ntext 数据类型时,该列可以存储多于 4000 个字符。

(4)日期和时间数据类型

  日期和时间数据类型包括 Datetime 和 Smalldatetime两种类型
  日期和时间数据类型由有效的日期和时间组成。例如,有效的日期和时间数据包括“4/01/98 12:15:00:00:00 PM”和“1:28:29:15:01AM 8/17/98”。前一个数据类型是日期在前,时间在后一个数据类型是霎时间在前,日期在后。在 Microsoft SQL Server中,日期和时间数据类型包括Datetime 和 Smalldatetime 两种类型时,所存储的日期范围是从 1753 年 1 月 1 日开始,到9999 年12 月 31 日结束(每一个值要求 8 个存储字节)。使用 Smalldatetime 数据类型时,所存储的日期范围是 1900年 1 月 1日 开始,到 2079 年 12 月 31 日结束(每一个值要求 4 个存储字节)。
  日期的格式可以设定。设置日期格式的命令如下:
  Set DateFormat {format | @format _var|
  其中,format | @format_var 是日期的顺序。有效的参数包括 MDY、DMY、YMD、YDM、MYD 和 DYM。在默认情况下,日期格式为MDY。
  例如,当执行 Set DateFormat YMD 之后,日期的格式为年 月 日 形式;当执行 Set DateFormat DMY 之后,日期的格式为日 月有年 形式

(5)数字数据类型

  数字数据只包含数字。数字数据类型包括正数和负数、小数(浮点数)和整数
  整数由正整数和负整数组成,例如 39、25、0-2 和 33967。在 Micrsoft SQL Server 中,整数存储的数据类型是    Int,Smallint和 Tinyint。Int 数据类型存储数据的范围大于 Smallint 数据类型存储数据的范围,而 Smallint 据类型存储数据的范围大于Tinyint 数据类型存储数据的范围。使用 Int 数据狗昔存储数据的范围是从 -2 147 483 648 到 2 147 483 647(每一个值要求4个字节存储空间)。使用 Smallint 数据类型时,存储数据的范围从 -32 768 到 32 767(每一个值要求2个字节存储空间)。使用Tinyint 数据类型时,存储数据的范围是从0 到255(每一个值要求1个字节存储空间)。
  精确小娄数据在 SQL Server 中的数据类型是 Decimal 和 Numeric。这种数据所占的存储空间根据该数据的位数后的位数来确定。
  在SQL Server 中,近似小数数据的数据类型是 Float 和 Real。例如,三分之一这个分数记作。3333333,当使用近似数据类型时能准确表示。因此,从系统中检索到的数据可能与存储在该列中数据不完全一样。

(6)货币数据表示正的或者负的货币数量 。

  在 Microsoft SQL Server 中,货币数据的数据类型是Money 和 Smallmoney

  Money数据类型要求 8 个存储字节,Smallmoney 数据类型要求 4 个存储字节。

(7)特殊数据类型

  特殊数据类型包括前面没有提过的数据类型。特殊的数据类型有3种,即    Timestamp、Bit 和 Uniqueidentifier
  Timestamp 用于表示SQL Server 活动的先后顺序,以二进投影的格式表示。Timestamp 数据与插入数据或者日期和时间没有关系。
  Bit 由 1 或者 0 组成。当表示真或者假、ON 或者 OFF 时,使用 Bit 数据类型。例如,询问是否是每一次访问的客户机请求可以存储在这种数据类型的列中。
  Uniqueidentifier 由 16 字节的十六进制数字组成,表示一个全局唯一的。当表的记录行要求唯一时,GUID是非常有用。例如,在客户标识号列使用这种数据类型可以区别不同的客户。

2.用户定义的数据类型

  用户定义的数据类型基于在 Microsoft SQL Server 中提供的数据类型。当几个表中必须存储同一种数据类型时,并且为保证这些列有相同的数据类型、长度和可空性时,可以使用用户定义的数据类型。例如,可定义一种称为   postal_code 的数据类型,它基于 Char 数据类型。
  当创建用户定义的数据类型时,必须提供三个数:数据类型的名称、所基于的系统数据类型和数据类型的可空性。

(1)创建用户定义的数据类型

  创建用户定义的数据类型可以使用 Transact-SQL 语句。系统存储过程 sp_addtype 可以来创建用户定义的数据类型。其语法形式如下:
  sp_addtype {type},[,system_data_bype][,'null_type']
  其中,type 是用户定义的数据类型的名称。system_data_type 是系统提供的数据类型,例如 Decimal、Int、Char   等等。 null_type 表示该数据类型是如何处理空值的,必须使用单引号引起来,例如'NULL'、'NOT NULL'或者'NONULL'。
  例子:
  Use cust
  Exec sp_addtype ssn,'Varchar(11)',"Not Null'
  创建一个用户定义的数据类型 ssn,其基于的系统数据类型是变长为11 的字符,不允许空。
  例子:
  Use cust
  Exec sp_addtype birthday,datetime,'Null'
  创建一个用户定义的数据类型 birthday,其基于的系统数据类型是 DateTime,允许空。
  例子:
  Use master
  Exec sp_addtype telephone,'varchar(24),'Not Null'
  Eexc sp_addtype fax,'varchar(24)','Null'
  创建两个数据类型,即 telephone 和 fax

(2)删除用户定义的数据类型

  当用户定义的数据类型不需要时,可删除。删除用户定义的数据类型的命令是 sp_droptype {'type'}。
  例子:
  Use master
  Exec sp_droptype 'ssn'
  注意:当表中的列还正在使用用户定义的数据类型时,或者在其上面还绑定有默认或者规则时,这种用户定义的数据类型不能删除。

SQL SERVER的字段类型说明

  以下为SQL SERVER7.0以上版本的字段类型说明。SQL SERVER6.5的字段类型说明请参考SQL SERVER提供的说明。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

See all articles