Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DB and Cloud

Emanuele Beratto
Contents
1 SQL essentials 1
1.1 Data extraction 1
1.2 Data and tables manipulation 2

1 SQL essentials
1.1 Data extraction
The most generic query for a single tabe looks like
1 SELECT /* attributes or expressions list */
2 FROM /* tables list */
3 [ WHERE /* simple conditions on tables attributes */ ]
4 [ GROUP BY /* grouping attributes list */ ]
5 [ HAVING /* grouping conditions on grouping functions */ ]
6 [ ORDER BY /* ordering attributes list */ ]

where
• * is used to select all columns;

• (column_name) new_name are used to give a new name to the column inside the
brackets;

• table_name.column_name is used to explicitly provide the table to which the col-


umn belongs (e.g. when two different tables have same column names);

• Both attributes and conditions can be obtained as results of subqueries. NB


in the WHERE condition remember to use IN command if using a subquery result
which is a list.
We can also explicitly join two tables on the common attributes with a query
1 SELECT /* attributes or expressions list */
2 FROM /* first table */ INNER JOIN /* second table */
3 [ ON /* simple conditions on tables attributes */ ]

If we want to extend the join even on lacking attributes we must use the OUTER
JOIN command specifying which of the two tables we want to preserve thanks to LEFT
or RIGHT indicators. The lacking fields will be initialised with NULL values.
1 SELECT /* attributes or expressions list */
2 FROM /* first table */ LEFT / RIGHT JOIN /* second table */
3 [ ON /* simple conditions on tables attributes */ ]

Clearly all the joins can also be nested to accommodate more than two tables.

–1–
1.2 Data and tables manipulation
We can both have global insertions of single values as
1 INSERT INTO /* table name */
2 ( /* list of columns */ )
3 VALUES ( /* list of values */ )

and selective insertions of list of values as


1 INSERT INTO /* table name */
2 ( /* list of columns */ )
3 SELECT ( /* list of columns */ )
4 FROM /* tables list */
5 /* etc . */

We can also update some entries of the tables as follows


1 UPDATE /* table name */
2 SET ( /* list of updates on columns */ )
3 [ WHERE /* simple conditions on table attributes */ ]

Finally we can delete entries by


1 DELETE FROM /* table name */
2 [ WHERE /* simple conditions on table attributes */ ]

To completely drop an entire table use


1 DROP TABLE /* table name */

When manipulating data on a DB, one can act to confirm or undo the manipu-
lations. To confirm we use COMMIT;; to undo we use ROLLBACK;.
We can create a table with the command
1 CREATE TABLE /* table name */
2 (
3 /* column_name data_type */ [ DEFAULT /* default_value */ ] [ /*
constraints */ ] ,
4 /* etc . */
5 )

where the most common constraints are

• NOT NULL constraints the field to be initialised as non-empty;

• PRIMARY KEY constraints the field to be the row unique identifier;

• FOREIGN KEY constraints the field of the slave table to be the row unique iden-
tifier of the master table (i.e. it is used when creating relations among tables);
for this reason it must be used in combo with REFERENCES as follows

–2–
1 CREATE TABLE /* table name */
2 (
3 /* column_name data_type */ FOREIGN KEY /* ma st er_ co lu mn _n am e */
REFERENCES /* maste r_tabl e_name */ ,
4 /* etc . */
5 )

• UNIQUE constraints the field to contain unique values for each row;

• CHECK constraints the field to contain values in a specified range; for this reason
it is used in combo with IN as follows
1 CREATE TABLE /* table name */
2 (
3 /* column_name data_type */ CHECK ( /* column_name */ IN /* range */
),
4 /* etc . */
5 )

We can also create a new table starting from an already existing one thanks to
a SELECT statement as follows
1 CREATE TABLE /* table name */
2 ( /* column_name1 , column_name2 , ... */ )
3 AS ( SELECT /* column_name1 , column_name2 , ... */
4 FROM /* table name */
5 [ WHERE /* simple conditions on table attributes */ ])

We can alter and modify an already existing table thanks to the ALTER TABLE
command as follows
1 ALTER TABLE /* table name */
2 /* COMMAND */ /* column_name data_type */ [ DEFAULT /* default_value */ ] [
/* constraints */ ]

where /*COMMAND*/ could be

• ADD to add a new column with the specified structure;

• MODIFY to modify an old column with the specified new structure;

• ADD CONSTRAINT to add a PRIMARY KEY, FOREIGN KEY or CHECK constraint.

–3–

You might also like