如果我不为数据库定义特定模式会怎样?
What happens if I don´t define a specific schema to the database?
如果数据库中未定义特定模式,数据库对象将存储在哪里?这是好事还是坏事?为什么?
In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema
如果在创建 table 时未定义架构,schema search path 中找到的第一个(现有)架构将用于存储 table。
在psql
create database sch_test;
CREATE DATABASE
\c sch_test
You are now connected to database "sch_test" as user "postgres".
--Show available schemas
\dn
List of schemas
Name | Owner
--------+----------
public | postgres
drop schema public ;
DROP SCHEMA
\dn
List of schemas
Name | Owner
------+-------
(0 rows)
show search_path ;
search_path
-----------------
"$user", public
create table tbl(id integer);
ERROR: no schema has been selected to create in
LINE 1: create table tbl(id integer);
create table test.tbl(id integer);
ERROR: schema "test" does not exist
LINE 1: create table test.tbl(id integer);
只是为了说明如果模式不存在,则可能不会创建对象。底线是需要在模式中创建的对象(table、函数等)。如果有 none 可供 search_path
查找,或者您专门指向一个不存在的对象,则对象创建将失败。
如果数据库中未定义特定模式,数据库对象将存储在哪里?这是好事还是坏事?为什么?
In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema
如果在创建 table 时未定义架构,schema search path 中找到的第一个(现有)架构将用于存储 table。
在psql
create database sch_test;
CREATE DATABASE
\c sch_test
You are now connected to database "sch_test" as user "postgres".
--Show available schemas
\dn
List of schemas
Name | Owner
--------+----------
public | postgres
drop schema public ;
DROP SCHEMA
\dn
List of schemas
Name | Owner
------+-------
(0 rows)
show search_path ;
search_path
-----------------
"$user", public
create table tbl(id integer);
ERROR: no schema has been selected to create in
LINE 1: create table tbl(id integer);
create table test.tbl(id integer);
ERROR: schema "test" does not exist
LINE 1: create table test.tbl(id integer);
只是为了说明如果模式不存在,则可能不会创建对象。底线是需要在模式中创建的对象(table、函数等)。如果有 none 可供 search_path
查找,或者您专门指向一个不存在的对象,则对象创建将失败。