"decimal" - Data truncated for column - mysql

I'm trying to add some test values into my database, but I'm always getting this Data truncated for column ... error when I enter decimal values.
For example I'm trying to enter this:
INSERT INTO `offers` (`id`, `userid`, `amount`, `onebtc`, `price`, `discount`) VALUES (NULL, '6', '14,22', '0', '10,32', '1');
into this table:
table

Have you tried to sending a decimal instead of a string??
INSERT INTO offers (id, userid, amount, onebtc, price, discount) VALUES (NULL, 6, 14.22, 0, 10.32, 1);
You're sending strings for everthing, so MySQL has to type cast it for you.

Related

Column count doesn't match value count in MySQL

Whenever I am trying to insert data in table:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
MySQL said:#1136 - Column count doesn't match value count at row 1
How can i fix it?
I don't understand what need to do.
TIA
The provided number of columns in the column list specification and number of column values in each record must match.
Assuming you don't want to insert email data, remove that from the column list:
INSERT INTO `operator`(`id`, `operator_name`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
or pass null for email:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink',null,'This is all about Banglalink'),
(2, 'Robi', null,'This is all about Robi');
Second method is useful when you may have emails for few records.
You pass one item - info:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink', NULL),
(2, 'Robi', 'This is all about Robi',NULL);
or
INSERT INTO `operator`(`id`, `operator_name`, `email`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');

1265: Data truncated for column 'envio' at row 1

I'm with a problem in following insert SQL below:
insert into cad_manutencao (id,
causa_provavel,
envio,
retorno,
nota_envio,
rastreamento,
transportadora,
chegou)
values (1,
'TESTANDO ASSISTENCIA',
'12/02/2015 15:00:00',
'12/02/2015 15:00:00',
'S/NF',
'AssistĂȘncia',
1,
1)
When i run this SQL the MySQL return this warning:
"1265: Data truncated for column 'envio' at row 1"
This SQL works and the register is added, but the date fields are filled as NULL
How can i solve this problem?
Thanks a lot!
This is not a valid format for a date in mysql. You have to change the query like this :
insert into cad_manutencao (id,
causa_provavel,
envio,
retorno,
nota_envio,
rastreamento,
transportadora,
chegou)
values (1,
'TESTANDO ASSISTENCIA',
'2015-02-12 15:00:00',
'2015-02-12 15:00:00',
'S/NF',
'AssistĂȘncia',
Date format for mysql is yyyy-mm-dd.

How would I select records in a database and insert them into another in MySQL?

I am trying to copy records from one database to another and narrow them down by Type. That is, all records with the type "Holiday" will be copied. When I run this statement, however, I get an error.
MySQL statement:
INSERT INTO nfb_events.nfb_events(`ID`, `Name`, `Type`, `Host`, `Location`, `Date`, `StartTime`, `EndTime`, `Description`)
SELECT * FROM eagles_events.eagles_events WHERE `Type`="Holiday";
Error:
Error Code: 1265. Data truncated for column 'Type' at row 1
I don't understand; the "Type" record is a Varchar with a length of only 10 characters. I am using MySQL Workbench.
I would start by explicitly naming the columns in the select *:
INSERT INTO nfb_events.nfb_events(`ID`, `Name`, `Type`, `Host`, `Location`, `Date`,
`StartTime`, `EndTime`, `Description`)
SELECT `ID`, `Name`, `Type`, `Host`, `Location`, `Date`, `StartTime`, `EndTime`, `Description`
FROM eagles_events.eagles_events
WHERE `Type`= 'Holiday';
One possibility is that the columns are created in a different order in the two tables, so this ensures that the right data is going to the right column.
If this doesn't work, check the types of all the columns in each table to be sure they are the same.

REPLACE INTO MySQL / Update instead of INSERT when record existing

I just performed 3x the following SQL:
REPLACE INTO `admin_gerschap`.`prices` (`ean`, `shopID`, `price`, `shipmentCost`, `productURL`) VALUES ('12', '1', '3', '11', '5')
This shouldn't create 3 records, right? But it created 3x the same record.
What am I doing wrong?

Making a Conditional MySQL Insert statement

So I have this table named SAKAI_REALM_RL_FN that has 3 fields
REALM_KEY
ROLE_KEY
FUNCTION_KEY
What this statement needs to do is that if a certain 2 combinations of ROLE_KEY & FUNCTION_KEY don't exist for each REALM_KEY, than do an insert.
I was already taking a look at this StackOverflow post
I also have the query I was using for the singular inserts:
INSERT INTO `sakai`.`SAKAI_REALM_RL_FN` (`REALM_KEY`, `ROLE_KEY`, `FUNCTION_KEY`) VALUES (248620, 8, 308);
Psuedo-Code:
if(ROLE_KEY equals 8 and FUNCTION_KEY=308 don't exist for REALM_KEYS)
than insert ROLE_KEY=8 & FUNCTION_KEY=308
INSERT INTO `sakai`.`SAKAI_REALM_RL_FN` (`REALM_KEY`, `ROLE_KEY`, `FUNCTION_KEY`)
SELECT *primaryKey*
FROM `sakai`.`SAKAI_REALM_RL_FN`
WHERE not exists (SELECT *primaryKey*
from `sakai`.`SAKAI_REALM_RL_FN`
where role_key = 8 and function_key = 308);
Hope that helps...
I wasn't quite sure what you wanted, but here's something that you might find useful.
Schema with few entries:
CREATE TABLE ALOHA (
REALM_KEY VARCHAR(32) NOT NULL,
ROLE_KEY VARCHAR(32) NOT NULL,
FUNCTION_KEY VARCHAR(32) NOT NULL
);
INSERT INTO ALOHA VALUES ('1', '1', '1');
INSERT INTO ALOHA VALUES ('1', '1', '2');
INSERT INTO ALOHA VALUES ('1', '2', '1');
INSERT INTO ALOHA VALUES ('1', '2', '2');
INSERT INTO ALOHA VALUES ('1', '2', '3');
INSERT INTO ALOHA VALUES ('1', '2', '4');
Try to insert 3 entries (only one gets inserted):
INSERT INTO ALOHA (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
SELECT * FROM (
SELECT '1' AS REALM_KEY, '2' AS ROLE_KEY, '1' AS FUNCTION_KEY
UNION ALL
SELECT '1', '2', '3'
UNION ALL
SELECT '1', '2', '5'
) s
WHERE NOT EXISTS
(SELECT 1 FROM ALOHA a
WHERE a.ROLE_KEY = s.ROLE_KEY
AND a.REALM_KEY = s.REALM_KEY
AND a.FUNCTION_KEY = s.FUNCTION_KEY);
The RDBMS is well-equipped to handle this, if you define the correct index.
Sounds like what you need is a compound UNIQUE index across all three columns. When you perform an INSERT IGNORE, the combination will be inserted if it does not already exist.
Note that this will fail if you already have non-unique rows in your table.
ALTER TABLE SAKAI_REALM_RL_FN ADD UNIQUE KEY `idx_unique_realm_role_function` (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
Then the INSERT selects all the REALM_KEY values and static values for the other 2 columns. If the values already exist, they're ignored. Otherwise they are inserted along with the REALM_KEY.
INSERT IGNORE INTO SAKAI_REALM_RL_FN (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
/* SELECT within INSERT gets all REALM_KEY plus the 2 static values */
SELECT
REALM_KEY,
8,
308
FROM SAKAI_REALM_RL_FN
Here's a demo
When you have completed the INSERT IGNORE, you can drop the UNIQUE KEY since it may no longer be needed.
ALTER TABLE SAKAI_REALM_RL_FN DROP KEY `idx_unique_realm_role_function`