Insert only Alphabets in a Field NOT Number in MySQL - mysql

I'm using MySQL 5.6, I want to declare such type of DATATYPE in a Field during table creation which accepts only Alphabets not Number, For Example, If I have a Column (Name) in a table, then I want it to accept only alphabets from A to Z, not Number or Numeric Value, varchar, char etc.

the data type is not the place to enforce your data integrity. it's either chars or numbers, and numbers can be chars. If you really need to enforce it at the db level vs in code (eg php) then you must do a stored procedure

Related

How are mysql data types processed at the moment of table creation?

I was wondering if SQL actually does something with a column data type at the moment of table creation. I mean I understand that mysql needs it when inserting data to understand what is allowed to insert in it. But at the moment of table creation does SQL allocate different areas of memory or something like that? Or data types are only mandatory at the moment of table creation for the ease of future table insert statements?
The datatypes are stored for use with queries.
During an INSERT, the data for the row being inserted is laid out based on the datatypes. INT will use 4 bytes for a binary integer. VARCHAR(40) will be laid out as a length plus up to 40 characters for a string. DATE takes 3 bytes in a certain format. Etc.
Most datatypes go in (via INSERT) and come out (via SELECT) as strings. So, the string '2020-12-31', when used in a DATE is turned into the 3-byte internal format.
If you try to put the string '123xyz' into INT, it converts that string to an integer, and gets 123. (This example is usually considered wrong, but that's what is done.)
When you JOIN two tables, the datatypes of the columns you are joining on should be the same. If they are different datatypes, then one is converted to the other if possible.

limiting the Index size of specific DB column in mysql

Good Day,
I have concern about indexing in mysql. I am trying to limit the Index size of specific DB table column which column names like ###ID.
this seems that the ID looks unique in first 8 bytes instead of entire length.
Thanks in advance.
As mysql documentation on creating indexes describes:
For string columns, indexes can be created that use only the leading part of column values, using col_name(length) syntax to specify an index prefix length.
Prefixes can be specified for CHAR, VARCHAR, BINARY, and VARBINARY column indexes.
Prefixes must be specified for BLOB and TEXT column indexes.
Prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements is interpreted
as number of characters for nonbinary string types (CHAR, VARCHAR,
TEXT) and number of bytes for binary string types (BINARY, VARBINARY,
BLOB). Take this into account when specifying a prefix length for a
nonbinary string column that uses a multibyte character set.
For spatial columns, prefix values cannot be given, as described later in this section.
The statement shown here creates an index using the first 10
characters of the name column (assuming that name has a nonbinary
string type):
CREATE INDEX part_of_name ON customer (name(10));
You can set the size after the field name. You must remove the old key first
ALTER TABLE mytab
ADD KEY `parent` (`parent`(8)) ;

When to use varchar for numeric data in InnoDB MySQL tables

When using MySQL, are there times that it would make sense to use a textual field (varchar, text, etc) instead of a numeric field (int, float, etc.) even if all data to be stored is numeric?
For example would it be better, faster or more efficient to store '5 digit zip code' data in a 5 char 'varchar' field rather than in an explicitly numeric field?
If yes, how can I determine when it is best to use a text field for numeric data?
I'm using InnoDB if relevant.
EDIT: I'm using a zipcode as an example above, but 'in general' when should choose a textual datatype over a numeric datatype when all data to be stored is numeric data.
Depends on how you are using the data. For Zip codes I would use a VARCHAR - simply because it is a part of an address and will not be using it for calculations. Also you may wish to include other countries in the future (in the UK we have post codes that are alpha numeric).
Ed's answer pretty much covers everything, as a general rule:
don't store numeric values in varchar columns - unless there are chances that other characters will have to be stored (examples are zip codes and phone numbers)
When you need computations/comparisons on the stored values, always use numerical data types (in string comparison 2 is bigger than 10)

Padding the beginning of a mysql INT field with zeroes

I have an INT field in a large MySQL database containing incremental numbers in an INT field. These numbers are currently regular autoincrement numbers (1, 2, 3) but I need to pad them to three digits with zeroes at the beginning (so I get 001, 002, 003.. 010, 011, etc).
What commands can I run on my database to change this column into the format I need?
You can add a ZEROFILL attribute to the column to pad the data in the database or, when querying,
SELECT LPAD(CONVERT(`col`,VARCHAR(3)),3,'0')
to retreive the data formatted as a 3 digit number
There is no such thing as having leading zeroes on data in a numeric field in the database; the data just isn't stored that way, any more than it is stored in roman numerals. All you've got is the number three; so if you want to get the string "003" out, you've got two options:
Change to use a string field in the database: not recommended because you can't easily get incrementing numbers.
Format the number as you retrieve it from the database to add leading zeroes: better, but it has its own disadvantages - e.g. comparisons will be slower because they aren't indexed.

Use varchar or text for a dynamic field in mysql?

I am building a table that will receive different values in a field, think of a log table that the "value" field can be a number, a tiny string or a big text etc.
So I was wonderig if I should create that "value" field as Text or create two fields, one for small inputs, like date, numbers and some string and another one only for the Texts inputs.
so, my question is this:
Should this "value" field be a Varchar along with some other "value2" as Text or create one field Text that the mysql will manage this corretcly?
I am afraid that creating only one Text field can be a bad thing for performance.
EDIT: the number, datetime etc are going to be cast as string before insertion, thats not the point
Thanks,
Joe
Do you know how large the largest input will be? If you impose a limit, or know how large the maximum input will be, then you could use varchar (which caps at 255 characters in versions < 5.0.3, and 65,535 in versions >= 5.0.3). Otherwise, you're probably better off with Text, since it holds significantly more (65,535*2^16-1).
As an alternative, if users are creating things that already have tables (such as adding events to a calendar), you could just put an "is_approved" column on the table and only display approved ones, or search through everything to check for duplicates.
http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
If there is a limit to the length of that the data stored use varchar() (as MySQL 5.0.3 varchar max length can be up to 65,535)
If there is no concrete limit then use a 'text' field type.
The varchar field can handle the different input's that you are mentioning but as a string, not as an integer or datetime.