IBM Db2 on Cloud script creating tables in the wrong schema - db2

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

Related

Not able to create a foreign key; Relation " " does not exist

I am trying to create a foreign key called 'user_id' for a 'transactions' table where the user_id references the 'user_accounts' table 'id' column. I keep getting an error when I execute the script that says:
SQL Error [42P01]: ERROR: relation "user_accounts" does not exist
The table clearly exists as I have been populating the user_accounts table with data that can be viewed in dbeaver. I am using Postgres and I am aware that quotes/capitalization can really make things difficult but I have executed my entire script without capitalizing or using quotes on any of the table or column names. Although, I did capitalize some of my column data types and I am wondering if that is the issue here? If so, what direction should I take to get my foreign key to work?
My script:
create table if not exists user_accounts (
id serial primary key,
first_name VARCHAR(30),
last_name VARCHAR(30),
username VARCHAR(20),
password VARCHAR(20),
deposit INT,
creditScore INT
)
create table if not exists transactions (
transaction_id serial primary key,
user_id INT references user_accounts(id) not null,
transaction_type VARCHAR(20),
amount INT
)

PostgreSQL displaying integer inserted without commas, with commas (formatted like currency or similar)

SQL Server guy, new to PostgreSQL. Created the below table, performed the below insert, then ran a SELECT to see the data, yet the row shows the integer formatted with columns to break up the integer. Is this just a formatting style in the HeidiSQL utility I'm using, or is the data actually being stored as x,xxx,xxx,xxx rather than xxxxxxxxxx.
Table:
CREATE TABLE customer (
business_partner_id INTEGER PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
organisation_name VARCHAR(200),
date_of_bith DATE,
gender VARCHAR(50),
customer_type VARCHAR(50),
store_joined VARCHAR(10),
store_joined_date DATE,
created_date_time TIMESTAMP,
updated_date_time TIMESTAMP);
Insert:
-- Insert a customer
INSERT INTO customer VALUES
(1029884766,'James','Matson','Unknown','1980-02-17','M','Standard','303',CURRENT_DATE,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)
Query results:

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.

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

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;