This might be a very general question but i have a doubt in this.
As a beginner I was unsuccessful in trying to insert rows into table B by selecting from table A .
A field from table A has DECIMAL datatype and its corresponding field in table B has
BIGINT datatype. Can a value with DECIMAL datatype be inserted in a field with BIGINT datatype?
To my knowledge ... No ... Big int is pretty much the same as INT just that the range is higher. It takes integer values 0 to 18446744073709551615.
More information here
Related
I have a field in my database table that is currently VARCHAR datatype but has numeric values in it so I want to change the data type. When I try to change it to MEDIUMINT, I get an error saying "#1265 - Data truncated for column 'fees' at row 1"
Any ideas how to change the data type? I'm using phpmyadmin to do this.
There may be beyond the range or may have string values in the column.
MEDIUMINT -8388608 to 8388607
https://dev.mysql.com/doc/refman/5.7/en/integer-types.html
I have a mysql table:
id int
user_id int
date datetime
what happens is, if I insert a varchar string in user_id, mysql insert this field setting it to 0.
Can I prevent this insert in mysql? if it is a string, do not convert to 0 and insert.
If the int column(s) are not nullable, something like this might work as a filter (depending on settings, MySQL might just convert the NULL to 0):
INSERT INTO theTable(intField)
VALUES (IF(CAST(CAST('[thevalue]' AS SIGNED) AS CHAR) = '[thevalue]', '[thevalue]', NULL))
;
If you have an opportunity to process and validate values before they're inserted in the database, do it there. Don't think of these checks as "manual", that's absurd. They're necessary, and it's a standard practice.
MySQL's philosophy is "do what I mean" which often leads to trouble like this. If you supply a string for an integer field it will do its best to convert it, and in the case of invalid numbers it will simply cast to 0 as that the closest it can get. It also quietly truncates string fields that are too long, for example, as if you cared about the additional data you would've made your column bigger.
This is the polar opposite of many other databases that are extremely picky about the type of data that can be inserted.
This must be a simple one but got no idea why is this happening.
Under this query:
INSERT INTO assist_reg (ar_id,ar_subid,ar_date) VALUES ('','2431052014','2014-05-31');
Field ar_subid on DDBB always records this value you can see on this screenshot:
ar_subid is a INT field with maximum of 20 characters, non-null with no predeterminate valur. this table is under UTF8-generalci.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html - you are exceeding the int data type's max value. You need a bigint
I have a 2 columns in my table: a varchar(8) and an int.
I want to auto-increment the int column and when I do, I want to copy the value into the varchar(8) column, but pad it with 0's until it is 8 characters long, so for example, if the int column was incremented to 3, the varchar(8) column would contain '00000003'.
My two questions are, what happens when the varchar(8) column gets to '99999999' because I don't want to have duplicates?
How would I do this in MySQL?
If my values can be between 00000000 to 99999999, how many values can i have before I run out?
This is my alternative approach to just creating a random 8 character string and checking MySQL for duplicates. I thought this was a better approach and would allow for a greater number of values.
Because your formatted column depends upon, and is derivable from, the id column, your table design violates 3NF.
Either create a view that has your derived column in it (see this in sqlfiddle):
CREATE VIEW myview AS
SELECT *, substring(cast(100000000 + id AS CHAR(9)), 2) AS formatted_id
FROM mytable
or just start your auto-increment at 10000000, then it will always be 8 digits long:
ALTER TABLE mytable AUTO_INCREMENT = 10000000;
Simple, if the column is unique, it will throw an exception telling that the value already do exists. But if not unique, after 99999999 you'll get error message that the value is truncated.
Alternatives, why not use INT AUTO_INCREMENT? or a custom ID with a combination of date/time, eg
YYMMDD-00000
This will have a maximum record of 99999 records per day. It will reset on the next day.
I have a VARCHAR field in a MySQL table like so -
CREATE TABLE desc(
`pk` varchar(10) NOT NULL UNIQUE,
...
);
The value in pk field is of the type - (xx0000001, xx0000002, ...). But when I insert these into my table the values in pk field get truncated to (xx1, xx2, ...).
How to prevent this?
UPDATE: Adding the INSERTstatement
INSERT INTO desc (pk) VALUES ("xx0000001");
It could be that the viewer you are using to LOOK at the values is displaying the info incorrectly because it is trying to interpret that string as a number, or that mysql may be interpreting your numbers as hexadecimal or something strange.
What happens if you do
INSERT INTO desc (pk) VALUES ("xx0000099");
Does it come back as xx99? or some other value?
Looks like you are referencing different tables in your two statements, text and desc?
Possibly somewhere along your program logic the value is interpreted as a hexadecimal or octal number?