My problem is the following:
I have a table foo_table with id column ID as bigint (primary key), name column NAME as varchar(80) and code COLUMN code as varchar(20). I have a unique constraint on the CODE column.
If I fill this table with only "number" strings in the code column, and then one day decide to add a row with a, surprise, "string" string in that code column, say putting "My Code", it works fine ! The error comes when I tried updating one of my previous records, one that has a CODE value being a number; in this case I get the famous error saying:
ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'My Code'.
I'm not even touching that record ! I presume MySQL is trying to check the unique constraint and try to convert every value in the CODE column to double, but that's insane, and very unprofessional.
Anyone has a solution ? Like turning off string<->number conversion in MySQL ?
Thanks in advance.
Edit:
Ok I have the sample code, but while creating it I saw how to fix the problem, which is still a problem in MySQL as far as I'm concerned:
CREATE TABLE foo_table (
ID bigint(20) NOT NULL DEFAULT '0',
NAME varchar(80) DEFAULT NULL,
CODE varchar(20) NOT NULL DEFAULT '',
UNIQUE KEY foo_table_uk (CODE)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
insert into foo_table (id, name, code) values (1, 'one', '1');
insert into foo_table (id, name, code) values (2, 'two', '2');
insert into foo_table (id, name, code) values (3, 'three', 'three');
update foo_table set name='a-one', code='1' where code=1; <-- This update throws the error
update foo_table set name='a-one', code='1' where code='1'; <-- this update works fine
Related
I'm new to MySQL & I try to enter records to mysql table. I'm getting following error
INSERT INTO advertising.discountauthorizationrequst SET DARDateTime=cast('2003-01-13 16:50:32' as datetime), `DARPubCode`=trim('DD'), `DARPubDate`=cast('2022-05-08' as date), `DARAutUser`=trim("U0001"), `DARDeviceID`=trim('123456789ABCDEFGHIJKL987456'), `DARMessage`=trim("This Is Test Message"), `DARGranted`=("0"), `DARUser`=trim("DATAENTRYUSERNAME") Error Code: 1054. Unknown column 'DARDateTime' in 'field list'
I listed my INSERT statement below. Someone please help me to solve this issue. I'm using mysql workbench 8.0.
Columns:
DARDateTime datetime PK
DARPubCode varchar(3) PK
DARPubDate date PK
DARAutUser varchar(5)
DARDeviceID varchar(50)
DARMessage varchar(100)
DARGranted varchar(1)
DARUser varchar(50) PK
Here is script
INSERT INTO `advertising`.`discountauthorizationrequst`
SET
`DARDateTime`=cast('2003-01-13 16:50:32' as datetime),
`DARPubCode`=trim('DD'),
`DARPubDate`=cast('2022-05-08' as date),
`DARAutUser`=trim("U0001"),
`DARDeviceID`=trim('123456789ABCDEFGHIJKL987456'),
`DARMessage`=trim("This Is Test Message"),
`DARGranted`=("0"),
`DARUser`=trim("DATAENTRYUSERNAME");
Edited..
Table Inspactor - DDL
CREATE TABLE `discountauthorizationrequst` (
`DARDateTime` datetime NOT NULL,
`DARPubCode` varchar(3) NOT NULL,
`DARPubDate` date NOT NULL,
`DARAutUser` varchar(5) DEFAULT NULL,
`DARDeviceID` varchar(50) DEFAULT NULL,
`DARMessage` varchar(100) DEFAULT NULL,
`DARGranted` varchar(1) DEFAULT NULL,
`DARUser` varchar(50) NOT NULL,
PRIMARY KEY (`DARDateTime`,`DARPubCode`,`DARPubDate`,`DARUser`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
You are actually confusing the SQL commands and coming up with a hybrid of them. The INSERT command most commonly is done in two ways..
insert into SomeTable
( these, columns )
values
( oneValue, anotherValue)
or
insert into SomeTable( these, columns )
select oneColumn, secondColumn
from SomeOtherTable
where SomeCondition
The UPDATE command is based on an EXISTING record that you want to change
Update SomeTable set
thisColumn = SomeValue,
anotherColumn = SomeOtherValue
where SomeCondition
So, what you appear to be doing would be written as
INSERT INTO advertising.discountauthorizationrequst
( DARDateTime,
DARPubCode,
DARPubDate,
DARAutUser,
DARDeviceID,
DARMessage,
DARGranted,
DARUser
)
values
(
cast('2003-01-13 16:50:32' as datetime),
'DD',
'2022-05-08',
'U0001',
'123456789ABCDEFGHIJKL987456',
'This Is Test Message',
'0',
'DATAENTRYUSERNAME'
)
Notice the readability with formatting, you can see each column that is needed followed by the explicit values (which could be parameterized during code later) are in the same ordinal context. So, if you ever needed to add a new column to the insert, easy to do with the same ordinal position in the values provided secondarily to it.
As for the 3rd column, by providing a string in YYYY-MM-DD, SQL typically auto-converts to a date format. Other fields, you dont need to explicitly TRIM() everything. If parameterized, you would pass the trimmed VALUE, when you get to that point in your development.
I found the mistake that I made. I created triggers for the above table. After I deleted those triggers its working.
I am using the INSERT function to try and add new values to a table. While there is no error when I run the query, it is not showing the new attributes added to the table. I have no idea why. I also have safe updates turned off. The values entered also match the value type for each column of the table.
CODE ENTERED:
INSERT INTO productlines
VALUES
('Jet Packs', 'Futuristic flying machines that only exist in prototype.', NULL, NULL),
('Jet Skis', 'Much more realistic things that very much exist already.', NULL, NULL),
('Wheelbarrows', 'I cannot believe we actually stock these.', NULL, NULL);
SELECT *
FROM productlines;
CREATE TABLE `productlines` (
`productLine` varchar(50) NOT NULL,
`textDescription` varchar(4000) DEFAULT NULL,
`htmlDescription` mediumtext,
`image` mediumblob,
PRIMARY KEY (`productLine`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO
[name of your **TABLE**](col1, col2, col3)#NAME OF YOUR COLUMNS
VALUES
('value for col1','value for col2','value for col3'),
You didn't add the column names for which you are performing the insert
i'm trying to create a table in mysql
CREATE TABLE IF NOT EXISTS`catalogue`(
`ID_CAT` int(11) NOT NULL AUTO_INCREMENT,
`NOM_CAT` varchar(25) NOT NULL,
`DESCRIOTION` text NOT NULL,
`PHOTO` varchar(25) NOT NULL,
PRIMARY KEY (`ID_CAT`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO catalogue (ID_CAT,NOM_CAT,DESCRIOTION,PHOTO) VALUES
(1,`Ordinateures`,`Ordinateures`,`ordinateures.jpg`),
(2,`Imprimantes`,`Imprimantes`,`imprimantes.jpg`),
(3,`Televiseur`,`Televiseur`,`televiseur;jpg`),
(4,`Accessoirs`,`Accessoirs`,`accessoirs.jpg`);
but I keep getting the same message:
#1054 - Unknown column 'Ordinateures' in 'field list'
INSERT INTO catalogue (NOM_CAT,DESCRIOTION,PHOTO) VALUES
('Ordinateures','Ordinateures','ordinateures.jpg'),
('Imprimantes','Imprimantes','imprimantes.jpg'),
('Televiseur','Televiseur','televiseur;jpg'),
('Accessoirs','Accessoirs','accessoirs.jpg');
You were using backquotes instead of single quotes ' for your insertion values. Also (but this is just a small improvement) there is no need to manually insert AUTO_INCREMENT values.
I'm not sure if the mistake you made qualifies as a simple typo or something more than that. In any case, you are incorrectly placing backpacks around the string literals in your INSERT statement. Use single quotes instead:
INSERT INTO catalogue (ID_CAT, NOM_CAT, DESCRIOTION, PHOTO)
VALUES
(1, 'Ordinateures', 'Ordinateures', 'ordinateures.jpg'),
(2, 'Imprimantes', 'Imprimantes', 'imprimantes.jpg'),
(3, 'Televiseur', 'Televiseur', 'televiseur;jpg'),
(4, 'Accessoirs', 'Accessoirs', 'accessoirs.jpg');
Backticks are meant for escaping column, table, and database names. Hence, you could have written the above INSERT as:
INSERT INTO `catalogue` (`ID_CAT`, `NOM_CAT`, `DESCRIOTION`, `PHOTO`)
VALUES
...
Here I have escaped the table and column names in backpacks. One reason you might want to escape names is if they contain something which is a reserved MySQL keyword.
MySql forces me to specify value for an auto-incremented column. I do not understand why i need to do that.
I have created a table with the following columns
CREATE TABLE IF NOT EXISTS Aask
( task_id INT(11) AUTO_INCREMENT,
SUBJECT VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
);
After creating the above table, when i try to insert rows using
INSERT INTO flask
VALUES ('Subject1','1892-12-27','1994-11-29','detailed description'),
('Subject2','1992-01-17','1694-11-31','HTML view');
I get an error message which says
Query: INSERT INTO flask VALUES ('Subject1','1892-12-27','1994-11-29','detailed description'), ('Subject2','1992-01-17','1694-11-31','H...
Error Code: 1136
Column count doesn't match value count at row 1
I know there are 5 columns in the table and i have given only 4 values in value list but why am i forced to mention value for auto increment column?
This may sound basic to most of you guys but i am just getting started with MySql so any help here would be greatly appreciated.
Use this insert
INSERT INTO flask (SUBJECT, start_date, end_date, description ) values 'Subject1','1892-12-27','1994-11-29','detailed description');
as you see in the title, even if i removed "not null" feature from the related field, it still doesn't let me to insert null value for that field although the field is nullable!
Any help would be appreciated.
EDITED
Create:
CREATE TABLE `review` (
..
`RATING` int(11) DEFAULT NULL,
..
(`CATALOG_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=31625 DEFAULT CHARSET=latin5 ROW_FORMAT=DYNAMIC
Query:
INSERT INTO review (RATING,..) VALUES (null,..);
Error message:
Error: Column 'RATING' cannot be null
SQLState: 23000
ErrorCode: 1048
I also try to insert without RATING in the insert query, even if it is default null and nullable field, it gives the same error message and never inserts the field.
Bohemian, thanks for your attention. You are right, I figured out that there is a trigger for insert action which effects the related field. I disabled the trigger and the error is fixed. Thanks.
First of all look your "datetime / created_at / updated_at" field, do not assign the default value, select current time. after that you can can update the fields name