Mysql : How to check uniqueness of pair - mysql

CREATE TABLE nodes (
id INTEGER PRIMARY KEY,
name VARCHAR(10) NOT NULL,
feat1 CHAR(1), -- e.g., age
feat2 CHAR(1) -- e.g., school attended or company
);
CREATE TABLE edges (
a INTEGER NOT NULL REFERENCES nodes(id) ON UPDATE CASCADE ON DELETE CASCADE,
b INTEGER NOT NULL REFERENCES nodes(id) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (a, b)
);
CREATE INDEX a_idx ON edges (a);
CREATE INDEX b_idx ON edges (b);
If we want to represent an undirected graph, we need to add a CHECK constraint on the uniqueness of the pair.
Since the SQL standard does not allow a subquery in the CHECK constraint,How can i check uniqueness of the pair?

You could rig up a trigger that fails upon seeing either (A,B) or (B,A) :
Here is the Trigger:
DELIMITER $$
CREATE TRIGGER edges_bi BEFORE INSERT
ON edges FOR EACH ROW
BEGIN
DECLARE found_count,dummy,diff,SomethingsWrong INT DEFAULT 0;
DECLARE errmsg VARCHAR(128);
SET diff = new.a - new.b;
IF diff = 0 THEN
SET errmsg = CONCAT('[',new.a,',',new.b,'] is Vertex, Not Edge');
SET SomethingsWrong = 1;
END IF;
SELECT COUNT(1) INTO found_count FROM edges
WHERE (a=NEW.a AND b=NEW.b) OR (a=NEW.b AND b=NEW.a);
IF found_count = 1 THEN
SET errmsg = CONCAT('[',new.a,',',new.b,'] Already Exists');
SET SomethingsWrong = 1;
END IF;
IF SomethingsWrong = 1 THEN
SELECT errmsg INTO dummy FROM edges WHERE 1=1;
END IF;
END; $$
DELIMITER ;
Here is a sample table:
DROP DATABASE if exists saurabh;
CREATE DATABASE saurabh;
USE saurabh
CREATE TABLE edges
(
a INTEGER NOT NULL,
b INTEGER NOT NULL,
PRIMARY KEY (a,b),
UNIQUE KEY (b,a)
);
Notice that I have a PRIMARY KEY and a UNIQUE KEY with the columns of the PRIMARY KEY reversed
Let's create the table:
mysql> DROP DATABASE if exists saurabh;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE DATABASE saurabh;
Query OK, 1 row affected (0.00 sec)
mysql> USE saurabh
Database changed
mysql> CREATE TABLE edges
-> (
-> a INTEGER NOT NULL,
-> b INTEGER NOT NULL,
-> PRIMARY KEY (a,b),
-> UNIQUE KEY (b,a)
-> );
Query OK, 0 rows affected (0.12 sec)
mysql>
Let's create the Trigger:
mysql> DELIMITER $$
mysql> CREATE TRIGGER edges_bi BEFORE INSERT
-> ON edges FOR EACH ROW
-> BEGIN
-> DECLARE found_count,dummy,diff,SomethingsWrong INT DEFAULT 0;
-> DECLARE errmsg VARCHAR(128);
-> SET diff = new.a - new.b;
-> IF diff = 0 THEN
-> SET errmsg = CONCAT('[',new.a,',',new.b,'] is Vertex, Not Edge');
-> SET SomethingsWrong = 1;
-> END IF;
-> SELECT COUNT(1) INTO found_count FROM edges
-> WHERE (a=NEW.a AND b=NEW.b) OR (a=NEW.b AND b=NEW.a);
-> IF found_count = 1 THEN
-> SET errmsg = CONCAT('[',new.a,',',new.b,'] Already Exists');
-> SET SomethingsWrong = 1;
-> END IF;
-> IF SomethingsWrong = 1 THEN
-> SELECT errmsg INTO dummy FROM edges WHERE 1=1;
-> END IF;
-> END; $$
Query OK, 0 rows affected (0.11 sec)
mysql> DELIMITER ;
Here is some sample data:
INSERT INTO edges (a,b) VALUES (5,3);
INSERT INTO edges (a,b) VALUES (3,3);
INSERT INTO edges (a,b) VALUES (3,5);
INSERT INTO edges (a,b) VALUES (5,5);
SELECT * FROM edges;
Let's try loading these into the edges table:
mysql> INSERT INTO edges (a,b) VALUES (5,3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO edges (a,b) VALUES (3,3);
ERROR 1366 (HY000): Incorrect integer value: '[3,3] is Vertex, Not Edge' for column 'dummy' at row 1
mysql> INSERT INTO edges (a,b) VALUES (3,5);
ERROR 1366 (HY000): Incorrect integer value: '[3,5] Already Exists' for column 'dummy' at row 1
mysql> INSERT INTO edges (a,b) VALUES (5,5);
ERROR 1366 (HY000): Incorrect integer value: '[5,5] is Vertex, Not Edge' for column 'dummy' at row 1
mysql> SELECT * FROM edges;
+---+---+
| a | b |
+---+---+
| 5 | 3 |
+---+---+
1 row in set (0.00 sec)
Note that blocking A=B conditions prevents any self-loops
CAVEAT
This trigger does not work if
you start with an empty table
enter (3,3) as the first row
because the BEFORE INSERT trigger does not fire on an empty table.
Once you enter a valid row with A <> B then all checks are performed properly.
Give it a Try !!!

MySQL does not support CHECK constraints.
You can create a BEFORE INSERT and BEFORE UPDATE triggers to check this situation, and throw an error if needed.
Example:
CREATE TABLE edges(
a INT(11) NOT NULL,
b INT(11) NOT NULL
);
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON edges
FOR EACH ROW
BEGIN
SET #cnt = NULL;
SELECT COUNT(*) INTO #cnt FROM edges
WHERE a = new.a AND b = new.b OR a = new.b AND b = new.a;
IF #cnt > 0 THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Error: uniqueness of pair';
END IF;
END
$$
DELIMITER ;
Also, create similar BEFORE UPDATE trigger to avoid NEW wrong values on updating, or just use a stored procedure because the code is the same.

CHECK is not supported on CREATE TABLE by MySQL, as the documentation dictates
The CHECK clause is parsed but ignored by all storage engines
In fact there is an open bug report on that issue since 2004 (!).
The approach I would take is to create a stored procedure trigger on insert and on update that deliberate fails if the pair exists.

I think an answer may depend on how you are populating the edges table, which is not clear from your question. If it is being populated from the nodes table, then you may be able to build a VIEW based on a SELECT query that excludes the mirrored pairs (i.e. 1,2 and 2,1). This might also solve the cascade and delete requirement.

Related

Work around for array in MySQL stored procedure [duplicate]

I got a situation where I have to pass a comma separated string to MySQL stored procedure and split that string and insert those values as rows in to a table.
For example if I passed 'jhon,swetha,sitha' string to MySQL stored procedure then it have to split that string by comma and insert those values as 3 records into a table.
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
But this does not work. Any solutions please?
You'll need to be a little more careful with your string manipulation. You can't use REPLACE() for this, because that will replace multiple occurrences, corrupting your data if one element in the comma-separated list is a substring of another element. The INSERT() string function is better for this, not to be confused with the INSERT statement used for inserting into a table.
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
Next, a table for testing:
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The new table is empty.
mysql> SELECT * FROM t1;
Empty set (0.00 sec)
Call the procedure.
mysql> CALL insert_csv('foo,bar,buzz,fizz');
Query OK, 1 row affected (0.00 sec)
Note the "1 row affected" does not mean what you would expect. It refers to the last insert we did. Since we insert one row at a time, if the procedure inserts at least one row, you'll always get a row count of 1; if the procedure inserts nothing, you'll get 0 rows affected.
Did it work?
mysql> SELECT * FROM t1;
+----+------+
| id | c1 |
+----+------+
| 1 | foo |
| 2 | bar |
| 3 | buzz |
| 4 | fizz |
+----+------+
4 rows in set (0.00 sec)

How to assign a set of value in mysql stored procedure [duplicate]

I got a situation where I have to pass a comma separated string to MySQL stored procedure and split that string and insert those values as rows in to a table.
For example if I passed 'jhon,swetha,sitha' string to MySQL stored procedure then it have to split that string by comma and insert those values as 3 records into a table.
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
But this does not work. Any solutions please?
You'll need to be a little more careful with your string manipulation. You can't use REPLACE() for this, because that will replace multiple occurrences, corrupting your data if one element in the comma-separated list is a substring of another element. The INSERT() string function is better for this, not to be confused with the INSERT statement used for inserting into a table.
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
Next, a table for testing:
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The new table is empty.
mysql> SELECT * FROM t1;
Empty set (0.00 sec)
Call the procedure.
mysql> CALL insert_csv('foo,bar,buzz,fizz');
Query OK, 1 row affected (0.00 sec)
Note the "1 row affected" does not mean what you would expect. It refers to the last insert we did. Since we insert one row at a time, if the procedure inserts at least one row, you'll always get a row count of 1; if the procedure inserts nothing, you'll get 0 rows affected.
Did it work?
mysql> SELECT * FROM t1;
+----+------+
| id | c1 |
+----+------+
| 1 | foo |
| 2 | bar |
| 3 | buzz |
| 4 | fizz |
+----+------+
4 rows in set (0.00 sec)

MySQL change type while trigger before insert

I want MySQL to encrypt data with AES automatically, so that I don't have do do it in the application layer. First question: Is this even possible?
I tried that simple approach:
Table:
measurement
id INT
value VARBINARY(50)
Trigger:
CREATE DEFINER = CURRENT_USER TRIGGER `openeHealth`.`measurement_BEFORE_INSERT` BEFORE INSERT ON `measurement` FOR EACH ROW
BEGIN
SET ##session.block_encryption_mode = 'aes-256-ecb';
DECLARE vKey = RANDOM_BYTES(256);
SET NEW.value HEX(AES_ENCRYPT(value, vKey));
END
It's my first try to use triggers so maybe it's a simple failure or maybe MySQL doesn't support something like this.
My testinsert has a Double value as 'value'.
And yes, I am aware of the fact that I need to store my random key somewhere. So maybe someone has an idea how to update another table due "insert before" to?
Thanks a lot
Error Message:
ERROR: Error 1064: 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 '.., 1)' at line 1
SQL Code:
INSERT INTO `openeHealth`.`measurement` (`id`, `owner_id`, `type_id`, `value`, `device_id`) VALUES (1, 1, 1, ..., 1)
Generated Code of MySQL Workbench:
START TRANSACTION;
USE `openeHealth`;
INSERT INTO `openeHealth`.`measurement` (`id`, `owner_id`, `type_id`, `value`, `device_id`) VALUES (1, 1, 1, ..., 1);
COMMIT;
14.6.3 DECLARE Syntax
...
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
...
Try:
mysql> DROP TABLE IF EXISTS `measurement`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `measurement`(
-> `id` INT,
-> `value` VARBINARY(50)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE DEFINER=CURRENT_USER TRIGGER `measurement_BEFORE_INSERT` BEFORE INSERT ON `measurement`
-> FOR EACH ROW
-> BEGIN
-> DECLARE `SESSION_block_encryption_mode` VARCHAR(33) DEFAULT ##SESSION.`block_encryption_mode`;
-> -- SET ##session.block_encryption_mode = 'aes-256-ecb';
-> -- DECLARE vKey = RANDOM_BYTES(256);
->
-> SET ##SESSION.`block_encryption_mode` := 'aes-256-ecb';
->
-> -- SET NEW.value HEX(AES_ENCRYPT(value, vKey));
-> SET NEW.`value` := HEX(AES_ENCRYPT(NEW.`value`, RANDOM_BYTES(256)));
-> SET ##SESSION.`block_encryption_mode` := `SESSION_block_encryption_mode`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> INSERT INTO `measurement`
-> (`id`, `value`)
-> VALUES
-> (1, 'myKey');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> `id`,
-> `value`
-> FROM
-> `measurement`;
+------+----------------------------------+
| id | value |
+------+----------------------------------+
| 1 | 10293FC4F42FC7BAAA91C94EFF004315 |
+------+----------------------------------+
1 row in set (0.00 sec)

Split a string and loop through values in MySQL stored procedure

I got a situation where I have to pass a comma separated string to MySQL stored procedure and split that string and insert those values as rows in to a table.
For example if I passed 'jhon,swetha,sitha' string to MySQL stored procedure then it have to split that string by comma and insert those values as 3 records into a table.
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
But this does not work. Any solutions please?
You'll need to be a little more careful with your string manipulation. You can't use REPLACE() for this, because that will replace multiple occurrences, corrupting your data if one element in the comma-separated list is a substring of another element. The INSERT() string function is better for this, not to be confused with the INSERT statement used for inserting into a table.
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
Next, a table for testing:
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The new table is empty.
mysql> SELECT * FROM t1;
Empty set (0.00 sec)
Call the procedure.
mysql> CALL insert_csv('foo,bar,buzz,fizz');
Query OK, 1 row affected (0.00 sec)
Note the "1 row affected" does not mean what you would expect. It refers to the last insert we did. Since we insert one row at a time, if the procedure inserts at least one row, you'll always get a row count of 1; if the procedure inserts nothing, you'll get 0 rows affected.
Did it work?
mysql> SELECT * FROM t1;
+----+------+
| id | c1 |
+----+------+
| 1 | foo |
| 2 | bar |
| 3 | buzz |
| 4 | fizz |
+----+------+
4 rows in set (0.00 sec)

MySQL - Trigger - Before Insert and using the SK (Auto Increment)

I have a simple posts table in MySQL which has a POST_ID as the SK (surrogate key).
Replies to the original post ID are stored in the same table in a PARENT_POST_ID column, but I want to perform the following logic:
BEFORE INSERT (I think ...)
IF a PARENT_POST_ID has not been defined on the INSERT, then default the row value to the newly generated POST_ID (from the auto-int sequence)
IF a PARENT_POST_ID has been defined on the INSERT, then set it to whatever has been passed.
Example
post_id | parent_post_id | date_time | message
12 12 2015-04-14 21:10 A new post (start of a thread)
13 12 2015-04-14 21:12 A reply to the post ID 12
The answer here: https://stackoverflow.com/a/11061766/1266457 looks like it might be what I need to do, although I am not sure what it's doing.
Thanks.
For before insert trigger you can not get the last inserted primary key , the other way of doing it is to get the max value from the table and increment it.
Here is a way to do it
delimiter //
create trigger posts_before_ins before insert on posts
for each row
begin
declare last_id int;
if new.parent_post_id is null then
select max(post_id) into last_id from posts ;
if last_id is null then
set new.parent_post_id = 1 ;
else
set new.parent_post_id = last_id+1 ;
end if ;
end if ;
end ;//
delimiter ;
So the trigger will check if there is no value of parent_post_id in the insert query it will get the max post_id. For the first entry it will be null so we are setting it as 1 i.e. and after that max post_id + 1 after each entry.
Here is a test case of this in mysql
mysql> select * from test ;
Empty set (0.00 sec)
mysql> delimiter //
mysql> create trigger test_is before insert on test
-> for each row
-> begin
-> declare last_id int;
-> if new.parent_id is null then
-> SELECT auto_increment into last_id
-> FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'test'
-> and TABLE_SCHEMA = 'test';
-> set new.parent_id = last_id ;
-> end if ;
-> end ;//
Query OK, 0 rows affected (0.12 sec)
mysql>
mysql> delimiter ;
mysql> insert into test (val) values ('aa');
Query OK, 1 row affected (0.10 sec)
mysql> insert into test (val) values ('bb');
Query OK, 1 row affected (0.04 sec)
mysql> select * from test ;
+---------+-----------+------+
| post_id | parent_id | val |
+---------+-----------+------+
| 1 | 1 | aa |
| 2 | 2 | bb |
+---------+-----------+------+
2 rows in set (0.00 sec)