HSQL - dropping/creating types - sql

This should be simple but have yet to find an answer.
I want to create a type A only if it does not already exists and/or drop the type if it already exists and recreate it at the startup of my HSQL database.
Now I know the drop and create commands:
CREATE TYPE myType as VARCHAR(100)
DROP TYPE myType
However I have yet to discern how to check for the existence of the type.

Actually turned out to be very simple:
DROP TYPE myType IF EXISTS

Related

Cannot find data type user defined data type

I am getting Column, parameter, or variable #1: Cannot find data type dbo.SUBSYSTEM_CODE. error on user datatype.
CREATE TABLE #PREDEFINED_SUBSYSTEMS
(
SUBSYSTEM_CODE dbo.SUBSYSTEM_CODE PRIMARY KEY
);
After I checked user defined datatype I can see. I am using SQL 2012 and also I applied set compatibility_level = 110 on datatype still didn't work.
What other alternatives I have to fix this?
Probably you need to define type inside tempdb:
USE tempdb;
GO
CREATE TYPE dbo.SUBSYSTEM_CODE ...
Our problem was with schema
we moved table type from dbo to hub schema, and after that we got this error,
you need to use schema before table name for it to work
and dont forget about dynamic sqls which you might have

Oracle SQL: rename object attribute

Looking at the documentation for the ALTER TYPE statement, there seems to be no possibility to change the name, only the type. Is there any possibility to rename an attribute of an SQL object?
My first thought would be to first create an attribute with the new name, copy the data, and then drop the old attribute, but I hope there is a better solution, something like alter type my_type rename attribute abc to xyz.
I believe you can use CREATE OR REPLACE TYPE [type_name]

Does Postgres Plus Advanced Server (PPAS) 9.5 support altering (dropping/adding) attributes to composite object types?

According to my reading of the PostgreSQL documentation on ALTER TYPE I should be able to do the following:
CREATE TYPE compfoo AS OBJECT (f1 int, f2 text);
CREATE TYPE compbar AS OBJECT (f1 int, f2 compfoo);
ALTER TYPE compbar DROP ATTRIBUTE f2;
ALTER TYPE compbar ADD ATTRIBUTE f2 varying(1000);
ALTER TYPE compbar ADD ATTRIBUTE f3 compfoo;
However, when I try it, I get the following error:
ERROR: ALTER TYPE ADD/DROP COLUMN statement not supported for object types
What am I missing here? Is there a server configuration that will allow me to do this? Is it because the type in question is a composite OBJECT type, as opposed to a composite of 'basic' types?
UPDATE 2015-02-10
I've updated the title, and the samples to more closely resemble the problem at hand. In short, within Postgres Plus Advanced Server, there's additional syntax for creating types, in the form CREATE TYPE foo AS OBJECT <--- that syntax is the root of the issue. You cannot alter attributes when the composite type was created AS OBJECT.
CREATE TYPE compfoo AS (f1 int, f2 text);
CREATE TYPE combbar AS (f1 int, f2 compfoo);
ALTER TYPE compbar DROP ATTRIBUTE f2;
---------------^ maybe you should write combbar?
To make it clear you are defining a type named "combbar" and then you try to alter a type with the name "compbar".
UPDATE
Besides that typo, the SQL seems to run fine.

How can I alter a UDT in HSQLDB?

In HSLQDB v 2.3.1 there is a create type clause for defining UDTs. But there appears to be no alter type clause, as far as the docs are concerned (and the db returns a unexpected token error if I try this).
Is it possible to amend/drop a UDT in HSQLDB? What would be the best practice, if for example I originally created
create type CURRENCY_ID as char(3)
because I decide I'm going to use ISO codes. But then I actually decide that I'm going to store the codes as integers instead. What is the best way to modify the schema in my db? (this is a synthetic example, obviously I wouldn't use integers in this case).
I guess I might do
alter table inventory alter column ccy set data type int
drop type CURRENCY_ID
create type CURRENCY_ID as int
alter table inventory alter column ccy set data type CURRENCY_ID
but is there a better way to do this?
After trying various methods, I ended up writing a script to edit the *.script file of the database directly. It's a plain text file with SQL commands that recreates the DB programmatically. In detail:
open db, shutdown compact
Edit the script file: replace the type definition, e.g. create type XXX as int to create type XXX as char(4)
For each table, replace the insert into table XXX values (i,...) with insert into table XXX values('str',...). This was done with a script that had the mappings from the old (int) value into the new (char) value.
In my particular case, I was changing a primary key, so I had to remove the identity directive from the create table statement, and also I had to remove a line that had a alter table XXX alter column YYY restart sequence 123.
save and close script file, open db, shutdown compact
This isn't great, but it worked. Advantages :
Ability to re-define UDT.
Ability to map the table values programmatically.
Method is generic and can be used for other schema changes, beside UDTs.
Cons
No checking that schema is consistent (although it does throw up errors if it can't read the script).
Dangerous when reading file as a text file. e.g. what if I have a VARCHAR column with newlines in it? When I parse the script file and write it back, this would need to be escaped.
Not sure if this works with non-memory DBs. i.e. those that don't only have a *.script file when shutdown.
Probably not efficient for large DBs. My DB was small ~ 1MB.

Altering data types within a table using SQL command ALTER

I have researched how to alter table data types and I understand how to do it but I cannot get it to work. I am trying to update my table Person within APP using the following command:
ALTER TABLE APP.PERSON ALTER uName varchar;
What I have tried so far:
using Modify but realised that, after I received errors, this should indeed be ALTER.
changing uName to UNAME.
changing it to a data type of varchar2.
adding in the size of the data type '(20)' and 'NOT NULL' at the end.
Where am I going wrong? I am using Netbeans 7.3 Beta 2 running on Mac OS X, this is all being done within the SQL Commands section of Netbeans, using Java-DB as my database if any of that matters.
It has already been answered here on SO. You basically need to create new column with desired datatype and delete the old one. However, if you take a look into Apache Derby doc, there is a SET DATA TYPE command, so try something like
ALTER TABLE APP.PERSON ALTER UNAME SET DATA TYPE VARCHAR(30)
EDIT
If code above doesn't work, then you just have to recreate the column as I suggested before.
ALTER TABLE APP.PERSON ADD COLUMN UNAME_NEW VARCHAR(30);
UPDATE APP.PERSON SET UNAME_NEW = UNAME;
ALTER TABLE APP.PERSON DROP COLUMN UNAME;
RENAME COLUMN APP.PERSON.UNAME_NEW TO UNAME;
Most databases require specific permissions to use DDL (Data Definition Language) commands like ALTER TABLE. Very often, the DB credentials used in an application tier do not have DDL permissions in the database.
Verify that the connection you are using has permission to run ALTER TABLE. If indeed it does, post the specific code you are using and any specific error messages.
I don't have JavaDB to test on, but according to the documentation it should be;
ALTER TABLE APP.PERSON ALTER uName SET DATA TYPE VARCHAR(20)