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?
Related
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>
I've run into a problem with the sqlite3 module in Python 3, where I can't seem to figure out how to replace NULL values from the database with other ones, mainly strings and integers.
This command doesn't do the job, but also raises no exceptions:
UPDATE table SET animal='cat' WHERE animal=NULL AND id=32
The database table column "animal" is of type TEXT and gets filled with NULLs where no other value has been specified.
The column "id" is primary keyed and thus features only unique integer row indices.
If the column "animal" is defined, not NULL, the above command works flawlessly.
I can replace existing strings, integers, and floats with it.
What am I overlooking here?
Thanks.
The NULL value in SQL is special, and to compare values against it you need to use the IS and IS NOT operators. So your query should be this:
UPDATE table
SET animal = 'cat'
WHERE animal IS NULL AND id = 32;
NULL by definition means "unknown" in SQL, and so comparing a column directly against it with = also produces an unknown result.
I am using the below query and it gives error that "Selected non-aggregate values must be part of the associated group. SELECT Command Failed."
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(*) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
I changed the above query as written below but the error still persists.
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(X_ISP_AFF_ADDR_SEQ) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
You presumably are missing a GROUP BY clause here:
SELECT TOP 100
X_ISP_AFF_ADDR_SEQ,
CAST(COUNT(*) AS BIGINT) AS COUNT_ROW
FROM S_CONTACT
WHERE
X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%'
GROUP BY
X_ISP_AFF_ADDR_SEQ;
The exact error you were seeing with your original query has to do with that selecting X_ISP_AFF_ADDR_SEQ instructs Teradata to return a value for each record in the table, whereas COUNT() returns a value over the entire table. It is not possible (in general) to mix aggregates and non aggregates in a select clause.
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
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.