User Tools

Site Tools


myhelp:sql

**This is an old revision of the document!**

SQLite

The Standard stuff is pretty easy. There is also a good summeray. Here we go with a quick start to create a DB-file with a table in it and do some stuff.

# sqlite3 
sqlite> .table mydb.db
sqlite> CREATE TABLE IF NOT EXISTS T_names(name TEXT, birthyear INT);
sqlite> INSERT INTO T_names VALUES ('John Smith', 1999);
sqlite> INSERT INTO T_names VALUES ('Jack Doe', 1998);
sqlite> SELECT * FROM T_names;
sqlite> SELECT name FROM T_names WHERE birthyear=1998;
sqlite> UPDATE T_names SET birthyear=1997 WHERE name = 'Jack Doe';
sqlite> SELECT * FROM T_names LIMIT 1;

PostgreSQL

A useful tutorial is here.

Create, drop and list DB

CREATE DATABASE dbname;
DROP DATABASE dbname;
\l #List DBs

Connect to DB

\c dbname #Connect to DB

Select with multiple LIKE and NOT LIKE

SELECT something, anything FROM T_table WHERE something != 'thing' AND anything LIKE '%any%' AND anything NOT LIKE '%stuff%' AND anything NOT LIKE '%other%';
SELECT something, anything FROM T_table WHERE something LIKE ANY (ARRAY['%som%', '%thing', 'any%', 'stuff%'])

Create a new DB and User and change DB-Owner

CREATE DATABASE mynewdb;
CREATE USER myuser WITH PASSWORD 'Aw5s0m4Secur3';
ALTER DATABASE mynewdb OWNER TO myuser;

Copying PostgreSQL database to another server

pg_dump -C mynewdb | psql -h db.server.host -U myuser mynewdb

Original command from [[https://stackoverflow.com/questions/1237725/copying-postgresql-database-to-another-server|stackoverflow].

myhelp/sql.1683623836.txt.gz · Last modified: by ulrich