简体   繁体   English

DB2中数据库名称(不是服务器)的关键字是什么?

[英]What is the keyword for the database name (not the server) in DB2?

I can get the current database server that I'm logged in to via: 我可以通过以下方式获取已登录的当前数据库服务器:

select
  current server
from sysibm.sysdummy1

That's not what I want. 那不是我想要的 My Google searches only gave this solution. 我的Google搜索仅提供了此解决方案。

I want the database name. 我想要数据库名称。

In TSQL, I could do the following to get the database name: 在TSQL中,我可以执行以下操作来获取数据库名称:

select db_name()

But, how do I get the current database name in DB2? 但是,如何获得DB2中的当前数据库名称?

EDIT : Thanks for the answers so far. 编辑 :感谢到目前为止的答案。 You are correct, I using the wrong term (what would be database in TSQL) for what I am looking to return in DB2. 没错,对于要在DB2中返回的内容,我使用了错误的术语(在TSQL中为数据库)。 So since I'm not sure what it is called (maybe tablespace): 因此,由于我不确定它的名称(也许是表空间):

Is there a command that will return the word "prodbeans" in the following? 是否有以下命令将返回“ prodbeans”一词?

select
  <command>
from
  prodbeans.BNSMPLS_PROMO x

As @Leons says in his comment, CURRENT SERVER does return the name of the database. 就像@Leons在他的评论中所说,CURRENT SERVER确实返回数据库的名称。

$ db2 connect to sample

   Database Connection Information

 Database server        = DB2/LINUX 9.1.9
 SQL authorization ID   = IDBJORH
 Local database alias   = SAMPLE

$ db2 values current server

1                 
------------------
SAMPLE            

  1 record(s) selected.

It seems like your confusion arises from the fact that a "database" in SQL server, Sybase and even MySQL are conceptually closer to a either a tablespace or a schema within DB2. 您似乎感到困惑的原因是,SQL Server,Sybase甚至MySQL中的“数据库”在概念上更接近DB2中的表空间或模式。

In DB2, a tablespace is a logical container that holds physical database objects (tables, indexes). 在DB2中,表空间是一个逻辑容器,用于保存物理数据库对象(表,索引)。 The tablespace has containers (the physical files that define where the data is written, which are the same thing as a file group in SQL server). 表空间具有容器(定义数据写入位置的物理文件,与SQL Server中的文件组相同)。

A schema in DB2 is a logical qualifier for objects (tables, indexes, views, etc.). DB2中的模式是对象(表,索引,视图等)的逻辑限定符。 By default, when a user connects to a database, the CURRENT SCHEMA special register defaults to the user's login ID; 默认情况下,当用户连接到数据库时,CURRENT SCHEMA特殊寄存器默认为用户的登录ID;否则,默认为。 however this can be changed by using the SET CURRENT SCHEMA statement. 但是,可以使用SET CURRENT SCHEMA语句更改此设置。 CURRENT SCHEMA is used to qualify objects in an SQL statement. CURRENT SCHEMA用于限定SQL语句中的对象。

In SQL Server the "master" database is akin to the SYSCATSPACE tablespace within a DB2 database, which stores the system catalog in the SYSCAT (and SYSIBM) schemas. 在SQL Server中,“主”数据库类似于DB2数据库中的SYSCATSPACE表空间,该表空间将系统目录存储在SYSCAT(和SYSIBM)模式中。 The system catalog tables contain metadata about all objects in the database. 系统目录表包含有关数据库中所有对象的元数据。

SQL Server's "Tempdb" database equivalent to temporary tablespaces in a DB2 database. SQL Server的“ Tempdb”数据库等效于DB2数据库中的临时表空间。 By default there is a tablespace called TEMPSPACE1, but there can be multiple temporary tablespaces within a single database. 默认情况下,有一个名为TEMPSPACE1的表空间,但单个数据库中可以有多个临时表空间。

Msdb corresponds to the SYSTOOLSPACE tablespace (and SYSTOOLS schema). Msdb对应于SYSTOOLSPACE表空间(和SYSTOOLS模式)。

DB2 does not have an equivalent to the Model database. DB2没有与Model数据库等效的数据库。 (DB2 will create an empty database when you execute the "create database" command, but you can't set up a template for what is included in this database. (执行“创建数据库”命令时,DB2将创建一个空数据库,但是您无法为该数据库中包含的内容设置模板。

If your DB2 database is using SQL replication, there may be a set of tables within the database that store replication status and information, but there is no standard naming convention for the schema holding this data. 如果您的DB2数据库正在使用SQL复制,则数据库中可能存在一组存储复制状态和信息的表,但是对于保存此数据的模式没有标准的命名约定。 (this would be equivalent to the Distribution database). (这等效于分发数据库)。

There is a "special register" named CURRENT SERVER and one named CURRENT SCHEMA. 有一个名为“ CURRENT SERVER”的“特殊寄存器”和一个名为“ CURRENT SCHEMA”的寄存器。

See also: how do i get the current schema on DB2 if i have a JDBC conneciton? 另请参阅: 如果我有JDBC连接,如何在DB2上获取当前模式? .

CURRENT SCHEMA is set by default to the username, but can be changed to a more appropriate value. 默认情况下,CURRENT SCHEMA设置为用户名,但可以将其更改为更合适的值。 The following will return prodbeans: 以下将返回prodbeans:

SET SCHEMA prodbeans;
select
  CURRENT SCHEMA
from
  BNSMPLS_PROMO x;

Note that you don't need to qualify the table name if you set the schema. 请注意,如果设置架构,则不需要限定表名。

Based on your edited question, I think you are looking for the schema. 根据您编辑的问题,我认为您正在寻找架构。

For reference purposes, the following will get you a table's tablespace: 供参考,以下内容将为您提供表的表空间:

SELECT
  tbspace
FROM
  sysibm.systables
WHERE
  name = 'BNSMPLS_PROMO'
  AND creator = 'PRODBEANS';

"name" is the table name and "creator" is the schema name. “名称”是表名称,“创建者”是架构名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM