您的位置 首页 > 德语词汇

schema是什么意思?用法、例句 PostgreSQL模式(schema)

大家好,感谢邀请,今天来为大家分享一下schema是什么意思?用法、例句的问题,以及和PostgreSQL模式(schema)的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!

模式(schema)可以连接为一个命名空间或目录。不同的模式下可以有相同的名称的表、函数等对象且互相不冲突。模式是为了便于管理,只要有权限,每个模式的对象可以互相调用.

在PostgreSQL中,一个数据库包含一个或多个模式,模式又包含了表、函数及操作符等数据库对象。在PostgreSQL中,不能同时访问不同数据库中的对象,当要访问另一个数据库中的表或其他对象时,需要重新连接到这个新的数据库,而模式没有此限制。

schema是什么意思?用法、例句 PostgreSQL模式(schema)

CREATESCHEMAschema_name[AUTHORIZATIONrole_specification][schema_element[...]]\nCREATESCHEMAAUTHORIZATIONrole_specification[schema_element[...]]\nCREATESCHEMAIFNOTEXISTSschema_name[AUTHORIZATIONrole_specification]\nCREATESCHEMAIFNOTEXISTSAUTHORIZATIONrole_specification\n\nwhererole_specificationcanbe:\n\nuser_name\n|CURRENT_USER\n|SESSION_USER

模式使用例子:

1.创建schemasalesapp\n[root@node1~]#su-postgres\n-bash-4.2$psql\npostgres=#\\ctestdb;\npostgres=#CREATEROLEsalesappwithLOGINENCRYPTEDPASSWORD'abc123';\ntestdb=#\\du\nListofroles\nRolename|Attributes|Memberof\n-----------+------------------------------------------------------------+-----------\npostgres|Superuser,Createrole,CreateDB,Replication,BypassRLS|{}\nsalesapp||{}\n\ntestdb=#CREATESCHEMAAUTHORIZATIONsalesapp;\nCREATESCHEMA\n\n这里可以直接testdb=#CREATESCHEMAsalesapp;或者testdb=#CREATESCHEMAsalesappAUTHORIZATIONsalesapp;\n\ntestdb=#\\dn\nListofschemas\nName|Owner\n----------+----------\npublic|postgres\nsalesapp|salesapp\n(2rows)\n\ntestdb=#createtablesalesapp.t1(c1int);\nCREATETABLE\n\ntestdb=#select*frompg_tableswhereschemaname='salesapp';\nschemaname|tablename|tableowner|tablespace|hasindexes|hasrules|hastriggers|rowsecurity\n------------+-----------+------------+------------+------------+----------+-------------+-------------\nsalesapp|t1|postgres||f|f|f|f\n(1row)\n\ntestdb=#insertintosalesapp.t1values(1);\nINSERT01\ntestdb=#insertintosalesapp.t1values(2);\nINSERT01\ntestdb=#select*fromsalesapp.t1;\nc1\n----\n1\n2\n(2rows)\n\n=====================================================================\n2.用salesapp用户连接数据库,可以看到这个schema下面的表\n-bash-4.2$psql-h127.0.0.1-p5432testdbsalesapp\nPasswordforusersalesapp:\npsql(13.2)\nType"help"forhelp.\n\ntestdb=>\\dt\nListofrelations\nSchema|Name|Type|Owner\n----------+------+-------+----------\npublic|e|table|postgres\npublic|t|table|postgres\npublic|t2|table|postgres\npublic|x1|table|postgres\npublic|x2|table|postgres\npublic|x3|table|postgres\nsalesapp|t1|table|postgres\nsalesapp|t11|table|postgres\n=====================================================================\n3.删除schema\ntestdb=#DROPSCHEMAsalesappold;\nERROR:cannotdropschemasalesappoldbecauseotherobjectsdependonit\nDETAIL:tablesalesappold.t1dependsonschemasalesappold\ntablesalesappold.t11dependsonschemasalesappold\nHINT:UseDROP...CASCADEtodropthedependentobjectstoo.\ntestdb=#\\dn\nListofschemas\nName|Owner\n-------------+----------\npublic|postgres\nsalesappold|sales\n(2rows)\n\ntestdb=#DROPSCHEMAsalesappoldCASCADE;\nNOTICE:dropcascadesto2otherobjects\nDETAIL:dropcascadestotablesalesappold.t1\ndropcascadestotablesalesappold.t11\nDROPSCHEMA\ntestdb=#\\dn\nListofschemas\nName|Owner\n--------+----------\npublic|postgres\n(1row)

