Skip to content

Instantly share code, notes, and snippets.

@paulocheque
Last active February 27, 2016 16:52
Show Gist options
  • Select an option

  • Save paulocheque/2cf44c2694e4ebec4559 to your computer and use it in GitHub Desktop.

Select an option

Save paulocheque/2cf44c2694e4ebec4559 to your computer and use it in GitHub Desktop.
SQL

Commands

sudo mysqld_safe

Select a random row

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Conditional Insert

INSERT INTO TABLE(col1, col2) SELECT val1, val2 FROM dual where not exists (select * from TABLE where col1 = val1 and col2 = val2);

-- To ignore unique contraint errors..
INSERT IGNORE INTO TABLE....

Insert or Update.

INSERT INTO TABLE (col1) values (val1) ON DUPLICATE KEY UPDATE col1 = val1

Select a random record

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Commands

postgres -D /usr/local/var/postgres

sudo -u paulocheque createuser dev

sudo -u postgres createuser dev

createdb DATABASENAME

Select a random row

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Counter

select nextval('table')
setval('table', value)

Dump/Restore

pg_dump -h localhost -p 5432 DBNAME > db.sql
pg_dump -h localhost -p 5432 DBNAME -U username -W password > db.sql
psql -h localhost DBNAME < db.sql

pg_dump -Fc -h localhost -p 5432 DBNAME > db.bin.dump
pg_restore -h localhost db.bin.dump

pg_restore --no-owner --clean --exit-on-error --disable-triggers --data-only --host localhost --port 5432 --username             "paulocheque" --dbname "DBNAME" --no-password  --verbose db.bin.dump

pg_dump -v -h [hostname] -p 5432 -U [user] -Fc [database] > [flename]
pg_restore -d [database] [filename]

SELECT

SELECT DISTINCT curso FROM aluno;

SELECT curso, count(*)
    FROM aluno
    GROUP BY curso
    HAVING count(*) > 1;

WHERE

WHERE nomeAluno LIKE ?Jos?%?;

WHERE numAluno IN ('22233', '77777777');

WHERE numAluno IN (SELECT numAluno FROM desempenho);

WHERE nota BETWEEN 6.0 AND 10.0;

ORDER BY curso DESC, nomeAluno DESC;

LIMIT 100 OFFSET 10

UPDATE

UPDATE table set column = column + 2

Join

http://stackoverflow.com/questions/406294/left-join-and-left-outer-join-in-sql-server

WHERE Aluno.numAluno = Desempenho.numAluno;

TRUNCATE

TRUNCATE table
TRUNCATE table CASCADE

DELETE

DELETE FROM table1 
where table2Id IN (
select id from table2 
where date >= '2014-08-01 00:00:00' and date <= '2014-09-15 23:59:59'
);


delete from table1 a using table2 b
where a.table2Id = b.id and b.date >= '2014-09-01 00:00:00' and b.date <= '2014-09-01 23:59:59';

Select a random row

SELECT TOP 1 column FROM table
ORDER BY NEWID()
@paulocheque
Copy link
Author

Heroku Postgres:

https://devcenter.heroku.com/articles/heroku-postgres-import-export
heroku pg:backups capture --app APP
curl -o latest.dump heroku pg:backups public-url --app APP
createdb DB
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U paulocheque -d DB latest.dump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment