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.6 later in this chapter.

如果需要修改表结构,可参考本章的5.6节。

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章,本章剩余的部分可稍后再看。

5.1. Table Basics相关推荐

  1. Postgres-XL 10 使用介绍(一)

    ## Preface-前言 ### What is PostgreSQL? ​    PostgreSQL 是一个基于加州大学伯克利分校计算机科学系开发的POSTGRES4.2版本的对象关系型数据库管 ...

  2. postgres语法_SQL Create Table解释了MySQL和Postgres的语法示例

    postgres语法 A table is a group of data stored in a database. 表是存储在数据库中的一组数据. To create a table in a d ...

  3. NFC Basics(基本NFC)——翻译自developer.android.com

    NFC Basics 关于收发NDEF格式的消息,以及相关的api. 非NDEFdata以及其它更深入技术请參见Advanced NFC. 在android端使用NDEF有两种情况: - 从标签读取N ...

  4. JS 打印 data数据_数据表格 Data Table - 复杂内容的15个设计点

    表格是桌面应用中常见的内容型组件,它包含大量的信息和丰富的交互形式,表格具有极高的空间利用率,结构化的展示保证了数据可读性.高效.清晰且易用是进行表格设计的原则性要求.本文将从表格的内容组织到交互作一 ...

  5. TeraData Basics

    TeraData Basics 持续更新中.. 插图画得很辛苦 1. Basic concept 1.1 Parallel Processing AMP(Access Module Processor ...

  6. Git Basics - Viewing the Commit History

    Git --distributed-even-if-your-workflow-isnt About DocumentationReferenceBookVideosExternal Links Do ...

  7. 调度增益控制基础概念Gain Scheduling Basics学习笔记

    文章目录 一.调度增益基础Gain Scheduling Basics 调度增益gain scheduling是什么? 调度增益gain scheduling是如何实现的? 调度增益gain sche ...

  8. zz Web Mapping Illustrated Table of Contents

    从amazon买了3本书: Web Mapping Illustrated PostgreSQL (2nd Edition)  MapServer Web Mapping Illustrated Us ...

  9. P4 开发实践 — NG-SDN Tutorial — Exercise 1: P4Runtime Basics

    目录 文章目录 目录 Exercise 1: P4Runtime Basics 1. Look at the P4 program 2. Compile P4 program 3. Start Min ...

最新文章

  1. 数字图像处理必备基本知识
  2. VScode快速一键生成html、vue、jsx、ajax、sass、docker等代码片段
  3. Jackson 序列化对象成 JSON 字符串,忽略部分字段(属性)
  4. Java学习笔记——封装
  5. tinyxml2解析XML文件
  6. python展示全部好友_python爬所有好友头像
  7. 一个简单demo通过em实现‘响应式设计、em、文字图标svg’
  8. 关于style设置方法,obj.setAttribute(style.)和obj.style.stylename
  9. 岑溪高考成绩查询2021,2019岑溪高考成绩喜报(归义中学、筋竹中学、南
  10. mysql查binlog删除时间_mysql的binlog日志删除查询
  11. PR曲线与ROC曲线绘制
  12. scrapy爬取京东所有图书
  13. 新唐N76E003单片机用APROM模拟EEPROM每次下载写入值复位为0XFF
  14. 信息完全技术之Enigma密码机【MATLAB程序及软件APP实现】
  15. android 签到自定义,Android日历签到,超级简单的实现方式
  16. 手把手教你如何连接阿里云RDS云数据库
  17. 如何利用html+css动画 实现水墨动画?
  18. 【docker入门】
  19. 蚂蚁森林能量自动化收集
  20. 7-1 用格里高利公式求给定精度的PI值 (15分)

热门文章

  1. 某数字化校园系统通用型文件上传漏洞getshell(渗透测试-0day)
  2. [高通SDM450][Android9.0]user版本uartlog常开
  3. 华为ENSP安装包(ensp100,ensp500及相关ar,fw镜像)
  4. python爬取电影天堂首页
  5. QOS流量监管与常见POLICER模型
  6. ipad版Qzone(纯swift编写)
  7. x9015数字电位器应用
  8. 神经网络算法计算过程推导
  9. 笨兔兔的故事——带你了解Ubuntu,了解Linux 第十章 人才
  10. 视觉表面缺陷检测技术概述