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

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)

Related

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;

Oracle SQL add new column based on value of other column

I'm looking for some help to create a new column based on values from another column - if this is even possible... This is not an ideal solution but I'm out running out of options.
I need to replace the beginning folder paths, change the direction of the \ and change the extension
Existing Field:
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
New Field:
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
Oracle version Version 19.2.1.247
Thank you in advance
You can add a new column to your table named NewField:
Alter table TableName add NewField varchar(500);
Then update NewField by replacing some characters as you wish from ExistingField.
update TableName set NewField= replace(replace(existingfield,'\','/'),'.cca','.wav')
Here I have just replace '' with '/' and '.cca' with '.wav'.
To replace path also:
update TableName set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
DB-Fiddle:
Schema and insert statements:
create table mytable (existingfield varchar(500));
insert into mytable values('
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca');
Add new column:
Alter table mytable add NewField varchar(500);
Update query:
update mytable set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
Select query:
select * from mytable;
Output:
EXISTINGFIELD
NEWFIELD
\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
db<>fiddle here

View's column contains data which looks like UUID but the column type is int. Why?

I've access to a view on a SQL Server 2016 database.
The column named 'id_key' contains such data:
id_key
D93F37FC-3C2A-EB11-B813-00505690E502
B03D37FC-3C2A-EB11-B813-00505690E502
AC644CFC-3C2A-EB11-B813-00505690E502
I've checked the type of the column: it's int
Truly, the result of:
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'yourTableName' AND
COLUMN_NAME = 'yourColumnName'
returns just int.
I've not found any explanation for that in SQL Server 2016 docs.
Have I missed something?
How int type store data which looks like strings/uuids?
If the view was not created using the WITH SCHEMABINDING option then the underlying tables that it references are freely able to change.
It is possible that the problematic column was originally using an int data type when the view was created but has subsequently changed to uniqueidentifier, e.g.:
drop view if exists dbo.yourViewName;
drop table if exists dbo.yourTableName;
go
create table dbo.yourTableName (
ignore int,
yourColumnName int
);
go
create view dbo.yourViewName --with schemabinding
as
select yourColumnName as id_key
from dbo.yourTableName
go
alter table dbo.yourTableName
drop column yourColumnName
go
alter table dbo.yourTableName
add yourColumnName uniqueidentifier
go
insert dbo.yourTableName (yourColumnName) values
('D93F37FC-3C2A-EB11-B813-00505690E502'),
('B03D37FC-3C2A-EB11-B813-00505690E502'),
('AC644CFC-3C2A-EB11-B813-00505690E502')
go
select * from dbo.yourViewName
go
select data_type
from information_schema.columns
where table_name = 'yourViewName'
and column_name = 'id_key'
Which yields:
id_key
------------------------------------
D93F37FC-3C2A-EB11-B813-00505690E502
B03D37FC-3C2A-EB11-B813-00505690E502
AC644CFC-3C2A-EB11-B813-00505690E502
data_type
----------
int
See the CREATE VIEW (Transact-SQL) documentation for more information.

Delete Data from Non-Nullable Column in 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

Alter every double fields of a table

I need to change every double precision field of a table to numeric(15,3) type,
how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ?
following query should return query to update these tables... you can add table filter if you want. Copy the result and run it.
select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)'
from information_schema.columns
where data_type = 'double precision'
and table_name = 'YOUR_TABLE_NAME'
TEST IT BEFORE RUN IT. I DID NOT TEST THIS YET.
While another question will do it for all columns; a simple "alter table [tablename] alter column [columnToAlter] type numeric(15,3). You shouldn't need to run them through a cursor; any value that's not going to be affected by this should remain unaffected.
If you can't do it by changing the datatype itself, a simple update [tablename] set [columnname] = cast(columnname as numeric(15,3) should also work.
Hope that helps!