Database 1: SQL Databases with DBeaver, SQL Scripts, and the Node-Postgres Module

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.

Deaver database startup for the SQL scriopt

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.

chevron-rightReference keys between tableshashtag

To avoid repeating data between related tables we reference keys (also called foreign keys).

We set a data type (most of the time ID int) and reference another table property, the referenced key will contain the entire table row.

To insert values in a table with referenced keys we:

Reference keys on different tables

To add a row we Insert into table_name() Values().

chevron-rightDecimal values on SQL tables keyshashtag

SQL tables can store decimal numbers using data types like FLOAT, NUMERIC, and DECIMAL.

NUMERIC and DECIMAL store exact decimal values using the (precision, scale) arguments. Precision defines the max number of digits , while scale defines the number of digits after the decimal point. The maximum representable value is related to 10^(precision - scale).

FLOAT stores decimals values of any length, but the stored value will be approximation in order to save storage data.

For logical operations use '=' instead of '=='.

We use select from to see Tables' data, and we can add logical operators.

chevron-rightMore Select options and matching pattern table %strings%hashtag

ORDER the table rows based on columns.

We Limit the number of rows selected or their key values.

We select query elements by pattern matching using like %.

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.

chevron-rightMore Alter columns SQL scriptshashtag

Alter table can include more table operations, like drop column.

Rename to change the name of the table.

Rename column to change a column's name.

We can "drop" the not null constraint from keys and change their data type.

You can return the alter rows by adding 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.

DROP and DELETE to remove tables and rows respectively:

1

Last updated