5.1. Table Basics

5.1. Table Basics
5.1. 表基础
A table in a relational database is much like a table on paper: It consists of rows and columns. The  number and order of the columns is fixed, and each column has a name. The number of rows is variable  — it reflects how much data is stored at a given moment. SQL does not make any guarantees about  the order of the rows in a table. When a table is read, the rows will appear in an unspecified order, unless sorting is explicitly requested. This is covered in Chapter 7. Furthermore, SQL does not assign  unique identifiers to rows, so it is possible to have several completely identical rows in a table. This  is a consequence of the mathematical model that  underlies SQL but is usually not desirable. Later in  this chapter we will see how to deal with this issue.
关系型数据库中的表类似于纸上的一个表:它由列和行组成。列的数量和顺序是固定的,且每列都有列名。行的数量是可变的--它反映的是在某一时间点表中所存储的数据量。SQL并不能保证表中数据的顺序。当读取一张表的时候,展示的行时无序的,除非显式的指定了排序规则。此内容可参见 第7章。而且,SQL并不能保证行的唯一性,所以表中存在完全一致的行时完全有可能的。 这是基于SQL的数学模型的结果,但此种行为通常不是所需的。 在本章的后面,我们将看到如何处理这个问题。
 
Each column has a data type. The data type constrains the set of possible values that can be assigned to  a column and assigns semantics to the data stored in the column so that it can be used for computations. For instance, a column declared to be of a numerical type will not accept arbitrary text strings, and  the data stored in such a column can be used for mathematical computations. By contrast, a column  declared to be of a character string type will accept almost any kind of data but it does not lend itself  to mathematical calculations, although other operations such as string concatenation are available.
每列都有数据类型。 数据类型限制了可以分配给列的可能的值的集合,并为存储在列中的数据分配了语义,以便可以将其用于计算。例如,声明为数值类型的列将不接受任意文本字符串的数据,并且存储在此列中的数据可用于数学计算。相比之下,声明为字符串类型的列几乎可以接受任何类型的数据,但是它不适合进行数学计算,尽管可以使用其他操作,例如字符串连接。
 
PostgreSQL includes a sizable set of built-in data types that fit many applications. Users can also  define their own data types. Most built-in data types have obvious names and semantics, so we defer  a detailed explanation to Chapter 8. Some of the frequently used data types are integer for whole  numbers, numeric for possibly fractional numbers, text for character strings, date for dates, time for time-of-day values, and timestamp for values containing both date and time.
PostgreSQL包括一组可适用于许多应用程序的大量内置数据类型。 用户还可以自定义数据类型。大多数内置数据类型都有明显的名称和语义,因此我们在第8章进行详细说明。对于数字来说,integer是一个最常用的数据类型,小数常用numeric类型,字符串用text类型,日期用date类型,一天中的时间用time类型,包含时间和日期一般用timestamp类型。
 
To create a table, you use the aptly named CREATE TABLE command. In this command you specify  at least a name for the new table, the names of the columns and the data type of each column. For  example:
可使用CREATE TABLE命令创建一张表。在此命令中,至少需要指定表名、列名及各列的数据类型。例如:
 
CREATE TABLE my_first_table (
first_column text,
second_column integer
);
 
This creates a table named my_first_table with two columns. The first column is named  first_column and has a data type of text ; the second column has the name second_column  and the type integer . The table and column names follow the identifier syntax explained in Section  4.1.1. The type names are usually also identifiers, but there are some exceptions. Note that the  column list is comma-separated and surrounded by parentheses.
上例创建了一张具有两列的表名为my_first_table的表。第一列是具有text数据类型的first_column列;第二列是具有integer数据类型的second_column列。表名和列名遵守 4.1.1节介绍的标识符语法规定。数据类型名称通常也是标识符,但有一些例外。注意,列的列表是包含在括号内,且以逗号分隔的。
 
Of course, the previous example was heavily contrived. Normally, you would give names to your  tables and columns that convey what kind of data they store. So let's look at a more realistic example:
当然,前面的示例设计仅为示例。 通常,表和列的命名,需表达出它们所存储的数据。 因此,让我们看一个更现实的例子:
 
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
 
(The numeric type can store fractional components, as would be typical of monetary amounts.)
(numeric数据类型可以存储小数部分,所以通常用作存储货币数额。)
 
Tip
小贴士
When you create many interrelated tables it is wise to choose a consistent naming  pattern for the tables and columns. For instance, there is a choice of using singular or  plural nouns for table names, both of which are favored by some theorist or other.
当创建许多有相互关联的表时,明智的做法是为表和列名选择一致的命名规则。例如, 可以选择使用单数形式或复数形式的名词作为表名,这两者都各有所好者。
 
There is a limit on how many columns a table can contain. Depending on the column types, it is  between 250 and 1600. However, defining a table with anywhere near this many columns is highly  unusual and often a questionable design.
表所能包含的列的个数是有一个限制的。取决于列的数据类型,列的可以可以为250到1600之间。然而,如果定义了一个有这么多列的表,这本身就是不正常且通常是可以(或者说需要)进行优化的设计。
 
If you no longer need a table, you can remove it using the DROP TABLE command. For example:
如果不在需要某张表,可以使用DROP TABLE命令删掉该表。例如:
 
DROP TABLE my_first_table;
DROP TABLE products;
 
Attempting to drop a table that does not exist is an error. Nevertheless, it is common in SQL script files  to unconditionally try to drop each table before creating it, ignoring any error messages, so that the  script works whether or not the table exists. (If you like, you can use the DROP TABLE IF EXISTS  variant to avoid the error messages, but this is not standard SQL.)
尝试删除不存在的表时不对的。不过,在SQL脚本中,在创建该表前先尝试删除要创建的表时比较常用的,忽略掉错误信息,以使该脚本不论表是否存在均可以正常执行。(如果想的话,可以使用DROP TABLE IF EXISTS来避免报错,但这并不是标准的SQL。)
 
If you need to modify a table that already exists, see Section 5.5 later in this chapter.
如果需要修改表结构,可参考本章的 5.5节
 
With the tools discussed so far you can create fully functional tables. The remainder of this chapter is  concerned with adding features to the table definition to ensure data integrity, security, or convenience. If you are eager to fill your tables with data now you can skip ahead to Chapter 6 and read the rest  of this chapter later.
到此,可根据讲到的知识创建所需的表了。本章剩余的部分讨论在表定义中添加其他的特性,以保证数据一致性、安全性或者便利性。如果你已经迫不及待的想要将数据放到表中,那么可以直接参考 第6章,本章剩余的部分可稍后再看。
 
发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104325603