postgresql两个列模糊比较_PostgreSQL使用简易教程

fd3abffd230c5ab4c95826c621af44ad.png

1. 一些基础概念

postgreSQL ,或者说数据库,有个很重要的概念就是 Schema(模式)和 View(视图)、Materialized View(物化视图)。

Schema,模式

Schema 类似与分组,它可以将数据库对象组织到一起形成逻辑组,方便管理。

我们在 postgreSQL 数据库中创建的任何对象(表、索引、视图和物化视图)都会在一个模式下被创建。如果未指定模式,这些对象将会在默认的模式下被创建。这个模式叫做 public。每一个数据库在创建的时候就会有一个这样的模式。

创建一个新的 schema 就是 CREATE SCHEMA my_schema;,要在这个指定的 schema 里建立表格:

CREATE TABLE my_schema.mytable ( ... );

删除一个空 schema 是 DROP SCHEMA my_schema;,如果不是空的就得 DROP SCHEMA myschema CASCADE; 了。

假如我们进入一个数据库并执行一个命令操作一个叫 my_table 的表格的时候,默认情况下数据库会在 public 这个模式中找,找不到就报错,哪怕这个 my_table 本身在另一个模式(比如 my_schema)里已经存在。这个时候我们就要设置搜索路径了:

SET search_path TO my_schema, public;

这样就把 my_schema 放到了搜索路径里 public 的前面。这个有点像 Linux 的用户 PATH 这个环境变量。设置了这个之后我们在建立数据库不指定模式的建立对象时默认都会放到 my_schema。但是需要注意,SET search_path 这个设置不是永久的,只在当前会话有效。这有点像 Linux 下终端里 export 一个变量,关掉终端之后就没了。

View & Materialized View,视图与物化视图

视图和物化视图就没那么好解释了,我 Google 了一下找到这个博客我觉得比较好理解:It's a view, it's a table... no, it's a materialized view!,节选下重点 :

Let's start with TABLE – it's basically an organized storage for your data - columns and rows. You can easily query the TABLE using predicates on the columns. To simplify your queries or maybe to apply different security mechanisms on data being accessed you can use VIEWs – named queries – think of them as glasses through which you can look at your data.

So if TABLE is storage, a VIEW is just a way of looking at it, a projection of the storage you might say. When you query a TABLE, you fetch its data directly. On the other hand, when you query a VIEW, you are basically querying another query that is stored in the VIEW's definition. But the query planner is aware of that and can (and usually does) apply some "magic" to merge the two together.

Between the two there is MATERIALIZED VIEW - it's a VIEW that has a query in its definition and uses this query to fetch the data directly from the storage, but it also has it's own storage that basically acts as a cache in between the underlying TABLE(s) and the queries operating on the MATERIALIZED VIEW. It can be refreshed, just like an invalidated cache - a process that would cause its definition's query to be executed again against the actual data. It can also be truncated, but then it wouldn't behave like a TABLE nor a VIEW. It's worth noting that this dual nature has some interesting consequences; unlike simple "nominal" VIEWs their MATERIALIZED cousins are "real", meaning you can - for example - create indices on them. On the other hand, you should also take care of removing bloat from them.

视图的本质是查询语句,而不是实在的表格。而物化视图的介于两者之间.....好吧,物化视图的解释没有很看懂。

我这个需求大概应该懂就行了吧。实际上在 mimic-code 提供的代码里基本上也都是在导入的原始数据上建立物化视图的。

因为视图和物化视图都是建立在查询上的,所以在创建时也就必须得有查询语句:

CREATE [MATERIALIZED] VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition];

删除视图类似删除表 DROP VIEW IF EXISTS view_name;,非空视图要DROP VIEW IF EXISTS view_name CASCADE;

2. PostgreSQL 中的数据类型

参考这一篇介绍 PostgreSQL 数据类型的博文:PostgreSQL数据类型。

数据类型指定要在表格中每一列存储哪种类型的数据。

创建表格时每列都必须使用数据类型。PotgreSQL中主要有三种数据类型:

  • 数值数据类型
  • 字符串数据类型
  • 日期/时间数据类型

数值

常见数值类型包括:

  • smallint:小范围整数;
  • integer:典型的整数类型;
  • bigint:可以存储大范围整数;
  • decimal,numeric:指定的精度的数字,精确数字;
  • real,double:可变精度数字,前者精度为 6 位,后者 15 位;

字符串字符串类型包括

  • char(size),character(size):固定长度字符串,size 规定了需存储的字符数,由右边的空格补齐;
  • varchar(size),character varying(size):可变长度字符串,size 规定了需存储的字符数;
  • text:可变长度字符串。

日期/时间

表示日期或时间的数据类型有:

  • timestamp:日期和时间,有或无时区;
  • date:日期,无时间;
  • time:时间,有或无时区;
  • interval:时间间隔。

其他数据类型类型还有布尔值 boolean (true 或 false),货币数额 money 和 几何数据等。

3. 入门命令

标准进入数据库的命令是 psql -U USER -d DB -h HOST -p PORT ,这样会要求密码然后进入 DB 数据库。但是我数据库是本地用,而且用户也添加到了数据库超级用户了,所以用户、主机、端口都可以省掉了。最后就是直接 psql -d DB 甚至 psql DB就行了。

极其常用命令列表一下:

d602c5db635188bd743dbbac645d6944.png

基本表格操作

创建一个含 name 和 date 这样两列的表格,而且指定了列的数据类型:

CREATE TABLE tab (name VARCHAR, date DATE);

在上述表格中插入一行数据:

INSERT INTO tab (name, date) VALUES ('Jackie', '20180708');

改动:

UPDATE tab SET name='Jack' WHERE name='Jackie';

删除行:

DELETE FROM tab WHERE name='Jack';

添加列:

ALTER TABLE tab ADD email VARCHAR;

更改列名:

ALTER TABLE tab RENAME COLUMN date TO date_add;

删除列:

ALTER TABLE tab DROP COLUMN email;

更改表格名:

ALTER TABLE tab RENAME TO my_tab;

删除表格:

DROP TABLE IF EXISTS my_tab;

问题归纳

1. postgresql 口令: psql: 致命错误: 用户 认证失败

在Windows环境下使用psql 命令出现认证失败。

网上找了很多,都没讲到要点

D:programPostgreSqlbin>psql

口令:

psql: 致命错误: 用户 "Lyncent" Password 认证失败

使用psql --help发现 当且仅当输入psql的时候,实际是后面跟着缺省 用户名,就是本地PC的用户名,

实际postgresql数据库里面根本没有这个用户;

初始用户只有postgres而已;

因此 使用

psql -U postgres 然后根据提示输入密码即可登录成功

2.cast用法:https://www.cnblogs.com/shuilangyizu/p/5952526.html

3. C#2010包的引用 直接放到建立的项目obj里 见下图,较容易打包。或者系统里

cb4f746c42f302db3a74c23a5b9f8936.png

4. 配置文件的制作及修改

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值