Mysql issue with triggers and delimiters from source file - mysql

I'm getting error if I use source command like the following:
mysql> source /home/sqlws/workspace/src.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
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 'END' at line 1
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 'TRIGGER trigg_cust_u INSERT INTO audit' at line 1ROW
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 'TRIGGER trigg_cust_d INSERT INTO audit' at line 1ROW
But if I copy & paste the contents of /home/sqlws/workspace/src.sql to mysql-cli, it works like a charm
mysql> DROP TABLE IF EXISTS audit_cust;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE audit_cust(audit_ts DATETIME DEFAULT CURRENT_TIMESTAMP, audit_id BIGINT(20) AUTO_INCREMENT, cust_id bigint(20),PRIMARY KEY (audit_id))ENGINE = InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.01 sec)
mysql> DROP TRIGGER IF EXISTS trigg_cust_i;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TRIGGER IF EXISTS trigg_cust_u;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TRIGGER IF EXISTS trigg_cust_d;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> DELIMITER //
mysql>
mysql> CREATE TRIGGER trigg_cust_i AFTER INSERT ON cust FOR EACH ROW
-> BEGIN
-> INSERT INTO audit_cust(audit_oprn, cust_id) VALUES('INSERT', NEW.cust_id);
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> CREATE TRIGGER trigg_cust_u AFTER UPDATE ON cust FOR EACH ROW
-> BEGIN
-> INSERT INTO audit_cust(audit_oprn, cust_id) VALUES('UPDATE', NEW.cust_id);
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> CREATE TRIGGER trigg_cust_d AFTER DELETE ON cust FOR EACH ROW
-> BEGIN
-> INSERT INTO audit_cust(audit_oprn, cust_id) VALUES('DELETE', OLD.cust_id);
-> END //
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> DELIMITER ;
Mysql version:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.21 |
+-----------+
1 row in set (0.00 sec)
Host version:
sqlws#tt49:$ lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
Could someone help me figure out the issue

Related

Re: MySQL 8.0 Command Line Client Error 1205

MySQL 8.0 Command Line Client is giving me a timeout error and I have restarting the transaction by typing "start transaction" another time. Keep in mind that I have another command line open with the table CIA_DATA.new_table and it is also being updated with the same changes. (I am doing this to follow a tutorial.) Here is the script:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update CIA_DATA.new_table set c1 = 2 where c1 = 1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Updated code for help in Answers Comments:
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> drop table CIA_DATA.new_table;
ERROR 1051 (42S02): Unknown table 'cia_data.new_table'
mysql> create table CIA_DATA.new_table ( c1 int primary key);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into CIA_DATA.new_table values (1);
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from CIA_DATA.new_table;
+----+
| c1 |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> update CIA_DATA.new_table set c1 = 2 where c1 = 1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> close transaction
-> \c
mysql> close transaction;
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 'close transaction' at line 1
mysql> --innodb-lock-wait-timeout=#
-> \c
mysql> --innodb-lock-wait-timeout=#;
->
Thanks,
thecoolgeek
What happen ony your computer:
One transaction locks the table and you didn't release the lock by endind the transaction.
you start a new tansaction and it encounters a lockesd table and waits for the release
As the first transaction and the lock wasn't released, the secnd goes into to timeout.
Locks are in the best case difficult. and sometimes it takes hours or days to solve them.
so close the tranaction as fast as you can, so that the timeout not happen or increase the timeout https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html#sysvar_innodb_lock_wait_timeout

is this a mysql bug about update check?

we have databases db1 and db2, each of them has a table t with a column a
create database db1;
create database db2;
create table t(a int);
insert into t values (1);
then we use db1 and run the SQLs:
mysql> update db2.t, (select 1 as a) as t set db2.t.a=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> update (select 1 as a) as t, db2.t set db2.t.a=1;
ERROR 1288 (HY000): The target table t of the UPDATE is not updatable
As you can see, the first SQL runs successfully, and the second report an error.
Why does this happen? is this a MySQL bug?
I am using MySQL version:
mysql> select version();
+-------------------------+
| version() |
+-------------------------+
| 8.0.22-0ubuntu0.20.04.3 |
+-------------------------+
1 row in set (0.00 sec)

mysql shows warning instead of error when inserting and not giving a value to column that is set as not null - field does't have a default value

6.24 running on windows 7 (from XAMPP v3.2.1).
It seems strange to me that when I have a table with a column with Allow NULL set to NOT NULL and there is no Default value
And I try to insert a row without specifying that column value I get a warning instead of an error.
I am pretty sure that in previous version MySQL did not allow that and produced and error and did not actually inserted the line. Is that a configuration issue? Is that how it should be?
I was able to reproduce your MySQL behaviour on my Linux Slackware MySQL 5.5.27:
mysql> CREATE TABLE `test` (
`field1` int(10) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.07 sec)
mysql> INSERT INTO test VALUES ();
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1364
Message: Field 'field1' doesn't have a default value
1 row in set (0.00 sec)
mysql> SELECT * FROM test;
+--------+
| field1 |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
This is related to sql_mode variable:
##GLOBAL.sql_mode
##SESSION.sql_mode
When you set it to "TRADITIONAL", it starts to give an error:
mysql> SET ##SESSION.sql_mode='TRADITIONAL';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO test VALUES ();
ERROR 1364 (HY000): Field 'field1' doesn't have a default value
Documentation about SQL modes

Rollback in the MySQL shell not working after ALTER TABLE

(MySQL 5.5, InnoDB tables) Why can't I rollback, either to a savepoint or to the previous begin statement?
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint id;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table sg_Section add column (published tinyint(1) default 0);
Query OK, 2 rows affected (0.30 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> rollback to savepoint id;
ERROR 1305 (42000): SAVEPOINT id does not exist
When not running any command after the savepoint, it seems to «work»:
mysql> savepoint id;
Query OK, 0 rows affected (0.00 sec)
mysql> rollback to savepoint id;
Query OK, 0 rows affected (0.00 sec)
DDL is not transactional in MySQL.
Any DDL statement implicitly commits the open transaction.
More details in the manual: http://dev.mysql.com/doc/refman/5.5/en/implicit-commit.html

Escaping in insert statement

How do I escape commas in mysql insert statements?
mysql> create table test.todel (name varchar(100));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test.todel values ('mcdonald's pizza');
'> ';
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 's pizza');
'' at line 1
mysql> select * from test.todel;
Empty set (0.02 sec)
I can escape the single comma, but that is not an option because I am using pretty complex shell script for inserting data.
mysql> insert into test.todel values ('mcdonald''s pizza');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test.todel;
+------------------+
| name |
+------------------+
| mcdonald's pizza |
+------------------+
1 row in set (0.00 sec)
use mysql_real_escape_string
Use mysql_real_escape_string to scape it safely.
your way is not secured BTW.
http://www.w3schools.com/php/func_mysql_real_escape_string.asp