暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

几道小题:几个易出错的PostgreSQL小用法(一)

数据库杂记 2024-07-14
82

声明:

本文中所谓的“题目”不来自于任何第三方机构,任何第三方机构或个人如果采用本文所出题目,造成任何后果,本文概不负责。

题目:

1、在PostgreSQL中,使用drop table test
,  表test对应的数据文件,会立即删除吗?

A. 会     B. 不会

简解:选“不会”。 只有在数据真正落盘的时候,才会真正删除对应的表数据文件。比如checkpoint调用完以后。

postgres=# create table t(id int);
CREATE TABLE
postgres=# insert into t values(1), (2);
INSERT 0 2

postgres=# \! oid2name -t t
From database "postgres":
  Filenode  Table Name
----------------------
     16450           t

// 找到filenode的完全path
postgres=# select oid, relname, (select setting from pg_settings where name = 'data_directory') || '/' || pg_relation_filepath(oid) from pg_class where relname = 't';
  oid  | relname |                ?column?
-------+---------+-----------------------------------------
 16450 | t       | /var/lib/pgsql/14/data/base/14486/16450
(1 row)

postgres=# \! ls /var/lib/pgsql/14/data/base/14486/16450
/var/lib/pgsql/14/data/base/14486/16450

// drop table以后,一小段时间文件仍存在
postgres=# drop table t;
DROP TABLE
postgres=# \! ls /var/lib/pgsql/14/data/base/14486/16450
/var/lib/pgsql/14/data/base/14486/16450

// 检查点执行完以后, 文件消失
postgres=# checkpoint
postgres-# ;
CHECKPOINT
postgres=# \! ls /var/lib/pgsql/14/data/base/14486/16450
ls: cannot access /var/lib/pgsql/14/data/base/14486/16450No such file or directory

2、PostgreSQL14及以上,是否支持下面的SQL语句:Create table test();
?

A.支持    B.不支持

答:A. 支持
解析:略

3、PostgreSQL14及以上, 针对表:test,  简单的建表句:create table t(id ) as values (1), (2);;
针对最简单的查询:

SELECT FROM test;

其返回值是什么?是否有语法错误?

A. 不支持这种语法,会报错

B. 语法上支持,返回结果集为空,0行

C. 语法上支持,返回结果集没有值,但是返回2行

解析:C
postgres=# create table t(id ) as values (1), (2);
SELECT 2
postgres=# select from t;
--
(2 rows)

这种‘骚’语法,也只有在PG中出现。如果是MYSQL, 还真的会报错:
create table test(id int);
insert into test values(1), (2);
select from test;


Query Error: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from test' at line 1

4、PostgreSQL 14及以上,表空间foo已经创建好,有关表空间的SQL语句:

create table t(id int primary key, col2 varchar(4000)) tablespace foo;

请问,表t以及它的索引对应的数据文件的存储位置,描述正确的是?

A. t以及索引t_pkey都存储在表空间foo对应的物理目录中

B. t存储于foo表空间当中,而t_pkey存储在默认表空间pg_default中

解析:选B
如果要想都存储到foo当中,则要使用SQL:
create table t(id int primary key tablespace foo, col2 varchar(4000)) tablespace foo;
这块儿有点啰嗦。但是得留意。
可以自行实验,过程略。

5、PostgreSQL中,已有建表语句:create table t(id ) as values (1), (2); 现在有一个语句:

table t;

请问:该语句的执行结果,描述正确的是?

A. 语法错误,不支持该语法

B. 等同于元命令\d t

C. 等同于select * from t

选C

postgres=# \d t
                 Table "public.t"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |

postgres=# table t;
 id
----
  1
  2
(2 rows)

postgres=# select * from t;
 id
----
  1
  2
(2 rows)

欢迎文后留言,以后会偶尔将一些知识点以题目的形式展示出来,便于加强巩固。

我是【Sean】,  欢迎大家长按关注并加星公众号:数据库杂记。有好资源相送,同时为你提供及时更新。已关注的朋友,发送0、1到7,都有好资源相送。

往期导读和参考:
1. PostgreSQL中配置单双向SSL连接详解
2. 提升PSQL使用技巧:PostgreSQL中PSQL使用技巧汇集(1)
3. 提升PSQL使用技巧:PostgreSQL中PSQL使用技巧汇集(2)
4. PostgreSQL SQL的基础使用及技巧
5. PostgreSQL开发技术基础:过程与函数

文章转载自数据库杂记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论