Database 1: SQL Databases with DBeaver, SQL Scripts, and the Node-Postgres Module
1
A database is a structured set of data, it checks data validity and sets relations between data structures. There are many Database types, relational databases (the most used), key/value, graph, and document databases(called NoSQL).
SQL, Structured Query Language, is the standard language for relational databases, it is used to create, insert, alter, update, and drop database records. SQL scripts are processed by an RDBMS, Relational Database Management System, to manage a relational database, like PostgreSQL.
PostgreSQL, an Object-Relational database system, started in 1986 and extends the SQL language to store and scale databases.
DBeaver is a SQL client software application and a relational database administrative tool, we create PostgreSQL database connections and SQL scripts to create database objects (like tables, etc).
The Postmaster is PostgreSQL's main server process. It initializes new database connections by starting dedicated backend processes called 'postgres' within RAM. These backend processes can execute SQL queries and retrieve data from the database's file system. We can control the postmaster's behavior, including starting and stopping the server, using the pg_ctl utility command.
DBeaver tables and SQL script
On DBeaver we create a connection-> select the PostSQL driver-> open the SQL script file (f3). To find our tables we schemas-> public-> tables.

In a relational database, data is stored in tables of rows and columns.
We CREATE TABLE() and set its column properties name (keys) and data type, not null for mandatory values on user rows. Varchar(n) for strings, int for integers, date for date() objects, and boolean for true/false values. The Serial primary key is a unique identifier for the user rows, it increments after each insert.
Check the composite data type section for insight into PostgreSQL table structure.
//The Varchar(n) is the max string.length(n)
//In SQL you need to select the code and execute it with crtl+enter
CREATE TABLE multi(
id SERIAL primary key,
name VARCHAR(15) not null,
employed boolean,
age int not null,
birthday date not null
)

To add a row we Insert into table_name() Values().
//We use -- to comment in a SQL file, any inserted string needs ''
//to add multiple values use (,)
//Table keys and inserted values need to be the same number
INSERT INTO hotels (name, rooms, postcode)
VALUES
('Triple Point Hotel', 10, 'CM194JS'),
('The 2-star palace', 7, 'AW124T5');
For logical operations use '=' instead of '=='.
We use select from to see Tables' data, and we can add logical operators.
//* selects every column, WHERE used for keys conditions
Select * from customers
select name, email from customers
SELECT * FROM hotels WHERE rooms > 7;
SELECT * FROM bookings WHERE checkin_date > '2019/10/01' AND nights >= 2;
SELECT * FROM hotels WHERE name='Royal Cosmos Hotel' OR rooms > 10;
Alter table and Update are used to modify the table's keys and row properties respectively. When updating any not-included key will use its old values.
//a new date_of_birth column was added to the customer's table,
//it will be empty for every pre-existing row
ALTER TABLE customers ADD COLUMN date_of_birth DATE;
//The night's property on the bookings table will be set at 25
//if id and customer_id are 1
update bookings set nights=25 where id= 1 and customer_id= 1
You can return the alter rows by adding returning:
//It returns all columns of the alter row
update varietas set numba=12311 where name='lory' returning *;
INNER joins multiple table columns, we use (.) to access a table property and add logical operators, we can select columns from multiple tables, as long as we JOIN them.
//We JOIN the customers and bookings columns when their properties are the same
SELECT * FROM customers INNER JOIN bookings ON customers.id = bookings.customer_id;
//A select made of columns from different tables, with condition
//remember to use the FROM table as the second element in any logical operation
SELECT bookings.checkin_date, customers.name, hotels.name FROM bookings
INNER JOIN customers ON customers.id=bookings.customer_id
INNER JOIN hotels ON hotels.id=bookings.hotel_id
WHERE customers.id=1;
DROP and DELETE to remove tables and rows respectively:
//To remove a table
table drop varietas
//delete a row with condition
delete from varietas where id=4;
1
Last updated
Was this helpful?