Dbms r19 - Unit-2 (Ref-2)
Dbms r19 - Unit-2 (Ref-2)
Satyanarayana Reddy
UNIT 2
RELATIONAL MODEL : Introduction to relational model, concepts of domain, attribute, tuple, relation,
importance of null values, constraints (Domain, Key constraints, integrity constraints) and their importance.
BASIC SQL : Simple Database schema, data types, table definitions (create, alter), different DML operations
(insert, delete, update), basic SQL querying (select and project) using where clause, arithmetic & logical
operations, SQL functions(Date and Time, Numeric, String conversion).
Attribute: A relation schema R, denoted by R(A1, A2, ... , An) is made up of a relation name R and a list of
attributes A1, A2, ..., An. Each attribute Ai is the name of a role played by some domain D in the relation
schema R. D is called the domain of Ai and is denoted by dom(A). A relation schema is used to describe a
relation; R is called the name of this relation.
The degree (or arity) of a relation is the number of attributes n of its relation schema.
Tuple: A relation (or relation state) r of the relation schema R(A1, A2, ... , An) also denoted by r(R), is a set of
n-tuples r = {t1,t2,t3...tn}. Each n-tuple t is an ordered list of n values t = <v1,v2,v3...vn>, where each value Vi 1 ≤
i ≤ n, is an element of dom(A) or is a special null value. The ith value in tuple t, which corresponds to the
attribute Ai is referred to as t[Ai] (or t[i] if we use the positional notation). The terms relation intension for the
schema R and relation extension for a relation state r(R) are also commonly used.
Relation: A (named) set of tuples all of the same form (i.e., having the same set of attributes). The term table is
a loose synonym.
1
www.Jntufastupdates.com 1
Database Management Systems Prof. B. Satyanarayana Reddy
Relational Schema: used for describing (the structure of) a relation. E.g., R(A1, A2, ..., An) says that R is a
relation with attributes A1, ... An. The degree of a relation is the number of attributes it has, here n.
Example: STUDENT(Name, SSN, Address)
Relational Database: A collection of relations, each one consistent with its specified relational schema.
Diagram shows an example of a STUDENT relation. Each tuple in the relation represents a particular student
entity.
We display the relation as a table, where each tuple is shown as a row and each attribute corresponds to
a column header indicating a role or interpretation of the values in that column. Null values represent attributes
whose values are unknown or do not exist for some individual STUDENT tuple.
Mathematically a relation can be defined as- A relation (or relation state) r(R) is a mathematical relation
of degree n on the domains dom(A1) , dom(A2), ... , dom(An), which is a subset of the Cartesian product of the
domains that define R:
r(R) ⊆(dom(A1) X dom(Az) X ... X dom(An)
The Cartesian product specifies all possible combinations of values from the underlying domains.
Hence, if we denote the total number of values, or cardinality, in a domain D by ID I (assuming that all domains
are finite), the total number of tuples in the Cartesian product is |dom(A1)| X |dom(Az) | X ... X |dom(An )|
Of all these possible combinations, a relation state at a given time-the current relation state-reflects only
the valid tuples that represent a particular state of the real world.
Characteristics of Relations:
Ordering of Tuples: A relation is a set of tuples; hence, there is no order associated with them. That is, it
makes no sense to refer to, for example, the 5th tuple in a relation. When a relation is depicted as a table, the
tuples are necessarily listed in some order, of course, but you should attach no significance to that order.
Similarly, when tuples are represented on a storage device, they must be organized in some fashion, and it may
be advantageous, from a performance standpoint, to organize them in a way that depends upon their content.
Ordering of Attributes: A tuple is best viewed as a mapping from its attributes (i.e., the names we give to the
roles played by the values comprising the tuple) to the corresponding values. Hence, the order in which the
attributes are listed in a table is irrelevant. (Note that, unfortunately, the set theoretic operations in relational
algebra (at least how E&N define them) make implicit use of the order of the attributes. Hence, E&N view
attributes as being arranged as a sequence rather than a set.)
2
www.Jntufastupdates.com 2
Database Management Systems Prof. B. Satyanarayana Reddy
Values of Attributes: For a relation to be in First Normal Form, each of its attribute domains must consist of
atomic (neither composite nor multi-valued) values. Much of the theory underlying the relational model was
based upon this assumption. The Null value: used for don't know, not applicable.
Interpretation of a Relation: Each relation can be viewed as a predicate and each tuple in that relation can be
viewed as an assertion for which that predicate is satisfied (i.e., has value true) for the combination of values in
it. In other words, each tuple represents a fact. Some relations represent facts about entities (e.g., students)
whereas others represent facts about relationships (between entities). (e.g., students and course sections).
Application-Based: Constraints that cannot be directly expressed in the schemas of the data model, and hence
must be expressed and enforced by the application programs. These are more general and are difficult to express
and enforce within the data model, so they are usually checked within application programs.
Schema-Based: Constraints that can be directly expressed in the schemas of the data model, typically by
specifying them in the DDL. These are again classified as:
Domain Constraints
Key Constraints
Constraints on nulls
Entity Integrity Constraints
Referential Integrity Constraints
1. Domain Constraints:
Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value from
the domain dom(A). Each attribute value must be either null or drawn from the domain of that attribute.
3
www.Jntufastupdates.com 3
Database Management Systems Prof. B. Satyanarayana Reddy
2. Key Constraints:
A key constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for
a tuple. A set of fields that uniquely identifies a tuple according to a key constraint is called a candidate key for
the relation; we often abbreviate this to just key. There are two parts to the definition of candidate key:
1. Two distinct tuples in a legal instance (an instance that satisfies all ICs, including the key constraint)
cannot have identical values in all the fields of a key.
2. No subset of the set of fields in a key is a unique identifier for a tuple.
Every relation is guaranteed to have a key. Since a relation is a set of tuples, the set of all fields is always a
superkey. If other constraints hold, some subset of the fields may form a key, but if not, the set of all fields is a
key.
A relation may have several candidate keys. For example, the login and age fields of the Students
relation may, taken together, also identify students uniquely. That is, {login, age} is also a key. It may seem that
login is a key, since no two rows in the example instance have the same login value. However, the key must
identify tuples uniquely in all possible legal instances of the relation. By stating that {login, age} is a key, the
user is declaring that two students may have the same login or age, but not both.
Out of all the available candidate keys, a database designer can identify a primary key. Intuitively, a
tuple can be referred to from elsewhere in the database by storing the values of its primary key fields. For
example, we can refer to a Students tuple by storing its sid value. As a consequence of referring to student
tuples in this manner, tuples are frequently accessed by specifying their sid value.
Primary key indirectly says that the set of attributes do not have duplicate and null values in them.
Specifying Key Constraints in SQL:
In SQL, we can declare that a subset of the columns of a table constitute a key by using the UNIQUE constraint.
At most one of these candidate keys can be declared to be a primary key, using the PRIMARY KEY constraint.
CREATE TABLE Students
(
sid CHAR(20) ,
name CHAR (30) ,
login CHAR(20) ,
age INTEGER,
gpa REAL,
UNIQUE (name, age),
CONSTRAINT StudentsKey PRIMARY KEY (sid)
);
This definition says that sid is the primary key and the combination of name and age is also a key. The
definition of the primary key also illustrates how we can name a constraint by preceding it with CONSTRAINT
constraint-name. If the constraint is violated, the constraint name is returned and can be used to identify the
error.
4
www.Jntufastupdates.com 4
Database Management Systems Prof. B. Satyanarayana Reddy
An IC involving both relations must be specified if a DBMS is to make such checks. The most common IC
involving two relations is a foreign key constraint.
Suppose that, in addition to Students, we have a second relation:
Enrolled(studid: string, cid: string, gTade: string)
To ensure that only bona fide students can enroll in courses, any value that appears in the studid field of
an instance of the Enrolled relation should also appear in the sid field of some tuple in the Students relation. The
studid field of Enrolled is called a foreign key and refers to Students. The foreign key in referencing relation
(Enrolled) must have the same no. of columns and compatible datatypes, although the columns names can be
different.
5
www.Jntufastupdates.com 5
Database Management Systems Prof. B. Satyanarayana Reddy
3. Constraints on nulls:
The null value is a member of all domains, and as a result is a legal value for every attribute in SQL by
default. For certain attributes, however, null values may be inappropriate. Consider a tuple in the student
relation where name is null. Such a tuple gives student information for an unknown student; thus, it does not
contain useful information. Similarly, we would not want the department budget to be null. In cases such as this,
we wish to forbid null values, and we can do so by restricting the domain of the attributes name and budget to
exclude null values, by declaring it as follows:
name varchar(20) not null
budget numeric(12,2) not null
The not null specification prohibits the insertion of a null value for the attribute. Any database modification that
would cause a null to be inserted in an attribute declared to be not null generates an error diagnostic.
Unique Constraint:
SQL also supports an integrity constraint:
unique (Aj1 , Aj2, . . . , Ajm )
The unique specification says that attributes Aj1 , Aj2, . . . , Ajm form a candidate key; that is, no two tuples in
the relation can be equal on all the listed attributes.
Example:
CREATE TABLE Persons
(
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
The relation Persons doesn't allow duplicate values for the attribute ID.
6
www.Jntufastupdates.com 6
Database Management Systems Prof. B. Satyanarayana Reddy
Check Constraint:
When applied to a relation declaration, the clause check(P) specifies a predicate P that must be satisfied
by every tuple in a relation. A common use of the check clause is to ensure that attribute values satisfy specified
conditions, in effect creating a powerful type system. For instance, a clause check (budget > 0) in the create
table command for relation department would ensure that the value of budget is nonnegative.
Example:
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
The above kind of Person relation doesn't allow the tuples with age <18.
7
www.Jntufastupdates.com 7
Database Management Systems Prof. B. Satyanarayana Reddy
BASIC SQL
SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management systems.
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
History of SQL
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational databases. He described a
relational model for databases.
1978 − IBM worked to develop Codd's ideas and released a product named System/R.
1986 − IBM developed the first prototype of relational database and standardized by ANSI. The first
relational database was released by Relational Software which later came to be known as Oracle.
SQL Process
When you are executing an SQL command for any RDBMS, the system determines the best way to carry out
your request and SQL engine figures out how to interpret the task.
There are various components included in this process.
These components are −
Query Dispatcher
Optimization Engines
Classic Query Engine
SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL query engine won't handle logical files.
Following is a simple diagram showing the SQL Architecture −
8
www.Jntufastupdates.com 8
Database Management Systems Prof. B. Satyanarayana Reddy
Bellow figure shows a relational database state corresponding to the COMPANY schema.
9
www.Jntufastupdates.com 9
Database Management Systems Prof. B. Satyanarayana Reddy
A database state that does not obey all the integrity constraints is called an invalid state, and a state that
satisfies all the constraints in IC is called a valid state. Attributes that represent the same real-world concept
mayor may not have identical names in different relations. Alternatively, attributes that represent different
concepts may have the same name in different relations.
10
www.Jntufastupdates.com 10
Database Management Systems Prof. B. Satyanarayana Reddy
Each relational DBMS must have a data definition language (DOL) for defining a relational database
schema. Current relational DBMSs are mostly using SQL for this purpose. Integrity constraints are specified on
a database schema and are expected to hold on every valid database state of that schema. In addition to domain,
key, and NOT NULL constraints, two other types of constraints are considered part of the relational model:
entity integrity and referential integrity.
Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute accuracy.
11
www.Jntufastupdates.com 11
Database Management Systems Prof. B. Satyanarayana Reddy
SQL Operations:
There are five types of SQL statements. They are:
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Retrieval Language (DRL) Or Data Query Language (DQL)
4. Transactional Control Language (TCL)
5. Data Control Language (DCL)
1. DATA DEFINITION LANGUAGE (DDL): The Data Definition Language (DDL) is used to create and
destroy databases and database objects. These commands will primarily be used by database administrators
12
www.Jntufastupdates.com 12
Database Management Systems Prof. B. Satyanarayana Reddy
during the setup and removal phases of a database project. Let's take a look at the structure and usage of four
basic DDL commands:
1. CREATE
2. ALTER
3. DROP
4. RENAME
1. CREATE:
(a) CREATE TABLE: This is used to create a new relation and the corresponding
Syntax: Example:
CREATE TABLE relation_name SQL> CREATE TABLE Student (
( sno NUMBER(3) PRIMARY KEY ,
field_1 data_type(Size), sname VARCHAR2(10),
field_2 data_type(Size), dob DATE,
.. . ); class CHAR(5)
);
Result: Table created.
(b)CREATE TABLE..AS SELECT....: This is used to create the structure of a new relation from the
structure of an existing relation.
Syntax: Example:
CREATE TABLE relation_name_1 ( field_1, SQL> CREATE TABLE std(rno, sname)
field_2, .....field_n) AS SELECT field_1, AS SELECT sno, sname FROM student;
field_2,...........field_n FROM relation_name_2; Result: Table Created.
DESC: It is used to describe a schema as well as to retrieve rows from table in descending order.
Syntax: DESC schema_name; /*Describes the schema */
EX: SQL> DESC Student;
NAME NULL? TYPE
-------------------------- ------------------------- ---------------------------
SNO NOT NULL NUMBER(3)
SNAME VARCHAR2(10)
DOB DATE
CLASS CHAR(5)
13
www.Jntufastupdates.com 13
Database Management Systems Prof. B. Satyanarayana Reddy
2. ALTER:
(a)ALTER TABLE ...ADD...: This is used to add some extra fields into existing relation.
Syntax: Example :
ALTER TABLE relation_name ADD ( SQL>ALTER TABLE Student ADD
new_field_1 data_type(size),
(Address CHAR(10) );
new_field_2 data_type(size),
..); Result: TABLE ALTERED.
/**To check whether column add or not check using desc command**/
SQL> DESC Student;
NAME NULL? TYPE
-------------------------- ------------------------- ---------------------------
SNO NOT NULL NUMBER(3)
SNAME VARCHAR2(10)
DOB DATE
CLASS CHAR(5)
ADDRESS CHAR(10)
(b)ALTER TABLE...MODIFY...: This is used to change the width as well as data type of fields of
existing relations.
Syntax: Example:
ALTER TABLE relation_name MODIFY ( SQL>ALTER TABLE Student MODIFY(
field_1 newdata_type(Size), sname VARCHAR2(20),
field_2 newdata_type(Size), .... sno NUMBER(5));
field_n_newdata_type(Size) );
Result: TABLE ALTERED.
14
www.Jntufastupdates.com 14
Database Management Systems Prof. B. Satyanarayana Reddy
3. DROP TABLE: This is used to delete the structure of a relation. It permanently deletes the records in
the table.
Syntax: Example:
DROP TABLE relation_name; SQL>DROP TABLE Student;
Result: Table dropped.
5. TRUNCATE: This command will remove the data permanently. But structure will not be removed.
Syntax: TRUNCATE TABLE <Table name>
Example TRUNCATE TABLE EMP1;
2. DATA MANIPULATION LANGUAGE (DML): The Data Manipulation Language (DML) is used to
retrieve, insert and modify database information. These commands will be used by all database users during the
routine operation of the database. Let's take a brief look at the basic DML commands:
1. INSERT
2. UPDATE
15
www.Jntufastupdates.com 15
Database Management Systems Prof. B. Satyanarayana Reddy
3. DELETE
1. INSERT INTO: This is used to add records into a relation. These are three type of INSERT INTO
queries. They are:
a) Inserting a single record
Syntax: INSERT INTO relationname ( field_1,field_2,.field_n)
VALUES (data_1,data_2,........data_n);
Example: SQL> INSERT INTO student (sno,sname,class,address)
VALUES(1,'satya','5','GNT');
16
www.Jntufastupdates.com 16
Database Management Systems Prof. B. Satyanarayana Reddy
3. DELETE-FROM: This is used to delete all the records of a relation but it will retain the structure of
that relation.
17
www.Jntufastupdates.com 17
Database Management Systems Prof. B. Satyanarayana Reddy
3. SELECT - FROM -WHERE: This query is used to display a selected set of fields for a selected set of
records of a relation.
Syntax: SELECT a set of fields FROM relation_name WHERE condition;
Example: SQL> select * from emp1 where deptno<20;
EMPNO ENAME JOB DEPTNAME DEPTNO HIREDATE SALARY EXP ADDRESS
101 ramesh asst.prof It 10 10-DEC-96 20000 2 GNT
102 Ramu asst.prof It 10 05-JUL-97 10000 0 Vij
103 Rakesh asst.prof It 10 12-AUG-97 5000 0 GNT
There are many constructs used for data retrieval like grouping, ordering, aggregation and set operations
which can be described in next chapters.
1. COMMIT: This command is used to end a transaction only with the help of the commit command
transaction changes can be made permanent to the database.
Syntax: SQL>COMMIT;
Example: SQL>COMMIT;
2. SAVE POINT: Save points are like marks to divide a very lengthy transaction to smaller once. They
are used to identify a point in a transaction to which we can latter rollback. Thus, save point is used in
conjunction with rollback.
Syntax: SQL>SAVE POINT ID;
Example: SQL>SAVE POINT xyz;
3. ROLLBACK: A rollback command is used to undo the current transactions. We can rollback the entire
transaction so that all changes made by SQL statements are undo (or) rollback a transaction to a save point
so that the SQL statements after the save point are rollback.
Syntax: ROLLBACK( current transaction can be rollback)
ROLLBACK to save point ID;
Example: SQL>ROLLBACK;
SQL>ROLLBACK TO SAVE POINT xyz;
18
www.Jntufastupdates.com 18
Database Management Systems Prof. B. Satyanarayana Reddy
1. GRANT: The GRANT command allows granting various privileges to other users and allowing them
to perform operations within their privileges.
For Example, if a uses is granted as ‘SELECT’ privilege then he/she can only view data but cannot
perform any other DML operations on the data base object GRANTED privileges can also be withdrawn
by the DBA at any time
Syntax: SQL>GRANT PRIVILEGES on object_name To user_name;
Example: SQL>GRANT SELECT, UPDATE on emp1 To hemanth;
2. REVOKE: To with draw the privileges that has been GRANTED to a uses, we use the REVOKE
command
Syntax: SQL>REVOKE PRIVILEGES ON object-name FROM user_name;
Example: SQL>REVOKE SELECT, UPDATE ON emp FROM ravi;
CONSTRAINTS:
1. NOT NULL: When a column is defined as NOTNULL, then that column becomes a mandatory column. It
implies that a value must be entered into the column if the record is to be accepted for storage in the table.
Syntax: CREATE TABLE Table_Name(column_name data_type(size) NOT NULL, );
Example: SQL> CREATE Table emp2(eno number(5) not null,ename varchar2(10));
Table created.
SQL> desc emp2;
Name Null? Type
---------------- ----------------- -------------
ENO NOT NULL NUMBER(5)
ENAME VARCHAR2(10)
2. UNIQUE: The purpose of a unique key is to ensure that information in the column(s) is unique i.e. a value
entered in column(s) defined in the unique constraint must not be repeated across the column(s). A table may
have many unique keys.
Syntax: CREATE TABLE Table_Name(column_name data_type(size) UNIQUE, ….);
Example: SQL> CREATE Table emp3(eno number(5) unique,ename varchar2(10));
Table created.
19
www.Jntufastupdates.com 19
Database Management Systems Prof. B. Satyanarayana Reddy
SQL> /
Enter value for eno: 1
Enter value for ename: sas
old 1: insert into emp3 values(&eno,'&ename')
new 1: insert into emp3 values(1,'sas')
insert into emp3 values(1,'sas')
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003006) violated
3. CHECK: Specifies a condition that each row in the table must satisfy. To satisfy the constraint, each row in
the table must make the condition either TRUE or unknown (due to a null).
Syntax: CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical
expression), ….);
Example: CREATE TABLE student (sno NUMBER (3), name CHAR(10),class
CHAR(5),CHECK(class IN(‘CSE’,’CAD’,’VLSI’));
4. PRIMARY KEY: A field which is used to identify a record uniquely. A column or combination of columns
can be created as primary key, which can be used as a reference from other tables.
A table contains primary key is known as Master Table.
It must uniquely identify each record in a table.
It must contain unique values.
It cannot be a null field.
It cannot be multi port field.
It should contain a minimum no. of fields necessary to be called unique.
20
www.Jntufastupdates.com 20
Database Management Systems Prof. B. Satyanarayana Reddy
5. FOREIGN KEY: It is a table level constraint. We cannot add this at column level. To reference any primary
key column from other table this constraint can be used. The table in which the foreign key is defined is called a
detail table. The table that defines the primary key and is referenced by the foreign key is called the master
table.
Syntax: CREATE TABLE Table_Name (
col_name type(size)
FOREIGN KEY(col_name) REFERENCES table_name
);
Example:
CREATE TABLE subject (
scode NUMBER (3) PRIMARY KEY,
subname CHAR(10),fcode NUMBER(3),
FOREIGN KEY(fcode) REFERENCE faculty
);
21
www.Jntufastupdates.com 21
Database Management Systems Prof. B. Satyanarayana Reddy
OPERATORS IN SQL:
An operator is a reserved word or a character used primarily in an SQL statement's WHERE clause to perform
operations, such as comparisons and arithmetic operations. These Operators are used to specify conditions in an
SQL statement and to serve as conjunctions for multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
22
www.Jntufastupdates.com 22
Database Management Systems Prof. B. Satyanarayana Reddy
!< Checks if the value of left operand is not less than the value of a!<b is false.
right operand, if yes then condition becomes true.
!> Checks if the value of left operand is not greater than the value of a!>b is true.
right operand, if yes then condition becomes true.
DATE FUNCTIONS:
1) Sysdate:
SQL>SELECT SYSDATE FROM DUAL;
18-MAY-17
2) next_day:
SQL>SELECT NEXT_DAY(SYSDATE,’WED’)FROM DUAL;
24-MAY-09
3) add_months:
SQL>SELECT ADD_MONTHS(SYSDATE,2)FROM DUAL;
28-JUL-09
23
www.Jntufastupdates.com 23
Database Management Systems Prof. B. Satyanarayana Reddy
4) last_day:
SQL>SELECT LAST_DAY(SYSDATE)FROM DUAL;
31-MAY-17
5) months_between:
SQL>SELECT MONTHS_BETWEEN(SYSDATE,18-AUG-17) FROM EMP;
3
6) Least:
SQL>SELECT LEAST('10-JAN-07','12-OCT-07')FROM DUAL;
10-JAN-07
7) Greatest:
SQL>SELECT GREATEST('10-JAN-07','12-OCT-07')FROM DUAL;
10-JAN-07
9) Round: returns starting day of the next week if format specified is 'DAY'
SQL>SELECT ROUND(SYSDATE,'DAY')FROM DUAL;
21-MAY-17
10) to_char:
SQL> select to_char(sysdate, "dd\mm\yy") from dual;
18-may-17
11) to_date:
SQL> select to_date(sysdate, "dd\mm\yy") from dual;
18-may-17
24
www.Jntufastupdates.com 24
Database Management Systems Prof. B. Satyanarayana Reddy
NUMERIC FUNCTIONS:
ABS( ) - Returns the absolute value of numeric expression.
ACOS( ) - Returns the arccosine of numeric expression. Returns NULL if the value is not in the range -
1 to 1.
ASIN( ) - Returns the arcsine of numeric expression. Returns NULL if value is not in the range -1 to 1.
ATAN( ) - Returns the arctangent of numeric expression.
ATAN2( ) - Returns the arctangent of the two variables passed to it.
BIT_AND( ) - Returns the bitwise AND all the bits in expression.
BIT_COUNT( ) - Returns the string representation of the binary value passed to it.
BIT_OR( ) - Returns the bitwise OR of all the bits in passed expression..
CEIL( ) - Returns the smallest integer value that is not less than passed numeric expression.
CEILING( ) - Returns the smallest integer value that is not less than passed numeric expression.
CONV( ) - Convert numeric expression from one base to another.
COS( ) - Returns the cosine of passed numeric expression. The numeric expression should be expressed
in radians.
COT( ) - Returns the cotangent of passed numeric expression.
DEGREES( ) - Returns numeric expression converted from radians to degrees.
EXP( ) - Returns the base of the natural logarithm (e) raised to the power of passed numeric expression.
FLOOR( ) - Returns the largest integer value that is not greater than passed numeric expression.
FORMAT( ) - Returns a numeric expression rounded to a number of decimal places.
GREATEST( ) - Returns the largest value of the input expressions.
INTERVAL( ) - Takes multiple expressions exp1, exp2 and exp3 so on.. and returns 0 if exp1 is less
than exp2, returns 1 if exp1 is less than exp3 and so on.
LEAST( ) - Returns the minimum-valued input when given two or more.
LOG( ) - Returns the natural logarithm of passed numeric expression.
LOG10( ) - Returns the base-10 logarithm of passed numeric expression.
MOD( ) - Returns the remainder of one expression by diving by another expression.
OCT( ) - Returns the string representation of the octal value of passed numeric expression. Returns
NULL if passed value is NULL.
PI( ) - Returns the value of pi.
POW( ) - Returns the value of one expression raised to the power of another expression.
POWER( ) - Returns the value of one expression raised to the power of another expression.
RADIANS( ) - Returns the value of passed expression converted from degrees to radians.
ROUND( ) - Returns numeric expression rounded to an integer. Can be used to round an expression to a
number of decimal points.
SIN( ) - Returns the sine of numeric expression given in radians.
25
www.Jntufastupdates.com 25
Database Management Systems Prof. B. Satyanarayana Reddy
STRING FUNCTIONS:
1) Concat: CONCAT returns char1 concatenated with char2. Both char1 and char2 can be any of the datatypes
SQL> SELECT CONCAT(‘ORACLE’,’CORPORATION’)FROM DUAL;
ORACLECORPORATION
2) Lpad: LPAD returns expr1, left-padded to length n characters with the sequence of characters in expr2.
SQL>SELECT LPAD(‘ORACLE’,15,’*’)FROM DUAL;
*********ORACLE
3) Rpad: RPAD returns expr1, right-padded to length n characters with expr2, replicated as many times as
necessary.
SQL>SELECT RPAD (‘ORACLE’,15,’*’)FROM DUAL;
ORACLE*********
6) Lower: Returns a character expression after converting uppercase character data to lowercase.
SQL>SELECT LOWER(‘DBMS’)FROM DUAL;
dbms
7) Upper: Returns a character expression with lowercase character data converted to uppercase.
SQL>SELECT UPPER(‘dbms’)FROM DUAL;
DBMS
26
www.Jntufastupdates.com 26
Database Management Systems Prof. B. Satyanarayana Reddy
8) Length: Returns the number of characters, rather than the number of bytes, of the given string expression,
excluding trailing blanks.
SQL>SELECT LENGTH(‘DATABASE’)FROM DUAL;
8
10) Instr: The INSTR functions search string for substring. The function returns an integer indicating the
position of the character in string that is the first character of this occurrence.
SQL>SELECT INSTR('CORPORATE FLOOR','OR',3,2)FROM DUAL;
14
27
www.Jntufastupdates.com 27