setting sequence value as default for column in HSQL - hsqldb

I have a query that looks like this in postgres:
ALTER TABLE alias_table ALTER COLUMN iddb SET DEFAULT nextval('alias_table_iddb_seq');
I would like to translate it into hsql. I know that I can do it by enabling PostgreSQL syntax Compatibility:
SET DATABASE SQL SYNTAX PGS TRUE;
ALTER TABLE alias_table ALTER COLUMN iddb SET DEFAULT nextval('alias_table_iddb_seq');
However I'm interested in pure hsql syntax. I know I could do it on create table syntax using something like:
CREATE TABLE alias_table (
iddb bigint GENERATED BY DEFAULT AS SEQUENCE alias_table_iddb_seq PRIMARY KEY
);
But, how can I do it from alter table syntax?

Currently the only supported ALTER TABLE syntax is in the PGS compatibility mode.
In the next version (2.5.1) syntax similar to that used for CREATE TABLE will be supported.
ALTER TABLE alias_table ALTER COLUMN iddb GENERATED BY DEFAULT AS SEQUENCE alias_table_iddb_seq

Related

Incorrect syntax near default in alter table

I want to alter the existing column Site_SiteId in SQL Server to make it as not null with default value 1 but getting a syntax error:
ALTER TABLE dbo.ImagingEvents
ALTER COLUMN Site_SiteId bit NOT NULL DEFAULT 1
default is a constraint so you need to add it to the table:
ALTER TABLE dbo.ImagingEvents ADD DEFAULT 1 FOR Site_SiteId
First you need to ALTER the column:
ALTER TABLE dbo.ImagingEvents ALTER COLUMN Site_SiteId bit NOT NULL;
Note that if you have any rows that already have the value NULL you will need to UPDATE them first, before performing the ALTER.
Then, personally, I would recommend creating a named constraint, like so:
ALTER TABLE dbo.ImagingEvents ADD CONSTRAINT DF_Site_SiteId DEFAULT 1 FOR Site_SiteId;
Having named constraints, rather than the automatically named ones, is far better for transferable code.

Error in changing column length in Postgres

I am trying to change the column size from 100 to 150 varchar data type using following query:
alter table data_warehouse.tbl_abc
alter column first_nm varchar(150) null;
Getting the following error:
SQL Error [42601]: ERROR: syntax error at or near "varchar"
Position: 77
The syntax is a bit different, so try this:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm type varchar(120);
The error in your syntax is that you missed a TYPE keyword:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm TYPE varchar(150);
and if you have a NOT NULL constraint you want to remove, add a new ALTER COLUMN inside the same ALTER TABLE statement:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm TYPE varchar(150),
ALTER COLUMN first_nm DROP NOT NULL;
for reference look here: https://www.postgresql.org/docs/current/sql-altertable.html
Edit: as in the comment, if you have a view which involves the same column, drop it and re-create it under transaction:
BEGIN TRANSACTION;
DROP VIEW [...];
ALTER TABLE [...];
CREATE VIEW [...];
COMMIT;
Be aware that to alter a table, you must acquire an exclusive lock on it, so during the whole process, all the queries over the same table and on the views of the table are locked, also if they don't read from the altered column (because the whole table is locked) - use with caution in production environment

Alter column set default unsupported feature

I want to alter the table and set the default sequence of a column which is identity. When I try to run
ALTER TABLE report.test_table MODIFY id set default test_table_seq.NEXTVAL;
it shows following error:
[0A000][2] Unsupported feature 'Alter Column Set Default'.
Here's create table sql:
create table report.test_table(
id int identity,
txt text
);
Considering snowflake documentation a column must have a sequence to use alter column set default and trusting snowflake docs too identity or autoincrement are synonyms and snowflake use sequence to autoincrement that column.
https://docs.snowflake.net/manuals/sql-reference/sql/create-table.html
Sadly, there's no other way. Snowflake uses a sequence in backend but doesn't allow applying another sequence on that. You can only alter the column to add a new sequence if it was added as default while table creation.

Unable to drop default constraint in Oracle

SQL> ALTER TABLE CUSTOMERS MODIFY AGE INT DEFAULT 10;
Table altered.
SQL > ALTER TABLE CUSTOMERS ALTER COLUMN AGE DROP DEFAULT;
ERROR at line 2:
ORA - 01735 : invalid ALTER TABLE OPTION.
Your ALTER statement is wrong and you cannot use two ALTER commands in one statement. And we never drop default value instead we set it to NULL.
If a column has a default value, then you can use the DEFAULT clause to change the default to NULL, but you cannot remove the default value completely. If a column has ever had a default value assigned to it, then the DATA_DEFAULT column of the USER_TAB_COLUMNS data dictionary view will always display either a default value or NULL.
ALTER TABLE
Use the following SQL command to drop the default.
ALTER TABLE constomers MODIFY age DEFAULT NULL;
Use MODIFY [COL_NAME] DEFAULT NULL instead of ALTER COLUMN [COL_NAME] DROP DEFAULT so far oracle does not accept the latter version.

How to alter the default value set to a column in a table in SQL?

How can you alter the default value set to a column in a table in SQL.
I got an error from:
ALTER TABLE tablename.tab ALTER COLUMN mess1 DEFAULT ('hi')
What was the correct query?
I would name your constraints. To change an existing one...
ALTER TABLE tablename.tab
DROP CONSTRAINT .... --you have a system generated name. Well done.
ALTER TABLE tablename.tab
ADD CONSTRAINT DF_tablename_mess1 DEFAULT 'hi' FOR mess1
Normally, the syntax is a variant of:
ALTER TABLE jankhana.jankh MODIFY (mess1 CHAR(10) NOT NULL DEFAULT 'hi');
Technically, the parentheses around the column specification are optional when there's just one column; if there are several, they are mandatory.
The details could vary by DBMS - DDL statements tend to be the most variable.