> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/the-sql-language/ddl/ddl-basics.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 5.1. 資料表基礎 \#

### 5.1. 資料表基礎 [#](#ddl-basics) <a href="#ddl-basics" id="ddl-basics"></a>

關聯式資料庫中的資料表很像紙上的表格：它由資料列與欄位組成。欄位的數量與順序是固定的，而且每個欄位都有一個名稱。資料列的數量則是可變動的——它反映了在某個時間點儲存了多少資料。SQL 對於資料表中資料列的順序不做任何保證。當讀取資料表時，除非明確要求排序，否則資料列出現的順序是未指定的。這在[第 7 章](https://docs.postgresql.tw/19/the-sql-language/queries.md)中會討論。此外，SQL 不會為資料列指派唯一的識別碼，因此資料表中有可能存在好幾筆完全相同的資料列。這是 SQL 背後數學模型所導致的結果，但通常並不是我們想要的。本章稍後會說明如何處理這個問題。

每個欄位都有一個資料型別。資料型別限制了可以指派給該欄位的值的集合，並且為欄位中所儲存的資料賦予語意，讓這些資料可以用於運算。例如，宣告為數值型別的欄位不會接受任意的文字字串，而儲存在這種欄位中的資料可以用來做數學運算。相對地，宣告為字元字串型別的欄位幾乎可以接受任何種類的資料，但它並不適合用來做數學計算，不過還是可以使用像字串串接這類其他運算。

PostgreSQL 內建了相當多的資料型別，足以滿足許多應用的需求。使用者也可以定義自己的資料型別。大多數內建資料型別的名稱與語意都很直觀，因此我們把詳細的說明留到[第 8 章](https://docs.postgresql.tw/19/the-sql-language/datatype.md)。一些常用的資料型別包括：用於整數的 `integer`、用於可能帶有小數之數字的 `numeric`、用於字元字串的 `text`、用於日期的 `date`、用於一天中時刻的 `time`，以及用於同時包含日期與時間之值的 `timestamp`。

要建立資料表，你要使用名符其實的 [CREATE TABLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createtable.md) 指令。在這個指令中，你至少要指定新資料表的名稱、各欄位的名稱，以及每個欄位的資料型別。例如：

```

CREATE TABLE my_first_table (
    first_column text,
    second_column integer
);
```

這會建立一個名為 `my_first_table` 的資料表，它有兩個欄位。第一個欄位名為 `first_column`，資料型別為 `text`；第二個欄位名稱為 `second_column`，型別為 `integer`。資料表與欄位的名稱遵循[第 4.1.1 節](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical.md#sql-syntax-identifiers)所說明的識別符號語法。型別名稱通常也是識別符號，但有一些例外。請注意，欄位清單是以逗號分隔，並且以括號括起來。

當然，前面這個例子是刻意編造的。通常你會為資料表與欄位取一個能表達它們所儲存資料種類的名稱。所以讓我們來看一個比較實際的例子：

```

CREATE TABLE products (
    product_no integer,
    name text,
    price numeric
);
```

（`numeric` 型別可以儲存小數部分，這對金額來說是很典型的需求。）

#### 提示

當你要建立許多彼此相關的資料表時，為資料表與欄位選擇一致的命名模式是明智的做法。例如，資料表名稱要使用單數或複數名詞是個選擇，而這兩種做法各有某些理論家偏好。

資料表所能包含的欄位數量是有上限的。依欄位型別而定，這個上限介於 250 到 1600 之間。不過，定義一個欄位數接近這個上限的資料表是非常不尋常的，而且往往是有問題的設計。

如果你不再需要某個資料表，可以使用 [DROP TABLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-droptable.md) 指令把它移除。例如：

```

DROP TABLE my_first_table;
DROP TABLE products;
```

嘗試移除不存在的資料表會產生錯誤。儘管如此，在 SQL 腳本檔案中，常見的做法是在建立每個資料表之前先無條件地嘗試移除它，並忽略任何錯誤訊息，這樣不論資料表存在與否，腳本都能運作。（如果你想要的話，可以使用 `DROP TABLE IF EXISTS` 這個變化形式來避免錯誤訊息，但這不是標準的 SQL。）

如果你需要修改已經存在的資料表，請參閱本章稍後的[第 5.7 節](https://docs.postgresql.tw/19/the-sql-language/ddl/ddl-alter.md)。

有了目前為止所討論的工具，你就可以建立功能完整的資料表了。本章其餘的部分則著重於在資料表定義中加入各種功能，以確保資料完整性、安全性或便利性。如果你現在就急著要把資料填入資料表，可以直接跳到[第 6 章](https://docs.postgresql.tw/19/the-sql-language/dml.md)，之後再閱讀本章其餘的內容。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ddl-basics.html)（原文版本：18.6；核對日期：2026-09-13）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/the-sql-language/ddl/ddl-basics.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
