Say I have the following table
item_a_id, item_b_id, value
Where item_a_id and item_b_id are a composite primary key. In my example a,b and b,a are equivalent. Therefore I want to ensure that item_a_id < item_b_id. Obviously the application logic will enforce this but is there a way to ensure the database does too?
In a reasonably current version of MySql you can use triggers to emulate a check constraint that produces the desired behavior.
Well, in your case, you could use trigger to check values before insert/update and swap it to ensure item_a_id will always less than item_b_id.
Assuming the table name is item_links, you could try this:
DELIMITER |
CREATE TRIGGER ensure_a_b_before_insert BEFORE INSERT ON item_links
FOR EACH ROW
BEGIN
IF NEW.item_a_id > NEW.item_b_id THEN
SET #tmp = NEW.item_b_id;
SET NEW.item_b_id = NEW.item_a_id;
SET NEW.item_a_id = #tmp;
END IF;
END;
|
CREATE TRIGGER ensure_a_b_before_update BEFORE UPDATE ON item_links
FOR EACH ROW
BEGIN
IF NEW.item_a_id > NEW.item_b_id THEN
SET #tmp = NEW.item_b_id;
SET NEW.item_b_id = NEW.item_a_id;
SET NEW.item_a_id = #tmp;
END IF;
END;
|
DELIMITER ;
Here's what I got when I test inserting:
mysql> INSERT INTO `item_links` (`item_a_id`, `item_b_id`, `value`)
-> VALUES ('1', '2', 'a')
-> , ('3', '2', 'b')
-> , ('4', '1', 'c');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM `item_links`;
+-----------+-----------+-------+
| item_a_id | item_b_id | value |
+-----------+-----------+-------+
| 1 | 2 | a |
| 2 | 3 | b |
| 1 | 4 | c |
+-----------+-----------+-------+
3 rows in set (0.00 sec)
Update works, too:
mysql> UPDATE `item_links`
-> SET `item_a_id` = 100, `item_b_id` = 20
-> WHERE `item_a_id` = 1 AND `item_b_id` = 2;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM `item_links`;
+-----------+-----------+-------+
| item_a_id | item_b_id | value |
+-----------+-----------+-------+
| 20 | 100 | a |
| 2 | 3 | b |
| 1 | 4 | c |
+-----------+-----------+-------+
3 rows in set (0.00 sec)
Related
I created a column called oilcompany that has SET data (Hunt, Pioneer, Chevron, BP)
I can enter any one of those into the oilcompany column and change from one to another one but I can not figure out how to change from one oilcompany to multiple oilcompany (eg. Hunt and BP)... any suggestion?
In the MySQL documentation there are not examples for UPDATE statements, but I normally use two ways to update these kind of columns:
Using text values
Using numeric values
Creating the test environment
mysql> CREATE TABLE tmp_table(
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> oilcompany SET('Hunt', 'Pioneer', 'Chevron', 'BP')
-> );
Query OK, 0 rows affected (0.54 sec)
mysql> INSERT INTO tmp_table(oilcompany) VALUES ('Hunt'), ('Pioneer');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#1: Using Text Values
As a SET is a collection of ENUM elements, and any ENUM element can be treated as a string, then we can do things like:
mysql> UPDATE tmp_table
-> SET oilcompany = 'Hunt,BP'
-> WHERE id = 1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt,BP |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#2: Using Numeric Values
Any SET element is stored internally as a 64bit number containing the combination of the bits that represent each SET element.
In our table: 'Hunt'=1, 'Pioneer'=2, 'Chevron'=4, 'BP'=8.
Also, mysql allows to use these numbers instead of text values. If we need to see the numeric value in the select, we need to use the SET column inside a numeric expression (E.g. adding zero).
Let's see the current values:
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+------------+
| 1 | 9 | Hunt,BP |
| 2 | 2 | Pioneer |
+----+--------------+------------+
2 rows in set (0.00 sec)
Here 9 = 'Hunt' (1) + 'BP' (8) and 2 = 'Pioneer' (2).
Now, let's change the Pioneer to 'Hunt' (1) + 'Chevron' (4):
mysql> UPDATE tmp_table
-> SET oilcompany = 5
-> WHERE id = 2;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+--------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+--------------+
| 1 | 9 | Hunt,BP |
| 2 | 5 | Hunt,Chevron |
+----+--------------+--------------+
2 rows in set (0.00 sec)
This is my trigger.
DELIMITER //
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare
FOR EACH ROW
BEGIN
IF (NEW.Data_Rezervarii=Data_Rezervarii) AND (NEW.NumarMasa=NumarMasa) THEN
SET NEW.NumarMasa= NULL;
END IF;
END //
DELIMITER ;
I want to make trigger on 1 table (rezervare).
When I execute the trigger, it has been created. But, when I insert data into table rezervare, It become
Error Code: 1054. Unknown column 'Data_Rezervarii' in 'field list'
I want to check if a reservation is already in the data base for that date
and mass required is already reserved for that date
From your attempt, it appears that you want to still insert a new row with the same Data_Rezervarii, but with NumarMasa NULL. If so, your trigger should be something like
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare FOR EACH ROW
SET NEW.NumarMasa = IF(EXISTS(
SELECT 1 FROM Rezervare
WHERE Data_Rezervarii=NEW.Data_Rezervarii
AND NumarMasa = NEW.NumarMasa
),NULL,NEW.NumarMasa);
Then it would work like this:
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
+-----------------+-----------+
3 rows in set (0.00 sec)
MariaDB [test]> INSERT INTO Rezervare VALUES ('2016-12-12',1),('2016-12-12',4);
Query OK, 2 rows affected (0.20 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
| 2016-12-12 | NULL |
| 2016-12-12 | 4 |
+-----------------+-----------+
5 rows in set (0.00 sec)
But if you actually want to skip the new record completely if one already exists in the table, it should be done by adding a unique index on these two columns and using INSERT IGNORE.
How can I use a trigger that accesses two databases?
This is what I tried:
CREATE DEFINER=`root`#`localhost` TRIGGER `db1`.`library_AFTER_UPDATE`
AFTER UPDATE ON `library` FOR EACH ROW
begin
insert into `db2`.email(account_code,account_name,admin_first_name,admin_email,last_updated_date,updated_by) values('146','Abcds','SSSSSSSS','jnkjk#gmli',now(),'anupam');
end
Here is a simple example on after Update trigger. I am using 2 databases test and test2. This example can help you-
use test
create table test.foo (a INT, b INT, ts TIMESTAMP);
create table test2.bar (a INT, b INT);
INSERT INTO test.foo (a,b) VALUES(1,1);
INSERT INTO test.foo (a,b) VALUES(2,2);
INSERT INTO test.foo (a,b) VALUES(3,3);
DELIMITER ///
CREATE TRIGGER ins_sum AFTER UPDATE ON foo
FOR EACH ROW
BEGIN
INSERT INTO test2.bar (a, b) VALUES(NEW.a, NEW.b);
END;
///
DELIMITER ;
select * from test.foo;
+------+------+---------------------+
| a | b | ts |
+------+------+---------------------+
| 1 | 1 | 2011-06-14 09:29:46 |
| 2 | 2 | 2011-06-14 09:29:46 |
| 3 | 3 | 2011-06-14 09:29:46 |
+------+------+---------------------+
3 rows in set (0.00 sec)
-- UPDATE without change
UPDATE test.foo SET b = 3 WHERE a = 3;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
select * from test2.bar;
+------+------+
| a | b |
+------+------+
| 3 | 3 |
+------+------+
1 row in set (0.00 sec)
In MySQL I have some tables I need to randomize the phone numbers and Email addresses to be randomly generated for development purposes.
In MySQL how could I generate 7 digit unique random numbers for the phone numbers?
How can I generate random email address like 545165498#mailinator.com.
How can I generate this random data with MySQL Queries?
MySQL rand() Returns a random floating-point value in the range 0 <= value < 1.0.
Multiply that by another number: UPPER_BOUND and get the floor of that, and you will get a random integer between 0 and (UPPER_BOUND-1) like this:
SELECT floor(rand() * 10) as randNum;
That will give you only one random number between 0 and 10.
Change the 10 to the number one higher than you want to generate.
Something like this :
UPDATE user
SET email = CONCAT(FLOOR(rand() * 10000000),'#mailinator.com'),
PhoneNo = FLOOR(rand() * 10000000)
This should give you a random number of 7 digits length
SELECT FLOOR(1000000 + RAND() * 8999999)
And something like this should update your phone numbers and e-mail addresses according to your requirement
UPDATE Customers
SET phone = CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR),
email = CONCAT(CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR), '#mailinator.com')
MySQL Generate random data walkthrough:
Random number between 0 (inclusive) and 1 exclusive:
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5485130739850114 |
+--------------------+
1 row in set (0.00 sec)
Random int between 0 (inclusive) and 10 exclusive:
mysql> select floor(rand()*10);
+------------------+
| floor(rand()*10) |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
Random letter or number:
mysql> select concat(substring('ABCDEF012345', rand()*36+1, 1));
+---------------------------------------------------------------------------+
| concat(substring('ABCDEF012345', rand()*36+1, 1)) |
+---------------------------------------------------------------------------+
| F |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
Random letter a to z:
mysql> select char(round(rand()*25)+97);
+---------------------------+
| char(round(rand()*25)+97) |
+---------------------------+
| s |
+---------------------------+
1 row in set (0.00 sec)
Random 8 character alphanumeric string:
mysql> SELECT LEFT(UUID(), 8);
+-----------------+
| LEFT(UUID(), 8) |
+-----------------+
| c26117af |
+-----------------+
1 row in set (0.00 sec)
Random capital letter in MySQL:
mysql> select CHAR( FLOOR(65 + (RAND() * 25)));
+----------------------------------+
| CHAR( FLOOR(65 + (RAND() * 25))) |
+----------------------------------+
| B |
+----------------------------------+
1 row in set (0.00 sec)
Load a random row into a table:
mysql> create table penguin (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into penguin values (0, LEFT(UUID(), 8));
Query OK, 1 row affected (0.00 sec)
mysql> select * from penguin;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
+------+----------+
1 row in set (0.00 sec)
Load random rows:
Make a procedure called dennis that loads 1000 random rows into penguin.
mysql> delimiter ;;
mysql> drop procedure if exists dennis;;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure dennis()
-> begin
-> DECLARE int_val INT DEFAULT 0;
-> myloop : LOOP
-> if (int_val = 1000) THEN
-> LEAVE myloop;
-> end if;
-> insert into penguin values (0, LEFT(UUID(), 8));
-> set int_val = int_val +1;
-> end loop;
-> end;;
Query OK, 0 rows affected (0.00 sec)
mysql> call dennis();;
mysql> select * from penguin;;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
| 1 | c5dc08ee |
| 2 | c5dca476 |
...
+------+----------+
Update all rows in a table to have random data:
mysql> create table foo (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into foo values (0,'hi');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi2');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi3');
Query OK, 1 row affected (0.00 sec)
mysql> select * from foo;
+----+------+
| id | msg |
+----+------+
| 1 | hi |
| 2 | hi2 |
| 3 | hi3 |
+----+------+
3 rows in set (0.00 sec)
mysql> update foo set msg = rand();
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from foo;
+----+---------------------+
| id | msg |
+----+---------------------+
| 1 | 0.42576668451145916 |
| 2 | 0.6385560879842901 |
| 3 | 0.9154804171207178 |
+----+---------------------+
3 rows in set (0.00 sec)
Here is an online tool to generate random data with many options. http://www.generatedata.com/
Just enter the parameters to define what kind of random data you want, and export it to the appropriate format, then you can load it.
I have the following table
id name address empid
1 AA aa 0
2 BB bb 0
3 CC cc 0
I need to write a query to set empid starting from 1. How to write it please. Do i have to use a stored procedure to that or can do it with a normal query?
Thank You.
Here is a way to do it that utilizes a pretty obscure assignment operator in MySQL. This solution won't skip numbers in the case of gaps in the primary key sequence like some of the other solutions.
set #count = 0;
update test set empid = #count := #count+1;
Here is the proof:
mysql> create table test (
-> id int unsigned primary key auto_increment,
-> name varchar(32) not null,
-> address varchar(32) not null,
-> empid int unsigned not null default 0
-> ) engine=innodb;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test (name, address)
-> values ('AA', 'aa'), ('BB', 'bb'), ('CC', 'cc');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from test;
+----+------+---------+-------+
| id | name | address | empid |
+----+------+---------+-------+
| 1 | AA | aa | 0 |
| 2 | BB | bb | 0 |
| 3 | CC | cc | 0 |
+----+------+---------+-------+
3 rows in set (0.00 sec)
mysql> set #count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set empid = #count := #count+1;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from test;
+----+------+---------+-------+
| id | name | address | empid |
+----+------+---------+-------+
| 1 | AA | aa | 1 |
| 2 | BB | bb | 2 |
| 3 | CC | cc | 3 |
+----+------+---------+-------+
3 rows in set (0.00 sec)
If you are looking to put 1 in empid for the first row, 2 for the second, etc. the easiest way would be to use your id field that is already doing this like so:
UPDATE table
SET empid = id
The only thing you need to worry about is missing numbers in the id column. If that would be an issue and you are missing id numbers, you will have to use a different method. To do that, you would need to do something like this:
DECLARE #counter int
SET #counter = 1
UPDATE table
SET #counter = empid = #counter + 1
As #BiggsTRC suggested you can use id to set empid. If not, you can create stored procedure or some PHP code to do that.
If your ID is not "AutoIncrement" field, you can consider a new column as autoincrement field and assign that value to emp with update query and later delete that new column. (These are some alternates, you need to choose the best one)
UPDATE `test` SET `empid`=`id`
But why would you want to do it? It's pretty much definition of redundancy.