Escolar Documentos
Profissional Documentos
Cultura Documentos
Aggregate Functions
Note: avg, min and max ignore tuples that have a null value for the specified attribute, but count
considers null values.
Execute the following query using postgreSQL and see the results:
Aggregates are also very useful in combination with GROUP BY clauses. For example, we can
get the maximum low temperature observed in each city with
Execute the following query using postgreSQL and see the results:
which gives us one output row per city. Each aggregate result is computed over the table rows
matching that city. We can filter these grouped rows using HAVING:
Execute the following query using postgreSQL and see the results:
which gives us the same results for only the cities that have all temp_lo values below 40. Finally,
if we only care about cities whose names begin with “S”, we might do
Execute the following query using postgreSQL and see the results:
Constraints
To recap, the SQL command for creating an empty table has the following form:
Basically two types of constraints are provided: column constraints are associated with a single
column whereas table constraints are typically associated with more than one column.
You can also give the constraint a separate name. This clarifies error messages and allows you to
refer to the constraint when you need to change it. The syntax is:
CREATE TABLE products (
product_no integer,
name text,
price numeric CONSTRAINT positive_price CHECK (price > 0)
);
So, to specify a named constraint, use the key word CONSTRAINT followed by an identifier
followed by the constraint definition.
A not null constraint is directly specified after the data type of the column and the constraint
simply specifies that a column must not assume the null value.
For example:
Begum Durgahee 2
Labsheet02 PostgreSQL7.4
• UNIQUE Constraints
Unique constraints ensure that the data contained in a column or a group of columns is unique
with respect to all the rows in the table.
The keyword unique specifies that no two tuples can have the same attribute value for this
column. Unless the condition not null is also specified for this column, the attribute value null
is allowed and two tuples having the attribute value null for this column do not violate the
constraint.
For example:
If a unique constraint refers to a group of columns, the columns are listed separated by commas:
CREATE TABLE example (
a integer,
b integer,
c integer,
UNIQUE (a, c)
);
• PRIMARY KEY
Technically, a primary key constraint is simply a combination of a unique constraint and a not-
null constraint.
So, the following two table definitions accept the same data:
Begum Durgahee 3
Labsheet02 PostgreSQL7.4
price numeric
);
Primary keys can also constrain more than one column; the syntax is similar to unique
constraints:
CREATE TABLE example (
a integer,
b integer,
c integer,
PRIMARY KEY (a, c)
);
A table can have at most one primary key (while it can have many unique and not-
null constraints).
• FOREIGN KEY
A foreign key constraint specifies that the values in a column (or a group of columns) must match
the values appearing in some row of another table. We say this maintains the referential integrity
between two related tables.
Say you have the product table that we have used several times already:
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
);
Let’s also assume you have a table storing orders of those products. We want to ensure that the
orders table only contains orders of products that actually exist. So we define a foreign key
constraint in the orders table that references the products table:
Now it is impossible to create orders with product_no entries that do not appear in the products
table.
We say that in this situation the orders table is the referencing table and the products table is the
referenced table. Similarly, there are referencing and referenced columns.
A foreign key can also constrain and reference a group of columns. As usual, it then needs to be
written in table constraint form. Here is a contrived syntax example:
CREATE TABLE t1 (
a integer PRIMARY KEY,
b integer,
Begum Durgahee 4
Labsheet02 PostgreSQL7.4
c integer,
FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)
);
A table can contain more than one foreign key constraint. This is used to implement many-to-
many relationships between tables. Say you have tables about products and orders, but now you
want to allow one order to contain possibly many products (which the structure above did not
allow). You could use this table structure:
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
);
Note also that the primary key overlaps with the foreign keys in the last table.
We know that the foreign keys disallow creation of orders that do not relate to any products. But
what if a product is removed after an order is created that references it? SQL allows you to
specify that as well. Intuitively, we have a few options:
• Disallow deleting a referenced product
• Delete the orders as well
• Something else?
To illustrate this, let’s implement the following policy on the many-to-many relationship example
above: When someone wants to remove a product that is still referenced by an order (via
order_items), we disallow it. If someone removes an order, the order items are removed as
well.
Modifying Tables
Begum Durgahee 5
Labsheet02 PostgreSQL7.4
You can
• Add columns,
• Remove columns,
• Add constraints,
• Remove constraints,
• Change default values,
• Rename columns,
• Rename tables.
All these actions are performed using the ALTER TABLE command
- Adding a Column
The new column will initially be filled with null values in the existing rows of the table.
You can also define a constraint on the column at the same time, using the usual syntax:
ALTER TABLE products ADD COLUMN description text CHECK (description <> ”);
- Removing Column
- Adding a Constraint
To add a not-null constraint, which cannot be written as a table constraint, use this syntax:
ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
The constraint will be checked immediately, so the table data must satisfy the constraint before it
can be added.
- Removing a Constraint
Begum Durgahee 6
Labsheet02 PostgreSQL7.4
- Renaming a Column
To rename a column:
ALTER TABLE products RENAME COLUMN product_no TO product_number;
- Renaming a Table
To rename a table:
ALTER TABLE products RENAME TO items;
Subqueries
EMP table: EMPNO is the primary key, ENAME is not null, DEPTNO is the foreign key.
PROJECT table: PNO is the primary key, PNAME is unique, PMGR is not null, BUDGET is
not null
DEPT table: DEPTNO is the primary key,
A query result can also be used in a condition of a where clause. In such a case the query is called
a subquery and the complete select statement is called a nested query.
A respective condition in the where clause then can have one of the following forms:
1. Set-valued subqueries
<expression> [not] in (<subquery>)
<expression> <comparison operator> [any|all] (<subquery>)
An <expression> can either be a column or a computed value.
Begum Durgahee 7
Labsheet02 PostgreSQL7.4
In a where clause conditions using subqueries can be combined arbitrarily by using the logical
connectives and and or.
Example: List the name and salary of employees of the department 20 who are leading
a project that started before December 31, 1990:
Explanation: The subquery retrieves the set of those employees who manage a project that
started before December 31, 1990. If the employee working in department 20 is contained in this
set (in operator), this tuple belongs to the query result set.
Example: List all employees who are working in a department located in BOSTON:
As long as the result of a subquery is not known in advance, i.e., whether it is a single value or a
set, it is advisable to use the in operator.
Example: List all those employees who are working in the same department as their
manager (note that components in [ ] are optional:
Explanation: The subquery in this example is related to its surrounding query since it refers to
the column E1.MGR. A tuple is selected from the table EMP (E1) for the query result if the value
for the column DEPTNO occurs in the set of values select in the subquery. One can think of the
evaluation of this query as follows: For each tuple in the table E1, the subquery is evaluated
individually. If the condition where DEPTNO in . . . evaluates to true, this tuple is selected.
Note that an alias for the table EMP in the subquery is not necessary since columns without a
preceding alias listed there always refer to the innermost query and tables.
• For the clause any, the condition evaluates to true if there exists at least on row selected
by the subquery for which the comparison holds. If the subquery yields an empty result
set, the condition is not satisfied.
• For the clause all, in contrast, the condition evaluates to true if for all rows selected by the
subquery the comparison holds. In this case the condition evaluates to true if the
subquery does not yield any row or value.
Example: Retrieve all employees who are working in department 10 and who earn at least as
much as any (i.e., at least one) employee working in department 30:
Begum Durgahee 8
Labsheet02 PostgreSQL7.4
Often a query result depends on whether certain rows do (not) exist in (other) tables. Such
type of queries is formulated using the exists operator.
Explanation: For each tuple from the table DEPT, the condition is checked whether there exists
a tuple in the table EMP that has the same department number (DEPT.DEPTNO). In case no such
tuple exists, the condition is satisfied for the tuple under consideration and it is selected. If
there exists a corresponding tuple in the table EMP, the tuple is not selected.
Begum Durgahee 9