Prevent extra space being inserted into a CHAR field? - ms-access

I create a table in MS Access using the following script:
CREATE TABLE POWERSQL (
ProposalNumber INTEGER PRIMARY KEY,
FirstName CHAR (15),
LastName CHAR (20),
Address CHAR (30),
City CHAR (25),
StateProvince CHAR (2),
PostalCode CHAR (10),
Country CHAR (30),
Phone CHAR (14),
HowKnown CHAR (30),
Proposal CHAR (50),
BusinessOrCharity CHAR (1) );
When insert some value to a CHAR filed which is shorter than the set length, e.g., 'John' in [FirstName], it fills the remaining characters with empty space which messes up with other queries and joins. Trim does not help. Any advice? Many thanks.

CHAR fields are half-supported in MS Access and are fixed-length. This means you can't insert less characters than the full field length, and if you do, Access will fill the unused positions with spaces.
Instead, use VARCHAR for variable-length character fields:
CREATE TABLE POWERSQL (
ProposalNumber INTEGER PRIMARY KEY,
FirstName VARCHAR (15),
etc...

Instead of the fixed length CHAR(x) data type, use TEXT, like:
FirstName TEXT,
.................
The TEXT data type is variable length and can store up to 255 characters

Related

How can I cast an array stored as string in MySQL table to integer array in PostgreSQL

I'm trying to import a MySQL table to a new PostgresSQL(14) table, using DBeaver. Seems like JSON values and int arrays are all stored as string.
One of the columns has values like "[1,2,3,4,...]"
So far this is what I'm looking at.
Source Type
Target Type
bigint
numeric
timestamp
timestamp
varchar (100)
text
varchar(255)
text
varchar (100)
text
timestamp
timestamp
timestamp
timestamp
timestamp
timestamp
varchar (255)
text
varchar (24)
text
varchar(255)
text
varchar (50)
text
varchar (150)
text
bigint
bigint
tinyint
bool
tinyint
bool
longtext (65535)
int4range?
varchar (20)
text
varchar (50)
text
varchar (50)
text
text(65535)
text
longtext(65535)
text
tinyint
int4
int
numeric
varchar (150)
text
bigint
numeric
varchar (10)
text
longtext (65535)
jsonb
Another thing is when I try interger[] as type. DBeaver throws this error;
Can't find specified data type by name: 'integer[]'
int4range is can be seen in the datatype list, but I'm not sure if that is what I think.
I figured out there is a transform section which I can write an expression, but I'm not sure what kind of expression can be written here, I couldn't find anything about it.
I've found a resource mentioning JEXL but not sure what that is. How this can be achieved? With or without DBeaver?
PS. Instead of using varchar(n), I'll be using constraints for text types.
Same table above, just for the reference;

Row Size too large Error even though it is not

Im quite new with MySQL and SQL in general now im trying to make a table with an approximate length of 31000 which is well in the limit is there something im missing?
CREATE TABLE gdasgds (profileName VARCHAR(255),
profilePic VARCHAR(255),
likes INTEGER,
comments INTEGER,
PostID CHAR(11),
time BIGINT,
type TINYINT,
content VARCHAR(10000),
Options VARCHAR(10000),
Votes VARCHAR(10000));
Values in VARCHAR columns are variable-length strings. The length can
be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to
65,535 in 5.0.3 and later versions.
so check for you real version ..
anyway you can use text for long string (or medium text or long text )
CREATE TABLE gdasgds (
profileName VARCHAR(255),
profilePic VARCHAR(255),
likes INTEGER,
comments INTEGER,
PostID CHAR(11),
time BIGINT,
type TINYINT,
content text,
Options text,
Votes text);
TEXT (and BLOB ) is stored off the table with the table just having a pointer to the location of the actual storage.
VARCHAR is stored inline with the table.
VARCHAR is faster when the size is reasonable
for my experience Even with heavy queries (100,0000 lines of text type) no significant differences are noticed.

Using CONCAT to combine basic attributes like FirstName / Last Name

I am pretty new to MYSQL, and have been struggling on getting Concat to work properly. I have tried several different variations found here, but without any luck. Can someone point me in the right direction on getting this to work. I am creating a very basic table and then a view to Concat the first and last name along with displaying the rest of the customer's information.
Create Table Customer(
CustomerId INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR (50),
Address VARCHAR (75),
City VARCHAR (50),
State VARCHAR (50),
Zip NUMERIC,
OrderID INT
);
CREATE VIEW CustomerInformation AS
SELECT FirstName, LastName, CONCAT(FirstName, ‘ ‘, lastname),
Address VARCHAR (75),
City VARCHAR (50),
State VARCHAR (50),
Zip NUMERIC,
FROM Customer
try with this:
SELECT FirstName, LastName, CONCAT(FirstName, ‘ ‘, lastname), Address, City, State, Zip
FROM Customer
Do not add the column data type in the SELECT statement. That's only needed in the CREATE TABLE or ALTER TABLE statements

What is difference between char and varchar

CREATE TABLE IF NOT EXISTS `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`country` varchar(5) NOT NULL,
`state` char(5) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I tried following query to insert data
INSERT INTO `test`.`test` (`id` ,`country` ,`state`)
VALUES (NULL , 'south-india', 'Gujarat');
When I execute above query It will shows following warning
Warning: #1265 Data truncated for column 'country' at row 1
Warning: #1265 Data truncated for column 'state' at row 1
I found Reference that VARCHAR is variable-length.CHAR is fixed length.
Then what you mean by
VARCHAR is variable-length.
CHAR is fixed length.
VARCHAR(5) will use at most 5 characters of storage, while CHAR(5) will always use exactly 5.
For a field holding a person's name, for example, you'd want to use a VARCHAR, because while on average someone's name is usually short, you still want to cope with the few people with very long names, without having to have that space wasted for the majority of your database rows.
As you said varchar is variable-length and char is fixed. But the main difference is the byte it uses.
Example.
column: username
type: char(10)
if you have data on column username which is 'test', it will use 10 bytes. and it will have space.
'test______'
Hence the varchar column will only uses the byte you use. for 'test' it will only use 4 bytes. and your data will be
'test'
THanks.
As you mentioned VARCHAR is variable-length. CHAR is fixed length.
when you say
Varchar(5) and if the data you store in it is of length 1, The
remaining 4 byte memory space will be used by others. example: "t"
on the other hand
Char(5) and if the data you store in it is of length 1, The remaining
4 byte memory space cant be used. The 4 byte will end up not used by
any other data. example: "t____" here ____ is the unused space.

INT Datatype in MySQL

Does the INT, TINYINT, MEDIUMINT, BIGINT in MySQL Accepts the character '-' ? I used the data type INT in a column and the data inside has the character '-'. And somehow it didn't accept it. If it's not possible for INT data types, should I use VARCHAR?
The values for an integer in SQL are:
-2147483648 through 2147483647
And the byte size is 4 bytes.
Other maximum values:
BigInt: -9223372036854775808 through 9223372036854775807 (8 bytes)
SmallInt: -32768 through 32767 (2 bytes)
TinyInt: 0 through 255 (1 byte)
it dosent accept any char or string values like "-"
use varchar datatype if u want to insert "-"
Use VARCHAR(45) or TEXT datatype, if you want to insert characters.
you have to use Varchar DataType for this.
It will support both Int and Special Symbol like '-'
If you are not going to do any arithmetic operaion means then you can
use Varchar or text DataType