create table in Db2 for iSeries - db2

I have a table structure like this in sql server:
CREATE TABLE [dbo].[taname](
[ID] [char](7) NOT NULL,
[SOURCE] [char](14) NOT NULL,
[TARGET] [char](14) NOT NULL,
[ID1] [char](100) NULL,
)
this similar table I'm trying to create in DB2:
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100) NULL --error is here
);
However, I'm getting error in "ID":
Keyword NULL not expected. Valid tokens: AS NO FOR NOT FILE WITH CCSID CHECK LOGGED UNIQUE COMPACT. Cause . . . . . : The keyword NULL was not expected here. A syntax error was detected at keyword NULL. The partial list of valid tokens is AS NO FOR NOT FILE WITH CCSID CHECK LOGGED UNIQUE COMPACT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
Processing ended because the highlighted statement did not complete successfully
I would like to create table similar to SQL Server and allow NULL in the ID field. How can I correct this?

NULL is the default... you can just leave it off...
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100)
);
alternatively, specify the DEFAULT clause...
CREATE TABLE schema.taname(
ID char(7) NOT NULL,
SOURCE char(14) NOT NULL,
TARGET char(14) NOT NULL,
ID1 char(100) DEFAULT NULL
);

Related

42601 ERROR: syntax error at or near "NULLS" using unique NULLS NOT DISTINCT on PostgreSQL 14.4 Ubuntu

I have a situation where I want my composite field null check to be UNIQUE. So I'm using the UNIQUE NULLS NOT DISTINCT syntax as follows:
create table codes
(
id serial primary key,
code text not null,
sub_code text null,
unique nulls not distinct (code,sub_code)
);
In this example the code can be entered again if it has a sub_code but only one version of codes can exist without a sub_code
It looks like PostgreSQL is rejecting the word nulls. Any ideas why? Is there a configuration to turn this syntax on?
You have to use the CONSTRAINT keyword like this:
create table codes
(
id serial primary key,
code text not null,
sub_code text null,
CONSTRAINT uq_sub_code
UNIQUE NULLS NOT DISTINCT (sub_code)
);

How to fix syntax error at or near “CREATE” in psql