在模式中修改名称和属主

ALTERSCHEMAnameRENAMETOnewname

ALTERSCHEMAnameOWNERTOnewowner

1.改模式名称\npostgres=#\\ctestdb;\nYouarenowconnectedtodatabase"testdb"asuser"postgres".\ntestdb=#\\dn\nListofschemas\nName|Owner\n----------+----------\npublic|postgres\nsalesapp|salesapp\n(2rows)\n\ntestdb=#alterschemasalesapprenametosalesappold;\nALTERSCHEMA\ntestdb=#\\dn\nListofschemas\nName|Owner\n-------------+----------\npublic|postgres\nsalesappold|salesapp\n(2rows)\n\n2.改模式属主\ntestdb=#CREATEROLEsaleswithLOGINENCRYPTEDPASSWORD'abc123';\nCREATEROLE\ntestdb=#alterschemasalesappoldownertosales;\nALTERSCHEMA\ntestdb=#\\dn\nListofschemas\nName|Owner\n-------------+----------\npublic|postgres\nsalesappold|sales\n(2rows)\n\n注意:要指定OWNER的话,请确保OWNER已存在。6.2公共模式

要创建或者访问模式中的对象,需要先写出一个受修饰的名字,这个名字包含模式名及表名,用"点"分开。

schema_name.table_name

通常情况下,创建和访问表的时候不用指定模式,实际上这时访问的是"public"模式。每当我们创建了一个新的数据库时,PostgreSQL会自动创建一个名为"public"的模式,当登陆该数据库时,如果没有特殊指定,都是以该模式(public)操作各种数据对象的。

使用一个数据库对象时,虽然可以使用它的全称来定位对象,但这样一来,每次都不得不输入schema_name.object_name,这显然很繁琐。PostgreSQL提供了模式搜索路径,指定在哪个模式下执行,类似于设置默认模式。

1.查看当前SCHEMA,并列出public模式下的表\npostgres=#\\ctestdb\nYouarenowconnectedtodatabase"testdb"asuser"postgres".\ntestdb=#SHOWsearch_path;\nsearch_path\n-----------------\n"$user",public\n(1row)\n\ntestdb=#\\d\nListofrelations\nSchema|Name|Type|Owner\n--------+------+-------+----------\npublic|e|table|postgres\npublic|t|table|postgres\npublic|t1|table|postgres\npublic|t2|table|postgres\npublic|x1|table|postgres\npublic|x2|table|postgres\npublic|x3|table|postgres\n\ntestdb=#createschemasales;\nCREATESCHEMA\ntestdb=#SETsearch_pathTOsales,public;\n或者\ntestdb=#SETsearch_pathTOsales;\n\ntestdb=#SHOWsearch_path;\nsearch_path\n---------------\nsales,public\n(1row)\n\ntestdb=#createtablesales.t1(c1int);\nCREATETABLE\ntestdb=#\\d\nListofrelations\nSchema|Name|Type|Owner\n--------+------+-------+----------\npublic|e|table|postgres\npublic|t|table|postgres\npublic|t2|table|postgres\npublic|x1|table|postgres\npublic|x2|table|postgres\npublic|x3|table|postgres\nsales|t1|table|postgres\n(7rows)\n\ntestdb=#createtablet2(c1int);\nCREATETABLE\ntestdb=#\\d\nListofrelations\nSchema|Name|Type|Owner\n--------+------+-------+----------\npublic|e|table|postgres\npublic|t|table|postgres\npublic|x1|table|postgres\npublic|x2|table|postgres\npublic|x3|table|postgres\nsales|t1|table|postgres\nsales|t2|table|postgres\n(7rows)\n\n可以看到,我们不指定schema建表,也会默认在sales下面。\n6.4模式权限

默认情况下,用户无法访问模式不属于他们的对象。若要访问,模式的所有者必须在模式上赋予他们"USAGE"权限。为了让用户使用模式中的对象,可能需要赋予适合该对象的额外权限。

用户也可以在别人的模式里创建对象,当然,这需要被赋予了该模式上的"CREATE"权限。请注意,默认情况下每个人在public模式上都有"CREATE"和"USAGE"权限,也就是允许所有可以连接到指定数据库上的用户在这里创建对象。如果你不打算这么做,可以撤销这个权限

如果你还想了解更多这方面的信息,记得收藏关注本站。

本站涵盖的内容、图片、视频等数据,部分未能与原作者取得联系。若涉及版权问题,请及时通知我们并提供相关证明材料,我们将及时予以删除!谢谢大家的理解与支持!

Copyright © 2023