default tablespace for schema in DB2 - db2

We have got some DDLs for our schema. These DDLs create tables and indexes in a given tablespace. Something like:
CREATE TABLE mySchema.myTable
(
someField1 CHAR(2) NOT NULL ,
someField2 VARCHAR(70) NOT NULL
)
IN MY_TBSPC
INDEX IN MY_TBSPC;
We want to reuse this DDLs to run some integration tests using APACHE DERBY. The problem is that such syntax is not accepted by DERBY. Is there any way to define a kind of default tablespace for tables and indexes, so we can remove this 'IN TABLESPACE' statements.

There is no deterministic way of defining a "default" tablespace in DB2 (I'm assuming we're dealing with DB2 for LUW here). If the tablespaces are not explicitly indicated in the CREATE TABLE statement, the database manager will pick for table data the first tablespace with the suitable page size that you are authorized to use, and indexes will be stored in the same tablespace as data.
This means that if you only have one user tablespace it will always be used for both data and indexes, so in a way it becomes the default. However, if you have more than one tablespace with different page sizes you may end up with tables (and their indexes) in different tablespaces.

Related

Difference between oid and relfilenode

I am reading Internals of postgreSQL chp 1 and I am unable to understand the difference between object identifier and relfilenode.
Tables and indexes as database objects are internally managed by individual OIDs, while those data files are managed by the variable, relfilenode. The relfilenode values of tables and indexes basically but not always match the respective OIDs
I get that both these are the attributes of the system catalog 'pg_class' and OID can be thought of as the primary key of the table, so what is the purpose of relfilenode and how is it different from OID?
relfilenode is the prefix for the name of the files that make up the table. Initially it is identical to the immutable object ID (oid), but SQL statements that rewrite the table will modify it (for example VACUUM (FULL), CLUSTER, TRUNCATE or the variants of ALTER TABLE that rewrite the table).

How do we map the logical table to the tablespace?

How do we map the logical table to the tablespace?
Does Postgres automatically creates tablespaces when we create a table or a schema?
Tablespaces and schemas are independent from each other: the first concerns the physical placement of the table, the second the namespace.
PostgreSQL automatically creates a tablespace: it is called pg_default and is located in the base subdirectory of the data directory.
All tables you create are placed there unless:
You use the TABLESPACE clause of CREATE TABLE to place it elsewhere.
The table is in a database with a different tablespace.
You set the default_tablespace parameter to a different tablespace.
Tablespaces are rarely needed, and usually it is best to keep all tables in the default tablespace.

How a Table space for a query execution is chosen? - DB2

For one of my issue, I am trying to understand the functionalities of below DB2 entities,
System Temporary table space.
Page Size.
Table Space.
Buffer Pool.
And below is my observation,
There is a TableSpace linked to a table in the DB2 table syscat.tables
The TableSpaces are linked to BufferPool with the relation defined in syscat.tablespaces
System Temporary table space is the table space that the DB might use while executing the query.
Page Size is an unit that defines the limit of a TableSpace and says how much data can a TableSpace can hold.
Is there something wrong in my above understandings? And when I excute a query how does the DB chooses which TableSpace to choose?

DB2 create table and insert in tablespace

my DBA created three different tablespaces in DB2 in order to store the same table (TABLE_IN_TBS), switching on a date field.
CREATE LARGE TABLESPACE "TBS_x" IN DATABASE PARTITION GROUP NODO0 PAGESIZE 32768 MANAGED BY DATABASE
USING (FILE 'x.LRG' 1G) ON DBPARTITIONNUMS (0)
EXTENTSIZE 32
PREFETCHSIZE AUTOMATIC
BUFFERPOOL BP32K0
OVERHEAD 12.670000
TRANSFERRATE 0.180000
AUTORESIZE YES
MAXSIZE 30 G
NO FILE SYSTEM CACHING
DROPPED TABLE RECOVERY ON;
Then I have TBS_x in which data in predefined range will be add, TBS_x+1 for the next range and so on..
My question is: I have to create the same table TABLE_IN_TBS in the different tablespaces? With the following syntax?
CREATE TABLE TABLE_IN_TBS
(
SomeColumns....
) TABLESPACE TBS_x;
And in which way I can refer to different tablespaces in order to insert data in the right tablespace based on my date field to switch?
I think you're talking about a partitioned table. It is defined like this:
CREATE TABLE TABLE_IN_TBS
(
SomeColumns....
)
PARTITION BY (your_date_column)
(STARTING FROM '2013-09-30' ENDING AT '2013-09-30' IN TABLESPACE TBS_1),
(STARTING FROM '2013-10-01' ENDING AT '2013-10-01' IN TABLESPACE TBS_2),
...;

Where will the tablespace be stored?

I am creating a table with tablespace:
CREATE TABLE SALARY.....
IN ACCOUNTING INDEX IN ACCOUNT_IDX
Where will the Accounting and Account_IDX be created?
The script that you have above will create SALARY in the ACCOUNTING tablespace, with indexes for that table in ACCOUNT_IDX tablespace.
The ACCOUNTING and ACCOUNT_IDX tablespaces need to be created in a separate script that has CREATE TABLESPACE statements.
If you look at the syntax for CREATE TABLESPACE, the USING part of the statement will tell DB2 where to put the files for the tablespace.
DB2 Create Tablespace Reference