gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 line
8.1KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>5.1. Table Basics</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="ddl.html" title="Chapter 5. Data Definition" /><link rel="next" href="ddl-default.html" title="5.2. Default Values" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">5.1. Table Basics</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl.html" title="Chapter 5. Data Definition">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ddl-default.html" title="5.2. Default Values">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DDL-BASICS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.1. Table Basics</h2></div></div></div><a id="id-1.5.4.3.2" class="indexterm"></a><a id="id-1.5.4.3.3" class="indexterm"></a><a id="id-1.5.4.3.4" class="indexterm"></a><p>
  3. A table in a relational database is much like a table on paper: It
  4. consists of rows and columns. The number and order of the columns
  5. is fixed, and each column has a name. The number of rows is
  6. variable — it reflects how much data is stored at a given moment.
  7. SQL does not make any guarantees about the order of the rows in a
  8. table. When a table is read, the rows will appear in an unspecified order,
  9. unless sorting is explicitly requested. This is covered in <a class="xref" href="queries.html" title="Chapter 7. Queries">Chapter 7</a>. Furthermore, SQL does not assign unique
  10. identifiers to rows, so it is possible to have several completely
  11. identical rows in a table. This is a consequence of the
  12. mathematical model that underlies SQL but is usually not desirable.
  13. Later in this chapter we will see how to deal with this issue.
  14. </p><p>
  15. Each column has a data type. The data type constrains the set of
  16. possible values that can be assigned to a column and assigns
  17. semantics to the data stored in the column so that it can be used
  18. for computations. For instance, a column declared to be of a
  19. numerical type will not accept arbitrary text strings, and the data
  20. stored in such a column can be used for mathematical computations.
  21. By contrast, a column declared to be of a character string type
  22. will accept almost any kind of data but it does not lend itself to
  23. mathematical calculations, although other operations such as string
  24. concatenation are available.
  25. </p><p>
  26. <span class="productname">PostgreSQL</span> includes a sizable set of
  27. built-in data types that fit many applications. Users can also
  28. define their own data types. Most built-in data types have obvious
  29. names and semantics, so we defer a detailed explanation to <a class="xref" href="datatype.html" title="Chapter 8. Data Types">Chapter 8</a>. Some of the frequently used data types are
  30. <code class="type">integer</code> for whole numbers, <code class="type">numeric</code> for
  31. possibly fractional numbers, <code class="type">text</code> for character
  32. strings, <code class="type">date</code> for dates, <code class="type">time</code> for
  33. time-of-day values, and <code class="type">timestamp</code> for values
  34. containing both date and time.
  35. </p><a id="id-1.5.4.3.8" class="indexterm"></a><p>
  36. To create a table, you use the aptly named <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> command.
  37. In this command you specify at least a name for the new table, the
  38. names of the columns and the data type of each column. For
  39. example:
  40. </p><pre class="programlisting">
  41. CREATE TABLE my_first_table (
  42. first_column text,
  43. second_column integer
  44. );
  45. </pre><p>
  46. This creates a table named <code class="literal">my_first_table</code> with
  47. two columns. The first column is named
  48. <code class="literal">first_column</code> and has a data type of
  49. <code class="type">text</code>; the second column has the name
  50. <code class="literal">second_column</code> and the type <code class="type">integer</code>.
  51. The table and column names follow the identifier syntax explained
  52. in <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS" title="4.1.1. Identifiers and Key Words">Section 4.1.1</a>. The type names are
  53. usually also identifiers, but there are some exceptions. Note that the
  54. column list is comma-separated and surrounded by parentheses.
  55. </p><p>
  56. Of course, the previous example was heavily contrived. Normally,
  57. you would give names to your tables and columns that convey what
  58. kind of data they store. So let's look at a more realistic
  59. example:
  60. </p><pre class="programlisting">
  61. CREATE TABLE products (
  62. product_no integer,
  63. name text,
  64. price numeric
  65. );
  66. </pre><p>
  67. (The <code class="type">numeric</code> type can store fractional components, as
  68. would be typical of monetary amounts.)
  69. </p><div class="tip"><h3 class="title">Tip</h3><p>
  70. When you create many interrelated tables it is wise to choose a
  71. consistent naming pattern for the tables and columns. For
  72. instance, there is a choice of using singular or plural nouns for
  73. table names, both of which are favored by some theorist or other.
  74. </p></div><p>
  75. There is a limit on how many columns a table can contain.
  76. Depending on the column types, it is between 250 and 1600.
  77. However, defining a table with anywhere near this many columns is
  78. highly unusual and often a questionable design.
  79. </p><a id="id-1.5.4.3.13" class="indexterm"></a><p>
  80. If you no longer need a table, you can remove it using the <a class="xref" href="sql-droptable.html" title="DROP TABLE"><span class="refentrytitle">DROP TABLE</span></a> command.
  81. For example:
  82. </p><pre class="programlisting">
  83. DROP TABLE my_first_table;
  84. DROP TABLE products;
  85. </pre><p>
  86. Attempting to drop a table that does not exist is an error.
  87. Nevertheless, it is common in SQL script files to unconditionally
  88. try to drop each table before creating it, ignoring any error
  89. messages, so that the script works whether or not the table exists.
  90. (If you like, you can use the <code class="literal">DROP TABLE IF EXISTS</code> variant
  91. to avoid the error messages, but this is not standard SQL.)
  92. </p><p>
  93. If you need to modify a table that already exists, see <a class="xref" href="ddl-alter.html" title="5.6. Modifying Tables">Section 5.6</a> later in this chapter.
  94. </p><p>
  95. With the tools discussed so far you can create fully functional
  96. tables. The remainder of this chapter is concerned with adding
  97. features to the table definition to ensure data integrity,
  98. security, or convenience. If you are eager to fill your tables with
  99. data now you can skip ahead to <a class="xref" href="dml.html" title="Chapter 6. Data Manipulation">Chapter 6</a> and read the
  100. rest of this chapter later.
  101. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-default.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 5. Data Definition </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.2. Default Values</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1