Delete Data from Non-Nullable Column in SQL - sql

Question: I have table with three column, with column names APP_NAME, APP_TYPE and VALUE_TIME.
I would like to edit VALUE_TIME for particular APP_NAME and APP_TYPE. So my query should look like below mentioned if VALUE_TIME column is Nullable. So what would be the best way to delete the data for particular condition ?
UPDATE TABLE_NAME
SET VALUE_TIME = null
WHERE APP_NAME = 'XYZ'
AND APP_TYPE = 'TEST';
Thanks

If you want to delete the row:
DELETE TABLE_NAME WHERE APP_NAME = 'XYZ' AND APP_TYPE = 'TEST';
The column VALUE_TYPE is defined as NOT NULL, so you can't set it to null. You could alter the table to make it nullable:
ALTER TABLE TABLE_NAME MODIFY VALUE_TYPE VARCHAR2(500) NULL;
And then run the UPDATE statement in your question.
Hopefully this answers your question - it wasn't clear what you want to do exactly.

Simple Answer, You can't update non-nullable column data to NULL or ' ' in oracle. I could only think of is Alter the column to have null.
ALTER
TABLE TABLE_NAME
MODIFY VALUE_TYPE VARCHAR2(500) NULL

Related

Change data type of the attribute Roll-No of the table STUDENT from Number (10) to Varchar (10) [duplicate]

I want to change the data type of multiple columns from float to int. What is the simplest way to do this?
There is no data to worry about, yet.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
ALTER TABLE tablename MODIFY columnname INTEGER;
This will change the datatype of given column
Depending on how many columns you wish to modify it might be best to generate a script, or use some kind of mysql client GUI
alter table table_name modify column_name int(5)
You can also use this:
ALTER TABLE [tablename] CHANGE [columnName] [columnName] DECIMAL (10,2)
If you want to change all columns of a certain type to another type, you can generate queries using a query like this:
select distinct concat('alter table ',
table_name,
' modify ',
column_name,
' <new datatype> ',
if(is_nullable = 'NO', ' NOT ', ''),
' NULL;')
from information_schema.columns
where table_schema = '<your database>'
and column_type = '<old datatype>';
For instance, if you want to change columns from tinyint(4) to bit(1), run it like this:
select distinct concat('alter table ',
table_name,
' modify ',
column_name,
' bit(1) ',
if(is_nullable = 'NO', ' NOT ', ''),
' NULL;')
from information_schema.columns
where table_schema = 'MyDatabase'
and column_type = 'tinyint(4)';
and get an output like this:
alter table table1 modify finished bit(1) NOT NULL;
alter table table2 modify canItBeTrue bit(1) NOT NULL;
alter table table3 modify canBeNull bit(1) NULL;
!! Does not keep unique constraints, but should be easily fixed with another if-parameter to concat. I'll leave it up to the reader to implement that if needed..
Alter TABLE `tableName` MODIFY COLUMN `ColumnName` datatype(length);
Ex :
Alter TABLE `tbl_users` MODIFY COLUMN `dup` VARCHAR(120);
To change column data type there are change
method and modify method
ALTER TABLE student_info CHANGE roll_no roll_no VARCHAR(255);
ALTER TABLE student_info MODIFY roll_no VARCHAR(255);
To change the field name also use the change method
ALTER TABLE student_info CHANGE roll_no identity_no VARCHAR(255);
You use the alter table ... change ... method, for example:
mysql> create table yar (id int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into yar values(5);
Query OK, 1 row affected (0.01 sec)
mysql> alter table yar change id id varchar(255);
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc yar;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)
If you want to alter the column details, set default value and add a comment, use this
ALTER TABLE [table_name] MODIFY [column_name] [new data type]
DEFAULT [VALUE] COMMENT '[column comment]'
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
You can also set a default value for the column just add the DEFAULT keyword followed by the value.
ALTER TABLE [table_name] MODIFY [column_name] [NEW DATA TYPE] DEFAULT [VALUE];
This is also working for MariaDB (tested version 10.2)

BigQuery Drop Table Column - DDL Bug

After removing a column from a table by:
ALTER TABLE MyTable
DROP COLUMN IF EXISTS MyColumn
In BigQuery UI I Can see that the column was deleted successfully & I can't query the specific column but when I query DDL I can see that the column still exists in the scheme:
SELECT DDL FROM MyDataSet.INFORMATION_SCHEMA.TABLES
WHERE DDL LIKE '%MyTable%'
What am I doing wrong?
This is a nasty, undocumented side effect of Bigquery's Time Travel. Time Travel makes it unsafe to use ALTER TABLE statements in bigquery.
Demonstration of problem:
create table apu.time_travel_problem
( id int64
, name string
);
select column_name, data_type
FROM apu.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'time_travel_problem';
column_name
data_type
id
INT64
name
STRING
This is all normal so far, but after an ALTER TABLE everything goes odd:
alter table apu.time_travel_problem drop column name;
select column_name, data_type
FROM apu.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'time_travel_problem';
column_name
data_type
id
INT64
name
STRING
The column we just dropped is still there!
Now try this:
alter table apu.time_travel_problem add column name string;
Column `name` was recently deleted in the table `time_travel_problem`. Deleted column name is reserved for up to the time travel duration, use a different column name instead.
Solution:
Do not use ALTER TABLE in bigquery. Instead DROP and reCREATE using a temporary table.
This is a jinja template which I use:
/* {{TABLE}} */
CREATE TABLE IF NOT EXISTS {{DATASET}}.{{TABLE}}_migration
OPTIONS (expiration_timestamp = timestamp_add(CURRENT_TIMESTAMP(), INTERVAL 8 HOUR))
AS SELECT * FROM {{DATASET}}.{{TABLE}};
DROP TABLE {{DATASET}}.{{TABLE}};
CREATE TABLE {{DATASET}}.{{TABLE}}
(
{{COLUMN_DDL}}
);
INSERT INTO {{DATASET}}.{{TABLE}}
(
{{COLUMN_LIST}}
)
SELECT
{{COLUMN_LIST}}
FROM {{DATASET}}.{{TABLE}}_migration;

How to reset DEFAULT value on a column On ORACLE TABLE

I have a table in which one of the column is set default to 0000 and the data type of of the column is number.
I want to remove the default value and when a record is saved null should save on this column.
You could override default to NULL:
ALTER TABLE tab_name MODIFY col_name DEFAULT NULL;
db<>fiddle demo
If columns was set as NOT NULL then:
ALTER TABLE tab_name MODIFY col_name type_name DEFAULT NULL NULL;
db<>fiddle demo2
First, update current records:
update tab set col=null where col=0;
Then remove default value:
alter tab modify col default null;

How to change datatype of the column in derby database?

Iam trying to change the datatype of the column from integer(9) to Numeric(14,3).
but not able to change
i tried below query
alter table TableName alter ColumnName NUMERIC (14,3);
hope below one will help.
ALTER TABLE Table_Name ALTER COLUMN Column_Name SET DATA TYPE NUMERIC(14,3);

Modify default option of a column

I am trying to change the default value of 2 columns from 'N' to 'Y' using the below query, but it throws a error. Any idea whats wrong in here. Error: Invalid Alter table option.
Alter Table USER
Modify
CONTACT_FLAG Default 'Y',
APPROVAL_FLAG Default 'Y' ;
You have to enclose the columns like this
alter table
table_name
modify
(
column1_name column1_datatype,
column2_name column2_datatype,
column3_name column3_datatype,
column4_name column4_datatype
);