Skip to content

Instantly share code, notes, and snippets.

Эта версия статьи устарела. Новая версия статьи перенесена по адресу: https://github.com/codedokode/pasta/blob/master/db/trees.md


Как хранить в БД древовидные структуры

Те, кто знают английский, могут сразу перейти сюда: http://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database

Древовидные структуры - это такие структуры, где есть родители и дети, например, каталог товаров:

@monaxmp
monaxmp / postgres_add_json_sub_array_unique.sql
Created July 27, 2019 12:43 — forked from mpneuried/postgres_add_json_sub_array_unique.sql
Add and remove elements unique to a Postgres jsonb sub key: Short a Set implemetation
UPDATE public.mytable SET
jsonfieldname = jsonb_set( jsonfieldname, '{json_obj_key}', array_to_json(
ARRAY(
SELECT DISTINCT( UNNEST( ARRAY(
SELECT json_array_elements_text( COALESCE( jsonfieldname::json->'json_obj_key', '[]' ) )
) || ARRAY['Element to add'] ) )
)
)::jsonb )
WHERE id = 23
RETURNING *;
@monaxmp
monaxmp / mysql-docker.sh
Created January 29, 2019 17:02 — forked from spalladino/mysql-docker.sh
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@monaxmp
monaxmp / install-docker.sh
Created January 29, 2019 13:17 — forked from dweldon/install-docker.sh
Install docker CE on Linux Mint 18.3
#!/usr/bin/env bash
# https://docs.docker.com/install/linux/docker-ce/ubuntu/
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
sudo apt-get update
sudo apt-get install docker-ce
# https://docs.docker.com/compose/install/
@monaxmp
monaxmp / phpstorm_xdebug.txt
Created September 17, 2018 12:42 — forked from VeryaskinMax/phpstorm_xdebug.txt
Phpstorm xDebug settings
1. в файле /etc/php/x.x/mods-available/xdebug.ini прописываем настройки:
zend_extension=xdebug.so
xdebug.show_error_trace = 1
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
1.1 делаем sudo service apache2 restart если надо
@monaxmp
monaxmp / postgres find dublicat
Last active June 18, 2018 15:50
postgres find dublicat
select user_id, profession_id, count(*)
from professions
group by user_id, profession_id
HAVING count(*) > 1;
@monaxmp
monaxmp / phpdismod xdebug
Last active June 18, 2018 15:51
phpdismod xdebug
sudo phpdismod xdebug
sudo phpenmod xdebug
@monaxmp
monaxmp / git command
Last active June 18, 2018 15:52
git command
------------------------------------------------------------------------------------------------------------
https://webhamster.ru/mytetrashare/index/mtb0/1413010541hzh3175lej
git checkout HEAD@{1}
-------------------------------
git branch temp
git checkout temp
git branch -f master temp
git checkout master
git branch -d temp
@monaxmp
monaxmp / postgres
Last active July 21, 2018 19:08
postgres
sudo -u postgres psql -c "DROP DATABASE art" && sudo -u postgres psql -c "CREATE DATABASE art" && psql art < 2018
sudo -u postgres psql -c "DROP DATABASE art"
sudo -u postgres psql -c "CREATE DATABASE art"
psql artdb < 2018-03-01_15-30-backup-artdb.sql
@monaxmp
monaxmp / git quickly start
Last active January 12, 2024 10:14
git quickly start
git config --global user.name "YourFullName"
git config --global user.email [email protected]
//------------------------------------------------------------------------------------
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/you_repository/you_project
git push -u origin master
git push -f origin master
//------------------------------------------OR-----------------------------------------