I add the following column in a table:
alter table my_table
add no_default bit null;
When I let Liquibase make a diff, it generates the following change:
ALTER TABLE my_table ADD no_default BIT DEFAULT 0 NULL;
As you see, it sets false (0) as default value although the column does not define it.
Is this an error or is there a way to prevent this?
// Edit: I am using a MariaDB 10.2 database.
Related
I am trying to define a table with a column type of UUID (CHAR(16) CHARACTER SET OCTETS). After reading through the Firebird 3.0 Developer's Guide, I found only context-variables/simple expression/constant are supported. I would like to know if there is a way to define a default constraint on a table to call GEN_UUID() to assign UUID for insertion?
You cannot do this with a DEFAULT clause, as that only allows literals and a select number of what the Firebird documentation refers to as 'context variables'.
To do what you want, you need to create a before insert trigger to generate the value. Something like this:
create trigger bi_yourtable before insert on yourtable
as
begin
new.uuid_column = gen_uuid();
end
Or, if you don't want unconditional generation of the UUID:
create trigger bi_yourtable before insert on yourtable
as
begin
if (new.uuid_column is null) then
begin
new.uuid_column = gen_uuid();
end
end
Is there a way in GORM to reset a column to its default value? MySql provides the DEFAULT keyword for this
UPDATE table SET col = DEFAULT WHERE id = 123
There is no mention of updating a column to its default value in the GORM Update documentation. Should the Raw SQL Builder be used for this?
Have you tried with tags?
My Idea is set DEFAULT tag and set value as null and update it. like this:
type Table struct {
// other fields
Number `sql:"DEFAULT:0"`
}
modelTable := Table{}
// jobs and insert
// get model and prepare then update Number
modelTable.Number = null.IntFromPtr(nil)
modelTable.Update()
for create null use null package.
If this doesnt work then you should use Raw sql.
So, I have something like this:
CREATE OR REPLACE TABLE TABLE_NAME (
ID NUMBER(38, 0) NOT NULL,
/* OTher elements */
)
With some values already (manually) inserted. I need to update this table so, for future inserts, the value of ID is taken from a sequence I just created:
CREATE OR REPLACE SEQUENCE S_TABLE_NAME_ID
START WITH 451;
For what I've seen in the documentation and in several forums, the syntax should be like this:
ALTER TABLE TABLE_NAME ALTER ID SET DEFAULT S_TABLE_NAME_ID.NEXTVAL;
But when I try to execute it, I get the following error message:
SQL Error [2] [0A000]: Unsupported feature 'Alter Column Set Default'.
Am I missing here something?
from Snowflake Doc (https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html):
"To change the default sequence for a column, the column must already
have a default sequence. You cannot use the command ALTER TABLE ...
SET DEFAULT <seq_name> to add a sequence to a column that does not
already have a sequence."
So I guess you have to set the sequence as column default when creating the table.
I want to add a nullable boolean column to an existing table with default value of null.
I have used this bit of script but it does not set the default value to null. it sets it to 0 instead.
ADD newColumnName BIT NULL
CONSTRAINT DF_tableName_newColumnName DEFAULT(null)
I just ran your example code snippet on my SQL Server 2008 R2 instance and then inserted a record. It initialized the column to null, as expected. The next step would be to post the alter statement and the insert statement that you used.
I used:
alter table tmp1 Add newColumnName bit null CONSTRAINT DF_tableName_newColumnName DEFAULT(null)
insert into tmp1(emp_id) values(9999)
select * from tmp1
After running the above, I used SQL Server Management Studio "Design" action to examine the properties of the new column. It showed that the "Default Value or Binding" was indeed (Null) as expected.
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