Error on INSERT statement: "VALUE is not valid at this position" - mysql

VALUE is not valid at this position
The error is on VALUES within the INSERT statement. It states it is not supported within this version. Does that mean i require an update or is my syntax wrong?
create schema Cleudo;
USE CLEUDO;
create table Victim(
Vic_ID INT NOT NULL AUTO_INCREMENT,
Vic_Title VARCHAR(10) NOT NULL,
Vic_Name VARCHAR(30) NOT NULL,
Vic_Room VARCHAR(30) NULL,
Vic_TOD VARCHAR(5) NULL,
Vic_Weapon VARCHAR(30) NULL,
PRIMARY KEY ( VIC_ID )
);
INSERT INTO Victim VALUES ('Miss','Scarlet','Library','10:45','candle-Stick');

When I run your code, I get the following error:
Column count doesn't match value count at row 1
This is happening because you are not giving a value to table Vic_ID (which makes sense, because it is auto-incremented). To avoid the error, you need to enumerate the target columns, like so:
INSERT INTO Victim (Vic_Title, Vic_Name, Vic_Room, Vic_TOD, Vic_Weapon)
VALUES ('Miss','Scarlet','Library','10:45','candle-Stick');
Demo on DB Fiddle

You are getting this error because the number of columns are not the same, so you need to change your query to this:
INSERT INTO Victim(Vic_Title,Vic_Name,Vic_Room,Vic_TOD,Vic_Weapon) VALUES ('Miss','Scarlet','Library','10:45','candle-Stick');
In case you want to specify the Vic_ID value you can try this query below instead:
INSERT INTO Victim VALUES (NULL,'Miss','Scarlet','Library','10:45','candle-Stick')

Related

NULL value when insering row with trigger SQL and CONCAT function

I put 10,000 avatar photos on a server and I would like for each row inserted into the 'studenttable' table, the 'photo' column to be the concatenation of the url of the folder of my photos + the id of the inserted student.
However, the CONCAT function returns a NULL value with the basic trigger used.
First, here is the above mentioned table :
CREATE TABLE `studenttable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` enum('Male','Female','Other') NOT NULL,
`email` varchar(100) DEFAULT NULL,
`birthDate` date DEFAULT NULL,
`photo` varchar(535) DEFAULT NULL,
`mark` double DEFAULT NULL,
`comment` varchar(535) DEFAULT NULL,
PRIMARY KEY (`id`)
)
and here is the basic trigger I created:
DELIMITER $$
create trigger IMAGE_LienApi
before insert on studenttable
for each row
begin
set NEW.photo = CONCAT('https://url-of-folder-with-my-images/',NEW.id,'.png');
end$$
DELIMITER ;
For information, the images are referenced in this way:
number.png
So when I insert a new student with this trigger, the photo column is always set to NULL.
The problem must come from NEW.id, because when I replace this value with a string, it works.
I also tried with
NEW.photo = 'https://url-of-folder-with-my-images/' + CONVERT(VARCHAR(5),NEW.id),'.png';
but it did not work
Thank you in advance for your help and if someone could explain to me especially why the CONCAT does not work, that would be great !
CONCAT() returns NULL if any of its arguments are NULL.
In a BEFORE INSERT trigger, the NEW.id value is NULL. It hasn't been generated yet.
But in an AFTER INSERT trigger, it's too late to change the NEW.photo column of your row. You can't change columns in an AFTER trigger.
You cannot make a BEFORE INSERT trigger to concatenate an auto-increment value with other columns. The best you can do is let the INSERT complete, and then subsequently do an UPDATE to change your photo column.
The alternative is to forget about using the builtin AUTO_INCREMENT to generate id values, instead generate them some other way and specify the value in your INSERT statement.

While Insert Records To MySQL, get in Error Code 1054

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.

ERROR 1001 (45000) old data received in mysql

I am trying to insert data to MySQL table, but without inserting it I got the error like
ERROR 1001 (45000) old data received error. But when I am trying to insert into with a different date it is inserted
Suppose my table schema is as follows
CREATE TABLE `sdp_transaction` (
`correlation_id` varchar(36) NOT NULL,
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`sp_id` varchar(32) DEFAULT NULL,
`sp_name` varchar(100) DEFAULT NULL,
`app_id` varchar(32) DEFAULT NULL,
`app_name` varchar(100) DEFAULT NULL,
`app_state` varchar(32) DEFAULT NULL,
);
I tried with following queries
query #1
INSERT INTO sdp_transaction VALUES ('921102201000000000','2022-03-22 00:09:02','SPP_000036','himuarif','APP_000165','loves','live');
query #2
INSERT INTO sdp_transaction VALUES ('921102201000000000','2022-03-22 16:09:02','SPP_000036','himuarif','APP_000165','loves','live');
The 1st query is not inserted, it came out mentioned error above, but the 2nd query is inserted without an error. Defining columns names to INSERT query also not working. With some experiments, I realise that data is not inserted before the data of 2022-03-22 at 15:00:00. But after that time records are inserted without any error. As I remember I applied RENAME query for the column app_state before its name was col1.
What is actually happed, Why this kind of error is coming ??? How to solve it ???

Explicitly specifying value for an auto-incremented column in MySql

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');

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') ,