学习sql注入:猜测数据库

What are SQL data types, why do we need them, and how to use them? Today, we’ll try to answer these questions by giving an overview of some most common data types used in SQL Server.

什么是SQL数据类型?为什么需要它们?如何使用它们? 今天,我们将通过概述SQL Server中使用的一些最常见的数据类型来尝试回答这些问题。

该模型 (The Model)

Before doing anything else, we’ll take one quick look at the model we’re using in this series.

在做其他事情之前,我们将快速浏览一下本系列中使用的模型。

You can notice that each table has columns defined by their name, but also with their type. While the name is pretty obvious, and we’re the one defining it, the thing is little different when it comes to types. We can choose a column type from a list of predefined types or our custom type (again build from predefined types).

您会注意到,每个表都有由其名称和类型定义的列。 虽然名称很明显,而且我们是一个定义它的人,但是在类型方面却几乎没有什么不同。 我们可以从预定义类型或自定义类型的列表中选择列类型(同样从预定义类型构建)。

SQL数据类型–快速介绍 (SQL Data Types – Quick Intro)

Talking about data types is not the hottest thing in programming. They are just one of these things that are here, and we expect it to work. Still, it’s necessary to understand what we have at disposal.

谈论数据类型并不是编程中最热门的事情。 它们只是这里的其中之一,我们希望它能起作用。 尽管如此,还是有必要了解我们所拥有的。

When we’re storing data, we need to simulate “measures & formats” used in the real world. E.g. we want to store height as a decimal number, birth date as a date, ‘this sentence’ as a text, etc. You could always go with an extreme and store everything as text or photo.

当我们存储数据时,我们需要模拟现实世界中使用的“度量和格式”。 例如,我们要将身高存储为十进制数字,将出生日期存储为日期,将“此句子”存储为文本,等等。您总是可以选择极端,将所有内容存储为文本或图片。

Since databases are meant to store data and work with them efficiently, we won’t do that. We’ll have several different SQL data types at our disposal. We won’t only have types to store text or number, but we’ll also go deeper, with types closely describing the nature of that text (how long is it) or number (is it integer or decimal).

由于数据库旨在存储数据并有效地使用它们,因此我们不会这样做。 我们将使用几种不同SQL数据类型。 我们不仅将具有用于存储文本或数字的类型,而且还将更深入地介绍类型,这些类型紧密描述了文本的性质(多长时间)或数字(整数或十进制)。

The main idea behind data types is to simulate the most common types from the real-world. By doing so, the DBMS (database management system), will be able to store them efficiently (less space used, better organized), and that will also lead to improved performance.

数据类型背后的主要思想是模拟现实世界中最常见的类型。 这样,DBMS(数据库管理系统)将能够有效地存储它们(使用的空间更少,组织得更好),并且还可以提高性能。

These types vary between different DBMSs (SQL Server, MySQL, Oracle), but most of them are very similar. We’ll focus on the most important SQL Server/Transact-SQL data types.

这些类型在不同的DBMS(SQL Server,MySQL,Oracle)之间有所不同,但是大多数都非常相似。 我们将重点介绍最重要SQL Server / Transact-SQL数据类型。

文字数据类型 (Textual Data Types)

As you could see in our model, there are 3 most common types of real-world data we need to store: texts, numbers, and dates. Therefore, most DBMS (and programming languages) will support these 3 main groups. In each of these groups, you’ll find more or less standardized SQL data types.

正如您在模型中看到的那样,我们需要存储3种最常见的现实世界数据类型:文本,数字和日期。 因此,大多数DBMS(和编程语言)将支持这3个主要组。 在每个组中,您都会找到或多或少的标准化SQL数据类型。

So, let’s quickly review textual data types first. In the table below, you’ll find Transact-SQL string/textual data types with short descriptions.

因此,让我们首先快速查看文本数据类型。 在下表中,您将找到带有简短说明的Transact-SQL字符串/文本数据类型。

The most important are:

最重要的是: