MYSQL insert error when trying to insert single columns - mysql

Trying to do an insert in MySQL and getting an error that I cannot figure out. The syntax (at least from my perspective) is right. I've tried tinkering around with a lot of little things and cannot figure it out. Also tried dropping and recreating the table and it still happens.
Insert Code:
insert into `apType` (`type`) values (`private`),(`public`),(`military`);
table creation code:
CREATE TABLE `apType`(
`id` int primary key AUTO_INCREMENT,
`type` varchar(255) NOT NULL
)ENGINE=MyISAM DEFAULT CHARSET=latin1;
error code generated:
1054 - Unknown column 'private' in 'field list'

This is a correct SQL - notice single quotes on values being inserted:
INSERT INTO `apType` (`type`) VALUES ('private'),('public'),('military');
What your SQL is actually doing is trying to insert values from fields private, public and military - which in fact do not exist.

Related

Unknown column 'XXXXX' in 'field list'

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.

1062 - Duplicate entry 'button_buynow' for key 'PRIMARY'

I know this has been discussed before but when I read the other threads, they don't seem to address my problem.
When I try to run the SQL query in PhpMyAdmin, I get the error :
#1062 - Duplicate entry 'button_buynow' for key 'PRIMARY'
I am sure the table was empty prior to me running the query so I don't know what's going on. Can somebody shed a light?
CREATE TABLE IF NOT EXISTS `buttons` (
`name` varchar(255) NOT NULL default '',
`value` text NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `buttons`
--
INSERT INTO `buttons` (`name`, `value`) VALUES
('button_buynow', 'buynowCC_LG.gif'),
('button_addtocart', 'x-click-but41.gif'),
('button_viewcart', 'viewcart_LG.gif'),
('button_freedownload', 'downloadnow.jpg');
I am sure the table was empty prior to me running the query so I don't know what's going on.
If you're sure that the table was empty you might have a trigger defined on this table that is the cause of this error.
You can check it this way
SELECT *
FROM information_schema.triggers
WHERE trigger_schema = schema()
AND event_object_table = 'buttons'
If you do in fact have a trigger then you either fix it or just drop it.

Mysql update: ERROR 1292 (22007): Truncated incorrect DOUBLE value

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

mysql - "column cannot be null"

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

MySQL bug? Select WHERE id='1blah'

MySQL Version 5.0.67
Take a look at this very simple table and tell me if I have found a MySQL bug, I have tried to search for an answer but as you can imagine it's a bit hard to come up with the right search terms
CREATE TABLE `product` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `product` VALUES (1, 'jim');
INSERT INTO `product` VALUES (2, 'bob');
From there I can then select the following
SELECT * FROM `product` WHERE `id` = '1';
Obviously this returns a row, but then, so does this
SELECT * FROM `product` WHERE `id` = '1blah';
Erm... WHY? Surely this is wrong or am I going mad? Will crawl the web a bit more before I file a bug report with MySQL.
It's automatically converting the string "1blah" into an integer. As the string begins with a "1" the resultant integer is simply 1.
As such, it's just trying to do the right thing, even though it might seem a bit counter-intuitive.
This happens because of type conversion. Since your column has integer value, '1blah' is converted to 1. Please, see http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html for more details.
If you didn't enclose the integer id in quotes, it'd work and you'd get an error as you'd hope. That is,
SELECT * FROM `product` WHERE `id` = 1;
1 row in set
works, while
SELECT * FROM `product` WHERE `id` = 1blah;
ERROR 1054 (42S22): Unknown column '1blah' in 'where clause'
errors.