Useful SQL commands
Below are some additional SQL commands that can give you some info that are not possible from inside Open Enventory
There are a lot more SQL commands in the following section:

CHANGING INFO OF MULTIPLE CHEMICALS

Install phpMyAdmin (See OE Settings)

To return all chemicals borrow by somebody (borrowed_by_person_id)

    1.
    Look up person_id by going into [Database]/person
    2.
    Show chemical borrow by that person (i.e. a person with person_id=8)
1
SELECT * FROM chemical_storage WHERE borrowed_by_person_id=8
Copied!
3. Return by:
1
UPDATE chemical_storage SET borrowed_by_person_id=NULL WHERE borrowed_by_person_id=8
Copied!

To change the owner info of a chemical (owner_person_id)

    1.
    Look up person_id by going into [Database]/person
    2.
    Show chemical owned by that person (i.e. a person with person_id=7)
1
SELECT * FROM chemical_storage WHERE owner_person_id=7
Copied!
3. Change, in this case, to no owner (NULL)
1
UPDATE chemical_storage SET owner_person_id=NULL WHERE owner_person_id=7
Copied!

Find person_id using Console command line in PHPMyAdmin

In the following example, show person_id of user with username=julia
1
SELECT person_id FROM person WHERE username='julia'
Copied!

Find storage ID using Console Command line in PHPMyAdmin

    1.
    First choose the desired database on the tree folder from the left
    2.
    Use:
1
SELECT storage_id FROM storage WHERE storage_name LIKE 'FH-2206%'
Copied!
Explain:
    LIKE 'FH-2206%' : show the storage_id of the storage location with name starting with FH-2206
    LIKE '%FH-2206%' would mean find anything with storage name containing FH-2206
    LIKE 'FH-2206%' would mean find anything with storage name ending with FH-2206

Show chemicals with multiple criteria

1
SELECT * FROM chemical_storage
2
WHERE owner_person_id=15 AND storage_id NOT LIKE 18
Copied!
Explain: the above command is used to
    Select all of the chemical in the database that match: owner_person_id=15 AND with storage_id is NOT 18

Change Responsible person for chemicals with multiple criteria

1
UPDATE chemical_storage
2
SET owner_person_id='NULL'
3
WHERE owner_person_id=15 AND storage_id NOT LIKE 18
Copied!
Explain: see above for explanation of the criteria.

Show storage_name and storage_id using username

1
SELECT storage_name,storage_id
2
FROM storage
3
WHERE storage_id IN
4
(SELECT storage_id
5
FROM chemical_storage
6
WHERE owner_person_id in
7
(SELECT person_id FROM person WHERE username='mingzhao')
8
)
9
GROUP BY storage_name
Copied!
Explain:
GROUP BY storage_name : is added so that duplicates are only shown once

Update responsible person for chemicals in many location (storage)

1
UPDATE chemical_storage
2
SET owner_person_id=null
3
WHERE owner_person_id=13 AND storage_id IN (19,7,5,4,12,8)
Copied!
Explain: the command above change responsible people to no one for all of the chemicals being owned by person_id=13 AND in all of the storage_id listed

Search for barcodes that are duplicated

The below command will search for all the barcode that is assigned to more than 1 container (chemical_storage). This search ignores disposed container as well as container without barcodes (chemical_storage_disabled IS NULL AND chemical_storage_barcode != "")
1
SELECT chemical_storage_barcode
2
FROM chemical_storage
3
WHERE chemical_storage_disabled IS NULL AND chemical_storage_barcode != ""
4
GROUP BY chemical_storage_barcode
5
HAVING Count(*) > 1;
Copied!

Update/Get extra info for containers with duplicated barcode (continued from “Search for barcodes that are duplicated”)

To select info from the list of duplicated barcodes. In this case, created by a specific user and created after some specific timestamp
1
SELECT molecule_id,
2
chemical_storage_id,
3
chemical_storage_created_when,
4
chemical_storage_created_by,
5
chemical_storage_barcode
6
FROM chemical_storage
7
WHERE chemical_storage_barcode IN
8
(SELECT chemical_storage_barcode
9
FROM chemical_storage
10
WHERE chemical_storage_disabled IS NULL AND chemical_storage_barcode != ""
11
GROUP BY chemical_storage_barcode
12
HAVING Count(*) > 1
13
) AND
14
chemical_storage_created_by = "user1" AND
15
chemical_storage_created_when > '2019-08-16 18:00:00'
16
ORDER BY chemical_storage_barcode;
Copied!

REMOVE ALL CONTAINERS (BOTTLES) OF CHEMICALS

WARNING!!! BE VERY CAREFUL BEFORE DOING THIS!
This will remove all of the containers, including info about manufactures, order date, barcode, owner, location, etc. However, info about molecule (MW, structures, safety, etc) and database related such as users will be reserved.
This is particularly helpful when you need to update a large amount of chemicals via text import.
    1.
    Login into your server via ssh
    2.
    Login into myswl using root account or user account with administrator priviledge for the database that you want to change. Replace “root” with the username if desired
1
mysql –u root -p
Copied!
3. Show database and then choose the database of interest. test is the name of the database in this example
1
show databases;
2
use test;
Copied!
4. Remove the chemical container by running the following command
1
truncate table chemical_storage;
2
truncate table cache;
3
truncate table change_notify;
Copied!
5. (Optional): Run the command below if you want to reset the id counter (chemical_storage_id as well as autogenerated barcode)
1
altertable chemical_storage auto_increment=1
Copied!
6. Log out of mysql, restart httpd and mariadb services
1
exit
Copied!
1
systemctl restart httpd mariadb
Copied!
7. Go back to OE website and import new tab-separated text file
Update (Oct 11, 2019): new function to delete multiple containers based on barcode has been added into Settings section. Only users with admin permission would see this option.

Find duplicated custom barcodes using SQL

This should be run on the database of interest, table chemical_storage
1
SELECT chemical_storage_id, chemical_storage_barcode, count(chemical_storage_barcode) as NumOccurrences
2
from `chemical_storage`
3
WHERE chemical_storage_disabled is NULL
4
GROUP BY chemical_storage_barcode
5
HAVING (COUNT(chemical_storage_barcode) > 1)
Copied!
    chemical_storage_disabled is NULL : to ignored disposed containers

Last modified 6mo ago