在这篇文章中,我们将带领您了解连接表的Postgres唯一多列索引的全貌,同时,我们还将为您介绍有关centos7下源码编译安装php支持PostgreSQLpostgresql手册postgresq
在这篇文章中,我们将带领您了解连接表的Postgres唯一多列索引的全貌,同时,我们还将为您介绍有关centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教、Postgres 唯一约束与索引、postgres 密码错误:请输入用户“postgres”的密码以连接服务器 - “PostgreSQL 13”、postgresql 9.6 建立多列索引测试的知识,以帮助您更好地理解这个主题。
本文目录一览:- 连接表的Postgres唯一多列索引
- centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教
- Postgres 唯一约束与索引
- postgres 密码错误:请输入用户“postgres”的密码以连接服务器 - “PostgreSQL 13”
- postgresql 9.6 建立多列索引测试
连接表的Postgres唯一多列索引
我在Postgres中有一个多对多联接表,我想索引到A)提高性能(显然)和B)强制唯一性。例如:
a_id | b_id1 | 2 <- okay1 | 3 <- okay2 | 3 <- okay1 | 3 <- not okay (same as row 2)
是否可以在两列上使用一个索引来强制值唯一?我应该使用哪种类型的索引?
答案1
小编典典作为主键
如果该唯一键是主键,请执行以下操作:
create table tbl( a_id int not null, b_id int not null, constraint tbl_pkey primary key(a_id,b_id));
不是主键
如果唯一是非主键,请执行以下操作:
create table tbl( -- other primary key here, e.g.: -- id serial primary key, a_id int not null, b_id int not null, constraint tbl_unique unique(a_id,b_id));
现有表
如果您已有表,请执行以下操作:
alter table tbl add constraint tbl_unique unique(a_id, b_id)
该更改表显示以下消息:
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "tbl_unique" for table "tbl"Query returned successfully with no result in 22 ms.
降低
如果您想删除该约束(您可能希望通过3个字段的组合来使其唯一):
ALTER TABLE tbl DROP CONSTRAINT tbl_unique;
索引与约束与空值
关于索引,来自Postgres doc:
当为表定义唯一约束或主键时,PostgreSQL自动创建唯一索引
资料来源:http :
//www.postgresql.org/docs/9.1/static/indexes-
unique.html
如果唯一性取决于某些规则,则应使用CREATE UNIQUE INDEX
,例如:
鉴于这种:
CREATE TABLE tbl( a_id integer NOT NULL, b_id integer NULL );alter table tbl add constraint tbl_unique unique(a_id, b_id);
唯一可以捕获这些重复项,这将被数据库拒绝:
insert into tbl values(1,1),(1,1);
但是,UNIQUE
CONSTRAINT无法捕获重复的null。空值用作未知数,它们用作通配符,这就是为什么允许在唯一约束中具有多个空值的原因。这将被数据库接受:
insert into tbl values(1,1),(1,null), -- think of this null as wildcard, some real value can be assigned later.(1,null); -- and so is this. that''s why both of these nulls are allowed
考虑一下UNIQUE CONSTRAINT
它允许延迟唯一性,因此可以接受上面的空值。
如果除了唯一约束之外,每个a_id只需要一个通配符(null b_id),则需要添加一个UNIQUE INDEX
。UNIQUE
CONSTRAINT上不能有表达式。 INDEX
并且UNIQUE INDEX
可以。这将是您用于拒绝多个null的完整DDL;
这将是您完整的DDL:
CREATE TABLE tbl( a_id integer NOT NULL, b_id integer NULL );alter table tbl add constraint tbl_unique unique(a_id, b_id);create unique index tbl_unique_a_id on tbl(a_id) where b_id is null;
现在,您的数据库将拒绝此操作:
insert into tbl values(1,1),(1,null),(1,null);
这将被允许:
insert into tbl values(1,1),(1,null);
与http://www.ienablemuch.com/2010/12/postgresql-said-sql-server2008-said-
non.html相关
centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教
1. 下载源码
$ mkdir /usr/downloads $ wget -c http://cn2.php.net/distributions/php-5.6.20.tar.gz $ tar -xvf php-5.6.20.tar.gz $ mv php-5.6.20 /usr/local/src $ cd !$ & cd php-5.6.20
2. 阅读安装指导
$ ls -also $ less README $ less INSTALL
3. 安装依赖包
$ yum install apr apr-util apr-devel apr-util-devel prce lynx
4. 安装httpd
$ wget -c http://apache.fayea.com//httpd/httpd-2.4.20.tar.gz $ tar -xvf httpd-2.4.20.tar.gz $ cd httpd-2.4.20 $ ./configure \ --prefix=/usr/local/programs/apache2 \ --enable-rewrite \ --enable-so \ --enable-headers \ --enable-expires \ --with-mpm=worker \ --enable-modules=most \ --enable-deflate \ --enable-module=shared $ make $ make install $ cd /usr/local/programs/apache2 $ cp bin/apachectl /etc/init.d/httpd ## 复制启动脚本 $ /etc/init.d/httpd start ## 启动apache服务器,访问http://localhost/ $ egrep -v ''^[ ]*#|^$'' /usr/local/apache2/conf/httpd.conf | nl ## 查看apache服务器的配置 ## 将apache加入系统服务 vi /etc/rc.d/rc.local ``` /usr/local/programs/apache2/bin/apachectl start ``` $ cat /etc/rc.local
4. 安装postgresql
立即学习“PHP免费学习笔记(深入)”;
$ yum install readline-devel ## 安装readline依赖 $ cd /usr/downloads $ wget -c https://ftp.postgresql.org/pub/source/v9.5.0/postgresql-9.5.0.tar.bz2 $ tar -xvf postgresql-9.5.0.tar.bz2 $ cd postgresql-9.5.0 $ ./configure --prefix=/usr/local/programs/postgresql $ make $ su $ make install $ /sbin/ldconfig /usr/local/programs/postgresql/lib ## 刷新下共享动态库 $ cd /usr/local/programs/postgresql $ bin/psql --version ## 检查运行情况 ## 开始对postgresql的配置 $ vi /etc/profile.d/postgresql.sh ## 增加环境变量,不推荐直接在/etc/profile中添加,系统更新升级时会需要merge ``` PATH=/usr/local/programs/postgresql:$PATH export PATH ``` $ source /etc/profile ## 更新环境变量 ## 增加用户和其他文件夹 $ adduser postgres $ passwd postgres $ mkdir /usr/local/programs/postgresql/logs $ mkdir /usr/local/programs/postgresql/data $ chown postgres /usr/local/programs/postgresql/data $ su - postgres ## 初始化数据库 $ ./bin/initdb -D ./data $ ./bin/createdb test $ ./bin/psql test ## 已有数据库,可导入data文件夹后尝试root访问,假如带密码,可能需要进一步研究下 $ ./bin/postgres -D ./data >./logs/start-log-1.log 2>&1 & $ ./bin/psql --list ##列出数据库 ## ok,安装完成 ## 自定义设置,权限控制等,可以跳过,等熟悉使用后再做 ## 编辑数据库配置及权限文件: $ vi /usr/local/programs/postgresql/data/postgresql.conf ## 数据库配置文件 $ chown postgres postgresql.conf $ chmod 644 postgresql.conf $ vi /usr/local/programs/postgresql/data/pg_hba.conf ## 权限文件 $ vi /usr/local/programs/postgresql/data/pg_ident.conf ## 设置开机自启动: $ vi /etc/rc.d/rc.local ## 添加如下内容 ``` /usr/local/programs/postgresql/bin/postgresql start ```
5. 安装php
## 源码已经在第一步中下载,现在开始安装: $ yum install libxml2 libxml2-devel libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel $ ./configure \ --prefix=/usr/local/programs/php \ --with-apxs2=/usr/local/programs/apache2/bin/apxs \ --with-zlib \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-zlib-dir \ --enable-mbstring \ --with-pgsql=/usr/local/programs/postgresql \ --with-pdo-pgsql=/usr/local/programs/postgresql $ make $ make test > Bug #42718 (unsafe_raw filter not applied when configured as default filter) [ext/filter/tests/bug42718.phpt] XFAIL REASON: FILTER_UNSAFE_RAW not applied when configured as default filter, even with flags > Bug #67296 (filter_input doesn''t validate variables) [ext/filter/tests/bug49184.phpt] XFAIL REASON: See Bug #49184 > Bug #53640 (XBM images require width to be multiple of 8) [ext/gd/tests/bug53640.phpt] XFAIL REASON: Padding is not implemented yet > zend multibyte (7) [ext/mbstring/tests/zend_multibyte-07.phpt] XFAIL REASON: https://bugs.php.net/bug.php?id=66582 > zend multibyte (9) [ext/mbstring/tests/zend_multibyte-09.phpt] XFAIL REASON: https://bugs.php.net/bug.php?id=66582 >Bug #70470 (Built-in server truncates headers spanning over TCP packets) [sapi/cli/tests/bug70470.phpt] XFAIL REASON: bug is not fixed yet ## 查阅官方的bug,发现: > id=66582: status : Closed. Fixed in master (PHP7) > id=42718: status : Assigned > id=42718: reference to id=49184, unsolved for many years ## 那就不关心了,直接装吧 $ make install > You may want to add: /usr/local/programs/php/lib/php to your php.ini include_path ## 那就按它说的设置吧 $ cp php.ini-development /usr/local/programs/php/lib/php.ini ``` include_path = ".;/usr/local/programs/php/lib/php" ## 然后,编辑httpd的设置,确保其能正确解析php文件 ``` ... LoadModule php5_module modules/libphp5.so ... AddType application/x-httpd-php .php AddType application/x-httpd-php-source .php5 ... <ifmodule dir_module> DirectoryIndex index.html index.php </ifmodule> ``` ## 重启httpd,测试 $ cd /usr/local/programs/apache2 $ bin/httpd -h $ bin/httpd -k stop $ bin/httpd -f conf/httpd.conf ## 默认设置的www页面在./htdocs/下,那就先去里面建一个测试页面吧 $ vi htdocs/index.php ``` <?php phpinfo(); ?> ``` $ curl http://localhost/index.php |grep postgresql #ok
后续应该做的事
* 1. 启动时,不需要要手动指定配置文件
* 2. php初始化www目录设置
* 3. php 用户、权限管理等
以上就介绍了centos 7下源码编译安装php支持PostgreSQL,包括了postgresql,centos 7方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
Postgres 唯一约束与索引
据我了解文档,以下定义是等效的:
create table foo (
id serial primary key,code integer,label text,constraint foo_uq unique (code,label));
create table foo (
id serial primary key,label text);
create unique index foo_idx on foo using btree (code,label);
但是,Postgres 9.4 手册中的注释说:
向表添加唯一约束的首选方法是
ALTER TABLE ... ADD CONSTRAINT
.
使用索引来强制唯一约束可以被认为是不应该直接访问的实现细节。
(编辑:此注释已从 Postgres 9.5 的手册中删除。)
只是风格好不好?选择这些变体之一的实际后果是什么(例如在性能方面)?
postgres 密码错误:请输入用户“postgres”的密码以连接服务器 - “PostgreSQL 13”
如何解决postgres 密码错误:请输入用户“postgres”的密码以连接服务器 - “PostgreSQL 13”?
我与 postgres 的连接有问题,我正在通过 EDB 安装程序(版本 13.2)在 Windows 10 上进行安装。我按照它安装好的所有步骤进行操作,但是当我尝试使用 pgadmin 输入默认数据库时,我总是遇到此错误:
Please enter the password for the user ''postgres'' to connect the server - "Postgresql 13"
在安装过程中 postgres 要求我指定我的主密码。然后当安装完成并打开我的 pgadmin 时,它再次要求我输入主密码,所以我做到了并且我能够成功登录但是当我尝试打开默认数据库时,当它要求我输入 posgres 用户的密码时,我尝试使用我的主密码但没有用
我安装和卸载它很多次都没有成功。我也尝试过 this solution 所以我确实将 pg_hba.config
中的方法从 scram-sha-256
更改为 trust
但是当我执行此命令 psql -U postgres
时它再次要求我输入密码
请帮忙
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
postgresql 9.6 建立多列索引测试
建立测试表结构
CREATE TABLE t_test
(
id integer,
name text COLLATE pg_catalog."default",
address character varying(500) COLLATE pg_catalog."default"
);
插入测试数据
insert into t_test SELECT generate_series(1,10000000) as key,'name'||(random()*(10^3))::integer,'ChangAn Street NO'||(random()*(10^3))::integer;
建立3列索引
create index idx_t_test_id_name_address on t_test(id,name,address);
1.以下查询语句可以使用索引且较快
索引第一列在where语句,与条件次序无关
一般3毫秒多出结果
explain analyze select * from t_test where id < 2000 and name like 'name%' and address like 'ChangAn%';
explain analyze select * from t_test where address like 'ChangAn%' and name like 'name%' and id < 2000 ;
explain analyze select * from t_test where name like 'name%' and id < 2000 and address like 'ChangAn%' ;
explain analyze select * from t_test where id < 2000
explain analyze select * from t_test where name like 'name%' and id < 2000
explain analyze select * from t_test where address like 'ChangAn%' and id < 2000 ;
explain analyze select * from t_test where address like 'ChangAn%' and name like 'name%' and id < 2000 ;
2.以下可以使用索引,但是查询速度较慢
索引第一列在order by
explain analyze select * from t_test where address like 'ChangAn%' and name like 'name%' order by id;
17S
explain analyze select * from t_test where address like 'ChangAn%' order by id;
8s
explain analyze select * from t_test where name like 'name%' order by id;
9s
以下语句无法使用索引,索引第一列不在 where 或者 order by
explain analyze select * from t_test where address like 'ChangAn%' and name like 'name%';
explain analyze select * from t_test where address like 'ChangAn%';
explain analyze select * from t_test where name like 'name%';
建立双列索引
create index idx_t_test_name_address on t_test(name,address);
以下语句会使用索引
explain analyze select * from t_test where name = 'name580';
explain analyze select * from t_test where address like 'ChangAn%' and name like 'name580';
explain analyze select * from t_test where address like 'ChangAn%' and name = 'name580';
下面语句不会使用索引
explain analyze select * from t_test where name like 'name%'
explain analyze select * from t_test where address like 'ChangAn%'
explain analyze select * from t_test where address = 'ChangAn Street NO416'
关于连接表的Postgres唯一多列索引的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于centos 7下源码编译安装php支持PostgreSQL postgresql手册 postgresql官网下载 postgresql视频教、Postgres 唯一约束与索引、postgres 密码错误:请输入用户“postgres”的密码以连接服务器 - “PostgreSQL 13”、postgresql 9.6 建立多列索引测试等相关内容,可以在本站寻找。
本文标签: