How to create tables in a specific database in a DB2 for z/OS subsystem using T-SQL? - tsql

Below T-SQL only creates table in the schema of current user, not the database specified. Why?
CREATE TABLE TEST ( URI VARCHAR(255) NOT NULL, PARENT_URI VARCHAR(255) ,
TITLE VARCHAR(1000) NOT NULL, MIMETYPE VARCHAR(100) , DESCRIPTION VARCHAR(1000) ,
MODIFIED_BY VARCHAR(1000)) IN DATABASE DB4TEST;
The table can be created, but I can only see it in current user's default schema, not in the database I specified. The database does exist.
Thanks.

You are confusing the two concepts of schema and database.
A DB2 database can contain multiple schemas.
If you want the table to be created inside SCHEMA DB4TEST in the DATABASE DB4TEST, try this:
CREATE TABLE DB4TEST.TEST ( URI VARCHAR(255) NOT NULL, PARENT_URI VARCHAR(255) ,
TITLE VARCHAR(1000) NOT NULL, MIMETYPE VARCHAR(100) , DESCRIPTION VARCHAR(1000) ,
MODIFIED_BY VARCHAR(1000)) IN DATABASE DB4TEST;

Related

DDL with Index and Hash Partition on Postgres Aurora

Hi i am trying to create a DDL in PostgreSQL Aurora with Hash partition on OBJECT_ID.
Also i want to create a Index on CUSTOMER_ID,OBJECT_TYPE,OBJECT_ID,PT_EVENT_ID
CREATE TABLE event_test(
ID varchar(255) PRIMARY KEY NOT NULL,
VERSION int(11) NOT NULL,
ORDER_TYPE varchar(255) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
CUSTOMER_ID varchar(255) DEFAULT NULL,
DETAILS text,
OBJECT_TYPE varchar(255) NOT NULL,
UTC_DATE_TIME date DEFAULT NULL,
EVENT_TO_UTC_DT date DEFAULT NULL,
GROUP_ID varchar(255) DEFAULT NULL,
OBJECT_NAME varchar(2001) DEFAULT NULL,
OBJECT_ID varchar(255) DEFAULT NULL,
USER_NAME varchar(1500) DEFAULT NULL,
USER_ID varchar(255) DEFAULT NULL,
PT_EVENT_ID varchar(255) DEFAULT NULL,
CUSTOM_NOTES varchar(1000) DEFAULT NULL,
SUMMARY varchar(4000) DEFAULT NULL
);
Can some please help me with the DDL .
If all those IDs are in fact UUIDs, the columns should be defined with the uuid type.
The supposedly "magic" limit of 255 does not enable some hidden performance or storage optimizations (at least in Postgres). So blindly using varchar(255) doesn't really make sense (of course if you have a valid business requirement that a value for order_type or event_type may never be longer than 255 characters, then of course keep that constraint.
As documented in the manual there is also no "length" parameter for the integer data type (and it's not a value restriction in MySQL either, so it's pretty much useless to begin with).
So the DDL should be something like this:
CREATE TABLE event_test(
ID uuid PRIMARY KEY NOT NULL,
VERSION integer NOT NULL,
ORDER_TYPE varchar(255) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
CUSTOMER_ID uuid DEFAULT NULL,
DETAILS text,
OBJECT_TYPE varchar(255) NOT NULL,
UTC_DATE_TIME date DEFAULT NULL,
EVENT_TO_UTC_DT date DEFAULT NULL,
GROUP_ID uuid DEFAULT NULL,
OBJECT_NAME varchar(2001) DEFAULT NULL,
OBJECT_ID uuid DEFAULT NULL,
USER_NAME varchar(1500) DEFAULT NULL,
USER_ID uuid DEFAULT NULL,
PT_EVENT_ID uuid DEFAULT NULL,
CUSTOM_NOTES varchar(1000) DEFAULT NULL,
SUMMARY varchar(4000) DEFAULT NULL
);
To create an index, you use create index:
create index on event_test (customer_id,object_type,object_id,pt_event_id);
If "most of your access" is through the object_id then you need an index where that is the leading column:
create index on event_test (object_id);
Hash partitioning won't really help you there to make things faster.
You can use partitioning for the table, but this is hardly a performance tool. Due to the limitations of the Postgres partitioning implementation you will also be forced to include the id column in the partitioning key if you want to keep that as the primary key. But given your statement that "most access is through object_id the partitioning key (id, object_id) wouldn't help you at all.

