Remove CHARACTER SET UNICODE_FSS from a column in a firebird database - sql

I have a Firebird database with several tables in it. There are several columns who were added when database was created as
alter table Machines add MachineVersion varchar(100) CHARACTER SET UNICODE_FSS
I want to modify these columns to drop the CHARACTER SET UNICODE_FSS so I ran the command
alter table Machines alter column MachineVersion type VARCHAR(100)
Still, when I open the database in SQL Manager the character set for these columns is still UNICODE_FSS.
Is there another syntax for the second command to remove the CHARACTER SET UNICODE_FSS?

alter table Machines alter column MachineVersion type VARCHAR(100)
This query won't change the character set.
If you want to remove charater set you should alter domain like:
update RDB$FIELDS set
RDB$CHARACTER_SET_ID = NULL
where RDB$FIELD_NAME = 'RDB$141'
Instead of RDB$141 use column domain

It is possible to add new column , copy data from old column to new and later to drop old column?

Related

ALTER TABLE using WHERE clause condition

I was under the assumption that they was a WHERE Clause for ALTER and I have understood now after some research that WHERE Clause doesn't exist for ALTER Command. How to handle cases where we might need to check some conditions in the ALTER Command?
this is my query , what's im doing wrong please ?
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL WHERE LENGTH(caption) > 255;
There is no WHERE. And there is no ability to change the length of a column in some rows but not others. But that is not a problem. You can change the length of the caption to VARCHAR(1000) and because the string is variable length, no additional space is used for shorter strings:
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL;
Note: I assume that this ALTER TABLE is valid in the database you are using. The statement varies significantly across databases.

update and concatenate columns in PostgreSQL

I want to update and concatenate 3 columns in a new a attribute I've already created in my table. However when I execute my code, it puts attributes that before had values in null.
My code example:
update saber2012_1
set estu_fecha_nacimiento = Concat(estu_nacimiento_dia,'/',estu_nacimiento_mes,'/',estu_nacimiento_anno);
In this picture all values that is in null before these had values.
when I make this update
ALTER TABLE saber2012_uno ADD COLUMN punt_c_naturales int;
update saber2012_uno set punt_c_naturales = round((PUNT_BIOLOGIA::numeric + PUNT_FISICA::numeric + PUNT_QUIMICA::numeric) / 3,0);
alter table saber2012_uno DROP COLUMN PUNT_BIOLOGIA;
alter table saber2012_uno DROP COLUMN PUNT_FISICA;
alter table saber2012_uno DROP COLUMN PUNT_QUIMICA;
all is well. but I do not know because previous update is incorrect.
Perhaps you want concat_ws():
update saber2012_1
set estu_fecha_nacimiento = concat_ws('/', estu_nacimiento_dia, estu_nacimiento_mes, estu_nacimiento_anno);
This will ignore any of the arguments that are NULL.
However, your update will not change any other columns in the table. Perhaps the results are just coming back in a different order, when you query after the update.

Upgrade column data type in derby DB

I need to alter my table and change a VARCHAR2 column to be CLOB.
The following script is too slow (8 minutes) for me when having 1 million records:
ALTER TABLE SA.EVENT_PARAMS ADD COLUMN NEW_VALUE CLOB(65344);
UPDATE SA.EVENT_PARAMS SET NEW_VALUE=VALUE;
ALTER TABLE SA.EVENT_PARAMS DROP COLUMN VALUE;
RENAME COLUMN SA.EVENT_PARAMS.NEW_VALUE TO VALUE;
Is there another way to do this? Talking about apache derby.
Thanks

Creating a sequence on an existing table

How can I create a sequence on a table so that it goes from 0 -> Max value?
I've tried using the following SQL code, but it does not insert any values into the table that I am using:
CREATE SEQUENCE rid_seq;
ALTER TABLE test ADD COLUMN rid INTEGER;
ALTER TABLE test ALTER COLUMN rid SET DEFAULT nextval('rid_seq');
The table I am trying to insert the sequence in is the output from another query. I can't figure out if it makes more sense to add the sequence during this initial query, or to add the sequence to the table after the query is performed.
Set the default value when you add the new column:
create sequence rid_seq;
alter table test add column rid integer default nextval('rid_seq');
Altering the default value for existing columns does not change existing data because the database has no way of knowing which values should be changed; there is no "this column has the default value" flag on column values, there's just the default value (originally NULL since you didn't specify anything else) and the current value (also NULL) but way to tell the difference between "NULL because it is the default" and "NULL because it was explicitly set to NULL". So, when you do it in two steps:
Add column.
Change default value.
PostgreSQL won't apply the default value to the column you just added. However, if you add the column and supply the default value at the same time then PostgreSQL does know which rows have the default value (all of them) so it can supply values as the column is added.
By the way, you probably want a NOT NULL on that column too:
create sequence rid_seq;
alter table test add column rid integer not null default nextval('rid_seq');
And, as a_horse_with_no_name notes, if you only intend to use rid_seq for your test.rid column then you might want to set its owner column to test.rid so that the sequence will be dropped if the column is removed:
alter sequence rid_seq owned by test.rid;
In PostgreSQL:
UPDATE your_table SET your_column = nextval('your_sequence')
WHERE your_column IS NULL;
I'm not fluent in postgresql so I'm not familiar with the "CREATE SEQUENCE" statement. I would think, though, that you're adding the column definition correctly. However, adding the column doesn't automatically insert data for existing rows. A DEFAULT constraint is for new rows. Try adding something like this afterwards to populate data on the existing rows.
DECLARE #i Int
SET #i = 0
SET ROWCOUNT 1
WHILE EXISTS (SELECT 1 FROM test WHERE rid IS NULL) BEGIN
UPDATE test SET rid = #i WHERE rid IS NULL
END
SET ROWCOUNT 0

How to alter a column datatype for derby database?

I am trying to alter a datatype for a derby db column. The current price column is set as DECIMAL(5,0). I would like to alter it to DECIMAL(7,2). I did this :
alter table item alter column price set data type DECIMAL(7,2);
But it did not work, and showing the error:
Error: Only columns of type VARCHAR may have their length altered.
May I know how is it possible to alter it? Thank you.
Here is the Derby SQL script to change column MY_TABLE.MY_COLUMN from BLOB(255) to BLOB(2147483647):
ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BLOB(2147483647);
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;
I think you can do like this:
ALTER TABLE SCHEMA.TABLE ALTER "COLUMN-NAME" SET DATA TYPE VARCHAR(255);
(column-Name SET DATA TYPE VARCHAR(integer)) for Datatype String as an example...
Here's a slightly more complicated way to alter the column's data type in this fashion:
Add a new column, of the desired data type
Issue "update ... set new-column = old-column to copy the data from the old column to the new column
drop the old column
Rename the new column to have the name of the old column.
Slightly more steps, but in the end the effect will be the same.
If you have trouble working out the exact details of the SQL to do this, let us know and we'll help.
You can alter table like this:
ALTER TABLE [table] ALTER COLUMN [column] SET DATA TYPE [type];
Or in Rails, just use:
change_column :table_name, :column_name, :integer
Posgtes Solution :
ALTER TABLE prices_table ALTER price_column TYPE decimal (7,2 )