Relational Databases : Querying databases
INSERT
, SELECT
, UPDATE
, DELETE
CREATE
verb, and modify any
structural information with ALTER
. Now we will act on data
INSERT
statement
INSERT into facilities(facid, name, membercost, guestcost, initialoutlay, monthlymaintenance)
values (9, 'Swimming Pool', 8, 16, 3000,3000)
UPDATE
statement
UPDATE facilities SET membercost=10 WHERE name='Swimming Pool';
SET
operator that targets a column, and replace the old valueUPDATE
query, always do a select to make like a backup of data before the update
DELETE
, as shown below
DELETE from facilities WHERE name='Swimming Pool';
DELETE
query, always do a select to make like a backup of data before the update
SELECT
verb in combination with FROM
operatorFROM
WHERE
clause=
, <
,>
, <>
,
AND
, OR
) to combine several conditions
select * from bookings;
show search_path ;
set search_path = "$user", public, "cd";
SELECT * FROM facilities
WHERE monthlymaintenance < 1000;
JOIN
operator, for example
SELECT * FROM facilities
JOIN bookings on bookings.facid = facilities.facid
WHERE monthlymaintenance < 15;
import psycopg2
connection = psycopg2.connect(user = "postgres",
password = "postgres",
host = "192.168.137.50",
port = "10532",
database = "postgres_db")
const { Client } = require('pg');
const client = new Client({
user: 'postgres',
host: '192.168.137.50',
database: 'postgres_db',
password: 'postgres',
port: 10532,
});
client.connect()
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
public class Launcher {
public static void main(String args[]) {
Class.forName("org.postgresql.Driver");
try (Connection c = DriverManager
.getConnection("jdbc:postgresql://192.168.137.50:10432/postgres_db",
"postgres", "postgres");){
System.out.println("connected to schema : " +connection.getSchema());
} catch (SQLException e) {
e.printStackTrace(); //TODO use a logger
}
}
}