mysql insert value in column : int DEFAULT 0 causing error - mysql

I am not able to find out what is the exact issue. When I remove index param from below mentioned query and try to insert the data it works fine, but trying to add index cause an error.
Here is my table schema:
CREATE TABLE IF NOT EXISTS `address_list` (
`email` varchar(100) NOT NULL,
`currency_name` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`label` varchar(100) NOT NULL,
`index` int DEFAULT 0,
`address_type` varchar(50) NOT NULL,
`balance` varchar(500) DEFAULT "0.0",
`timestamp` TIMESTAMP default CURRENT_TIMESTAMP
)
and here is my insert query :
insert into address_list (email,currency_name,address,label,index,address_type) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X','test address',3,'wallet');
error :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X'' at line 1

index is a reserved word in mysql; rename your column to something else, or else place it in backticks where you want to use it:
insert into address_list (email,currency_name,address,label,`index`,address_type) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X','test address',3,'wallet');
See https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Related

Sql syntax issue with adding values [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 2 years ago.
I have an error in my sql on second addSql() line:
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, value VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (id,key,value) VALUES (1,'theKey','14')");
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key,value) VALUES (1,'theKey','14')' at line 1
Can someone help to locate it..
The 'key` word is reserved word in MySQL. I advice to avoid using reserved as column names. Any way is it necessary use backticks to quote them:
$this->addSql('CREATE TABLE user (`id` INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, `value` VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (`id`, `key`, `value`) VALUES (1,'theKey','14')");
Test it on SQLize.online
changing VALUE to VALUES should do the trick

MariaDB INSERT INTO FROM SELECT

Below is the block I am trying to execute, the documentation says this should work but instead I am met with an error that states "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO Location_Inventory_Column (Aisle, Name)
SELECT Aisle, Name ' at line 2"
~Any help would be highly appreciated!
START TRANSACTION
INSERT INTO Location_Inventory_Column (`Aisle`, `Name`)
SELECT `Aisle`, `Name` FROM TMPTableImport
Table Inserting Into:
CREATE TABLE `Location_Inventory_Column` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`Aisle` INT(11) NOT NULL DEFAULT 0,
`Name` VARCHAR(50) NOT NULL,
`Description` VARCHAR(50) NULL DEFAULT NULL,
`Disabled` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
TMPTable contains only Aisle and Name columns (See image)
TMPTABLE Image
As mentioned by #Akina the statement "START TRANSACTION" was the cause as it did not have a delimiter following the semicolon.
You must remove the single quotes in the Location_Inventory_Column (Aisle, Name) like this:
Location_Inventory_Column (Aisle, Name)

MySql Error no 1064

My table format is
CREATE TABLE IF NOT EXISTS `clinicReg` (
`clinicRegId` varchar(10) NOT NULL,
`clinicName` varchar(20) NOT NULL,
`clinicAddress` varchar(500) NOT NULL,
`clinicContactNo` int(20) NOT NULL,
`clinicContactNO1` int(20) NOT NULL,
`clinicMobileNo` int(20) NOT NULL,
`clinicMobileNo1` int(20) NOT NULL,
`clinicCatagories` varchar(50) NOT NULL,
`clinicServices` varchar(500) NOT NULL,
`clinicLogo` longblob NOT NULL,
`ownerName` varchar(20) NOT NULL,
`clinicEmailId` varchar(20) NOT NULL,
`clinicEmailId1` varchar(20) NOT NULL,
`loginTimeStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`clinicRegId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
my insert query is
insert into 'harshal.clinicreg' (clinicRegId,clinicName,
clinicAddress,clinicContactNo,clinicContactNO1,
clinicMobileNo,clinicMobileNo1,clinicCatagories,
clinicServices,clinicLogo,ownerName,clinicEmailId,
clinicEmailId1,loginTimeStamp)
Values
('ORCCli1','Smile Clinic','Mulund',
3456,544,234,567,'Gen','ABC',
load_file(C:\Users\harshal420\Pictures\Camera Roll\Capture.jpg),
'Smile','abc#xyz.com','def#pqr.com', CURDATE());
It is giving me error 1064 can any one help me???
Your INSERT statement has two issues that I can see upfront:
insert into 'harshal.clinicreg'
You have to wrap the db/table name with backticks, not single-quotes:
insert into `harshal`.`clinicreg`
After this, the load_file() function takes a string-input, but you're passing a literal. Try updating the full query to:
INSERT INTO `harshal`.`clinicreg`
(clinicRegId,clinicName, clinicAddress,clinicContactNo,clinicContactNO1,clinicMobileNo,clinicMobileNo1,clinicCatagories,clinicServices,clinicLogo,ownerName,clinicEmailId, clinicEmailId1,loginTimeStamp)
VALUES
('ORCCli1','Smile Clinic','Mulund', 3456,544,234,567,'Gen','ABC',
load_file('C:\\Users\\harshal420\\Pictures\\Camera Roll\\Capture.jpg'),
'Smile','abc#xyz.com','def#pqr.com', CURDATE()
);
You have 2 errors in your insert query.
1 -
Your table name is wrong it should be
insert into clinicReg
you can also put dbname.clinicReg
2- in your query load_file(C:\Users\harshal420\Pictures\Camera Roll\Capture.jpg)
this gives error
It is happening because you have an error in your SQL syntax. More specifically it is because you have used the incorrect quoting character when quoting your database/table name; you should be using backticks (`) or no quotes when specifying a database name, table name or field name.
You also need to quote the path to your data using single or double quotes. See my example below.
insert into `harshal`.`clinicreg` (clinicRegId,clinicName,clinicAddress,clinicContactNo,clinicContactNO1,clinicMobileNo,clinicMobileNo1,clinicCatagories,clinicServices,clinicLogo,ownerName,clinicEmailId,clinicEmailId1,loginTimeStamp) Values ('ORCCli1','Smile Clinic','Mulund',3456,544,234,567,'Gen','ABC',load_file('C:\Users\harshal420\Pictures\Camera Roll\Capture.jpg'),'Smile','abc#xyz.com','def#pqr.com', CURDATE());
You probably want to do
insert into `harshal`.`clinicreg` (...)
instead of
insert into 'harshal.clinicreg' (...)
In your example MySQL gets a string instead of table identifier which is what it excepts here.
You can also miss backticks here at all.
There also second syntax error in your query. You should quote path string passed to load_file function as parameter. Should be:
load_file('C:\Users\harshal420\Pictures\Camera Roll\Capture.jpg') ,

You have an error in your SQL syntax; MySql

Table schema:
CREATE TABLE IF NOT EXISTS `movie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`desc` text NOT NULL,
`review` text NOT NULL,
`image_url` text NOT NULL,
`promo_url` text NOT NULL,
`created_on` datetime NOT NULL,
`modified_on` datetime NOT NULL,
PRIMARY KEY (`id`)
)
Insert statement:
INSERT INTO movie (name, desc, review, image_url, promo_url, created_on, modified_on) VALUES ('?p0', '?p1', '?p2', '?p3', '?p4', '?p5', '?p6')
Error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, review, image_url, promo_url, created_on, modified_on) VALUES ('?p0', '?p1' at line 1
I am not able to figure out error source, can anyone please point it out?
desc is a reserved word. Either wrap it in ticks or change it to "description" or any other non-reserved name.
For me the code works. Perhaps the "desc" makes a problem (though you have it in backticks)?
DESC is a reserved word in MySQL.
You can still use it in the table definition, though. Just wrap it in backticks:
(name, `desc`, ...

MYSQL syntax error - why is this happening?

I use mysql in my php scripts for more than 6 years but i never encountered error like this.
When i execute this SQL command:
SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (show=1) LIMIT 1
it throws me this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show=1) LIMIT 1' at line 1
The table structure is:
CREATE TABLE IF NOT EXISTS `discount_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`image` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`discount` float NOT NULL,
`price1` float NOT NULL,
`price2` float NOT NULL,
`bought` int(11) NOT NULL,
`target` int(11) NOT NULL,
`desc` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`link` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`active` tinyint(1) NOT NULL,
`start` int(11) NOT NULL,
`end` int(11) NOT NULL,
`position` int(11) NOT NULL DEFAULT '1',
`show` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I don't get whats wrong. Obviously the 'show' field cause the problem but i already tried everything.. (there must be something wrong with show field because:
SELECT `discount_items`.* FROM `discount_items` WHERE (show=1) AND (active=1) AND (end<=1344007212) AND (position=1) LIMIT 1
throws
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show=1) AND (active=1) AND (end<=1344007212) AND (position=1) LIMIT 1' at line 1
So the problem moves with the show field.
I am sorry if this is common problem but i googled and found nothing. This error is too global and doesn't explain anything to me.
Thanks for any help and tips!
show is a MySQL reserved word. If you are going to use it to reference a field name, you must use backticks around it like this:
SELECT `discount_items`.*
FROM `discount_items`
WHERE
(position=1)
AND (active=1)
AND (end<=1344007212)
AND (`show`=1) /* backticks added here */
LIMIT 1
show is a reserved word. Either change it or place it in ticks
SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (`show`=1) LIMIT 1
show is a MySQL reserved word. Enclose it in backticks to make it work.