Professional Documents
Culture Documents
1. Numeric functions:
Abs ( ): this function is used to convert a negative value to positive value.
Syntax: abs(No.)
Example:
Sql >select abs(100-120) from dual;
Output: 20
Sqrt ( ): this function returns the square root of a given number.
Syntax _ SQRT (No.)
Example:
SQL> select SQRT (81) from dual;
Output: 9
Mod ( ): this function returns the modulus.
Syntax: mod (dividend, divisor)
Example:
SQL> select mod (15,2) from dual,
Output: 1
Power ( ): this function returns the power of given number as per the raised
number specified.
Syntax: power (number, raised number)
Example:
SQL> select power (4, 3) from dual;
Output: 64
Round ( ): this function is used to round off the required number of decimals.
Syntax – round (No., No. of decimals)
Example:
SQL> select round (23.6789, 2) from dual;
Output: 23.68
2. String functions:
Upper ( ): this function converts a string into upper case.
Syntax: upper (‘string’)
Example:
Sql> select upper (‘hello’) from dual;
Output: HELLO
Lower ( ): this function converts a string into lower case.
Syntax: lower (‘string’)
Example:
Sql> select upper (‘HELLO’) from dual;
Output: hello
Initcap ( ): this function converts a string into initial caps.
Syntax: initcap (‘string’)
Example:
Sql> select initcap (‘computer education’) from dual;
Output: Computer Education
Itrim ( ): this function is availed to remove the unnecessary spaces or characters
available to a string.
Syntax: Itrim (‘string’, ‘unnecessary char’)
Example:
Sql> select Itrim (‘xyxyhello’, ‘xy’)
Output: hello
Rtrim ( ): this function is used to remove the unnecessary characters or spaces
available on the right side of a string.
Syntax: rtrim (‘string’, ‘unnecessary char’)
Example:
Sql> select rtrim (‘computerxyzxy’, ‘xy’) from dual;
Output: computerxyz
3. Aggregate functions:
Aggregate functions return single values by performing action of a group of values.
consider the following example of table Emp to simply understand its working
Table Emp
4. Date functions:
Sysdate: this function returns the system date in the format of dd-mm-yy.
Syntax: sql> select sysdate from dual;
Last_ day ( ): this function returns the last date of a given date.
Syntax: last _day(date)
Next _ day ( ): this function returns the next date of a given date.
Syntax: next _day (date, ‘dy’)
Add _ months ( ): this function is availed to add or subtract the number of months
to a given date.
Syntax: add _months(date, no. of months)
Months _ between ( ): this function returns the number of months present
between two given dates.
Syntax: months _between(date1, date2)
5. Conversion functions:
To _ char ( ): this function converts a string or value into characters or in a
specified format.
Syntax: to_ char(value, ‘format’);
To _ date ( ): this function is used to convert a string value into date format.
Syntax: to _date(‘value’, ‘format’);
To _ number ( ): this function converts a string value into numeric.
Syntax: to_ number(‘value’);
SQL provides many built-in functions to perform operations on data. These functions are
useful while performing mathematical calculations, string concatenations, sub-strings
etc. SQL functions are divided into two categories,
1. Aggregate Functions
2. Scalar Functions
Aggregate Functions
These functions return a single value after performing calculations on a group of
values. Following are some of the frequently used Aggregate functions.
AVG() Function
Average returns average value after calculating it from values in a numeric column.
Its general syntax is,
SELECT AVG(column_name) FROM table_name
avg(salary)
8200
COUNT() Function
Count returns the number of rows present in the table either based on some condition or
without condition.
Its general syntax is,
SELECT COUNT(column_name) FROM table-name
count(name)
Example of COUNT(distinct)
Consider the following Emp table
count(distinct salary)
FIRST() Function
First function returns first value of a selected column
Syntax for FIRST function is,
SELECT FIRST(column_name) FROM table-name;
first(salary)
9000
LAST() Function
LAST function returns the return last value of the selected column.
Syntax of LAST function is,
SELECT LAST(column_name) FROM table-name;
Using LAST() function
Consider the following Emp table
last(salary)
8000
MAX() Function
MAX function returns maximum value from selected column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name;
MAX(salary)
10000
MIN() Function
MIN function returns minimum value from a selected column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name;
MIN(salary)
6000
SUM() Function
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name;
SUM(salary)
41000
Scalar Functions
Scalar functions return a single value from an input value. Following are some frequently
used Scalar Functions in SQL.
UCASE() Function
UCASE function is used to convert value of string column to Uppercase characters.
Syntax of UCASE,
SELECT UCASE(column_name) from table-name;
UCASE(name)
ANU
SHANE
ROHAN
SCOTT
TIGER
LCASE() Function
LCASE function is used to convert value of string columns to Lowecase characters.
Syntax for LCASE is,
SELECT LCASE(column_name) FROM table-name;
Using LCASE() function
Consider the following Emp table
LCASE(name)
anu
shane
rohan
scott
tiger
MID() Function
MID function is used to extract substrings from column values of string type in a table.
Syntax for MID function is,
SELECT MID(column_name, start, length) from table-name;
MID(name,2,2)
nu
ha
oh
co
ig
ROUND() Function
ROUND function is used to round a numeric field to number of nearest integer. It is used
on Decimal point values.
Syntax of Round function is,
SELECT ROUND(column_name, decimals) from table-name;
ROUND(salary)
9001
8001
6000
10000
8000
There are various types of integrity constraints that the database provides.
Each has its own function and each ensures data integrity in its own way.
Integrity constraints can be divided into the following:
Entity integrity constraints as the name implies is applied on each entity. i.e.
it is applied on individual rows. The constraint here is to have a unique
value for each row in the column or a group of columns it is applied to. This
attribute is essential when a particular record or row of data is to be
accessed. It can be accessed using the entity integrity constraint by
supplying the unique value and accessing the entire record.
There are two such constraints which ensure uniqueness of data. They are-
Primary key
Unique keyword
Primary key ensures that values in a column are unique so that duplicate
values are not allowed and also the primary key column cannot be null.
Thus it focuses on two properties- uniqueness and not null. A table can
contain only one primary key. Primary key can consist of a column or a
group of columns. It is used to uniquely identify records in a table.
Unique keyword is just like the primary key but it allows null values. Both
these are defined on columns during defining the structure of table. Thus
these are used in data definition language.
Domain Integrity Constraints:
These are the constraints on the domain value and thus are column level
constraints unlike entity integrity constraint which are row level. The domain
integrity constraints are used to impose restrictions on some particular
column. Thus they affect the domain value. These can be enforced in the form
of the following-
Check value
Default value
Check is used to impose certain checks like checking if a value is greater than
or lesser than a particular value etc. thus the upper and the lower limit can be
set.
Foreign key
Foreign key normally references the primary key of same or another table. But
it can refer to other columns too. Whenever, same type of attribute exists in
two different tables, the attribute in one of the table is declared as the primary
key and in the other, it is made foreign key, so that the values in both become
consistent. Foreign key is dependent on primary key.
Generalization –
Generalization is the process of extracting common properties from a set of entities and
create a generalized entity from it. It is a bottom-up approach in which two or more entities
can be generalized to a higher level entity if they have some attributes in common. For
Example, STUDENT and FACULTY can be generalized to a higher level entity called PERSON
as shown in Figure 1. In this case, common attributes like P_NAME, P_ADD become part of
higher entity (PERSON) and specialized attributes like S_FEE become part of specialized
entity (STUDENT).
Specialization –
In specialization, an entity is divided into sub-entities based on their characteristics. It is a
top-down approach where higher level entity is specialized into two or more lower level
entities. For Example, EMPLOYEE entity in an Employee management system can be
specialized into DEVELOPER, TESTER etc. as shown in Figure 2. In this case, common
attributes like E_NAME, E_SAL etc. become part of higher entity (EMPLOYEE) and specialized
attributes like TES_TYPE become part of specialized entity (TESTER).
Specialization
For example : In a database of hospital, a person can either be a doctor,
staff or patient which can be spirited according to their functions
and specialties.
Specialization : Example
2. Generalization
For example : Goat, Lion and Monkey have some come traits and they
all together can be combined in class of animals.
Generalization: Example
BASIS FOR
GENERALIZATION SPECIALIZATION
COMPARISON
manner. manner.
Entities The higher level entity must The higher level entity may
Constraints on specialization/generalization
There are three constraints that may apply to a specialization/generalization:
membership constraints, disjoint constraints and completeness constraints.
Membership constraints
Condition defined: Membership of a specialization/generalization
relationship can be defined as a condition in the requirements e.g.
tanker is a ship where cargo = “oilâ€
User defined: Sometimes the designer can define the superclass-
subclass relationship. This can be done to simplify the design model or
represent a complex relationship that exists between entities.
Disjoint constraints
Disjoint: The disjoint constraint only applies when a superclass has more
than one subclass. If the subclasses are disjoint, then an entity
occurrence can be a member of only one of the subclasses, e.g.
postgrads or undergrads – you cannot be both. To represent a disjoint
superclass/subclass relationship, ‘Or’ is used.
Figure 7.14
Completeness constraints
Total: Each superclass (higher-level entity) must belong to subclasses
(lower-level entity sets), e.g. a student must be postgrad or undergrad.
To represent completeness in the specialization/generalization
relationship, the keyword ‘Mandatory’ is used.
Partial: Some superclasses may not belong to subclasses (lower-level
entity sets), e.g. some people at UCT are neither student nor staff. The
keyword ‘Optional’ is used to represent a partial
specialization/generalization relationship.
Some members of a university are both students and staff. Not all members of
the university are staff and students.
A student in the university must be either an undergraduate or postgraduate,
but not both.
A simple ER Diagram:
In the following diagram we have two entities Student and College and their
relationship. The relationship between Student and College is many to one as a
college can have many students however a student cannot study in multiple
colleges at the same time. Student entity has attributes such as Stu_Id,
Stu_Name & Stu_Addr and College entity has attributes such as Col_ID &
Col_Name.
Here are the geometric shapes and their meaning in an E-R Diagram. We will
discuss these terms in detail in the next section(Components of a ER Diagram)
of this guide so don’t worry too much about these terms now, just go through
them once.
Components of a ER Diagram
1. Entity
Weak Entity:
An entity that cannot be uniquely identified by its own attributes and relies on
the relationship with other entity is called weak entity. The weak entity is
represented by a double rectangle. For example – a bank account cannot be
uniquely identified without knowing the bank to which the account belongs, so
bank account is a weak entity.
2. Attribute
1. Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute
1. Key attribute:
A key attribute can uniquely identify an entity from an entity set. For example,
student roll number can uniquely identify a student from a set of students. Key
attribute is represented by oval same as other attributes however the text of
key attribute is underlined.
2. Composite attribute:
4. Derived attribute:
A derived attribute is one whose value is dynamic and derived from another
attribute. It is represented by dashed oval in an ER Diagram. For example –
Person age is a derived attribute as it changes over time and can be derived
from another attribute (Date of birth).
3. Relationship
When a single instance of an entity is associated with more than one instances
of another entity then it is called one to many relationship. For example – a
customer can place many orders but a order cannot be placed by many
customers.
When more than one instances of an entity is associated with a single instance
of another entity then it is called many to one relationship. For example –
many students can study in a single college but a student cannot study in many
colleges at the same time.
When more than one instances of an entity is associated with more than one
instances of another entity then it is called many to many relationship. For
example, a can be assigned to many projects and a project can be assigned to
many students.
A Total participation of an entity set represents that each entity in entity set
must have at least one relationship in a relationship set. For example: In the
below diagram each college must have at-least one associated Student.