How do I create VARCHAR in a column table DDL? - snappydata

This DDL for a column table results in CLOB for the id_ and name_ fields.
How can I get VARCHARs instead?
snappy> CREATE TABLE EXAMPLE_COLUMN_TABLE (
id_ VARCHAR(64),
name_ VARCHAR(128),
time_ TIMESTAMP,
number_ INTEGER
)
USING column
OPTIONS(PARTITION_BY 'time_', buckets '113', PERSISTENT 'ASYNCHRONOUS');

Unfortunately, this is due to lack of a VARCHAR type in Spark SQL. So, we turn this into a 'String' which lands up becoming a CLOB in our data dictionary.
In any case, this will be resolved in the upcoming release. VARCHARs will be fully honored. And, String types will in fact be turned into VARCHAR with unspecified length.
For now, can you just use the CAST function?
select CAST(id_ AS VARCHAR(64)), ... from example_column_table

Related

Is using Alter here wrong as opposed to Cast?

So I created a table with all varchar (255) then decided to use CAST to change to UNSIGNED (since all +ve values). When I checked, it has been changed to unsigned. However, I noticed when I check the whole table again the columns are still considered as varchar.
Is my understanding correct that CAST only works for the specific code and will not permanently change and if I wish to change the column type permanently, will require me to use ALTER as shown below?
If so why do people use cast instead of Alter?
CREATE table project.worldcup_players (
MatchID varchar (255),
Team_Initials varchar (255),
Coach_Name varchar (255),
Player_Name varchar (255)
);
SELECT * FROM project.worldcup_players;
SELECT CAST(MatchID AS UNSIGNED) AS MatchID FROM project.worldcup_players;
ALTER TABLE project.worldcup_players
CHANGE COLUMN `MatchID` `MatchID` INT NULL DEFAULT NULL ;
CAST only changes the result of an expression in the query. You could use CAST if you only want to change to an unsigned integer sometimes, without changing the way the data are stored.
ALTER TABLE is required if you want to change the way the data are stored.
Suppose your MatchID was represented by a number only for some matches. In other matches, the match is identified by an alpha string. In that case, the column must be a varchar, because the column must be stored as the same data type on all rows in a given table. Don't alter the table, because it would cause all the non-numeric strings to be changed to their numeric equivalent, 0.

Why can I not change mysql int value?

It seems like a bug because when I set the integer value on a column it says it has been changed successfully but nothing happens and the integer value remains blank.
I can't use the database because I get the error that all my integer columns have incorrect integer values, but when I try to change them to int(11) e.g. nothing is happening.
Does anyone know how to fix this?
I can set columns with varchar datatypes to have values and they work fine.
Fatal error: Uncaught mysqli_sql_exception: Incorrect integer value: '' for column 'topic_id' at row 1 in C:\wamp64\www\mycode\upload2.php on line 32
mysqli_sql_exception: Incorrect integer value: '' for column 'topic_id' at row 1 in C:\wamp64\www\mycode\upload2.php on line 32
Code:
ALTER TABLE `topics` CHANGE `topic_id` `topic_id` INT(11) NOT NULL
AUTO_INCREMENT;
// This isn't changing the int value at all!
You have multiple errors in what you are attempting to do.
First, there is the problem that the values in the table are not integers.
Second, you cannot set a column to auto-increment unless it is the primary key.
One option is to drop the primary key and auto-increment idea. Then you can update the values to NULL and change the column to an int:
update topics
set topic_id = null
where topic_id regexp '[^0-9]';
ALTER TABLE `topics` CHANGE `topic_id` `topic_id` INT(11) ;
Here is a db<>fiddle.
If you really want topic_id to be an auto-increment primary key, then I would suggest recreating the table. Something like this:
create table temp_topics as
select *
from topics;
drop table topics; -- be very careful here!
create table topics (
topic_id int auto_increment primary key,
. . . -- the rest of the columns
);
insert into topics (<list of columns here>)
select <list of columns here>
from temp_topics;
if you wanna change a value you have to update that row.
what you are trying to do is wrong , int data type has fixed length (4 bytes), so when you give it a length , it actually doesn't mean anything and its been ignored by the sql engine
see MySql Ref: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
numeric data types are divided into 3 categoies :
Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC
Floating-Point Types (Approximate Value) - FLOAT, DOUBLE

MySQL converting to TINYINT

Under what circumstances will MySQL convert types to TINYINT? I know that a BOOL or BOOLEAN type in mysql DDL will automatically be converted to TINYINT(1) for for true or false. I am analyzing a database which has a type of varchar(16) on a field in one table, and tinyint(4) on the same field on another table? E.g t1.name varchar(15) and t2.name tinyint(4) where t1.name=t2.name.
Don't rely on implicit type conversion, do your datatype analysis manually:
First lets see what MySQL thinks as the best col-type for your data. Run a
SELECT * FROM table PROCEDURE Analyse()
Analyse your data further by saying
SELECT * FROM table WHERE varcharCol NOT REGEXP '^[0-9].*$'
To get all non-numeric values in varcharCol. If there are non you finally have to check value-ranges of different MySQL-types here.
Then you are ready to convert your varcharCol e.g. to TINYINT.

how to auto number ( auto increment ) guid field in mysql database

In sql-server there is function to auto-increment guid fields.
CREATEGUID()
how can I do the same thing in Mysql?
I'm new in database programing.
I want to create primary key field 16byte auto-incremented.
You can create a BEFORE INSERT trigger to check new value for the auto-increment guid field, and if it is NULL, set value = UUID().
This function may be useful too - UUID_SHORT().
UUID_SHORT() - Returns a “short” universal identifier as a 64-bit unsigned integer (rather than a string-form 128-bit identifier as returned by the UUID() function).
MySQL has UUID() function which returns char/varchar. You can use, for example, either BINARY(16) or CHAR(36) to save data like that (binary requires some data translation before insertion).
But MySQL supports AUTO_INCREMENT for numeric datatypes only.

database column int limit

How can I limit my database column's integral input to a specific number of digits ?
CREATE TABLE tab (id INT <1 digit> not null, value INT <10 digits> not null);
Thank you
Add a check constraint (SQL Server) or trigger (MySQL, doesn't support CHECK constraints)
SQL Server example:
CREATE TABLE tab (
id tinynot null CHECK (id BETWEEN 0 AND 9),
value INT not null CHECK (id BETWEEN 1000000000 AND 9999999999)
);
If you only want one digit though, then use tinyint
If you aren't storing numbers (eg "123456789 bananas") but, say, phone numbers then use a varchar type. See https://stackoverflow.com/a/8284674/27535
Edit, you'd need a trigger in MySQL
The short version is using TINYINT UNSIGNED NOT NULL will be a more suitable data type, but it can't limit the values stored.
The longer version is that you may wish to read up on MySQL integer data types. You'll see that TINYINT is sufficient for your purpose as that is a 1-byte column that stores values from -128 to +127, or 0 to +255.
Secondly if you define it as TINYINT(1) then you are defining it as being a TINYINT with a display width of 1 digit. This will not prevent values larger than 10 being stored though. For more reading on this behaviour check numeric type attributes.