Search

Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

PosgreSQL: backup with pg_dump

Dump database in bz2 file
pg_dump -U dbuser dbname | bzip2 -c > dumpfilename.bz2

Dump single table (structure and data) in bz2 file
pg_dump -U dbuser -t tablename -v dbname| bzip2 -c > dumpfilename.bz2



Dump single table (structure only) in sql file
pg_dump -s -t tablename db name -U dbuser > dumpfilename.sql

Dump single table (data only) in sql file
pg_dump -a -t tablename dbname -U dbuser > dump.sql

List all insert statement for a table in a sql file
pg_dump --column-inserts -t tablename dbname -U dbuser > dump.sql

Import dump from bz2 file
bunzip2 dumpfilename.bz2 -c | psql dbname 

Import dump from sql file
psql -U postgres mydatabase < dumpfilename.sql

Backup single table
CREATE  TABLE bktablename AS select * from tablename


OrientDB create, populate, connect and query

Create Class (Table)
Examples: 
create class Person extends V 
create class Restaurant extends V

Create Vertex (Populate)
Examples:
create vertex Person set name = 'Luca'
create vertex Person set name = 'Bill'
create vertex Person set name = 'Jay'

create vertex Restaurant set name = 'Dante', type = 'Pizza'
create vertex Restaurant set name = 'Charlie', type = 'French'

Create Edge (Relatio)
Examples:
create class Eat extends E 
create class Friend extends E

Create Connections
Examples: 
create edge Eat from (select from Person where name = 'Luca') to (select from Restaurant where name = 'Dante')

If you know the RID of vertices you can connect them with a shorter and faster command: 
create edge Eat from #11:1 to #12:0
create edge Eat from #11:2 to #12:1 
create edge Friend from #11:0 to #11:2

Cross Edges (Join)
To cross edges we can use special graph functions like:
  • out(), to retrieve the adjacent outgoing vertices
  • in(), to retrieve the adjacent incoming vertices
  • both(), to retrieve the adjacent incoming and outgoing vertices
Examples:
Note: expands() is a special function very useful to transforms a resultset of vertices in a expanded resultset (showing all data).

To know all the people that eat in Dante restaurant we can get Dante's record and then cross back traversing the incoming edges to discover the Person records connected 
select expand( in() ) from Restaurant where name = 'Dante'

In the following example we've passed the Edge's class "Friend" as argument of the both() function to cross only the relationships of kind "Friend" (so skip the "Eat" this time). 
select expand( both('Friend') ) from Person where name = 'Luca' 

Get all the restaurants where go Luca's friends. 
select expand( both('Friend').out('Eat') ) from Person where name = 'Luca'

OrientDB console commands and query syntax

Run console via
[OrientDB_Home]/bin/console.sh

Type the "help" or "?" command to see all available console commands.

All avilable console commands at this url: Tutorial:-Run-the-console



Some usefull console commands and query

Connect to database or a remote server instance
Synopsis: 
connect <url> <user> <password>
Ex:  
connect remote:localhost root someUglyPassword
connect remote:localhost/dbname admin admin

Let's analyze the URL we have used: remote:localhost/demo.
The first part is the protocol, remote in this case, which means to contact the server using the TCP/IP protocol. localhost is the host name or the IP address where the server resides, in this case it is on the same machine. demo is the name of the database with which we want to connect.

List all databases
Synopsis: 
list databases

Database info
Synopsis: 
info

Create a new database
Synopsis: 
create database <database-url> <user> <password> <storage-type> <db-type>
Parameters:
- storage-type (local or memory)
- db-type (document or graph)

Ex:
create database remote:localhost/mytest root admin local graph

Display all classes on database
Synopsis: 
classes

Display all clusters on database
Synopsis: 
clusters

Class info
Synopsis: 
info class <class-name>
Ex:
info class Student

Create a class
Synopsis: 
create class <command-text>
Ex:
create class Student 

Create a class property
Synopsis: 
create property <command-text>
Ex:
create property Student.surname string
create property Student.birthDate date

Alter a class property
Synopsis: 
alter property <command-text>
Ex:
alter property Student.name min 3

Retrieve all records of a class
Synopsis: 
browse class <class-name>
Ex:
browse class OUser

Display  record attributes
Synopsis: 
display record <number>
Parameters:
- number (record number)
Ex:
display record 0

Select a record (OrientDB syntax)
Synopsis: 
load record <record-id>
Ex:
load record #12:4

Select records (SQL like syntax)
Synopsis: 
select <query-text>

Base Ex:
  • select from MyClass (select work on classes by default)
  • select from cluster:MyCluster (peform select on cluster)
  • select from #10:3 oppure select from [#10:3, #10:3, #10:5](select specific records by RID)
  • select from OUser where name like 'l%'
  • select from Employee where city = 'Rome' order by surname asc, name asc
  • select sum(salary) from Employee where age < 40 group by job
  • select from Employee where gender = 'male' limit 20
  • select from Employee where gender = 'male' skip 40 limit 20 (usefull for pagination)
  • select name, phones from student where phone.keys() in ('mobile') (select by the key "mobile" of th asociative map attribute "phone")
Join Ex:
  • Perform JOIN between "city" and "country" tables (classes)
    select * from city where country.name = 'Italy'
  • Select first 3 names of city from records of type "address" for italian (country) address
    select city.name from address where city.country.name = 'Italy' limit 3 
  • Select all account with atleast  one address (Note: "addresses" is a special attribute of type collection in the class (table) "account".  It realizes a 1-N relation).
    select from account where addresses.size() > 0
  • Find accounts with at least one address of Washington.
    Note: "addresses" is a special attribute of type collection in the class (table) "account".  It realizes a 1-N relation.
    Note: FLATTEN() ectrat results from different collections and put all them in a single collection to facilitate data management.
    select flatten(addresses) from account where addresses contains ( city.country.name = 'Washington' ) 
Note: when we perform a select, values inside an array are not shown. By default we can see only a number of element inside the collection.

Insert records (SQL like syntax)
Note:
in OrientDB sql insert syntax, the list of fields is mandatory. The reason is simple: OrientDB is a schema-less DBMS;
is it possible to insert array and associatives mapps values using "[]" and "{}".

Ex:
insert into student (name,surname) values ('Jay','Miner')

Update records (SQL like syntax)

Ex:
UPDATE test SET city = 'Rome' WHERE name = 'Luca'

UPDATE has been extended with new keywords:
  • ADD and PUT, to insert elementin collections as array or map respectively. If the array or mapp does not existit will be created automatically;
  • REMOVE, to remove an element from a collection. Creiamo una nuova collection di string, che rappresentarà il set di tag che assoceremo ai record: 

Add element to array:
UPDATE student ADD tags = 'tutorial'

Add element to associative ma:
UPDATE test put phones = 'mobile', '+393333888' WHERE name = 'Luca'

Note: when we perform a select, values inside an array are not shown. By default we can see only a number of element inside the collection. 

Delete records (SQL like syntax) 

Ex:
DELETE FROM student WHERE tags.size() > 0