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'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容