Alter column set default unsupported feature - sql

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.

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.

setting sequence value as default for column in HSQL

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

How DB2(v10.5.0.5) add auto increment column to an exists table

I'm trying to add an auto increment column in an existing table of DB2.
DB2 version is v10.5.0.5.
Following is my query:
alter table DB2INST1.AAA_BJ_BOND
ADD COLUMN id INTEGER NOT NULL DEFAULT 0;
ALTER TABLE DB2INST1.AAA_BJ_BOND ALTER COLUMN id
set generated always as identity (start with 1);
but I got following error:
"com.ibm.db2.jcc.am.SqlSyntaxErrorException: ALTER TABLE "DB2INST1.AAA_BJ_BOND"
specified attributes for column "ID" that are not compatible with the existing
column.. SQLCODE=-190, SQLSTATE=42837, DRIVER=4.13.127"
What can I do to solve this problem?
You must drop the column DEFAULT value first.
This is mentioned in the description of SQL0190N:
If SET GENERATED ALWAYS AS (expression) is specified, but the column
is already defined with a form of generation (default, identity, or
expression) and there is no corresponding DROP in the same statement.
ALTER TABLE DB2INST1.AAA_BJ_BOND
ALTER COLUMN id drop default;
ALTER TABLE DB2INST1.AAA_BJ_BOND ALTER COLUMN id
set generated always as identity (start with 1);
Now I have successfully added auto-increasing ID to the table through the following three steps:
ALTER TABLE DB2INST1.AAA_SEAT ADD COLUMN ID INTEGER NOT NULL DEFAULT 0;
ALTER TABLE DB2INST1.AAA_SEAT ALTER COLUMN ID DROP DEFAULT;
ALTER TABLE DB2INST1.AAA_SEAT ALTER COLUMN ID SET GENERATED ALWAYS AS IDENTITY (START WITH 1);

Cannot insert a new column in a table

I have a Product table in which I want to create a new columns Modified_By and Modified_Date_Time. These columns do not allow nulls.
However, as the database already has data, in order to create this column, I had to defined as "allowing nulls". Then, I run a process which updated the new column. The last step was to uncheck the "Allow nulls" property, but when I tried to save the table changes, I got the following error:
'Product_Details' table
- Unable to modify table.
Cannot insert the value NULL into column 'Modified_Date_Time', table 'Vendor Products.dbo.Tmp_Product_Details'; column does not allow nulls. INSERT fails.
The statement has been terminated.
All the rows were succesfully updated with the correct value in the "Modified_By" and "Modified_Date_Time" column, so I don't know why I get this error...Anyway, it seems like a new "temporary" table was created by SQL Server 2008, because I don't have any table with the name "Tmp_Orders"
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
You have to set a default value.
ALTER TABLE Product ADD Modified_By datetime DEFAULT (GETDATE())
The default value will be set to today.
I find the interactive design is not very good at this sort of thing. It's better to simply add the constraint manually
ALTER TABLE Table_Name ALTER COLUMN Column_Name DataType NOT NULL
E.g.
ALTER TABLE MyTable ALTER COLUMN A_Column Int NOT NULL;

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.