IBM Db2 on Cloud script creating tables in the wrong schema

On IBM Db2 on Cloud I have imported a script. I created a new schema under which I want to have the new tables created, but when I run the script, it keeps trying to create the tables in a previous schema. Not sure how to get the scripts to create the tables in the new schema.
I have tried the below script without the .SQL_GROUPING_SORTING and it tries to add the tables to a different schema. I have changed the default schema in the Run SQL window within db2 to SQL_GROUPING_SORTING and am now getting the error
""KZF72118" does not have the privilege to perform operation "IMPLICIT CREATE SCHEMA".. SQLCODE=-552, SQLSTATE=42502, DRIVER=4.26.14"
DDL statement for table 'HR' database:
CREATE TABLE EMPLOYEES.SQL_GROUPING_SORTING (
EMP_ID CHAR(9) NOT NULL,
F_NAME VARCHAR(15) NOT NULL,
L_NAME VARCHAR(15) NOT NULL,
SSN CHAR(9),
B_DATE DATE,
SEX CHAR,
ADDRESS VARCHAR(30),
JOB_ID CHAR(9),
SALARY DECIMAL(10,2),
MANAGER_ID CHAR(9),
DEP_ID CHAR(9) NOT NULL,
PRIMARY KEY (EMP_ID));
CREATE TABLE JOB_HISTORY.SQL_GROUPING_SORTING (
EMPL_ID CHAR(9) NOT NULL,
START_DATE DATE,
JOBS_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9),
PRIMARY KEY (EMPL_ID,JOBS_ID));
CREATE TABLE JOBS.SQL_GROUPING_SORTING (
JOB_IDENT CHAR(9) NOT NULL,
JOB_TITLE VARCHAR(15) ,
MIN_SALARY DECIMAL(10,2),
MAX_SALARY DECIMAL(10,2),
PRIMARY KEY (JOB_IDENT));
CREATE TABLE DEPARTMENTS.SQL_GROUPING_SORTING (
DEPT_ID_DEP CHAR(9) NOT NULL,
DEP_NAME VARCHAR(15) ,
MANAGER_ID CHAR(9),
LOC_ID CHAR(9),
PRIMARY KEY (DEPT_ID_DEP));
CREATE TABLE LOCATIONS.SQL_GROUPING_SORTING (
LOCT_ID CHAR(9) NOT NULL,
DEP_ID_LOC CHAR(9) NOT NULL,
PRIMARY KEY (LOCT_ID,DEP_ID_LOC));
With the Db2 on Cloud Lite Plan
The Lite plan uses one database schema.
So the only schema you can use is the one that matches your user name. In your case this would be KZF72118
Create your tables with out a schema name, and they will be created in schema KZF72118.
You would need to use one of the other plans to remove this restriction

How do I copy information from a text file into a table in my created database in postgresql via pgadmin?

