Показаны сообщения с ярлыком SQL. Показать все сообщения
Показаны сообщения с ярлыком SQL. Показать все сообщения

вторник, 20 августа 2013 г.

среда, 9 ноября 2011 г.

PostgreSQL. How Disable All Constraints

Sometimes shit happens.
In my situation it was two plain-text SQL files. One of them contains DB schema and second with data.
I successfully create new DB and create DB schema using command:
psql -U my_user my_db < db_schema.sql
but the next steps gave many errors.
pg_restore didn’t want to read this format and psql didn’t want insert data because constraints were already turn on.
I did not find correct way for turn off all constraints in PosgressSQL. I found information about how disable triggers for specific table but not for all:
http://www.postgresql.org/docs/8.1/static/sql-altertable.html
After some experiments I found the next way which works for PostgreSQL v8.3.3:
Disable all triggers(run this sql command):
update pg_trigger set tgenabled='D' ;
Insert data:
psql -U my_user my_db < db_data.sql
Enable all triggers:
update pg_trigger set tgenabled='O' ;

понедельник, 5 января 2009 г.

MySQL Создание и удаление БД

Для создания БД в MySql необходимо:
  1. создать пользователя
  2. создать БД
  3. раздать ему права на пользование базой
Итак по порядку.
  1. Создаем пользователя:
    CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';
  2. Создаем БД:
    CREATE DATABASE IF NOT EXISTS mydb;
  3. Раздаем ему права:
    выборочно:
    GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user1'@'localhost';
    все права:
    GRANT ALL ON *.* TO 'user1'@'localhost';
    можно вообще раздать права с автоматическим созданием пользователя:
    GRANT ALL ON *.* TO 'user2'@'localhost' IDENTIFIED BY 'pass1';
    Раздавать права можно не только на пользование всеми базами но и на пользовыание только определенной. Едля етого следует указать имя БД. Например:
    GRANT ALL PRIVILEGES ON mydb . * TO 'user1'@'localhost';

Удаление пользователя:
DROP USER 'user1'@'localhost';

Удаление БД:
DROP DATABASE mydb