Professional Documents
Culture Documents
APSV 20 21 TEMA 11 InformationSystemsUsage EN
APSV 20 21 TEMA 11 InformationSystemsUsage EN
UPM
Information Systems
~ Database Use~
Jose M. del Alamo
Departamento de Ingeniería de
Sistemas Telemáticos
dit
UPM
Data manipulation
SQL (Structured Query Language) is a declarative language to let
the database know what you want to achieve:
◆ Data Definition Language (DDL): To implement and modify the
database schema (tables and constraints)
◆ Data Manipulation Language (DML): To work with the data stored
in the database
SQL is case insensitive, but UPPER-CASE LETTERS are used to write
the language keywords
You can use ; to end a statement (though is not required)
Some examples:
◆ SELECT acronym FROM groups;
◆ SELECT name FROM researchers;
◆ SELECT * FROM projects;
◆ SELECT DISTINCT rgroup FROM researchers;
dit
UPM
Built-in functions
Aggregate functions return a single value, calculated from values in
a column
AVG(), COUNT(), MAX(), MIN()…
◆ SELECT COUNT(*) FROM researchers;
◆ SELECT MAX(budget) FROM projects;
◆ SELECT MAX(budget), name FROM projects;
Some examples:
◆ SELECT * FROM projects ORDER BY budget ASC;
◆ SELECT * FROM projects ORDER BY budget DESC;
◆ SELECT * FROM researchers WHERE rgroup=‘STRAST’ ORDER BY name;
◆ SELECT * FROM researchers ORDER BY rgroup, name;
dit
UPM
Querying several tables
SELECT * FROM researchers, groups
WHERE researchers.rgroup=groups.acronym
Example:
◆ INSERT INTO groups(name, acronym)
VALUES(‘Grupo de Sistemas Inteligentes', ’GSI')
dit
UPM
UPDATE statement
Changes the values to one or more columns in one or several tables
◆ The WHERE clause is used to choose the rows to update
◆ If WHERE is not used all rows will be updated
Syntax:
UPDATE table
SET colX=val1, colY=val2
WHERE colZ comp val;
Example:
◆ UPDATE groups
SET acronym=‘gSI’
WHERE acronym=‘GSI’;
dit
UPM
DELETE statement
Deletes rows from a table
◆ The WHERE clause is used to choose the rows to delete
◆ If WHERE is not used all rows will be deleted
Syntax:
DELETE FROM table
WHERE colZ comp val;
How to go deeper
◆ Details on SQL
http://www.w3schools.com/sql
◆ Details on PostgreSQL
https://www.postgresqltutorial.com/