Constraints are the rules enforced on the data columns of a table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints can be divided into following two types:
Column level constraints : limits only column data
Table level constraints : limits whole table data
Aggregate Functions
3. Oracle Database, SQL3
Constraints
Constraints are the rules enforced on the data columns of a table. These are used to limit the type of data that
can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints can be divided into following two types:
• Column level constraints : limits only column data
• Table level constraints : limits whole table data
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
4. Oracle Database, SQL4
NOT NULL
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a
column, you cannot pass a null value to that column. It enforces a column to contain a proper value. One
important point to note about NOT NULL constraint is that it cannot be defined at table level.
CREATE TABLE Student
(s_id int NOT NULL,
Name varchar(60),
Age int);
5. Oracle Database, SQL5
NOT NULL
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a
column, you cannot pass a null value to that column. It enforces a column to contain a proper value. One
important point to note about NOT NULL constraint is that it cannot be defined at table level.
6. Oracle Database, SQL6
UNIQUE
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE constraint field will
not have duplicate data. UNIQUE constraint can be applied at column level or table level.
7. Oracle Database, SQL7
PRIMARY KEY
PRIMARY KEY constraint uniquely identifies each record in a database. A Primary Key must contain unique
value and it must not contain null value. Usually Primary Key is used to index the data inside the table.
8. Oracle Database, SQL8
FOREIGN KEY
FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint is also used to restrict actions that would
destroy links between tables
10. Oracle Database, SQL10
CHECK
CHECK constraint is used to restrict the value of a column between a range. It performs check on the values,
before storing them into the database. Its like condition checking before saving data into a column
11. Oracle Database, SQL11
Aggregate Functions
Aggregate functions return a single result row based on groups of rows, rather than on single rows. Aggregate
functions can appear in select lists and in ORDER BY and HAVING clauses. They are commonly used with the
GROUP BY clause in a SELECT statement, where Oracle Database divides the rows of a queried table or view into
groups. In a query containing a GROUP BY clause, the elements of the select list can be aggregate functions,
GROUP BY expressions, constants, or expressions involving one of these. Oracle applies the aggregate functions
to each group of rows and returns a single result row for each group.
All the dependent columns or columns used in GROUP BY function must form the basis of grouping, hence must
be included in GROUP BY clause also.
GROUP BY clause does not support the use of column alias, but the actual names.
GROUP BY clause can only be used with aggregate functions like SUM, AVG, COUNT, MAX, and MIN. If it is used
with single row functions,Oracle throws and exception as "ORA-00979: not a GROUP BY expression".
Aggregate functions cannot be used in a GROUP BY clause. Oracle will return the "ORA-00934: group function
not allowed" here error message.
SELECT DEPARTMENT_ID, JOB_ID, SUM (SAL) FROM employees GROUP BY DEPARTMENT_ID, JOB_ID;
12. Oracle Database, SQL12
Aggregate Functions
Many (but not all) aggregate functions that take a single argument accept these clauses:
• DISTINCT and UNIQUE, which are synonymous, cause an aggregate function to consider only distinct values
of the argument expression. The syntax diagrams for aggregate functions in this chapter use the keyword
DISTINCT for simplicity.
• ALL causes an aggregate function to consider all values, including all duplicates.
16. Oracle Database, SQL16
Count
COUNT(*) OR COUNT(ALL/DISTINCT expr)
Returns the number of rows in the query. If you specify expr then count ignore nulls. If you specify the asterisk
(*), this function returns all rows, including duplicates and nulls. COUNT never returns null.
SELECT
COUNT(manager_id),
COUNT(*),
COUNT(ALL manager_id),
COUNT(DISTINCT manager_id)
FROM
employees;
19. Oracle Database, SQL19
Aggregate Functions
A WHERE clause is used to filter rows BEFORE the GROUPING action
A HAVING clause filters rows AFTER the GROUPING action
SELECT
department_id,
SUM(salary)
FROM
employees
WHERE
department_id <> 100
GROUP BY
department_id
HAVING
SUM(salary) >= 15000;