Can we reduce column size filled with records - plsql

Can we decrease a size of a column? Suppose there is table A having column size 10. After inserting the data in the table, I want to reduce the size of the column. Can we reduce it?
Create table A
(Emp varchar2(10));
Insert into A values ('Ana');
Alter table A modify (varchar2(5));

Yes you can. An error will be thrown if the existing values have a larger size than the new datatype. See below.
koen>create table things (name VARCHAR2(100));
Table THINGS created.
koen>insert into things(name) values ('Car');
1 row inserted.
koen>alter table things modify name VARCHAR2(5);
Table THINGS altered.
koen>alter table things modify name VARCHAR2(2);
Error starting at line : 1 in command -
alter table things modify name VARCHAR2(2)
Error report -
ORA-01441: cannot decrease column length because some value is too big
01441. 00000 - "cannot decrease column length because some value is too big"
*Cause:
*Action:
koen>

Related

Data truncation warning without width change

I have a BINARY(2) DEFAULT NULL column in a MariaDB InnoDB table to store country code, but I would like to remove nullability (the value is always set), so I am trying to run the following query:
ALTER TABLE `table_name` MODIFY COLUMN `country_code` BINARY(2) NOT NULL;
However, I have the following error:
Error 1265: Data truncated for column 'country_code' at row 357
There are no rows with values that exceed 2 characters, checked with
SELECT MAX(LENGTH(`country_code`)) FROM `table_name`
Which returns 2.
In that case, what is the possible cause of data truncation, and how to workaround?

SQLite - condition check before execute a section of script

I understand that SQLite does not have If-Else condition check, and people have been using case statements to get around it. However I want to do a if condition check before executing a certain portion of the script, like the following:
IF (condition = true)
INSERT INTO tableA(A, B)
VALUES (a, b)
....
END
From what I have been trying, case statement doesn't seem to work. Is there any way I can accomplish the above in SQLite?
Thanks for all your help!
You could perhaps use an INSERT SELECT
INSERT INTO table SELECT ...;
The second form of the INSERT statement contains a SELECT statement
instead of a VALUES clause.
A new entry is inserted into the table for
each row of data returned by executing the SELECT statement.
If a
column-list is specified, the number of columns in the result of the
SELECT must be the same as the number of items in the column-list.
Otherwise, if no column-list is specified, the number of columns in
the result of the SELECT must be the same as the number of columns in
the table.
Any SELECT statement, including compound SELECTs and SELECT
statements with ORDER BY and/or LIMIT clauses, may be used in an
INSERT statement of this form.
extract from SQL As Understood By SQLite - INSERT
e.g.
INSERT into xxx
SELECT null as id,
CASE
WHEN filesize < 1024 THEN 'just a little bit'
WHEN filesize >= 1024 THEN 'quite a bit'
END AS othercolumn
FROM filesizes
WHERE filesize < 1024 * 1024
The above will insert rows into table xxx which consists of 2 columns id (rowid alias) and othercolumn according to the results (2 columns id (always set as null) and othercolumn) of the SELECT, which is selecting from the filesizes table where the value of the filesize column is less than 1024 * 1024 (1048576), thus conditionally inserting.
Furthermore, if the filesize is less than 1024 the othercolumn is set to just a little bit, if the filesize is greater than 1023 then the othercolumn is set to quite a bit. So making the conditional insert more complex.
Assuming the filesizes table were :-
The running the above would result in :-

Increase int value in null column

I made a stupid mistake and created a column like this:
CREATE TABLE mytable (mycol INTEGER, ...)
As you can see, I forgot to define a default value like "DEFAULT 0".
In my code, I need to raise the value in "mycol" by 1.
I was baffled when I found out that this code didn't have any effect.
UPDATE mytable SET mycol=(mycol+1)
The column value stays as it is. In my case "EMPTY" (=no value at all).
I would like to avoid re-creating the table if possible.
I would like to ask if there is any easy way to fix this in the SQL statement so that an EMPTY value is seen as 0 so that
UPDATE mytable SET mycol=(mycol+1)
on a column value of EMPTY would finally produce the new column value of 1.
You can use such as below if your column has null value:
UPDATE mytable SET mycol= ifnull(mycol,0)+1

SQL Server 2012 How to change the data type of a column from bit to datefield?

I have a table Person with a column called onvacation.
This column is of data type bit since it's a boolean in the code. It has values null, 0 and 1.
I would like to change the data type of this column from bit to datetime so that all values that are 1, are converted to a new date (could be current date). and 0 and null values would both be just null.
I tried following w3bschool's tutorial and did a query:
ALTER TABLE Person ALTER COLUMN onvacation datetime
But that gives an error 'DF____Person__onvac__59062A42' is dependent on column 'onvacation'.
you get this error because DF____Person__onvac__59062A42 sql object Depends on onvacation column.
You can Find Dependency of Person table by Right Click-->View Dependancy
remove that dependent object and try to alter column

Parametric recursive looped SQLite insert - do all columns have to be supplied?

I added a new column to my table, so there are now 4 instead of 3, and am now getting the following error when do a parametric insert (looped):
table 'test' has 4 columns but 3 values were supplied
Does this mean that you have to code your query for EVERY column the table has (as opposed to just the columns you want populated) when doing inserts, and that SQLite won't just add a default value if a column is missing from the query?
My query is:
"INSERT OR IGNORE INTO test VALUES (NULL, #col2, #col3)"
And this is the code that controls what's inserted in the recursive lopp:
sqlStatement.clearParameters();
var _currentRow:Object = _dataArray.shift();
sqlStatement.parameters["#col2"] = _currentRow.val2;
sqlStatement.parameters["#col3"] = _currentRow.val3;
sqlStatement.execute();
Ideally, I'd like column 4 to be left blank, without having to code it into the query.
Thanks for taking a look.
If you're inserting less values than there are columns, you need to explicitly specify the columns you are inserting to. For example
INSERT INTO test(firstcolumn,secondcolumn) VALUES(1,2);
Those columns that are not specified will get the default value, or NULL if there is no default value.

Resources