Escaping in insert statement - mysql

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

Related

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)

How to execute a control-flow statement in MySQL with user-defined variables?

I want to execute a simple control-flow statement in MySQL with user-defined variables.
The following are my SQL query:
mysql> SET #something=2;
Query OK, 0 rows affected (0.00 sec)
mysql> SET #z=1;
Query OK, 0 rows affected (0.00 sec)
mysql> IF #z = 1 THEN SET #something=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 'IF #z = 1 THEN SET #something=1' at line 1
mysql> select #z, #something;
+------+------------+
| #z | #something |
+------+------------+
| 1 | 2 |
+------+------------+
1 row in set (0.00 sec)
My control statement is saying SQL syntax error. But when I check the syntax here, it seems correct. My guess is because it is of the user-defined variable but could not find any similar issue or example execution.
Anyone knows how to properly execute this SQL query?
Thanks for the help.

Mysql issue with triggers and delimiters from source file

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

how to create procedures in mysql

I am creating procedure in mysql. but I am facing some issues while creating that.
I am applying query i.e
CREATE PROCEDURE simpleproc( param1 INT) BEGIN SELECT COUNT(*)
INTO param1 FROM 91_nidhi; END//
and The error is
#1064 - You have an error in your SQL syntax; c
heck the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 1
This how you need to do
- You are not passing any input value rather you are using an output value
- so specify the param as OUT
Below is the example
mysql> create table 91_nidhi (id int,val varchar(20));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into 91_nidhi values (1,'aa'),(2,'bb'),(3,'cc');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
Now lets create the procedure
delimiter //
CREATE PROCEDURE simpleproc( out param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM 91_nidhi;
END; //
Then change the delimiter in the terminal as
mysql> delimiter ;
Now lets call the procedure
mysql> call simpleproc(#res);
Query OK, 0 rows affected (0.00 sec)
mysql> select #res ;
+------+
| #res |
+------+
| 3 |
+------+
1 row in set (0.00 sec)

Can I make mysql table columns case insensitive?

I am new to mysql (and sql in general) and am trying to see if I can make data inserted into a column in a table case insensitive.
I am storing data like state names, city names, etc. So I want to have a unique constraint on these types of data and on top of that make them case insensitive so that I can rely on the uniqueness constraint.
Does mysql support a case-insensitive option on either the column during table creation or alternatively when setting the uniqueness constraint on the column? What is the usual way to deal with such issues? I would appreciate any alternate ideas/suggestions to deal with this.
EDIT: As suggested, does COLLATE I think only applies to queries on the inserted data. But to really take advantage of the uniqueness contraint, I want to have a case insensitivity restriction on INSERT. For e.g. I want mysql to not allow insertions of California and california and cALifornia as they should be the same. But if I understand the uniqueness constraint prooperly, having it on the StateName will still allow the above four inserts.
By default, MySQL is case-insensitive.
CREATE TABLE test
(
name VARCHAR(20),
UNIQUE(name)
);
mysql> INSERT INTO test VALUES('California');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO test VALUES('california');
ERROR 1062 (23000): Duplicate entry 'california' for key 'name'
mysql> INSERT INTO test VALUES('cAlifornia');
ERROR 1062 (23000): Duplicate entry 'cAlifornia' for key 'name'
mysql> INSERT INTO test VALUES('cALifornia');
ERROR 1062 (23000): Duplicate entry 'cALifornia' for key 'name'
mysql> SELECT * FROM test;
+------------+
| name |
+------------+
| California |
+------------+
1 row in set (0.00 sec)
Use BINARY when you need case-sensitivity
To make case-sensitive in MySQL, BINARY keyword is used as follows
mysql> CREATE TABLE test
-> (
-> name varchar(20) BINARY,
-> UNIQUE(name)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> INSERT INTO test VALUES('California');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO test VALUES('california');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO test VALUES('cAlifornia');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO test VALUES('cALifornia');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT * FROM test;
+------------+
| name |
+------------+
| California |
| cALifornia |
| cAlifornia |
| california |
+------------+
4 rows in set (0.00 sec)
You can use COLLATE operator: http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html