Im trying to get this program to add a new row into two different tables: bilde and student.
The values entered into bilde should come from the values used in student.
At this point i get this message: error code: 1136. Column count dosen't match value at row 1, and im not quite sure what to do to fix this.
DELIMITER $$
DROP PROCEDURE IF EXISTS NyStudent $$
CREATE PROCEDURE NyStudent (
brukernavn VARCHAR(45)
,fornavn VARCHAR(45)
,etternavn VARCHAR(45)
,klassekode INT
)
BEGIN
START TRANSACTION;
INSERT INTO bilde (filnavn, beskrivelse)
VALUES (CONCAT('bilder/', fornavn, '.jpg'), CONCAT('bilde av ', fornavn, ' ', etternavn));
INSERT INTO student
VALUES ('donaldduck','donald','duck','1');
COMMIT;
END$$
DELIMITER ;
CALL NyStudent('donaldduck','donald','duck','1');
Try adding the column names in the 'INSERT INTO' statement for students, as that should be the major reason for column count error.
Also, Even when we insert values for every column of the table, it is a best practice to include column names after INSERT INTO statement. Following this would avoid any breakage in code to a certain extent, even on schema changes in the future.
Related
Creating a trigger is not working as expected, whenever I try to insert data into master table it give me error that count does't match. I am unable to identify where I'm doing wrong.
I have attached error image please look for further demonstration
DELIMITER $$
DROP TRIGGER IF EXISTS `trg_apl_b_info_after_insert`
CREATE
TRIGGER `trg_apl_b_info_after_insert` AFTER INSERT ON `tbl_appli_basic_info`
FOR EACH ROW BEGIN
DECLARE vApplicant VARCHAR(256);
-- Find appli_basic_info_id & apli_reg_no of Applicant performing the INSERT into table
SELECT USER() INTO vApplicant;
-- Insert record into tbl_appli_basic_info_after_insert table
INSERT INTO tbl_appli_basic_info_after_insert
( appli_basic_info_id,
apli_reg_no,
full_name,
after_insert_datetime)
VALUES
( NEW.appli_basic_info_id,
NEW.apli_reg_no,
NEW.full_name,
SYSDATE(),
vApplicant );
END;
$$
DELIMITER ;
Error in phpMyAdmin
Your insert statement lists 4 fields however you provided 5 values. Hence count not matched.
UPDATE!
The Delimiter thing actually solved my original problem. But I now ended up with a new Error. I'll describe it below along with the implementet changes that solved the first problem.
I am trying to write a trigger that will log changes in the database to a seperate table. But I keep getting the same error. I have looked at the MYSQL documentation and searched this forum and found a lot of helpfull answers. Trouble is that I now have a piece of SQL code that looks exactly like the answers given in this forum but I still get an error.
The trigger I am trying to use is:
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END;
The error message I get back is:
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 '' at line 5
I have tried using " instead of single quotes.
someone surgested in on of the answers here that you should use backticks, so tried that as well.
No matter what, the error message is exactly the same.
A friendly soul here posted a fix for my original error. I needed to add DELIMITER to the statement so that it should look like this:
DELIMITER $$
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END$$
DELIMITER ;
This change solved the original error, but also let to a new error.
The new error is:
Unknown Coloumn 'id' in table NEW
This is the table I'm trying to write to:
CREATE TABLE datalog (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP,
data1 VARCHAR(255) NOT NULL,
data2 DECIMAL(5,2) NOT NULL
);
Hope someone here has an idea of what is wrong.
Kind regards,
Jonas
Use DELIMITER for the trigger
Like
DELIMITER $$
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS kund2orderNew;
DELIMITER ;;
CREATE PROCEDURE kund2orderNew(kundId2 INT)
BEGIN
IF kundId2 <> (SELECT kundId FROM kund2order) THEN
INSERT INTO kundOrder VALUES ();
INSERT INTO kund2order VALUES (kundId2, (SELECT id FROM kundOrder));
END IF;
END
;;
DELIMITER ;
Alright am I doing something wrong here? What im trying to do is to check if kundId is in the kund2order, if its not then what I want to do is create a new row in the kundOrder table that just uses the default values and then take the recently created id from that row in the kundOrder and put it inside the new row in kund2order (together with kundId).
For some reason it just gives me (node:18328) UnhandledPromiseRejectionWarning: Error: ER_BAD_NULL_ERROR: Column 'kundId' cannot be null
I am a bit confused as to what the problem is, both tables are empty after I have called this procedure. Is the problem my if statement or is it something else?
That's not the correct way to check if an ID is already in the table. When you use a SELECT query as an expression, it has to return just one row. You can use:
IF NOT EXISTS (SELECT * FROM kund2Order WHERE kundId = kundId2) THEN
And if you want to insert the auto-increment of the row that was just inserted into kundOrder, you should use LAST_INSERT_ID():
INSERT INTO kund2order VALUES (kundId2, LAST_INSERT_ID());
I can't seem to get one of the triggers im trying to make to work at the moment it looks like this (has been messed around with alot in an attempt to get it to work so probably doesn't make any sense now)
CREATE TRIGGER `insertproductebay` AFTER INSERT ON `product`
FOR EACH ROW INSERT INTO eBayLinked
(product_id,company_id,eBay_ID,ebay_token_id,ebay_username)
SELECT ebay_token_id,ebay_username
FROM
'company'
WHERE
'company_id' = 'company_id'
VALUES
(NEW.product_id,NEW.company_id,NEW.eBay_ID,ebay_token_id,ebay_username)
The following makes it past the 1065 error
Care must be taken to use back-ticks around table and column names, and not to use single quotes there.
drop trigger if exists `insertproductebay`;
DELIMITER $$
CREATE TRIGGER `insertproductebay` AFTER INSERT ON `product`
FOR EACH ROW
INSERT INTO eBayLinked
(product_id,company_id,eBay_ID,ebay_token_id,ebay_username)
SELECT NEW.product_id,NEW.company_id,NEW.eBay_ID,ebay_token_id,ebay_username
FROM `company`
WHERE `company_id` = 'company_id'
$$
DELIMITER ;
The remaining problem as I see it could be what are you meaning by
WHERE `company_id` = 'company_id'
in a trigger. Because a trigger is a faceless piece of code that runs in the background, succeeding or failing silently. In other words, there is no user interface associated with it.
You can't mix the INSERT ... SELECT and INSERT ... VALUES syntaxes in that way. However, you can provide literal values in a select list, thusly:
CREATE TRIGGER insertproductebay AFTER INSERT ON product FOR EACH ROW
INSERT INTO eBayLinked
(product_id, company_id, eBay_ID, ebay_token_id, ebay_username)
SELECT NEW.product_id, NEW.company_id, NEW.eBay_ID, ebay_token_id, ebay_username
FROM company
WHERE company_id = NEW.company_id
I am trying to create a trigger (this is my first trigger, and question, so be gentle) that will insert new rows into two different tables.
* Edit *
Adding this in as I forgot to mention it until ypercube answered.
I am trying to avoid listing all of the column names, as in the real world usage the table this will be used on has a very large number of columns (not my design, too late to refactor).
* End Edit *
Here's what I have so far.
CREATE TABLE test_table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
message VARCHAR(255)
);
CREATE TABLE test_table_copy LIKE test_table;
DELIMITER $$
CREATE TRIGGER copy_test_table_data AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO test_table_copy SELECT * FROM NEW;
END;$$
DELIMITER ;
Unfortunately this results in an error.
mysql> INSERT INTO test_table VALUES (1, 'This is a message');
ERROR 1146 (4502): Table 'test_db.NEW' doesn't exist
I am not quite sure what the problem is, I thought NEW referred the table data was being inserted into?
You could possibly get a list of column names in that table from information_schema views, then use them to create a prepared statement (using cursor to iterate column names) and CONCAT() function to glue together the query string. Then execute the prepared statement.
Seems very contrived even to me, and I'm not sure it would work (and if it did, how efficient it would be)