MySQL and phMyAdmin - mysql

I'm new to this website and using Mysql and phpMyAdmin. I need help with one of my table and I would really appreciate it. So, I created a table that has an Integer column I want to be able to limit it to only 7(Seven) digits I'm not quiet sure if this is possible using Mysql or phpMyAdmin.
I haven't tried any query on it. I want to limit the Integer type to only 7(Seven) digits.

This might not be the best possible solution but I think that if you were to store the integer as string in the format char(7) to limit the number of characters able to be entered it would get the job done.
I'm not familiar with Mysql in particular but here's some documentation on it : https://dev.mysql.com/doc/refman/8.0/en/char.html
I hope this helped.

In MySQL <8.0.16 You can't restrict the number of digits for an Integer. That has no meaning.
You can, however, use a DECIMAL type that allows you to specify the number of digits and the number of decimal places.
For example, DECIMAL(7,0) will define what you want.
Your CREATE statement becomes something like
CREATE TABLE IF NOT EXISTS myTable (
id INT AUTO_INCREMENT PRIMARY KEY,
someText VARCHAR(255) NOT NULL,
decimalValue DECIMAL(7,0)
) ;
If you're using MySQL 8.0.16 or later you can use a CHECK constraint to limit the value (as distinct from limiting the number of digits).
The example above becomes
CREATE TABLE IF NOT EXISTS myTable (
id INT AUTO_INCREMENT PRIMARY KEY,
someText VARCHAR(255) NOT NULL,
decimalValue INT,
CONSTRAINT `decValue_chk` CHECK (`decimalValue` <= 9999999))
) ;

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.

Converting SQL Server code to MySQL

I am trying to concatenate two integers as the default value in a third field. My create table in SQL Server works fine:
CREATE TABLE MEI_Tbl
(
MEI_ID int PRIMARY KEY IDENTITY (1,1),
SRC tinyint NOT NULL DEFAULT '2',
HEI_ID AS (Cast (SRC as varchar)+ Cast (MEI_ID as varchar))
);
but when I try to create it in MySQL, I cannot find the equivalent for the concatenation of the two integers (Line 5 HEI_ID...).
**
I am aware of changing IDENTITY (1,1) to AUTO_INCREMENT for MySQL.
**
I have also tried several concat methods, but to no avail.
MySQL seems happier if I define the datatype for HEI_ID, and I have done so as varchar and int but again no success.
I have spent too much time reading about tool kits to convert SQL Server to MySQL. I just want to create the table in MySQL.
Any input would be appreciated.
MySQL does not support computed columns. Instead, you can use a view:
CREATE TABLE MEI_Tbl (
MEI_ID int PRIMARY KEY AUTO_INCREMENT,
SRC tinyint NOT NULL DEFAULT 2
);
CREATE VIEW v_MEI_Tbl as
SELECT MEI_ID, SRC,
CONCAT(src, mei_d) as HEI_ID
FROM MEI_Tbl
);
Then query from the view.

MYSQL > inserting steam id in int(11) with nosteam account that seems to long

A friend of mine is asking me for help, he has servers for steam games. He uses a mysql data base and has a plugin on his game that stocks STEAM ID (ID of a player in game)
I've tested the data out of the game (STEAM ID conversion from 64bit to 32bit) and compare it to what is inserted in the db... the results are not equal...
Strangely an ID "2150032574" become in mysql data field int(11) "-2144934722"
I have tested all the code PHP and SMX in the game to verify all datas... and according to me (but i am not sure) it would come from the SQL and the relation of the data (ID) lenght and not from the code.
Could it be possible that the length of the ID is too big (maybe because of the unsigned ID value?) and is converted i don t know how into that negative value ?
And what could i do to fix this problem ? I imagined to change int(11) into a varchar(128) for example... but that seems so so so brutal and abnormal for me...
Please could someone help ?
Cheers.
http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
Signed INTs (the 11 does not relate to the max value the field can store) can store values up to 2147483647. UNSIGNED INTs can store values up to 4294967295. But since you wrote that you were getting a negative value after storing 2150032574, your field isn't UNSIGNED and so, can only store values up to 2147483647.
Assuming you don't have primary or foreign key constraints on your field, you could ALTER it to make it UNSIGNED, like so: ALTER TABLE steamids MODIFY COLUMN steamid INT(11) UNSIGNED;.
Ok the problem is solved by simply change the structure of the field authId from int() to bigint()
ALTER TABLE DBTABLE CHANGE FIELD FIELD BIGINT( 255 ) NOT NULL

Create mysql table limit integer to positive values

I try to create a table with an INTEGER attribute which should be limited to positive numbers. I know there is an UNSIGNED option, but that does the wrong thing. As it allows adding -10 as a value. It will just make a 10 out of it.
Is it possible to deny a wrong entry? I tried using CHECK
DROP TABLE Produkt;
CREATE TABLE Produkt (
Bezeichnung VARCHAR(237) PRIMARY KEY,
ProduktNr INTEGER NOT NULL,
Produktart VARCHAR(3) DEFAULT "XXX",
CONSTRAINT onlyPositive CHECK(ProduktNr >= 0)
);
But I can still add -10 as a value... What am I doing wrong?
1) In a strict sql_mode if you define your column as
ProduktNr INT UNSIGNED NOT NULL,
and then try to insert a negative value you'll get an error
ERROR 1264 (22003): Out of range value for column 'ProduktNr' at row 1
Here is SQLFiddle demo. Uncomment insert statement and click Build Schema
2) MySQL still lacks support for CHECK constraints. The CHECK clause is parsed but ignored by all storage engines.
3) On a side note: don't use a VARCHAR(237) column as a PRIMARY KEY, especially if you're using InnoDB engine (all secondary indices on the table also include PK values).
I believe you can just add the check without naming the constraint. This seemed to work for me:
CREATE TABLE Produkt (
Bezeichnung VARCHAR(237),
ProduktNr INTEGER NOT NULL,
Produktart VARCHAR(3) DEFAULT "XXX",
PRIMARY KEY (Bezeichnung),
CHECK(ProduktNr >= 0)
);
I also moved the declaration of the primary key. I'm not 100% certain that you can declare a key the same time as a field, but I did put what I knew.

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.