I have this stored procedure:
DROP PROCEDURE IF EXISTS buildMySomething;
CREATE PROCEDURE buildMySomething()
BEGIN
UPDATE current_amount SET current_m_amount = 2 WHERE m_id = 1;
END //
This gives me the following error : ERROR 1054 (42S22): Unknowing column 'current_m_amount' in 'field list'
After looking around on the internet, it is apparent to me that people get this error if the column does not exist, an unexpected character, or simply a syntax error (they have typed the column name wrong)... however... i have checked these possibilities a countless number of times. what am i missing here?
+--------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------+------+-----+---------+----------------+
| m_id | int(11) | NO | PRI | NULL | auto_increment |
| current_m_amount | int(11) | NO | | NULL | |
+--------------------+---------+------+-----+---------+----------------+
I think it might be something to do with the delimiter in use. Try this, which executes on my test server:
DROP PROCEDURE IF EXISTS buildMySomething;
delimiter //
CREATE PROCEDURE buildMySomething()
BEGIN
UPDATE current_amount SET current_m_amount = 2 WHERE m_id = 1;
END //
delimiter ;
Related
I have a structure that looks like this:
Attribute Values Table
+----+-----------+---------------+-------------+-------------+
| id | option_id | integer_value | price_value | text_value |
+----+-----------+---------------+-------------+-------------+
| 1 | 4 | NULL | NULL | NULL |
| 2 | NULL | 24 | NULL | NULL |
| 3 | NULL | NULL | NULL | Lorem Ipsum |
| 4 | NULL | NULL | 30.50 | NULL |
+----+-----------+---------------+-------------+-------------+
(Some columns were removed for brevity)
But basically the type of the entry can either be a reference to a select option (option_id), or an integer value (integer_value), or a decimal value (price_value), or a text value (text_value). One of the omitted columns is attribute_id which corresponds to an entry that has an enum which stores which one of these four columns is it.
Is there any way to add a constraint in the Laravel schema that at least one of these four columns has to be non-null?
If you will always have only one of these column filled at any time I would recomend altering your schema to just two colums
Like
+----------+--------------+------------+
| id | value | type |
+----------+--------------+------------+
Where the value will always have a value and type will contain the 4 types that you have mentioned
ie: option_id, integer_value, price_value and text_value
This way its clean.
In laravel you cannot add the specified constraint. You must validate your input before insertion.
However if you really want to add a database level constraint you could try adding a trigger before each insert to validate your need using the DB::unprepared function on the table.
and the trigger could look like this (I have not tried this)
DB::unprepared("
DELIMITER $$
CREATE TRIGGER `foo`
BEFORE INSERT ON `table`
FOR EACH ROW
BEGIN
IF (
new.option_id IS NULL
AND new.integer_value IS NULL
AND new.price_value IS NULL
AND new.text_value IS NULL
)
THEN SIGNAL SQLSTATE '02000'
SET MESSAGE_TEXT = 'your message here';
END IF;
END$$
DELIMITER ;
");
I'm using Mysql Workbench 8.0.12, I have a table (tblproduction) with three columns:
idProduction INT PK AI
dateProduction DATETIME
statusProduction VARCHAR(15)
When I try to change the value of the column statusProduction for example, from 'queue' to 'done' as the query below (SELECT, INSERT AND DELETE querys are OK):
UPDATE tblproduction
SET statusProduction = 'done'
WHERE idProduction=1;
The system returns "Error Code: 1241. Operand should contain 1 column(s)."
I've done some research and found out that this error code is related to syntax errors, but I didn't find any problem on my query.
If I try to do something like this:
UPDATE tblproduction
SET statusProduction = 'done'
WHERE idProduction='a';
The system actually run the query although 0 rows were affected, but it gave me the same error message if I try this instead:
UPDATE tblproduction
SET statusProduction = 'done'
WHERE idProduction='1';
I really don't know what is wrong, and I know it's a silly question but if anyone could help me.
please try using this method :
I have created a table and inserted the record like this :
select * from tblproduction;
+--------------+---------------------+------------------+
| idProduction | dateProduction | statusProduction |
+--------------+---------------------+------------------+
| 1 | 2018-11-19 08:22:11 | queue |
+--------------+---------------------+------------------+
use update query :
UPDATE `test`.`tblproduction` SET `statusProduction` = 'done' WHERE `tblproduction`.`idProduction` =1;
got result :
--------------+---------------------+------------------+
| idProduction | dateProduction | statusProduction |
+--------------+---------------------+------------------+
| 1 | 2018-11-19 08:22:11 | done |
+--------------+---------------------+------------------+
I am unable to reproduce your problem, see https://rextester.com/OQI5184
#MySQL 5.7.12
#'\\' is a delimiter
DROP TABLE IF EXISTS tblProduction;
CREATE TABLE IF NOT EXISTS tblProduction (
idProduction INT NOT NULL AUTO_INCREMENT
, dateProduction DATETIME NOT NULL
, statusProduction VARCHAR(15) NOT NULL
, PRIMARY KEY (idProduction)
)
;
INSERT INTO tblProduction(dateProduction,statusProduction) VALUES ('2018-11-20','start');
SELECT * FROM tblProduction;
UPDATE tblproduction
SET statusProduction = 'done'
WHERE idProduction=1;
SELECT * FROM tblProduction;
RESULTS:
+---+--------------+---------------------+------------------+
| | idProduction | dateProduction | statusProduction |
+---+--------------+---------------------+------------------+
| 1 | 1 | 20.11.2018 00:00:00 | start |
+---+--------------+---------------------+------------------+
+---+--------------+---------------------+------------------+
| | idProduction | dateProduction | statusProduction |
+---+--------------+---------------------+------------------+
| 1 | 1 | 20.11.2018 00:00:00 | done |
+---+--------------+---------------------+------------------+
So the problem wasn't neither the query nor the table, there was an update trigger on this table (witch I dropped for testing and didn't work), to sum up I had to drop the database and created it all once again without creating the trigger. This time the update query worked, I'm working on the trigger now to see if I can find what's wrong
when I execute the INSERT INTO account values(110,1250); statement on the following table
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| acc_no | int(11) | YES | | NULL | |
| amount | bigint(20) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
I get this error
ERROR 1054 (42S22): Unknown column 'amount' in 'field list'
what can be the cause of it?
Trigger code is
create trigger demo before insert on account
for each row
set #diff=#diff+amount;
As I am learning triggers so I used a basic trigger defination
You have to put code like this:
INSERT INTO account values('110', '1250');
If you select varchar in mysql then it should be string. if you select INT in mysql then no need put ''.
There are tons of these posts on Stack Overflow, however from the 20 or so that I looked at they were either coding errors faced when interfacing with MySQL (which I am not trying to do) or simply wanted null values but had their table defined incorrectly.
I am seeing an error in MySQL 5.6.19 where I have a column that is not allowed to have a null value. This is fine as it shouldn't have a null value. Here is the table desc below.
mysql> describe z;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| a | int(11) | NO | PRI | NULL | auto_increment |
| data | char(30) | NO | | NULL | |
| t | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
My problem is that I am inserting valid data....
mysql> insert into z (data, t) values('helloworld', sysdate());
ERROR 1048 (23000): Column 'data' cannot be null
There is one other piece of information that might be of some concern... or may not be.
I have a trigger and procedure that execute upon the implementation of inserts into this column. However I don't see that it should be a problem due to the trigger being activated after the insert statement completes.
Here is the trigger:
mysql> show triggers\G
*************************** 1. row ***************************
Trigger: insertuser
Event: INSERT
Table: z
Statement: begin
call triggerproc(sysdate(),user(),(select data from z where a = last_insert_id()));
end
Timing: AFTER
Created: NULL
sql_mode: NO_ENGINE_SUBSTITUTION
Definer: root#localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
And the Procedure:
mysql> show create procedure triggerproc\G
*************************** 1. row ***************************
Procedure: triggerproc
sql_mode: NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`#`localhost` PROCEDURE `triggerproc`(in a datetime, in b char(30), in c char(30))
begin
insert into record (t,u,data) values(a,b,c);
end
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
Just for good measure I will include the definition for the record table as well.
mysql> desc record;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| t | datetime | NO | | NULL | |
| u | char(30) | NO | | NULL | |
| data | char(30) | NO | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
I have looked through the MySQL reference manual for anything that could be of use, however it doesn't seem to have any details on this other than the standard error and to check that your column is not defined as not null... or I missed it...
In any case I would be greatly appreciative if anyone can help me out with finding out either the reason for this error or how I can go about finding the reason.
Thanks in advance.
EDIT: My question was answered wonderfully by TheConstructor he informed me that to grab new information from a column that was just inserted through a trigger that the NEW.column operator may be used. Furthermore he followed up with documentation that helps to understand this issue located at Trigger Syntax.
I only wonder why the trigger that I had wouldn't work with the insert statement even though it should activate after the previous statement, which makes me believe that it should (theoretically) work.
Reading the documentation on LAST_INSERT_ID() I would suggest that the value is only updated after the last trigger runs. I also created a trigger which inserts the result of LAST_INSERT_ID() into another table and it would always insert the id of the row inserted by the INSERT statement before or 0 if there was no previous INSERT.
From within an insert or update trigger you can always refer to the state after the statement by using NEW.column where column is a column-name of your table. See the documentation for examples
Table structure is:
mysql> DESC groups;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| PKey | varchar(64) | NO | PRI | NULL | |
| group_name | varchar(64) | YES | | NULL | |
| Region | varchar(128) | NO | | NULL | |
| Role | varchar(128) | NO | | NULL | |
| parent_group | varchar(64) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
When i am executing this Trigger , i'm having a compilation error
DELIMITER $$
CREATE
TRIGGER `group_before_delete` BEFORE DELETE
ON `groups`
FOR EACH ROW BEGIN
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!`;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.Pkey;
END IF;
END$$
DELIMITER ;
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 ';
ELSE
t_group=old.parent_group
t_group=old.PKey;
END IF;' at line 6
mysql> DELIMITER ;
Can you tell me what i'm missing here ??
You get an error in your code because the syntax of your UPDATE statement is not valid.
The links you give are 4 and 5 years old!
Since the SIGNAL statement is available in MySQL since version 5.5.0 (released 3 years ago), this is really not a good idea to use the hacks described in these 2 webpages. Instead, use the SIGNAL statement.
Note: From the comments we learn that the OP is not using MySQL 5.5, so SIGNAL is not available.
In your IF statement, the following is not a valid SQL statement:
UPDATE `Error: deletion RootGroup is prohibited!`;
This should be this:
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!` set x=1;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.PKey;
END IF;
I have never done things this way. It is a bit of an ugly way of doing it. But if it works, what the heck.