I have this simple table:
create table Customers
(
Id bigint not null primary key auto_increment,
Name varchar(100) not null,
IsVip boolean null
)
Now I want to set a default value for IsVip column. I tried:
alter table Customers
modify IsVip set default 0
But it doesn't work. How should I do it?
According to the ALTER TABLE syntax you use the syntax
| ALTER [COLUMN] col_name SET DEFAULT literal | (expression)
or the syntax
| MODIFY [COLUMN] [IF EXISTS] col_name column_definition
In your case it can be
alter table Customers ALTER COLUMN IsVip set default 0
Related
I have a table A with schema.
Name Null? Type
-------- -------- ------------
NAME VARCHAR2(10)
TABLE_ID NOT NULL NUMBER
I wanted Table_ID to be an auto-incrementing column so I created a sequence.
create sequence table_id minvalue 1 start with 1 cache 10;
and I modified the Table_ID column as
alter table A
modify table_id default on null table_id.nextval;
now I cannot drop the table or the column or the constraint.
It is giving me this error.
An error was encountered performing the requested operation:
ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
30667.0000 - "cannot drop NOT NULL constraint on a DEFAULT ON NULL column"
*Cause: The NOT NULL constraint on a DEFAULT ON NULL column could not be
dropped.
*Action: Do not drop the NOT NULL constraint on a DEFAULT ON NULL column.
The only way to drop the constraint is to remove the ON NULL
property of the column default.
Vendor code 30667
I have tried to purge the recyclebin but it is not working either.
I read other posts but none of this seems to make sense.
Please help.
If you just need to remove the constraint, just set a default value on that column. The constraint will be removed automatically:
alter table a modify table_id default 0;
Full example:
create table a(name varchar2(10), table_id number not null);
create sequence table_id minvalue 1 start with 1 cache 10;
alter table a modify table_id default on null table_id.nextval;
select * from user_constraints where table_name = 'A'; -- one constraint "TABLE_ID" IS NOT NULL
alter table a drop constraint SYS_C0026367; -- ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
alter table a modify table_id default 0;
alter table a drop constraint SYS_C0026367; -- ORA-02443: Cannot drop constraint - nonexistent constraint
select * from user_constraints where table_name = 'A'; -- the constraint was dropped automatically
I ran this code on Oracle Database 19c Standard Edition 2 Release 19.0.0.0.0 - Production and it worked fine:
create table a(name varchar2(10), table_id number not null);
create sequence table_id minvalue 1 start with 1 cache 10;
alter table a modify table_id default on null table_id.nextval;
drop table a;
Table A created.
Sequence TABLE_ID created.
Table A altered.
Table A dropped.
I have table my_transitions and I am trying to delete field called myStage.
I am using this command:
alter table `my_transitions` drop `myStage`
I am seeing this error:
Key column 'myStage' doesn't exist in table
But when I list all fields using this command:
describe my_transitions
I can see
id bigint(20) unsigned NO PRI NULL auto_increment
myStage varchar(255) NO MUL NULL
updated_at timestamp YES NULL
Anyone can see if I am doing something wrong?
EDIT:
If I run show create table my_transitions;, I am getting:
CREATE TABLE `my_transitions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`myStage` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`myStage1` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_stage_combination` (`myStage`,`myStage1`),
KEY `my_transitions_myStage` (`myStage`),
KEY `my_transitions_myStage1` (`myStage1`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I have solved it by first deleting the unique key
ALTER TABLE my_transitions DROP INDEX unique_stage_combination;
It seems like it is not possible to delete a column if it is a part of index key in Maria DB 10.5.8.
This is a pecular bug in MariaDB. It affects MariaDB 10.5.
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=867204670347fa29e40bd5eb510c6956
The workaround is to drop the UNIQUE KEY that column mystage is part of first, then drop the column.
alter table my_transitions drop key unique_stage_combination, drop column mystage;
P.S.: I tested this on MySQL 8.0 and it does not require the workaround. It does drop the column, but it leaves the table with a UNIQUE KEY column on just one column mystage1, which might not be what you want.
I have a table with this:
MyField VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT ''
I want to drop the default empty string value so that it defaults to null. I've discovered that I can set the default value to null, but then it actually says DEFAULT NULL in the DDL. This toys with the scripts I use to compare DDL diffs between multiple database environments, so I want to look simply like this:
MyField VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC
Edit: I am wanting this to behave the same as if I were changing a column from NOT NULL to nullable.
Non-nullable column:
n INTEGER NOT NULL
Nullable column:
n INTEGER
Notice how the latter doesn't say:
n INTEGER NULL
You need to drop and create the column as below:
alter table DBC.TEST
drop MY_FIELD
alter table DBC.TEST
ADD MY_FIELD VARCHAR(50)
I have tested it and its working.
I have one Auto Increment Field, rest are Integer,Text and Datetime field. How do I fix it out?
The Table Structure is given below:
CREATE TABLE "q1" (
"sb_id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"sb_title" text(100,0) NOT NULL,
"sb_details" text(300,0) NOT NULL,
"sb_image" text(30,0) NOT NULL,
"sb_type" integer(4,0) NOT NULL DEFAULT '1',
"sb_date" datetime NOT NULL
)
It could be because in your insert command
connection.execute("INSERT INTO q1(sb_title,sb_details)VALUES(?,?)",a,b);
you didn't insert any values for sb_image or sb_date, both of which are NOT NULL and have no default defined. SQLite doesn't know what to put in there. You should either take away the NOT NULL constraint on those columns, define a default for the columns, or insert something explicitly.
I can't add a not null constraint or remove a default constraint. I would like to add a datetime column to a table and have all the values set to anything (perhaps 1970 or year 2000) but it seems like i cant use not null without a default and I cant remove a default once added in. So how can i add this column? (once again just a plain datetime not null)
Instead of using ALTER TABLE ADD COLUMN, create a new table that has the extra column, and copy your old data. This will free you from the restrictions of ALTER TABLE and let you have a NOT NULL constraint without a default value.
ALTER TABLE YourTable RENAME TO OldTable;
CREATE TABLE YourTable (/* old cols */, NewColumn DATETIME NOT NULL);
INSERT INTO YourTable SELECT *, '2000-01-01 00:00:00' FROM OldTable;
DROP TABLE OldTable;
Edit: The official SQLite documentation for ALTER TABLE now warns against the above procedure because it “might corrupt references to that table in triggers, views, and foreign key constraints.” The safe alternative is to use a temporary name for the new table, like this:
CREATE TABLE NewTable (/* old cols */, NewColumn DATETIME NOT NULL);
INSERT INTO NewTable SELECT *, '2000-01-01 00:00:00' FROM YourTable;
DROP TABLE YourTable;
ALTER TABLE NewTable RENAME TO YourTable;