What do the parentheses mean after INTEGER in sqlite? - sqlite

I see a sqlite statement that looks like this
CREATE TABLE IF NOT EXISTS dogs (dog_id INTEGER(4) PRIMARY KEY, dog_name VARCHAR(80))
What does the INTEGER(4) mean?
How big is that integer?
Thank you.

SQLite uses dynamic typing and does not put any limits on the values you can insert into a column.
For compatibility with other databases, you can specify one or two numbers in parentheses, but SQLite ignores them.

Related

Why is the datatype not working in ipython-sql?

When creating a database table in jupyter, we specify restrictions on data types in the columns of the table, but for some reason we can still add other data types. For example, the st_gr column should contain only numbers, but nothing will stop us from adding a line (code below) Why? How to fix?
%%sql sqlite://
CREATE TABLE students(
st_id INTEGER PRIMARY KEY AUTOINCREMENT,
fname VARCHAR(15) NOT NULL,
lname VARCHAR(15) NOT NULL,
st_gr NUMERIC
)
%%sql sqlite://
INSERT INTO students (fname, lname, st_gr) VALUES('Barack', 'Obama', 'text not num')
SQLite uses dynamic type system. Declared column type only indicates the preferred value type, unless the table is strict.
If you are using sqlite version > 3.37.0, look into using STRICT tables.
If not, perhaps check constraints? software verification? a different database?

Does VARCHAR has all the Numeric properties?

I created a table in teradata and specified varchar as datatype for all fields, however my variables contained only numbers.
When I tried using
SELECT SUM(var1)
FROM thetable
I was surprised that it worked. So, I am confused, are the numeric datatypes in teradata are simply special cases of VARCHAR?
No, VARCHARs are strings and nothing else :-)
But SUM is an numeric operator and Teradata does an automatic typecast when datatypes don't match. In your case the result will be a FLOAT, because this is the most flexible numeric datatype.

Restricting floating point values in sqlite3

I am using sqlite3 and I came across a strange issue.
For example, I have the following table.
CREATE TABLE demo (effort integer);
insert into demo values (10.5); - works fine
insert into demo values (10.5); - works fine
insert into demo values (10.5555); - works fine
I have two concerns:
1.Why is it allowing float values when I have declared the datatype to be integer not real ?
2.Is there any way, that I can restrict it upto 2 decimal places?
Is there any workaround so that I can restrict only integer values(strictly no floating values).
Please suggest.
SQLite uses dynamic typing.
To enforce the column type, add a constraint:
CREATE TABLE demo(
effort INTEGER NOT NULL CHECK(typeof(effort) = 'integer')
)

Sqlite3 rowid vs. AUTOINCREMTENT

Im working on my second project with sqlite3 and have a question.
What is the difference between working with rowid and/or working with own AUTOINCREMENT INTEGER value?
Is one of both better/faster than the other?
According to the Sqlite documentation the difference is that the rowid can be reassigned, while an AUTOINCREMENT INTEGER can not.
To quote the important part:
(T)he use of AUTOINCREMENT requires additional work to be done as each row is inserted and thus causes INSERTs to run a little slower.

Is there a direct equivalent in SQLite to MySQL's Binary?

I have a MySQL DB which stores data into a column of type 'binary' in this way:
INSERT INTO t VALUES(0x00000000000000000000000000000001)
I want to do the same in SQLite, so I need to figure out two things:
What is the 'binary' type equivalent in sqlite? There is a blob, but that might behave differently.
How can bin data be represented while inputting using INSERT statements. In MySQL for example, the above format of 0xbin works. But what about SQLite?
Found the answers, here they are for posterity.
There is a 'binary' type in sqlite3 but it doesn't seem to behave any differently from the 'blob' type, which is unusual.
Binary is inserted as such
create table t (b blob);
insert into t values (X'00000000000000000000000000000002');

Resources