postgreSQL表操作及基础语法.md

新建一个表

CREATE TABLE test_table xxx (
    string_test1 varchar(32)
    number_test int
);

取表

SELECT * FROM test_table2

PostgreSQL数据类型是为数据库表中字段的数值的类型,如整数,字符串,布尔等数值。在创建PostgreSQL数据库表时,必须为字段指定数据类型。PostgreSQL中用户可以使用CREATE TYPE创建自定义数据类型。

数字类型

数字类型由2、4或8字节的整数以及4或8字节的浮点数和可选精度小数组成。

名字 存储尺寸 描述 范围
<tt class="TYPE" style="box-sizing: border-box;">smallint</tt> 2字节 小范围整数 -32768 到 +32767
<tt class="TYPE" style="box-sizing: border-box;">integer</tt> 4字节 整数的典型选择 -2147483648 到 +2147483647
<tt class="TYPE" style="box-sizing: border-box;">bigint</tt> 8字节 大范围整数 -9223372036854775808 到 +9223372036854775807
<tt class="TYPE" style="box-sizing: border-box;">decimal</tt> 可变 用户指定精度,精确 最高小数点前131072位,以及小数点后16383位
<tt class="TYPE" style="box-sizing: border-box;">numeric</tt> 可变 用户指定精度,精确 最高小数点前131072位,以及小数点后16383位
<tt class="TYPE" style="box-sizing: border-box;">real</tt> 4字节 可变精度,不精确 6位十进制精度
<tt class="TYPE" style="box-sizing: border-box;">double precision</tt> 8字节 可变精度,不精确 15位十进制精度
<tt class="TYPE" style="box-sizing: border-box;">smallserial</tt> 2字节 自动增加的小整数 1到32767
<tt class="TYPE" style="box-sizing: border-box;">serial</tt> 4字节 自动增加的整数 1到2147483647
<tt class="TYPE" style="box-sizing: border-box;">bigserial</tt> 8字节 自动增长的大整数 1到9223372036854775807

数字类型使用实例

1)可变精度数字类型有两个参数:第一个参数是精度,即:数字的所有位数;第二个参数是比例,即:小数点后的数字位数。

<pre class="codesnippet" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 1em 0px 1.2em; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; overflow-wrap: break-word; background-color: rgb(247, 247, 249); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">复制 `NUMERIC(precision, scale)

数字 23.5141 的精度为6而比例为4

