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.
Related
I'm trying to run this query on my server
INSERT INTO `new_table` (`userID`, `referenceName`) VALUES (`213526487623121521`, `#RandomUser#5524`)
on this table
CREATE TABLE `new_table` (
`userID` varchar(20) DEFAULT NULL,
`referenceName` varchar(45) DEFAULT NULL,
`id` INT NOT NULL,
PRIMARY KEY (`id`)
but it always seem to spit out
Error Code: 1054. Unknown column '295284816490790912' in 'field list'
Any ideas? Searching on Google seems to point that the datatypes are the culprit, but I'm not sure what datatypes should be used in these characters.
Escape strings with single quotes ' and column names with backticks:
(`userID`, `referenceName`) VALUES ('213526487623121521', '#RandomUser#5524')
^ backtick ^ single quote
Try it
INSERT INTO `new_table` (`user_id`, `test`) VALUES ("213526487623121521", "#RandomUser#5524")
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.
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
I have a MySQL table that looks like this:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`company_id` int(8) unsigned NOT NULL,
`term_type` varchar(255) NOT NULL DEFAULT '',
`term` varchar(255) NOT NULL DEFAULT '',
I would like to be able to do this...
INSERT IGNORE INTO table ( company_id, term_type, term )
VALUES( a_company_id, 'a_term_type', 'a_term' )
... but I'd like the insert to be ignored when the same combination of company_id, term_type and term already exists. I am aware that if I have a unique index on a single field when I try to insert a duplicate value, the insert will be ignored. Is there a way to do the combo that I'm attempting? Could I use a multi-column index?
I'm trying to avoid doing a SELECT to check for this combination before every insert. As I'm processing hundreds of millions of rows of data into this table.
Maybe something like this:
ALTER TABLE table ADD UNIQUE (company_id, term_type,term);
If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.
So if you have a multicolumn primary key - it works.
I have a php form with three text boxes (webmeasurementsuiteId, webmeasurementsId, Id) and the values in the text boxes are retrieved from other tables of the database. Now my task is to submit the retrieved values in this php form named (mapping) to the database. I have created the table with the following syntax:
CREATE TABLE `mapping` (
`webmeasurementsuiteId` INT NOT NULL,
`webmeasurementsId` INT NOT NULL,
`Id` INT NOT NULL,
PRIMARY KEY (Id)
);
But I am getting an sql error as follows:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values ('','','7')
ERROR: Incorrect integer value: '' for column 'webmeasurementsuiteId' at row 1
Can anyone correct my error?
all your columns are INT that means numbers while your insert statement is inserting STRINGs (text) remove the ' around the values in the INSERT-statement and it should work
example:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,7)
So you used '' for webmeasurementsuiteId which indicates it as a string. just leave the integers to 0 without the parantheses to indicate them as an integer. values (0,0,7) should probably do it.
you cannot insert blank in the values field .
try 0 instead of ''
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,'7')
or alter the columns to take the null values