Constraints: Not Null Unique Key Check Primary Key Foreign Key
Constraints: Not Null Unique Key Check Primary Key Foreign Key
Integrity Constraints are used to prevent the entry of invalid information into tables.
Constraint types
Not Null
Unique Key
Check
Primary Key
Foreign Key
Primary Key
Each table can have one primary key, which uniquely identifies each row in a table.
Foreign Key
the values in the FOREIGN KEY column must refer to existing values in the other table.
A foreign key column can refer to primary key or unique key column of other tables.
Note:
You cannot delete a parent record if any existing child record is there.
If you define the FOREIGN KEY with ON DELETE CASCADE option then you can
delete the parent record and if any child record exist it will be automatically deleted.
Example
Examples
Defining Constraints in CREATE TABLE statement.
create table emp (
empno number(5) constraint emppk Primary key,
ename varchar2(20) constraint namenn not null,
sal number(10,2) constraint salcheck check (sal between 1000 and 20000)
idno varchar2(20) constraint id_unique unique
);
create table attendance (
empno number(5) constraint empfk references emp (empno) on delete cascade,
month varchar2(10),
days number(2) constraint dayscheck check (days <= 31)
);
The name of the constraints are optional.
If you don’t define the names then oracle generates the names randomly like ‘SYS_C1234’
Another way of defining constraint in CREATE TABLE statement.
create table emp (
empno number(5),
ename varchar2(20) not null,
sal number(10,2),
idno varchar2(20),
constraint empfk foreign key (empno) references emp (empno) on delete cascade,
constraint dayscheck check (days <= 31)
);
Check Constraint
Alter table attendance add constraint dayscheck
Check (days <= 31);
Dropping constraints
To drop primary key constraint from emp table.
Alter table emp drop constraint emppk;
Note: You have to first drop the foreign key and then drop the primary key.
GROUP BY:
The GROUP BY clause can be used in SELECT statement by grouping up the data from multiple
records from a table (or tables), and at least one arithmetic operator should appear on
SELECT statement.
Syntax:
GROUP BY StudentName
Operators
|| Concatenates character strings SELECT 'The Name of the
employee is: ' || ENAME FROM
EMP;
!=, ^=, <> Inequality test. SELECT ENAME FROM EMP WHERE
SAL ^= 5000;
<= Less than or equal to test. SELECT ENAME FROM EMP WHERE
SAL <= 1500;
ALL Compares a value with every value in a list or SELECT * FROM emp WHERE sal
returned by a query. Must be preceded by =, ! >= ALL (1400, 3000);
=, >, <, <=, or >=. Evaluates to TRUE if the
query returns no rows.
[NOT] [Not] greater than or equal to x and less than SELECT ENAME, JOB FROM EMP
WHERE SAL BETWEEN 3000 AND
BETWEEN x or equal to y.
5000;
and y
EXISTS TRUE if a sub-query returns at least one row. SELECT * FROM EMP WHERE
EXISTS (SELECT ENAME FROM
EMP WHERE MGR IS NULL);
IS [NOT] Tests for nulls. This is the only operator that SELECT * FROM EMP WHERE COMM
IS NOT NULL AND SAL > 1500;
NULL should be used to test for nulls.
NOT Returns TRUE if the following condition is SELECT * FROM EMP WHERE NOT
(job IS NULL)
FALSE. Returns FALSE if it is TRUE. If it is SELECT * FROM EMP WHERE NOT
UNKNOWN, it remains UNKNOWN. (sal BETWEEN 1000 AND 2000)
AND Returns TRUE if both component conditions SELECT * FROM EMP WHERE
job='CLERK' AND deptno=10
are TRUE. Returns FALSE if either is
FALSE; otherwise returns UNKNOWN.
OR Returns TRUE if either component condition SELECT * FROM emp WHERE
is TRUE. Returns FALSE if both are FALSE. job='CLERK' OR deptno=10
Otherwise, returns UNKNOWN.
UNION Returns all distinct rows selected SELECT * FROM
by either query.
(SELECT ENAME FROM EMP WHERE
JOB = 'CLERK'
UNION
UNION
INTERSECT and Returns all distinct rows selected SELECT * FROM orders_list1
INTERSECT ALL by both queries.
INTERSECT
MINUS Returns all distinct rows selected SELECT * FROM (SELECT SAL FROM
EMP WHERE JOB = 'PRESIDENT'
by the first query but not the
second.
MINUS
Single row or Scalar functions return a value for every row that is processed in a query.
2) Group Functions:
These functions group the rows of data based on the values returned by the query. This is discussed in
SQL GROUP Functions. The group functions are used to calculate aggregate values like total or
average, which return just one total or one average value after processing a group of rows.
There are four types of single row functions. They are:
1) Numeric Functions:
These are functions that accept numeric input and return numeric values.
These are functions that accept character input and can return both character and number values.
3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of
datatype DATE, except for the MONTHS_BETWEEN function, which returns a number.
4) Conversion Functions:
These are functions that help us to convert a value in one form to another form.
For Example: a null value into an actual value, or a value from one datatype to another datatype
like NVL, TO_CHAR, TO_NUMBER, TO_DATE etc.
You can combine more than one function together in an expression. This is known as nesting of functions.
Output:
DUMMY
-------
X
Output:
777 * 888
---------
689976
1) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept numeric values as input and
return numeric values as output. Few of the Numeric functions are:
Function
Return Value
Name
CEIL (x) Integer value that is Greater than or equal to the number 'x'
FLOOR (x) Integer value that is Less than or equal to the number 'x'
TRUNC (x, y) Truncates value of number 'x' up to 'y' decimal places
ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places
The following examples explains the usage of the above numeric functions
ABS (1) 1
ABS (x)
ABS (-1) -1
CEIL (2.83) 3
CEIL (-1.6) -1
FLOOR (2.83) 2
FLOOR (-1.6) -2
TRUNC (-54, 1) 54
ROUND (x, y)
TRUNC (5.7) 5
For Example:
Let's consider the product table used in sql joins.
We can use ROUND to round off the unit_price to the nearest integer,
if any product has prices in fraction.
LTRIM (string_value,
All occurrences of 'trim_text' is removed from the left of 'string_value'.
trim_text)
RTRIM (string_value,
All occurrences of 'trim_text' is removed from the right of 'string_value' .
trim_text)
TRIM (trim_text FROM All occurrences of 'trim_text' from the left and right of 'string_value' ,
SUBSTR (string_value, m, Returns 'n' number of characters from 'string_value' starting from the 'm'
n) position.
LPAD (string_value, n, Returns 'string_value' left-padded with 'pad_value' . The length of the whole
RPAD (string_value, n, Returns 'string_value' right-padded with 'pad_value' . The length of the whole
For Example, we can use the above UPPER() text function with the column value as follows.
SELECT UPPER (product_name) FROM product;
The following examples explains the usage of the above character or text functions
TRIM (trim_text FROM string_value) TRIM ('o' FROM 'Good Morning') Gd Mrning
3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of datatypes
DATE, except for the MONTHS_BETWEEN function, which returns a number as output.
ADD_MONTHS (date, n) Returns a date value after adding 'n' months to the date 'x'.
MONTHS_BETWEEN (x1,
Returns the number of months between dates x1 and x2.
x2)
Returns the date 'x' rounded off to the nearest century, year, month, date, hour,
ROUND (x, date_format)
minute, or second as specified by the 'date_format'.
Returns the date 'x' lesser than or equal to the nearest century, year, month,
TRUNC (x, date_format)
date, hour, minute, or second as specified by the 'date_format'.
NEXT_DAY (x, week_day) Returns the next date of the 'week_day' on or after the date 'x' occurs.
It is used to determine the number of days remaining in a month from the date
LAST_DAY (x)
'x' specified.
The below table provides the examples for the above functions
4) Conversion Functions:
These are functions that help us to convert a value in one form to another form. For Ex: a null value into an
actual value, or a value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER,
TO_DATE.
Few of the conversion functions available in oracle are:
TO_DATE (x [, Converts a valid Numeric and Character values to a Date value. Date is
NVL (x, y) If 'x' is NULL, replace it with 'y'. 'x' and 'y' must be of the same datatype.
DECODE (a, b, c, d, e, Checks the value of 'a', if a = b, then returns 'c'. If a = d, then returns 'e'. Else,
The below table provides the examples for the above functions
nvl2 will return different values based on whether the input value is NULL or not.