NUMERIC(6, 4)`</pre>

或者设置比例为0:

<pre class="codesnippet" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 1em 0px 1.2em; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; overflow-wrap: break-word; background-color: rgb(247, 247, 249); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">复制 NUMERIC(precision)</pre>

2)serial为自动增长类型,在设置主键时使用,数字类型使用实例

<pre class="codesnippet" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 1em 0px 1.2em; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; overflow-wrap: break-word; background-color: rgb(247, 247, 249); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">复制 CREATE TABLE tablename ( id SERIAL, colname1 INTEGER, colname1 NUMERIC(10, 4), );</pre>

字符类型

名字 描述
<tt class="TYPE" style="box-sizing: border-box;">character varying(<tt class="REPLACEABLE" style="box-sizing: border-box;">n</tt>)</tt>, <tt class="TYPE" style="box-sizing: border-box;">varchar(<tt class="REPLACEABLE" style="box-sizing: border-box;">n</tt>)</tt> 定义可变长度字符串,字符串的最大长度为n,即:字符串占用的最大长度为n
<tt class="TYPE" style="box-sizing: border-box;">character(<tt class="REPLACEABLE" style="box-sizing: border-box;">n</tt>)</tt>, <tt class="TYPE" style="box-sizing: border-box;">char(<tt class="REPLACEABLE" style="box-sizing: border-box;">n</tt>)</tt> 定长字符串,如果字符串长度不到n,使用空格填充
<tt class="TYPE" style="box-sizing: border-box;">text</tt> 无限变长字符串

PostgreSQL提供了两种基本的字符类型character varying(n)和character(n),其中n为一个正整数。varchar(n)是character varying(n)的简写形式,char(n)是character(n)的简写形式。

字符类型使用实例

<pre class="codesnippet" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 1em 0px 1.2em; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; overflow-wrap: break-word; background-color: rgb(247, 247, 249); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">复制 CREATE TABLE tablename ( id SERIAL, colname1 char(32), colname1 varchar(64), );</pre>

日期/时间类型

名字 存储尺寸 描述 最小值 最大值 解析度
<tt class="TYPE" style="box-sizing: border-box;">timestamp [ (<tt class="REPLACEABLE" style="box-sizing: border-box;">p</tt>) ] [ without time zone ]</tt> 8字节 包括日期和时间(无时区) 4713 BC 294276 AD 1微秒 / 14位
<tt class="TYPE" style="box-sizing: border-box;">timestamp [ (<tt class="REPLACEABLE" style="box-sizing: border-box;">p</tt>) ] with time zone</tt> 8字节 包括日期和时间,有时区 4713 BC 294276 AD 1微秒 / 14位
<tt class="TYPE" style="box-sizing: border-box;">date</tt> 4字节 日期(没有一天中的时间) 4713 BC 5874897 AD 1日
<tt class="TYPE" style="box-sizing: border-box;">time [ (<tt class="REPLACEABLE" style="box-sizing: border-box;">p</tt>) ] [ without time zone ]</tt> 8字节 一天中的时间(无日期) 00:00:00 24:00:00 1微秒 / 14位
<tt class="TYPE" style="box-sizing: border-box;">time [ (<tt class="REPLACEABLE" style="box-sizing: border-box;">p</tt>) ] with time zone</tt> 12字节 仅仅是一天中的时间,带有时区 00:00:00+1459 24:00:00-1459 1微秒 / 14位
<tt class="TYPE" style="box-sizing: border-box;">interval [ <tt class="REPLACEABLE" style="box-sizing: border-box;">fields</tt> ] [ (<tt class="REPLACEABLE" style="box-sizing: border-box;">p</tt>) ]</tt> 16字节 时间间隔 -178000000年 178000000年 1微秒 / 14位

time、timestamp和interval可以配置一个可选的精度值 p,它表示在秒后面保留的小数的位数,p的值可以为0--6。

interval类型有一个附加选项,它可以通过写下面之一的短语来限制存储的fields的集合:

<pre class="codesnippet" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 1em 0px 1.2em; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; overflow-wrap: break-word; background-color: rgb(247, 247, 249); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">复制 YEAR MONTH DAY HOUR MINUTE SECOND YEAR TO MONTH DAY TO HOUR DAY TO MINUTE DAY TO SECOND HOUR TO MINUTE HOUR TO SECOND MINUTE TO SECOND</pre>

注意如果fields和p被指定,fields必须包括SECOND,因为精度只应用于秒。

货币类型

名字 存储尺寸 描述 范围
money 8 bytes 货币额 -92233720368547758.08到+92233720368547758.07

money存储固定小数精度的货币数字,小数的精度由数据库的lc_monetary参数决定。表中展示的范围假设有两个小数位。可接受的输入格式很多,包括整数和浮点数文字,以及常用的货币格式,如'$1,000.00'。 输出通常是最后一种形式,但和区域相关。

布尔类型

名字 存储字节 描述
<tt class="TYPE" style="box-sizing: border-box;">boolean</tt> 1字节 状态为真或假

"真"状态的有效文字值是:

  • TRUE
  • 't'
  • 'true'
  • 'y'
  • 'yes'
  • 'on'
  • '1'

而对于"假"状态,你可以使用下面这些值:

  • FALSE
  • 'f'
  • 'false'
  • 'n'
  • 'no'
  • 'off'
  • '0'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容