I know truncating data means a column has less capacity.
But in my case, all I'm doing is to change the nullability of a numeric column, that has no value at all.
I'm using MariaDB + Adminer. I have not executed an insert query. I just used Adminer to change the nullability of a column named Order.
How is this error even possible? What does it mean in this context?
This error usually happens if a column type will be changed and existing values cannot be converted without truncation:
e.g Changing INT to TINYINT (and column has values > 0xFF)
changing to NOT NULL if column has NULL values
shrinking of CHAR/BLOB
..
If you're ok with any truncation (which might end up in loss of data and/or integrity) use ALTER IGNORE TABLE
Related
I did a mistake and declared a SQLite table column in CREATE TABLE DDL statement as STRING. Inserting '00123' into that column results as numeric value 123 being written (so leading zeroes are lost). I now know I should have used TEXT.
Is there any way, using FireDAC, to somehow force TEXT affinity for that column without dropping and recreating the table? Or, in other words, force leading zeroes to be written in the STRING column type?
I've tried INSERT INTO table_name (string_column) VALUES (CAST('00123' AS TEXT)) with no luck.
If drop/create table is the only way forward, is there a way to know for a table whether a column was created as STRING (so, get its affinity)?
-žarko
I'm trying to select some rows when a specific column are equal to my variable.
Some of these rows have that column set as null.
For this, i made a query using a variable to get that match
SELECT
MT.Column1,
MT.Column2,
MT.SpecificColumn
FROM
MyTable MT
WHERE
MT.SpecificColumn = :Variable
So, it happens that when this variable receives NULL, the query doesn't return the rows that have the SpecificColumn value NULL.
The only way i got the rows was using this condition:
WHERE
MT.SpecificColumn IS NULL
but that case doesn't fit to me, because i need it to be dynamic.
From Codd's rules
Systematic treatment of null values:
Null values (distinct from the empty character string or a string of blank characters and distinct from zero or any other number) are supported in fully relational DBMS for representing missing information and inapplicable information in a systematic way, independent of data type.
So you cannot compare missing data, but you can check if data is missing.
If you want to check NULL without IS NULL, you need to transform it, e.g.
SELECT fields FROM MyTABLE WHERE IFNULL(MT.SpecificColumn, "NULL VALUE")=#variable
If you set #variable="NULL VALUE" it will find the NULL values. If it's effective or not is of course another question.
I want to limit numeric column type to 10 symbols before decimal separator and 4 symbols after decimal separator. I executed the following command:
ALTER TABLE scustdisc ADD COLUMN spec_price numeric(10,4)
The command executed without errors but when I try to insert value in spec_price 10.123456 I am able to do it. It should give error and the value not to be inserted. Am I wrong in my alter command?
SQLite has a dynamic type system and the column types have a limited impact, but can be virtually any name. They are resolved to one of TEXT, NUMERIC, INTEGER, REAL or BLOB.
numeric(0,0) - numeric(99999999,99999999) and more resolve to NUMERIC.
As such 10,4 4,10 etc means nothing and makes no difference to SQLite.
With one exception bar constraints a column may hold any type of value. The column type only comes into play in determining the way the data is stored.
A must read is Datatypes In SQLite Version 3
You may also find How flexible/restricive are SQLite column types?
You may be able to resolve this by using a CHECK constraint CREATE TABLE or by using a TRIGGER or multiple TRIGGERs.
You could format the number(s) appropriately when they are displayed.
You could utilise the round(x,y) function Core Functions
Background: I find that when I imported a *.csv file into SQLite (using RSQLite) a few values in a text affinity column got stored as 'integer' values. Unfortunately the value of the first row of that column is one of those "integer" values. Now, when I then extract that column into R (using RSQLite), I get a nice message that the column is mixed type, but the first value encountered is numeric, so all the rest of the values are coerced to type numeric (which winds up as 0).
Question: Is there a way to change the storage type of a value once it is in SQLite? I have tried:
update table XXX set YYY to (cast YYY as text) where typeof(YYY) = 'integer';
But that doesn't seem to change anything.
Any suggestions deeply appreciated.
Larry Hunsicker
I just imported a huge text file into a table, using the .import command. Everything is OK, except for the fact that it seems to treat clearly numeric values as text. For instance, conditions such as WHERE field > 4 are always met. I did not specify datatypes when I created the table, but this doesn't seem to matter when small tables are created.
Any advice would be welcome. Thanks!
Edit/conclusion: It turns out some of the values in my CSV file were blanks. I ended up solving this by being a bit less lazy and declaring the datatypes explicitly.
The way SQLite handles types is described on this page: http://www.sqlite.org/datatype3.html
In particular:
Under circumstances described below,
the database engine may convert values
between numeric storage classes
(INTEGER and REAL) and TEXT during
query execution.
Section 3.4 (Comparison Example) should give you concrete examples, which are likely to explain the problem you have. This is probably this example:
-- Because column "a" has text affinity, numeric values on the
-- right-hand side of the comparisons are converted to text before
-- the comparison occurs.
SELECT a < 40, a < 60, a < 600 FROM t1;
0|1|1
To avoid the affinity to be guessed, you can use CAST explicitly (see section 3.2 too):
SQLite may attempt to convert values
between the storage classes INTEGER,
REAL, and/or TEXT before performing a
comparison. Whether or not any
conversions are attempted before the
comparison takes place depends on the
affinity of the operands. Operand
affinity is determined by the
following rules:
An expression that is a simple reference to a column value has the
same affinity as the column. Note that
if X and Y.Z are column names, then +X
and +Y.Z are considered expressions
for the purpose of determining
affinity.
An expression of the form "CAST(expr AS type)" has an affinity
that is the same as a column with a
declared type of "type".
Otherwise, an expression has NONE affinity.
Here is another example:
CREATE TABLE test (value TEXT);
INSERT INTO test VALUES(2);
INSERT INTO test VALUES(123);
INSERT INTO test VALUES(500);
SELECT value, value < 4 FROM test;
2|1
123|1
500|0
It's likely that the CSV import create columns of affinity TEXT.