please why is this code not working?
nodelogin-# CREATE TABLE user
nodelogin-# (id BIGSERIAL PRIMARY KEY NOT NULL,
nodelogin(# name VARCHAR(200) NOT NULL,
nodelogin(# email VARCHAR(200) NOT NULL,
nodelogin(# password VARCHAR(200) NOT NULL,
nodelogin(# UNIQUE (email));
ERROR: syntax error at or near "CREATE"
LINE 2: CREATE TABLE user
^
user is a reserved key word in PostgreSQL. Basically, PostgreSQL doesn't like the name. If you try a different name (user2, user_, comp_user) it should work

Postgresql jsonb column to row extracted values

I have the below table
BEGIN;
CREATE TABLE IF NOT EXISTS "public".appevents (
id uuid DEFAULT uuid_generate_v4() NOT NULL,
"eventId" uuid NOT NULL,
name text NOT NULL,
"creationTime" timestamp without time zone NOT NULL,
"creationTimeInMilliseconds" bigint NOT NULL,
metadata jsonb NOT NULL,
PRIMARY KEY(id)
);
COMMIT;
I would like to extract with a query the metadata jsonb column as a row and tried with the below query.
SELECT
userId
FROM
appevents, jsonb_to_record(appevents.metadata) as x(userId text)
Unfortunately, all the rows returned for userid have the value NULL which is not true. The only weird thing noticed is that it is converting camelcase to lowercase but doesn't seem like the issue.
Here are the 2 records I currently have in the database where userId exists.
The only weird thing noticed is that it is converting camelcase to lowercase but doesn't seem like the issue.
Actually that is the culprit - column names are case-insensitive by default, and userId is normalised to userid, for which the JSON doesn't contain a property. Quoting the identifier (… as x("userId" text)) should work.
However, there's a much simpler solution for accessing json object properties as text: the ->> operator. You can use
SELECT metadata->>'userId' AS userid FROM appevents

Can't create table with text[] data type

I'm trying to store an entity in my postgresql database. This entity has a List in it, so I'd like to use postgresql type TEXT[]. But everytime I'm trying I get a SQL error, I have no idea why.
I don't get the syntax error, really. I'm sure it's a dumb issue but can you help me?
Thank you
I tried some alternatives, creating it directly from h2 console but I always get the same error
The script I use with flyway for creating the table
CREATE TABLE discrimination(
id SERIAL PRIMARY KEY NOT NULL ,
location VARCHAR(255) NOT NULL,
criteria TEXT[] NOT NULL,
domain VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
name_organ VARCHAR(55) NOT NULL,
function_disc VARCHAR(55) NOT NULL
);
my application config for h2 & flyway
h2:
console:
enabled: true
path: /h2
datasource:
url: jdbc:h2:mem:formation-iris;MODE=PostgreSQL
username: test
password: test
driver-class-name: org.h2.Driver
flyway:
locations: classpath:db/migration
enabled: true
And the error I get
Syntax error in SQL statement "CREATE TABLE DISCRIMINATION(
ID SERIAL PRIMARY KEY NOT NULL ,
LOCATION VARCHAR(255) NOT NULL,
CRITERIA TEXT[[*]] NOT NULL,
DOMAIN VARCHAR(255) NOT NULL,
DESCRIPTION TEXT NOT NULL,
NAME_ORGAN VARCHAR(55) NOT NULL,
FUNCTION_DISC VARCHAR(55) NOT NULL
) "; expected "(, FOR, UNSIGNED, INVISIBLE, VISIBLE, NOT, NULL, AS, DEFAULT, GENERATED, ON, NOT, NULL, AUTO_INCREMENT, BIGSERIAL, SERIAL, IDENTITY, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
From H2 documentation:
Compatibility Modes
For certain features, this database can emulate
the behavior of specific databases. However, only a small subset of
the differences between databases are implemented in this way.
Which means that H2 can emulate certain DB-specific behaviours, but it won't be fully compatible with the selected DB.
That's especially true for SQL syntax.
So, if you want to use arrays in H2, then you should use the H2 syntax ARRAY instead of TEXT[]
Which also means that you will need a separate SQL script for production (PostgreSQL) and for tests (H2). Luckily, flyway supports that. It can load the vendor-specific scripts from different folders. Extend the flyway configuration this way:
spring.flyway.locations=classpath:db/migration/{vendor}
and add the vendor-specific SQL scripts under the /h2 and /postgresql folders respectively.

Declaring a default constraint when creating a table

I am creating a new table in Microsoft SQL server 2000 by writing the code instead of using the GUI, I am trying to learn how to do it "the manual way".
This is the code I am actually using, and it works fine:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE())
)
I have specified the primary key, foreign key and check constraints on their own because in this way I can define a name for them, otherwise declaring them inline would make SQL Server generate a random name, and I do not "like" it.
The problem arose when I tried to declare the default value constraint: looking at the informations on the internet and how Microsoft SLQ Server Management Studio creates it, I understood that it can be created both inline and on its own:
"load_date" SMALLDATETIME NOT NULL DEFAULT GETDATE()
or
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
The inline method works fine, but it generates as usual a random name for the constaint, the stand alone method throws an error, saying Incorrect syntax near 'FOR'..
Also, if I create the table and then ALTER it, the command works:
ALTER TABLE "attachments"
ADD CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
As a reference, here is the full code I am trying to execute:
CREATE TABLE "attachments"
(
"attachment_id" INT NOT NULL,
"load_date" SMALLDATETIME NOT NULL,
"user" VARCHAR(25) NOT NULL,
"file_name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_attachments" PRIMARY KEY ("attachment_id"),
CONSTRAINT "fk_users" FOREIGN KEY ("user") REFERENCES "users" ("user"),
CONSTRAINT "ch_load_date" CHECK ("load_date" < GETDATE()),
CONSTRAINT "df_load_date" DEFAULT GETDATE() FOR "load_date"
)
I'm totally at loss here, is what I am trying to do not possible, or I am doing something wrong?
Edit:
David M showed how to add a named default constraint using the inline syntax, I am still looking to understand if the stand alone syntax is completely wrong or it is my fault.
Do it inline with the column creation:
[load_date] SMALLDATETIME NOT NULL
CONSTRAINT [df_load_date] DEFAULT GETDATE()
I have used square brackets rather than quotes as many readers won't work with QUOTED_IDENTIFIERS on by default.