Professional Documents
Culture Documents
• 1970 – Birth of the relational databases. Main ideas: split the data in tables, creation of primary keys,
relations between tables, and queries to be executed in the information structure.
• Mathematics concepts were used to define databases
• New concepts were born: table, keys, queries, etc..
• The databases where searchable (yes! before they were not!!)
• SQL language appeared.
• Pointers to disc address were not used any more.
• Query optimization.
• Not so big players.
Technologies
• User status in online games
• Series of telemetry data
• etc.. •
•
Relational databases
NoSQL databases
• Numerical Series databases
• In memory databases
• etc...
Market of databases
We will focus on:
Relational databases
• Manage the relation of data stored
• Widely used in market
• High level of standardization
• VirtualBox http://www.virtualbox.org
• Install Virtualbox .
• And the extension pack also.
• A virtual machine with the distribution of Kubuntu with all the software and
databases ready to use. See https://cv.uab.cat
• You have to downloand and import the OVA to your virtualbox environment.
DBMS ACID Model
ACID model of Databases provide
strong consistency view
• Elements of a database schema: pet_id pet_name Pet_type Pet_owner Date_of_bir Vaccinated Keeper
th
• Tables: Set of records that all of them have the 1 Kily Cat Ms Jones 8/5/2016 Yes Mr Sanchez
same “columns”
• Record: Set of values (one per column) that have 2 Boot Dog Mr Martins 18/7/2006 Yes Ms Smith
a relation together.
• Columns or fields: Set of pieces of information, 3 Marumba Elephant Ms Jones 9/4/1999 Yes Mr. Malone
• From another table you can identify a register by its key. That 4 Mr Cooper 896446877
reference is called “Foreign key”.
Index
Stored procedures
• Are programs designed to be run on the DBMS.
• As the DBMS knows it, it can introduce optimization structures internally in the database.
• They can be launched by the users (persons or programs).
• Are written in a more or less standard language.
Entity-relationship diagram
In order to represent graphically the
database there is a standardized way of
representing it, it is the Entity-Relationship
diagram.
• Tables are represented with all their
fields
• The relations between tables are
painted too (references, indexes, etc..)
SQL
SQL = Standard Query Language
• SQL is a language designed to interact with DBMS and it has become a standard.
• The differences between the implementation that different DBMS make of SQL is small, but there is.
• There are rules and design principles to 2 Boot Dog Mr Martins 18/7/2006 Yes Ms Smith
help us in the design of the database. 3 Marumba Elephant Ms Jones 9/4/1999 Yes Mr. Malone
• The structure of the database should be 5 Wally Dogh Mr Cooper 7/12/2019 Yes Ms Smith
“normalized”
But still:
• “DB2” could be misspelled in some cases…
• Changing a database name is dangerous if
all names are repeated.
2NF – Eliminate redundant data
• The Database table still contains repetitions of data.
See that Oracle, Access, and DB2 are repeated.
• The same concept “Oracle” f.ex. has two
DatabaseIDs… it is not a representation of reality
• The primary key of the table is DID
• Te table represents concepts (Databases) and
relations..
We should:
• Have a table with concepts (Databases)
• Have a table with relations (User – Database)
• The table that stablishes the relations will have as
primary Key (Member ID, Database ID).
3NF – Eliminate columns that do not depend on
keys
In this case the member table mixes information
about the member and the company.
1 1 1
users_software 2 2 2 users_books
MID SID 2 3 3 MID BID
1 1 3 1 N/A 1 1
2 2 4 N/A 4 2 2
2 3 2 3
3 1 4 4
5NF – Isolate semantically related multiple
relationships
• Now in the previous example we will add
the information about what software is
covered in each book.
software books
• This relation is independent of any other
user
MID name SID Software_name BID book_title
previous relation 1 Bill 1 ERWin 1 ERWin Bible
1 1 1 1 1 1
2 2 2 2 2 2
2 3 3 3 2 3
3 1 4 4 4 4
Exercise: Create a DB model.
Martian Empire is a company that exploits tourism in Mars.
On the other hand, now we are only admitting Martian people, because
they are kinder than Earthers, but we have created a marketing
database to store Earthers that may be interested in coming to Mars
and providing us more wealth for standing them.
SQL
Now that we know what do we want to to, let’s see how to do it!!
The types supported by PostgreSQL includes the INT 4 bytes Typical integer -2147483648 to
2149483647
following. BIGINT 8 bytes Long range integer -9223372036854775808 to
+9223372036854775807
It is just a subset, also include arrays, geographical
information, etc.. DECIMAL Variabl
e
User-specified precision exact Up to 131072 digits before
decimal point and 16383
after decimal point
Full info at DOUBLE 8 bytes Variable precision inexact 15 decimal digits precision
TIMESTAMP 8 bytes
);
<field definition> REFERENCES <table_name> (<fields>), salary int check (salary> 0),
for example:
https://www.postgresql.org/docs/13/ddl-constraints.html
Table removal and alteration
To remove a table:
Drop table <table_name>;
To alter a table:
alter table <table_name> add column <column_name> <type> <constrict>;
alter table <table_name> drop column <column_name>;
alter table <table_name> add check (<constraint>);
alter table <table_name> add constraint <constraint_name> UNIQUE (<column_name>);
alter table <table_name> add foreign key <column_name> references <table_name>(<column_name>);
https://www.postgresql.org/docs/9.5/ddl-alter.html
Exercise
Create a script that creates the database structure that you have
previously defined.
Next session:
• Have installed VirtualBox and the VM.
• We will talk about SQL + PostgreSQL + Dbeaver