I am working on windows. I have installed postgresql-12 and pgadmin-4 Now through pgadmin I have created a database and through the query tool I have punched in some syntax to create a table within the created database. The syntax is as follows:
CREATE TABLE Calendar (
Date date NOT NULL,
ISO varchar(10) NOT NULL,
datenum int NOT NULL,
DOW char(3) NOT NULL,
DOWint smallint NOT NULL,
Year smallint NOT NULL,
Month smallint NOT NULL,
DOM smallint NOT NULL,
MonthAbbr char(3) NOT NULL,
DOY smallint NOT NULL,
Mondays smallint NOT NULL,
Tuesdays smallint NOT NULL,
Wednesdays smallint NOT NULL,
Thursdays smallint NOT NULL,
Fridays smallint NOT NULL,
Saturdays smallint NOT NULL,
Sundays smallint NOT NULL,
NumHolidays int NOT NULL,
HolidayName varchar(255) NULL,
HolidayType varchar(9) NULL,
hol_National varchar(255) NULL,
hol_Minor varchar(255) NULL,
hol_Christian varchar(255) NULL,
hol_Jewish varchar(255) NULL,
hol_Muslim varchar(255) NULL,
hol_Chinese varchar(255) NULL,
hol_Other varchar(255) NULL
) ;
Now I punch in the following piece of code to fill the created table with data:
COPY Calendar FROM 'C:\Users\LENOVO\SQLbook_database\Calendar.txt'
WITH HEADER NULL 'NULL' DELIMITER ' ' CSV;
When i run the above code it shows me the following message:
ERROR: could not open file "C:\Users\LENOVO\SQLbook_database\Calendar.txt" for reading: Permission denied
HINT: COPY FROM instructs the PostgreSQL server process to read a file. You may want a client-side facility such as psql's \copy.
SQL state: 42501
COPY is a server side command. It can only work with files that the user the server runs as has permissions for. For more information see COPY Your choices are to change the file permissions to allow the server user to access it or do as the HINT: says and use psql's \copy command psql.
UPDATE
I don't use pgAdmin much so I forgot, you can also use the Import/Export Dialog in pgAdmin).

Query two tables with one to many relationship

I'm using two Postgres tables that have a one to many relationship. The primary table is called users and the other table is called files.
The users table has the following columns:
id SERIAL PRIMARY KEY,
email VARCHAR(128) NOT NULL,
username VARCHAR(128) UNIQUE NOT NULL,
password_hash VARCHAR(128) NOT NULL
The files table has the following columns:
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title VARCHAR(128) NOT NULL,
url VARCHAR(128) NOT NULL
When I log into my app, I'm querying all the files to display by doing
cur.execute('SELECT * from files')
and when I want a specific users files I run
cur.execute('SELECT * from files where user_id = %i' % user_id)
For my query that fetches all the files, I'd like to adjust it so that I get the username associated with each file also. How should I tailor my execute statement to make that happen?
Try the following. I know this syntax would work with other dbms':
cur.execute('SELECT f.*, u.username from files as f, users as u where u.id = f.user_Id)

Data not inherited in postgresql table inheritance

I have created two tables in postgresql as follows:
CREATE TABLE legals.cashaccount
(
cashaccid serial NOT NULL,
cashserial bigint NOT NULL DEFAULT nextval('legals.global_id_sequence'::regclass),
memo character varying(50) NOT NULL,
credit numeric(255,0),
debit numeric(255,0),
transactdate timestamp without time zone,
CONSTRAINT cashaccount_pkey PRIMARY KEY (cashaccid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE legals.cashaccount
OWNER TO postgres;
and
CREATE TABLE legals.ledgeraccount
(
ledgerid bigserial NOT NULL,
cashaccid integer NOT NULL,
memo character varying(50) NOT NULL,
credit numeric(255,0),
debit numeric(255,0),
cashserial bigint NOT NULL,
clientserial bigint NOT NULL,
clientaccid bigint NOT NULL,
transactdate timestamp without time zone,
CONSTRAINT ledgeraccount_pkey PRIMARY KEY (ledgerid)
)
INHERITS (legals.cashaccount)
WITH (
OIDS=FALSE
);
ALTER TABLE legals.ledgeraccount
OWNER TO postgres;
However, I find that while table ledgeraccount inherits the structure of table cashaccount. Any data inserted into cash account is not inherited by ledgeraccount. Does this mean that table inheritance in postgresql only applies to the structural part of the tables and not to data contained in the tables?
I find table inheritance in postgresql odd for instance ledgeraccount will inherit structures of cashaccount but not any data inserted into cashaccount. However any data inserted into the child table ledgeraccount will be inherited by the parent table cash account. Further research by watching Meet PostgreSQL by Pluralsight by Xavier Shay section on inheritance clarified the issue.