DBMS Unit-2 Notes (1)
DBMS Unit-2 Notes (1)
MANAGEMENT SYSTEM
DBMS CORE GROUP
UNIT-2
INTRODUCTION TO
RELATIONAL MODEL
Table of Contents
3.0 Introduction..............................................................................................................................................4
3.1 Structure of Relational Model .................................................................................................................5
3.1.1 Database Schema ..............................................................................................................................5
After implementing the conceptual model of an application's database, the next step is to
develop the logical model of the application database. The logical model is implemented using
the Relational Model, which means the model is conceptualized around the mathematical
concept of Relations.
The relational model provided a standard way of representing and querying data used by any
application. The most fundamental elements in the relational model are relations, which
users and modern RDBMSs recognize as tables. A relation is a set of tuples or rows, with each
tuple sharing a set of attributes or columns.
The other strength of the relational model is to use structured query language (SQL) to write
and query data in a relational database. SQL has been widely used as the language for
relational database queries.
To ensure that data is always accurate and accessible, relational databases follow certain
integrity rules. For example, an integrity rule can specify that duplicate rows are not allowed
in a relation to eliminate the potential for erroneous information entering the database.
Organizations of all types and sizes use the simple yet powerful relational model for a wide
variety of information needs. A relational database can be considered for any information
need in which data points relate to each other and are managed in a secure, rules-based,
consistent way.
This chapter will learn the concept of a relational model, terminology of the model,
characteristics, constraints, integrity rules, and implementation of relations using SQL.
3.1 Structure of Relational Model
The database schema description is called the database schema, which is specified during
database design and is not expected to change frequently.
The term "schema" refers to the organization of data as a blueprint of how the database is
constructed (divided into database relations in the case of relational databases).
The relation schemas of the SMS database (the case study covered in Unit-2) can be shown
as follows:
A database schema with primary key and foreign key dependencies can be depicted pictorially
by schema diagrams. A schema diagram of the SMS database is shown in
Figure 3.1.
Figure 3.1 Schema diagram for the SMS database
Database Instance is a snapshot of the data in the database at a given instant in time. The
data stored in a database at a particular moment is called an Instance of a Database.
In this section, we will understand few terminologies related to relational model objects as
mentioned below.
Database
Relation/Table
Attribute/Field/Column
Domain
Record/Row/Tuple
Degree /Arity
The relational model is best suitable for managing the structured data in a database. It
represents the database as a collection of relations. Informally, each relation resembles a
table of values. When a relation is thought of as a table of values, each row in the table
represents a collection of related data values. A row represents a fact that typically
corresponds to a real-world entity or relationship. The table name and column names are
used to help to interpret the meaning of the values in each row.
Figure 3.2 is called STUDENT because each row represents facts about a particular student
entity. The column names – roll_no, first_name, middle_name, last_name, etc. specify how
to interpret the data values in each row.
Figure 3.3
Figure 3.3, a row is called a tuple, a column header is called an attribute, and the table is called
a relation. A domain of possible values represents the data type describing the types of values
that can appear in each column. The number of attributes in a relation is called degree or
arity.
Here relation STUDENT has degree 11 and cardinality 6.
In general terminology, the tuple is also called a row or record; an attribute is called a field
or column. A relation is called a table. The domain defines the data type, format, and possible
set of values for a column.
A) Relation
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, … , tm}. Each n-tuple t is an ordered list of n values t =<v1, v2, …,vn>,
where each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or special NULL value. The ith value in
tuple t, which corresponds to the attribute Ai, is referred to as t[Ai] or t.Ai.
Figure 3.2, relation STUDENT of the relation schema STUDENT (roll_no, first_name, middle_
name, last_ name, dob, gender, house_no, street_name, city, state, pin code) is a set of 5-
tuples r = {t1, t2, t3, t4, t5}. Second tuple t2 is an ordered list of 12 values t2 = <191006345,
Satynder, Kumar, Bhatia, 15.06.2001, Male, B/35, Kavi Nagar, Ghaziabad, Uttar Pradesh,
200100>, where a value suppose Kavi Nagar is an element from dom(street_name). The 6 th
value in tuple t2 corresponds to the attribute (gender) and can be referred to as t2 [gender] or
t2.gender.
We display the relation as a table, where each tuple is shown as a row. 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.
B) Domains
A domain is the original set of atomic values used to model data. By atomic, we mean that
each value in the domain is indivisible. A common method of specifying a domain is to specify
a data type, format, and possible set of values from which the data values are drawn.
Some examples of domains and their logical definitions are mentioned in Error! Reference
source not found.:
These domains get applied to the attributes to understand what type of data, set of values,
and format attributes. Let us now discuss the attributes and how domains are applied to
attributes.
C) Attribute
A relation schema R, denoted by R(A1, A2, …, An), comprises 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 A i and is denoted by dom(Ai). A relation
schema is used to describe a relation; R is called the name of this relation. The degree of a
relation is the number of attributes n of its relation schema.
A relation of degree eleven, which stores information about the students, would contain
twelve attributes describing each student as follows:
STUDENT(roll_no, first_name, middle_name, last_name, dob, gender, house_no,
street_name, city, state, pincode)
For this relation schema, STUDENT is the name of the relation, which has twelve attributes.
In the preceding definition, we showed the assignment of generic types such as string or
integer to the attributes. More precisely, we can specify the following previously defined
domains for some of the attributes of the STUDENT relation: dom(roll_no): Roll_number;
dom(first_name) :Names; dom(middle_name): Names; dom(last_name): Names; dom(dob)
=Dates, dom(gender):Genders, dom(house_no): Address, dom(street_name): Address,
dom(city): City_names, dom(state): State_names, dom(Pincode): Pincodes
The earlier definition of relation implies specific characteristics that make a relation different
from a file. We will now discuss some of these characteristics.
Each relation in a database must have a distinct or unique name that would separate
it from the other relations in a database, e.g., the relations STUDENT and
DEPARTMENT have distinct names.
A relation must not have two attributes with the same name. Each attribute must have
a distinct name, e.g., the relation DEPARTMENT has three distinct attributes dept_id,
dept_name, and dept_location
Each tuple must have exactly one data (atomic) value for an attribute.
Figure 3. 6: Representation of student_phone_no relation having the multi-valued attribute (not allowed)
Figure 3.7: Representation of Student_phone_number relation having atomic value attribute (allowed)
Tuples in a relation do not have to follow a significant order as the relation is not order-
sensitive. Both of these DEPARTMENT relations are the same.
Figure 3.8: Different ways of representing same department relation
Relational Model Notation
For example, each row (tuple) contains the value of attributes (columns) for one specific
student in the student relation.
In a database relation, the data is stored (logically) into the data field, an intersection of
relation row and column. Each data element in a relation can be accessed with reference
to the specific row and column.
There are mainly seven different types of Keys in DBMS and each key has its different
functionality:
A super key (SK) is a set of one or more attributes that allows us to uniquely identify a row
(tuple) in the relation.
SK is a set of attributes of a relation schema R with the property that no two tuples t 1 and
t2 in any relation state r of R should have the same combination of values for these attributes.
This is known as uniqueness property.
Thus, we can state that:
t1[SK] ≠ t2[SK] or t1.SK ≠ t2.SK
For example, two attribute set (roll_no, first_name, last_name)1 and (roll_no)2 are both
super keys of STUDENT relation because no two student tuples can have the same value for
these set of attributes, or we can say that t 1[roll_no, first_name, last_name] ≠ t2[roll_no,
first_name, last_name] similarly, t1[roll_no] ≠ t2[roll_no].
A relation can have many super keys. Consider the STUDENT schema below to understand
this:
STUDENT (adhaar_no, roll_no, first_name, last_name, address, dob, gender, mobile_no,
email_id).
Below are some sets of super keys for the above STUDENT schema since each set, i.e., SK(i),
uniquely identifies a unique student in the Student relation.
SK1 = (adhaar_no)
SK2 = (roll_no)
SK3 = (mobile_no)
SK4 = (email_id)
SK5 = (adhaar_no, first_name, last_name)
SK6 = (roll_no, first_name, last_name )
SK7 = (first_name, last_name, address, mobile_no)
SK8 = (roll_no, first_name, gender)
SK9 = (first_name, last_name, address, email_id)
SK10 = (first_name, mobile_no, email_id, dob) etc.
A combination of all the attributes in a super key can identify each tuple uniquely in the given
relation; however, all of them may not be necessary to establish the uniqueness of a row
(tuple).
Maximum no. of possible super keys for a relation with n attributes = 2n-1
A candidate key is a minimal super key that can identify each row (tuple) of a given relation
uniquely. It means that a candidate key is a super key from which we cannot remove any
attributes and still have the uniqueness property hold.
In other words, a candidate key is a super key without any redundant attribute. A candidate
key has a minimum number of attributes that are definitely needed to establish a row (tuple)
uniqueness in a given relation.
We can also say that the candidate key is a super key of which no proper subset exists as a
super key.
However, A proper subset of a set A is a subset of A that is not equal to A. In other words,
if B is a proper subset of A, then all elements of B are in A, but A contains at least one
element that is not in B.
For example, if A={1,3,5} then B={1,5} is a proper subset of A. The set C={1,3,5} is a subset
of A, but it is not a proper subset of Asince C=A. Proper Subset is denoted by the symbol
‘⊂'. So as per the above example, B ⊂ A and C ⊆ A.
Given below are the examples of candidate keys or minimal super keys since each set consists
of minimal attributes required to identify each student uniquely in the Student relation:
CK1 = (adhaar_no)
CK2 = (roll_no)
CK3 = (first_name, last_name, address)
CK4 = (email_id)
Therefore, the candidate keys obtained above examples are the minimal set of super keys.
The set of all possible candidate keys is known as the candidate key set.
Hence {roll_no, adhaar_no, email_id, (first_name, last_name, address)} is a possible set of
candidate keys.
We now know that a relation can have many candidate keys, which can uniquely identify each
row (tuple) of a given relation. Any one of these candidate keys can be chosen as a Primary
key (PK) of a given relation.
The database designer chooses any one of the candidate keys as the primary key of the
relation.
1. Its attributes are never, or very rarely, changed , i.e., time-invariant. For instance, the
adhaar_no, roll_no, email_id, and (first_name, last_name, address) are the candidate
keys of the student relation, out of which email_id and address are time-variant. On
the other hand, adhaar_no & roll_no are guaranteed never to change.
2. It should consist of either a single attribute or a small number of attributes.
Hence, a database designer can choose either adhaar_no or roll_no as a primary key for the
student relation satisfying the above two conditions.
A composite key is a primary key consisting of two or more columns that uniquely identify
rows in a relation. The combination of columns guarantees uniqueness, though individually
uniqueness is not guaranteed. Hence, they are combined to identify records in a relation
uniquely.
Example:
Figure 3.13, (section_name) and (dept_id) individually cannot be a primary key as it does not
uniquely identify a record. However, a composite key consisting of (section_name) and
(dept_id) could be used to identify each row (tuple) uniquely.
Secondary or Alternate key (s) are the candidate keys that are left unimplemented or unused
after implementing the primary key.
If in the student relation, (roll_no) is designated as a primary key, the remaining candidate
keys – (adhaar_no), (email_id), and (first_name, last_name, address) are the secondary keys
or alternate keys of the student relation.
The secondary keys are used for data retrieval purposes and indexing. Indexing is done for
better and faster data searching and retrieval from the database.
Figure 3.14: Representation of different keys in a Venn diagram
3.2.6 Surrogate Keys
A surrogate key is a system-generated value with no business meaning that is used to identify
a record in a relation uniquely. This key is used as a primary key in a given relation when a
natural primary key is not available. The surrogate key is usually an integer and thus does not
lend any meaning to the data in the relation. A surrogate key is a value generated right before
the record is inserted into a relation.
When the primary key is too big or complicated, Surrogate keys are preferred. A surrogate
key is generated for each unique combination of the primary key.
Example:
The primary purpose of the foreign key (FK) is to define and create a relationship between
the two relations. When the primary key (PK) of one relation (also referred to as parent
relation) is included as a non-unique attribute in another relation (also referred to as child
relation), then such a database key is called a foreign key (FK). Please note that there is no
uniqueness constraint for a foreign key (FK).
The relation which is being referenced is called referenced relation, and the corresponding
attribute is called a referenced attribute. The relation which refers to the referenced relation
is called referencing relation, and the corresponding attribute is called referencing attribute.
For example, attribute (dept_id) in course relation, as shown in Figure 3.16 is a foreign key
(FK), and all values for this attribute will match the values of the primary key (PK) attribute
(dept_id) in the department relation.
Figure 3.16: Representation of Foreign Key in a Course Table with referenced to Department Table
The foreign key of a relation references the primary key of the other relation.
A foreign key can take only those values which are present in the primary key of the
referenced relation.
A foreign key may have a name other than that of a primary key.
A foreign key can take the NULL value.
There is no restriction on a foreign key to be unique. In fact, the foreign key is not
unique most of the time.
Referenced relation may also be called the parent relation or primary relation.
Referencing relation may also be called the child relation.
There may be more than one foreign key in a relation.
Unique identifier for rows of a table Unique identifier for rows of a table when
primary key is not present
Only one primary key can be present in a Multiple Unique Keys can be present in a table
table
Selection using primary key creates Selection using unique key creates non-
clustered index (the order of the rows does clustered index (the order of the rows does not
not match the physical order of the actual match the physical order of the actual data)
data)
3.3 Constraints
A relational database is a collection of relations. These relations store data about a business
or a business process. Functional users or business managers define the rules that must be
applied to this data before storing it in the database. For example, in our SMS case study,
Each student must have a roll number which must be unique in the records
maintaining student data.
No student in a department can work on more than one mini-project.
A student can belong to only one department, etc.
There will typically be many relations in a relational database, and the tuples in those relations
are usually related in various ways. The state of the whole database will correspond to the
states of all its relations at a particular point in time. There are generally many rules or
restrictions on the actual values in a database state. We need to enforce these rules on the
data values stored in tuples for a particular column. The data that satisfies these rules will be
stored in the respective column of a relation; otherwise, it will be rejected.
The business rules or restrictions which are enforced on data being stored in a relation are
called Constraints. These constraints ensure that data stored in relations will be valid.
Note: Even if a single column of the record being entered into the relation fails a constraint,
then the entire record is rejected and not stored in a relation.
We will now discuss the various restrictions on data specified on a relational database in the
form of constraints. Constraints on databases are divided into three main categories:
The constraints that are implicit in a data model are called inherent model-based
constraints. These constraints are:
Values and NULLs in the Tuples: Each value in a tuple is an atomic value; that
is, it is not divisible into components within the framework of the basic
relational model. Hence, multi-valued attributes are not allowed. An important
concept is that of NULL values, which represent the values of attributes that
may be unknown or may not apply to a tuple. A special value, called NULL, is
used in these cases. For example, if a student does not have a middle_name in
student relation, it will be represented by NULL.
The constraints that can be directly expressed in the schemas of the relational model
by specifying them in the Data Definition Language (DDL) are schema-based. These
are further categorized as:
Entity integrity constraint or Key constraint
Referential integrity constraint
Domain Integrity constraints
o Check constraint
o Unique constraint
o Null constraint
o Default constraint
3. Application-based constraints:
Constraints that cannot be directly expressed in the schemas of the data model and
hence must be expressed and enforced by the application programs are called
application-based constraints. For example, a student can take a maximum of 25
credits in a semester.
The constraints that we will discuss in this section are schema-based constraints that can be
expressed in a schema of the relational model via the DDL statements.
Integrity constraints ensure that changes made to the database by authorized users do not
result in a loss of data consistency. Thus, integrity constraints guard against accidental
damage to the database. Let us discuss various types of integrity constraints.
3.3.1.1 Entity Integrity Constraint or Key Constraint
Entity integrity constraint is a major component of overall data integrity, and it ensures that
there are no duplicate tuples in a relation. Entity integrity constraint is based on the primary
key (PK). The primary key of a relation ensures there are no duplicate tuples in a relation. The
value of a primary key is unique and not null.
For example, in
Figure 3.18 below, (dept_id) is the primary key of department relation. (Dept_id) will be
unique and not null for all tuples of the department relation.
Figure 3.18: Representation of Primary Key (dept_id) in Department Relation (Entity Integrity Constraint)
Referential Integrity constraints in DBMS are based on the concept of Foreign Keys. The
referential integrity constraint says, that if a relation R2 has a foreign key attribute (FK)
matching the primary key attribute (PK) of other relation R1, then every value of FK in R2 must
either be equal to the value of PK in some tuple of R1 or the FK value must be null. R1 and R2
are not necessarily distinct (in the case of a recursive relationship).
For example, as shown in Figure 3.19, the department id attribute in the Course and
department must be the same. Attribute (dept_id) in the course relation is referring to the
primary key attribute (dept_id) of department relation. Thus the Course is called the
referencing relation, and the department is the referenced relation. All values in (dept_id) of
Course, relation satisfy the referential integrity constraint except the value D06 as it is not
present in (dept_id) attribute in department relation. Referential integrity constraint will not
allow D06 as a value for the foreign key (dept_id) in the course relation.
Figure 3.19: Representation of Foreign Key (dept_id) in Course Relation (Referential Integrity Constraint)
Domain constraints specify the set of possible values that may be associated with an attribute.
A common method of specifying a domain for an attribute is to specify a data type from
which the data values forming the domain are drawn. Below are some of the most
commonly used domain constraints.
A. Null constraint
It specifies whether null values are permitted for an attribute. NOT NULL constraint
makes sure that a column does not hold NULL value. When we do not provide a value
for a particular attribute (column) while inserting a record into a relation, it takes NULL
value by default. By specifying the NOT NULL constraint, we can ensure that a
particular column cannot have NULL values.
For example, if every department must have a valid, non-null value for the
(dept_name) attribute, then the (dept_name) attribute for the department relation is
constrained to be NOT NULL. As shown in Figure 3.20, each tuple of department
relation must have the value for the (dept_name) column as it cannot be left empty.
A unique constraint (also referred to as a unique key constraint) is a rule that forbids
duplicate values in one or more columns within a relation. This constraint is used to
implement secondary or alternate keys.
However, attributes declared as unique are permitted to have null values unless they
have been explicitly declared NOT NULL.
For example, as shown in
Figure 3.20, each row of Department relation must have the unique value for the
(dept_name) column.
C. Check constraint
A check constraint is defined on a column for specifying the range of values that can
be inserted into it, using a predefined condition. When this constraint is set on a
column, it ensures that the specified column must have the value falling in the
specified range.
Example 1 - We understand that the salary of the faculty cannot be a negative value;
it must always be greater than 0. So, in faculty relation (salary), the salary of the faculty
will have a check constraint, i.e., the constraint will ensure that the value of (salary)
will be greater than zero always.
Example 2 - In student relation, the gender can be Female, Male, or Trans. So, (gender)
will have a check constraint that will ensure that the value inserted in each row must
be any one of (Female, Male, or Trans).
D. Default constraint
For example, the default value for the (status) column of Research Project relation can
be taken as "Ongoing".
3.4 Case Study Design with Complete Constraints
Data Dictionary provides the metadata about each relational table in the database, e.g., data
item or column name, suggested data type for that column, maximum allowed column width,
data format to be stored in on the disc, constraints applied on column level and relational
table level, a brief description of the column and example representing actual data item to be
filled in the column.
Since the composite keys are implemented on table level rather than column level, they are
described as separate column UniqueID.
The data dictionary of all the relational tables in the case study is provided below, which will
help us while implementing the actual database operations.
Structured Query Language (SQL) is designed for managing data held in a relational database
management system (RDBMS) like Oracle, MySQL, SQL Server, etc.
It is based on the relational model described by Dr. E.F. Codd in 1970 in the paper, "A
Relational Model of Data for Large Shared Data Banks."
Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use
Codd's model. SEQUEL later became SQL (still pronounced "sequel").
In 1979, Relational Software, Inc. (now Oracle) introduced the first commercially available
implementation of SQL. It was standardized by American National Standards Institute (ANSI)
in 1986 and International Organization for Standardization (ISO) in 1987.
Features of SQL
The ANSI standard for operating relational databases
Open-Source
Efficient
Functionally complete
Easy to learn and use
Case insensitive
Below is the list of data types used in MySQL database. This is based on MySQL 8.0.
The commands used in SQL are generally termed as SQL statements. SQL statements are
categorized into four major categories:
When we start working on implementing the relational model, the first thing starts with
creating the database schema. A database schema contains structures of tables along with
other related items to be discussed in subsequent chapters.
To create a table, SQL provides the CREATE TABLE statement. The CREATE TABLE statement
requires the table name, its column names, data types, and size to be allocated. Some data
types do not require size.
Before moving further you need to install the required software using
“Appendix A: Installation of Oracle”
Example:
Let us create a sample database table using Oracle RDBMS to manage the books in a library.
We can define the table name as Book with column names (also referred to as attributes or
data items or field names) as bookno, title, author, and price. Assuming the maximum
number of books to be stored is a numeric value 9999, the maximum size of the book title is
100 characters long string. The maximum size of the author's name is 50 characters long
string, and the maximum price of the book is a numeric value of 9999.99.
Oracle provides the NUMBER data type to store numeric data and store dynamic length string;
it provides VARCHAR2 data type. Most of the other RDBMS gives VARCHAR as the data type
for dynamic size strings, but Oracle gives us the flexibility to use VARCHAR or VARCHAR2 as
the data type.
Figure 3.21: Representation of table creation in Oracle SQL Developer
Once the table structure is created, we can insert data of books generally referred to as
records or rows or tuples.
SQL provides an INSERT statement to insert the records in the database table.
While inserting data in a database table, we need to remember that the string type and
date/time values are enclosed in a single quote.
Let us insert few records of the books.
While writing multiple SQL statements, use a semicolon (;) to terminate a statement.
Once we insert the record in the database table, we can display the records.
SQL provides SELECT statements to view the records as per choice. We can view all records
with all their columns using the following syntax.
SELECT * FROM tablename;
Example:
To view all the records, present the book table, we can write the following SQL statement.
SELECT * FROM book;
Figure 3.23: Displaying of records from table book in Oracle SQL Developer
While inserting records, as shown above, if we re-run the Insert commands, it will execute
the commands again and insert three records again, creating duplicate records of books
creating ambiguity and inconsistency in the database since we have not specified any
restrictions or constraints on the table book. Now we need to specify the constraints on the
table.
Now re-create the table with the PRIMARY KEY constraint on the column bookno.
Now again, insert the three records into the database table by executing the following
statements.
Figure 3.26: Inserting records in BOOK Tables
Records get inserted property. However, if we repeat the same statement by mistake, an
error will appear from RDBMS due to the PRIMARY KEY constraint applied on the column
bookno. Try to re-run the INSERT statements.
They can be applied when a table is created or when its structure is altered. The constraints
are specified within the SQL DDL command, like the 'CREATE TABLE' command.
The general syntax of 'CREATE TABLE' command specifying the integrity constraint:
Column-level:
Table-level:
Entity integrity constraint implemented using SQL by defining any column as the PRIMARY
KEY. By defining the primary key attribute in a relation, we ensure that the attribute will not
accept a null value. Also, the values will be unique for the tuple.
Column –Wise:
Example:
The following example identifies the dept_id column as the PRIMARY KEY for the Department
table:
Table-Wise:
Example
The primary key constraint in this example is defined after the column comma list in the
CREATE TABLE statement.
Whenever we declare any attribute in relation as the Primary Key attribute, it is not necessary
to specify it explicitly to be not null in addition to the primary key constraint.
A foreign key is a column in a child table that references a primary key in the parent table. A
foreign key constraint is a primary mechanism used to enforce referential integrity between
tables in a relational database. A column defined as a foreign key refers to a column defined
as a primary key in another table.
At column-level:
Example:
CREATE TABLE course (
course_id CHAR(6) PRIMARY KEY,
course_name VARCHAR(50),
course_credit NUMBER(2),
dept_id CHAR(3) REFERENCES department(dept_id)
);
In this example, the dept_id column of the course table has been designated as the foreign
key for the course table. This foreign key references the dept_id column in the department
table. This foreign key ensures that for every dept_id in the course table, there is a
corresponding dept_id in the department table.
At table-level:
Example:
NOT NULL is a constraint that can place on a table's column. This constraint disallows the
entrance of NULL values into a column; in other words, data is required in a NOT NULL column
for each row of data in the table. NULL is generally the default column if NOT NULL is not
specified, allowing NULL values in a column.
Syntax:
Example:
In this example, the NOT NULL constraint is applied on the dept_name column and
dept_location column. The columns will not allow NULL values.
UNIQUE Constraint
This constraint ensures that a column or a group of columns in each row has a distinct value.
A column(s) can have a null value, but the values cannot be duplicated.
Column level:
Example:
Column Level:
Table Level:
CREATE TABLEtablename (
columnname1 datatype(size),
columnname2 datatype(size),
...,
UNIQUE (columnname1)
);
Example:
In the above example, the UNIQUE keyword has been used with the dept_name column. This
column will have distinct values.
CHECK Constraint
This constraint defines a business rule on a column. The check constraint specifies conditions
for the data inserted into a column. Each row inserted into a table or each value updating the
column's value must meet these conditions. The CHECK clause is used to specify check
constraints. The constraint can be applied to a single column or a group of columns.
Syntax at column level:
Column level:
CREATE TABLEtablename (
columnname1 datatype(size) CHECK (condition),
columnname2 datatype(size),
...
);
Table level:
Example:
In the faculty table, the salary of the employee will always be greater than zero.
Column Level:
CREATE TABLE faculty(
faculty_id CHAR(4) PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(20),
designation VARCHAR(50) NOT NULL,
doj DATE NOT NULL,
gender CHAR(6) NOT NULL CHECK (gender in (‘Male’,’Female’,’Trans’)),
salary NUMBER(6) NOT NULL CHECK(salary>0),
dept_id CHAR(3) NOT NULL,
hod_id CHAR(4),
FOREIGN KEY (dept_id) REFERENCES department(dept_id),
FOREIGN KEY (hod_id) REFERENCES faculty(faculty_id)
)
Table Level:
CREATE TABLE faculty(
faculty_id CHAR(4) PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(20),
designation VARCHAR(50) NOT NULL,
doj DATE NOT NULL,
gender CHAR(6) NOT NULL,
salary NUMBER(6) NOT NULL,
dept_id CHAR(3) NOT NULL,
hod_id CHAR(4),
FOREIGN KEY (dept_id) REFERENCES department(dept_id),
FOREIGN KEY (hod_id) REFERENCES faculty(faculty_id),
CHECK(salary>0),
CHECK (gender in (‘Male’,’Female’,’Trans’)
)
DEFAULT Constraint
DEFAULT is a constraint that you can place on a table's column. This constraint inserts the
given default value in the record column if no value is provided for that column while inserting
a record.
Syntax:
Example:
In this example, the DEFAULT constraint is applied to the student_nos column. The
student_nos columns will have zero (0) if we do not specify any value while inserting the
record in the section table.
Appendix A: Installation of Oracle
Here we will learn how to install the required software to implement the database using
Oracle XE RDBMS Software. It is the free version of Oracle RDBMS software with limited
capabilities. In Oracle XE, the XE stands for Express Edition.
Before installing the software, you need to check the minimum hardware specifications
required
Download the latest available software from the link given below
https://www.oracle.com/database/technologies/appdev/xe.html
You will get the following webpage (accessed on 21st May 2021) or a similar page.
Click on the Green button Download Oracle Database XE to download the software.
It will ask you to select your operating system and agree with terms and conditions. Download
the software as a ZIP file and save it somewhere on your disc.
Once the download is complete, you can install the software on your laptop or desktop.
Unzip the downloaded ZIP file and click on the Setup.exe file to start the setup.
While installing the software, it will ask you for a password. Enter the password and
remember that password. It will be required while performing database operations and
schema creation. It is the password for three built-in users SYS, SYSTEM, and PDBADMIN.
Now download the latest available Oracle SQL Developer software from the Oracle website
using the following link
https://www.oracle.com/tools/downloads/sqldev-downloads.html
Figure 3.31: Display of webpage for downloading Zip file of Oracle SQL Developer
Start the Oracle SQL Developer Software from the folder downloaded
Figure 3.32: Display of Oracle SQL Developer Software
Now click on in the top left corner to create a new connection with Oracle XE RDBMS
software.
Define the following information
Name: Any Name of your choice e.g. ABESEC
User Name as SYS. It is the built-in superuser.
Password as given at the time of installation
Role as SYSDBA for Database Administration
Check the check box [ ] Save Password
Service Name as XEPDB1 (default database name)
Now Click on the Test button to check the connection status
If status us Success, that means everything is perfect. Click on Connect button to complete
the connection creation process.
Table of Contents
6.1 Introduction to SQL ................................................................................................................... 4
6.2 Introduction to various RDBMS Software.................................................................................. 5
6.3 Creating and using Database ..................................................................................................... 5
6.4 Managing Tables using DDL Statements in SQL ...................................................................... 11
6.4.1 Using CREATE TABLE statement ......................................................................................... 12
6.4.2 Using ALTER TABLE statement............................................................................................ 15
6.4.3 Using DROP TABLE statement ............................................................................................ 18
6.4.4 Creating tables from case study using data dictionary ....................................................... 19
6.5 Managing Data using DML Statements in SQL ......................................................................... 25
6.5.1 Inserting data using INSERT Statement ............................................................................ 25
6.5.2 Retrieving data using the SELECT statement..................................................................... 26
6.5.3 Restricting data access using WHERE clause ..................................................................... 27
6.5.5 Deleting data using DELETE Statement ............................................................................. 41
6.5.6 Constraint violation using DML Statements...................................................................... 41
6.6 Sorting Data using ORDER BY .................................................................................................. 45
6.7 Using Single-row Functions to Customize Output.................................................................... 48
6.7.1. CHARACTER Functions ...................................................................................................... 49
6.7.1.1 CASE Manipulation function ...................................................................................... 49
6.7.1.2 CHARACTER Manipulation function ........................................................................... 50
6.7.2 NUMBER Functions........................................................................................................... 53
6.7.3 DATE Functions................................................................................................................. 55
6.7.4 GENERAL Functions .......................................................................................................... 58
6.7.5 Conditional Expression ..................................................................................................... 60
6.7.6 CONVERSION Functions .................................................................................................... 61
6.8 Reporting Aggregated Data Using the Aggregate Functions .................................................... 64
6.9 Retrieving Data from Multiple Tables ....................................................................................... 74
6.9.1 Using Comma Operator .................................................................................................... 74
6.9.2 Using SQL Joins ................................................................................................................ 76
6.10 Retrieving data using Subqueries ......................................................................................... 82
6.11 Using Set Operators ................................................................................................................ 90
6.12 Working with Views ................................................................................................................ 96
6.13 Working with Sequences ...................................................................................................... 103
6.14 Controlling user access with DCL Statements ....................................................................... 105
6.15 Transaction Control using TCL Statements ........................................................................ 108
6.15.1 COMMIT Statement ...................................................................................................... 108
6.15.2 SAVEPOINT Statement .................................................................................................. 109
6.16 Exporting data to CSV Format ............................................................................................... 111
6.17 Conclusion ............................................................................................................................ 117
6.1 Introduction to SQL
Structured Query Language (SQL) is a database query language used for storing and managing
data by a Relational Database Management System (RDBMS). It is a common language used
for interacting with an RDBMS. It provides a set of commands called SQL statements to
perform various database operations like
Creation of tables
Insert, update, delete and display the records
Controlling the user access
Managing transactions during insertion, updation and deletion of records
The SQL statements use a set of keywords defined by it like CREATE, ALTER, DROP, INSERT,
UPDATE, DELETE, SELECT, WHERE; to perform the desired actions. It is a case insensitive
language, but for better understanding the syntax and usage of SQL statements, we generally
write the keywords in capital letters and other elements in small letters.
Some keywords are common to all RDBMS, but some keywords can be particular to an
RDBMS. In this unit, we are going to discuss three RDBMS software; Oracle, SQL Server and
MySQL; that are used by most of database professionals and learners. You need to take care
of the common and specific keywords while working on an RDBMS. We will provide you
details of such keywords wherever required.
These SQL statements are divided into four major categories depending on their actions.
To manage the structure of the tables, SQL provides the statements called DDL
statements, where DDL stands for Data Definition Language. The main keywords
under DDL are CREATE, ALTER and DROP.
To manage the records inside the tables, SQL provides the statements called DML
statements, where DML stands for Data Manipulation Language. The main keywords
under DML are INSERT, UPDATE, DELETE and SELECT.
To manage the user access control, SQL provides the statements called DCL, where
DCL stands for Data Control Language. The main keywords under DCL are GRANT and
REVOKE.
To manage the changes made by DML statements in the database, SQL provides the
statements called TCL statements, where TCL stands for Transaction Control
Language. The main keywords under TCL are COMMIT, ROLLBACK and SAVEPOINT.
All such keywords will be covered in detail inside various sections of this unit.
6.2 Introduction to various RDBMS Software
There are many RDBMS software available to manage the data using SQL. Some of them are;
Oracle Database, Microsoft SQL Server, MySQL, IBM DB2, PostgreSQL and MariaDB.
From the given set of RDBMS, we have selected the first three RDBMS to discuss in this unit;
Oracle Database, Microsoft SQL Server and MySQL.
The SQL Server is an RDBMS developed by Microsoft. You can download the latest available
SQL Server Express and Microsoft SQL Server Management Studio from the website for
Microsoft using the following URL
https://www.microsoft.com/en-in/sql-server/sql-server-downloads
The Oracle Database, simply called Oracle, is an RDBMS developed by Oracle Corporation.
You can download the latest available version of Oracle Database from the following URL
https://www.oracle.com/in/downloads
MySQL is an open-source RDBMS. You can download the latest available software from the
following URL
https://dev.mysql.com/downloads
Once the software is ready in your machine, you need to create a database to manage the
records using various database elements like tables, view, stored procedures, triggers, etc.
When we require to manage the records in a database using some RDBMS, we must have a
database. The database stores all your records using a set of tables. Before creating any table,
we need to create a database.
Some RDBMS provides a graphical way to create and use the databases and others may
provide the SQL statements.
Let us create a database named SMS, to manage the tables as per our case study titled
Student Management System.
It creates a database in the name of SMS for you that you can see inside the Object Explorer.
Creating a database in Oracle
First, launch the SQL*plus program using SYS user with SYSDBA role and giving the password
given at the time of Oracle installation. Here DBA stands for Database Administrator.
Now connect with the default database XEPDB1 created at the time of installation using the
following SQL statement at SQL Prompt (SQL>).
Don't type SQL> as mentioned in the statement. It is already typed by the software.
Create a new user to work with. Oracle RDBMS automatically creates a new database for that
user. Use the following SQL statement to create a user user1 with the password 123456
Now give the permissions to the newly created user to work with the Oracle RDBMS. In
technical terminology, it is called Grant the privileges. Use SQL statement GRANT to grant
the privileges. It will be discussed later in detail in subsequent sections.
First, start the MySQL Workbench software and double click on available Local instance
MySQL80
Enter the password given at the time of installation and check the checkbox to save the
password
Once your MySQL Workbench software is ready, now we can create a query window where
we can write the SQL statements.
Type the following SQL statement inside the available query window, select it and execute it
to create the database SMS.
Now we are ready to create our tables in all the RDBMS and work with SQL statements to
store and manage our data as per the case study or your own choice.
Using a database
Before doing any operation on a database, we need to select that database. Different RDBMS
provide different ways to use the current database.
Right click on the SMS database in the Object Explorer window to get the context menu and
select New Query option to open a window to write and execute SQL Statements.
Use the USE statement to make a database as the current database. Write the following
statement and execute it to make the SMS database the current database.
USE sms;
Data Definition Language (DLL) provides the SQL statements to work on the structure of tables
and other elements like Views, Stored Procedures, Triggers and Indexes that will be discussed
in the following sections and units.
To work with the structure of the tables, DDL provides three statements
CREATE TABLE
ALTER TABLE
DROP TABLE
Some RDBMS also provides the graphical tools to create the tables and generate the SQL
statements as a background process. We will learn both ways, but to better understand the
concept, we must start with understanding and writing the SQL statements and later use the
GUI tools to accomplish a task quickly.
The CREATE TABLE statement is used to create a new table inside the current database. In
general, it requires three elements
Column names
Data type of columns
Maximum allowed size in a column
General Syntax of CREATE TABLE
column_name1 datatype(size),
column_name2 datatype(size),
);
SQL does not provide its own data types. The data types are specific to an RDBMS that we are
using to manage our data. Some commonly used data type names used by different RDBMS
are CHAR, VARCHAR, VARCHAR2, NUMBER, INT, FLOAT, DATE, DATETIME and TIMESTAMP.
The CHAR and VARCHAR data types are used to store string type data but with a bit of
difference. The CHAR data type is used to store fixed-length strings, e.g., pincode, mobile. In
general, the pincode is stored in 6 digits and the mobile number is stored in 10 digits. The
VARCHAR data type is used to store variable-length strings, e.g., address.
To manage the variable-length strings, SQL Server and MySQL provide VARCHAR data type
while Oracle provides VARCHAR2 data type.
The default length for VARCHAR2 is 4000 bytes which can go up to 32767 bytes.
VARCHAR uses a total of 65535 bytes where the first two bytes store the length of the string
and 65533 bytes to store the data.
If we use VARCHAR inside the Oracle, it treats it as a pseudotype for VARCHAR2. While using
oracle prefer to use VARCHAR2 rather than VARCHAR.
Different RDBMS generate a different version of SQL based on their own data types. The
SQL editors also have different colors and fonts.
Example
Create a table book having four columns; bookno as integer number with max limit of 9999,
title as variable length string with max size of 50 characters long, status as fixed-length string
of 1 character, price as the floating number with max limit 9999.99.
If you see the above SQL statements, Oracle has NUMBER data type for storing integer and floating
values while SQL Server and MySQL have INT data type for integer type numeric values and FLOAT
data type for floating type numeric values.
Creating a table with constraints
While creating a table for business applications, we need to apply the business rules
(restrictions or constraints) on the tables. The CREATE TABLE statement also requires a fourth
element called constraint. Each business rule or constraint can be given a name using the
CONSTRAINT keyword. It is optional but suggested to have the constraint names for better
database design and allow making changes in the rules later if required.
Constraints can be applied to column level or table level both. At the column level, constraints
are applied to the column, i.e., immediately after defining the length of the column. While in
table level, constraints are applied after declaring all the column(s), i.e., at the end. We can
also use a combination of both column or table level constraints during table creation. The
same is discussed in the syntax mentioned below:
Example
Create a table book having four columns; bookno as integer number with max limit of 9999,
title as variable length string with max size of 50 characters long, status as fixed-length string
of 1 character, price as a floating number with max limit 9999.99.
Apply the following constraints on different columns of the book table. The bookno is a
PRIMARY KEY, title cannot be NULL, status has default value Y and price cannot be NULL and
should be greater than 0.
ALTER TABLE is used to add, delete, modify or rename columns in an existing table
ALTER TABLE is also used to add, modify and drop various constraints on an existing
table
ALTER TABLE is also used to enable or disable the imposed constraint on the table
Adding a constraint
We can apply the constraints even after creating the tables using ADD CONSTRAINT with
ALTER TABLE.
Example: Add a UNIQUE constraint on title column in book table and name the constraint as
book_title_u
We can remove a named constraint from a table using DROP CONSTRAINT with ALTER TABLE.
Syntax | Oracle | SQL Server | MySQL
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
Modifying a constraint
We cannot modify an existing constraint but we can modify the NULL and NOT NULL
constraints if required.
Example: Apply NULL or NOT NULL constraint on title column of book table
Syntax | Oracle
ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
ALTER TABLE table_name ENABLE CONSTRAINT constraint_name;
Example 1: Add a UNIQUE constraint book_title_u on title column of book table.
SQL Statement | Oracle
ALTER TABLE book ADD CONSTRAINT book_title_u UNIQUE(title);
Renaming a column
SQL Server RDBMS do not support renaming of columns via SQL statement. We need to
drop a column and add new column. But Oracle and MySQL support renaming the columns.
The DROP TABLE statement is used to delete a table from a database including its records.
Table once deleted cannot be recovered. Use this statement very carefully.
You can have a look at Microsoft SQL Management Studio, Oracle SQL Developer, MySQL
Workbench, where you have to actually write and execute these SQL statements in the
following figures.
Creating the course table with defined constraints
While using CHECK constraints, we have used IN operator for domain check and relational
operator (>) for comparison. Operators will be discussed in detail in later sections.
Once the structure of tables is created, now we can insert, update, delete and select the data.
All such operations together are called CRUD operations, where CRUD stands for Create,
Read, Update and Delete.
SQL statements used to perform such operations are known as DML statements, where DML
stands for Data Manipulation Language. There are four statements under DML.
INSERT
UPDATE
DELETE
SELECT
The INSERT statement is used to insert the records in the table, the UPDATE statement is used
to modify the records, DELETE statement is used to delete the records and SELECT statement
is used to display the records.
The INSERT statement is used to insert new records in the tables using different syntaxes
depending on requirements.
Example
INSERT INTO section (section_name,max_capacity,dept_id) VALUES('A',70,'D01');
Now we can insert the sample records given in "Appendix B: SQL Script for SMS Case Study
using Oracle” of Unit 3 into our tables to understand the concept better.
Once the data get inserted into the tables, we can retrieve it and display it. SELECT command
is used to retrieve or fetch the stored data. There are broadly four ways to retrieve data:
All columns with all records
Specific column with all records
Specific columns with selected set of records.
All columns with selected sets of records.
Example
SELECT * FROM department;
Output
DEPT_ID DEPT_NAME DEPT_LOCATION
D01 CS Aryabhatta Block
D02 CSE Aryabhatta Block
D03 IT Aryabhatta Block
D04 CSE-DS Bhabha Block
D05 ME Ramanujan Block
Example
SELECT dept_id,dept_name FROM department;
Output
DEPT_ID DEPT_NAME
D01 CS
D02 CSE
D03 IT
D04 CSE-DS
D05 ME
Such syntax of data access is called restricted data access and explained in section 5.6.3
Example
SELECT * FROM department WHERE pincode = '110063';
The WHERE clause is used to limit the number of records retrieved from a table. It is used to
select a set of records based on given conditions.
In the syntax,
WHERE: restricts the query to the records that meet a condition.
Condition(s) : is composed of a column names, expressions, constants and operators i.e.
column|expression operator values
WHERE clause may contain one or more than one condition that must be satisfied, and it
appears immediately after FROM clause. If the condition evaluates to true, the records
meeting the condition are returned.
Arithmetic Operator
The table below lists the arithmetic operators that are available in SQL.
Table 1
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Arithmetic operators can be used in any clause of a SQL statement ( except the FROM
clause)
With the DATE and TIMESTAMP data types, only addition and subtraction operators
will work.
2) -: Subtract operator
3) *: Multiply operator
4) / : Divide Operator
Operator Precedence
Concatenation Operator
Links columns or character strings to other columns.
Is represented by two vertical bars(||)
Creates a resultant column that is a character expression.
Comparison Conditions
Comparison conditions are used in conditions in WHERE clause to compare one expression
value to another expression value. Different types of comparison operators with their
meanings are listed in the table below:
Operator Meaning
= Equal To
> Greater Than
>= Greater Than equal to
< Less Than
<= Less Than equal to
<> , ^= , != Not equal to
BETWEEN…..AND….. Between two values (inclusive)
IN(set) Match any set of values
LIKE Match a character pattern
IS NULL Is a Null value
Syntax
…… where expr operator value
Example
…… where dept_id= 'D02'
…… where salary >= 50000
1) = : Equal To
Explanation: In the example, the SELECT statement retrieves the faculty_id, first_name and
salary of all faculty members those have salary greater than and equal to 70000
Values that are specified with the BETWEEN operator are inclusive.
It is also known as the RANGE operator. The range must be specified with a
lower limit followed by an upper limit. Remember, the lower limit must be
specified first.
BETWEEN operator can be used for character values also.
Output
FACULTY_ID FIRST_NAME SALARY
1231 Ram 54000
9765 Laxman 72000
2765 Grish 62000
1734 Nidhi 56000
1386 Shyam 57000
1497 Mala 70300
3768 Aakash 63000
1695 Sneha 76000
1123 Ashutosh 76000
1762 Ankur 56000
1793 Babita 70000
1859 Divya 77000
1998 Ashutosh 57000
1711 Elina 55000
Explanation: In the example, the SELECT statement retrieves the faculty_id, first_name and
salary of all faculty members whose salary is in the range of [54000, 85000], i.e., BETWEEN
54000 and 85000.
8) IN(set): Match any set of values, provided the set of values must be of similar data
type.
For testing specified values IN condition is used.
The IN condition is also known as the membership condition.
The IN condition can be used with any data type.
Output
FACULTY_ID FIRST_NAME SALARY
1231 Ram 54000
9765 Laxman 72000
1734 Nidhi 56000
1762 Ankur 56000
Explanation: The example displays faculty_id, first_name and salary from faculty where salary
is 54000 or 56000 or 72000.
Note: The above query could be written using multiple OR operators. However, we prefer to use
IN operator in terms of optimization.
SELECT faculty_id, first_name, salary FROM faculty WHERE salary = 54000 OR
salary=56000 OR salary =72000;
Output
FACULTY_ID FIRST_NAME SALARY
1231 Ram 54000
1386 Shyam 57000
Explanation: The select statement in the above example return the faculty_id and first_name
from faculty table for those faculties whose first name contains the last letter as m.
Output
FACULTY_ID FIRST_NAME SALARY
1734 Nidhi 56000
Explanation: The select statement in the above example return the faculty_id and first_name
from faculty table for any faculty whose first name contains an id letter at any position.
Output
FACULTY_ID FIRST_NAME DEPT_ID HOD_ID
1231 Ram D01 NULL
9765 Laxman D02 NULL
2765 Grish D05 NULL
1734 Nidhi D03 NULL
1859 Divya D04 NULL
Explanation: The select statement in the above example return the faculty_id,first_name,
dept_id and hod_id from faculty table for those faculty which do not have any HOD
associated with them.
Logical Conditions
Table 4
Operator Meaning
AND Return True if both component conditions are true
OR Return True if either component conditions are true
NOT Return True if following conditions is false
Output
ROLL_NO FIRST_NAME DEPT_ID SECTION_NAME
191306280 Aayushi D01 A
190006876 Nikhil D02 A
191306286 Swati D02 A
191106654 Anmol D05 A
191106660 Amit D03 A
Explanation: In this example, the select statement return roll_no, first_name, dept_id and
section_name of those students who either belong to 'D02' department or belongs to section
'A'.
Rules of Precedence
Operator Meaning
1 Arithmetic Operators
2 Concatenation Operator
3 Comparison Conditions
4 IS [NOT] NULL , LIKE ,NOT IN
5 [NOT] BETWEEN
6 NOT Equal To
7 NOT Logical Conditions
8 AND Logical Conditions
9 OR Logical Conditions
Output
ROLL_NO FIRST_NAME DEPT_ID SECTION_NAME
190006876 Nikhil D02 A
191306286 Swati D02 A
Explanation: In this example, the select statement return roll_no, first_name, dept_id and
section_name from student table where student is Male and must belong to either dept_id
'D02' or dept_id 'D01'.
The UPDATE statement is used to modify one or more records present in the table based on
the given condition. Beware that if no condition is not specified, it will update all the records.
Example 1
If we want to change the department location (dept_location) of dept_id 'D04' from 'Bhabha
Block' to 'Raman Block', we have to use the following SQL statement.
Example 2
If we want to increase the salary of all faculty members by 20% and this increment must
reflect in the salary column of faculty table, we have to use the following SQL statement.
The DELETE statement is used to delete one or more records from the table based on given
condition. Beware that, if no condition is specified in WHERE clause, all the records in the
table will be deleted.
Note: DELETE Statement is used to delete complete record from the table, while to delete
any specific column value we will use UPDATE command and set value as NULL.
Example 1: If we want to delete the records of those of a student from the table
student_phone_no having rollo_no as 191306280, we have to use the following SQL
statement
Example 2: If we want to delete the all the records present in student_phone_no table, we
have to use the following SQL statement
When we make changes in the records using DML Statements like INSERT, UPDATE or DELETE,
some constraint violations may occur.
INSERT Operations
While inserting records in a table, it may cause violation of the constraints in the following
way:
Domain constraint: Domain constraint gets violated only when a given value to the
attribute does not appear in the corresponding domain or it is not of the appropriate
data type. For example, in the department table, the dept_id column has three
character length, but it will violate the domain constraint if you provide data greater
than three characters. Therefore, RDBMS will terminate the statement and data will
not be inserted in the table.
SQL Statement | Oracle | SQL Server | MySQL
INSERT INTO department VALUES('D006','MBA','Raman Block');
Output from Oracle RDBMS:
ORA-12899: value too large for column "SMS"."DEPARTMENT"."DEPT_ID" (actual: 4,
maximum: 3)
Entity Integrity Constraint: On inserting NULL values to any part of the primary key of
a new record in the table can cause violation of the Entity integrity constraint. For
example, if we try to insert NULL values for the dept_id column in the department
table, it will violate the entity integrity constraint and RDBMS rejects the insertion.
SQL Statement | Oracle | SQL Server | MySQL
INSERT INTO department VALUES(NULL,'MBA','Raman Block');
Output from Oracle RDBMS:
ORA-01400: cannot insert NULL into ("SMS"."DEPARTMENT"."DEPT_ID")
Key Constraints: On inserting a value in the new record in a table that already exists
in another record of the same table for a column defined as primary key, it can cause
violation of the key constraint. For example, if we try to insert the dept_id as D01 again
in the department table, it will violate the key constraint and RDBMS will reject the
insertion of the new record.
SQL Statement | Oracle | SQL Server | MySQL
INSERT INTO department VALUES('D001','MBA','Raman Block');
Referential integrity: On inserting a value in the foreign key of table 1, for which there
is no corresponding value in the primary key, which is referred to in table 2, in such
case, referential integrity is violated. For example, in the mini_project table, the
column roll_no is defined as a foreign key referred from the student table. Therefore,
if we try to insert a new record for a mini-project with a roll number that does not
exist in the student table (e.g., 191306290), it will violate the referential integrity
constraint and RDBMS will reject the insertion.
SQL Statement | Oracle | SQL Server | MySQL
INSERT INTO mini_project VALUES('MP007','Maleware
Detector','Security','Information Security','Maleware Detection
Tool','191306290');
Output from Oracle RDBMS:
ORA-02291: integrity constraint (SMS.FK_MINPRO_STU) violated - parent key not
found
DELETE Operations
When deleting the record from a table, it may cause only a violation of Referential integrity
constraints.
Referential Integrity Constraints: It causes violation only if the record in table 1 is
deleted, referenced by a foreign key from other records of table 2 in the database. If
such deletion occurs, then the values in the foreign key record in table 2 will become
empty, which will eventually violate the referential integrity constraint.
Solutions that can correct the violation of referential integrity due to deletion are called ON
DELETE, which can be applied in three ways.
RESTRICT
CASCADE
SET NULL or SET DEFAULT
If RESTRICT is applied with ON DELETE, the RDBMS rejects the deletion. It is by default.
If CASCADE is applied with ON DELETE and a record in the parent table(referencing relation)
is deleted, then the corresponding records in the child table(referenced relation) will
automatically be deleted.
IF SET NULL or SET DEFAULT is applied with ON DELETE, we can modify the referencing
attribute values that cause violation and we either set NULL or change to another valid value.
For example, if we create two sample tables, countries and states, the counties table
references the state. If we delete any country code, its data in the states table will also be
deleted automatically.
Now we will have the following data in the countries table and states table.
If we issue the DELETE statement to delete a country code, it will automatically delete all the
state records for a given country code.
SQL Statement | SQL Server | MySQL
DELETE FROM countries WHERE countryid='1';
If you try to see the records in the states table, it will display no record.
UPDATE Operation
On updating the record from a table, it may cause only violation of Referential integrity
constraints.
Referential Integrity Constraints: When we create a foreign key using UPDATE
CASCADE, the referencing rows are updated in the child table when the referenced
row is updated in the parent table, which has a primary key.
Now, if we use these tables, then we will have the following data
SQL Statement | SQL Server | MySQL
SELECT * FROM countries;
Output
COUNTRYID COUNTRYNAME COUNTRYCODE
1 United States USA
2 United Kingdom UK
If we issue the UPDATE statement to update a country code in the countries table, it will
automatically delete the country code in the states table.
Order By Clause
The ORDER BY clause retrieves the rows of resultant query either in ascending or descending
order based on any column/expression/numeric position of the column as per specified
syntax. We can use more than one column in the ORDER BY clause, i.e., nested sorting.
ORDER BY clause must be the last clause of the SQL statement.
Note:
Sorting can be done using the column which is not in the SELECT list in a particular
query.
NULL values are displayed last for ascending sequences and first for descending
sequences
Functions make the SQL query more powerful. They are broadly categorized into two
categories:
a) Single row Function
b) Multiple row function
The functions accept a set of arguments and manipulate data values. They are used to
perform the following operations:
Perform calculations on data
Manipulate individual data item
Manipulate output for groups of rows
Format dates and numbers for display
Convert the data types if needed for display
In this section, we will discuss the single-row functions. Single-row functions return a single
result row for every row of a queried table or view. These functions can appear in select lists,
WHERE clauses, and HAVING clauses. Single row functions are of 6 different types, as shown
in Figure 6.7.1:
Character
Conversion Date
Character functions accept character data as an input and can return both character and
number values. Input can be a value of character type or column of character data type.
Character function as shown in Figure 6.7.2 below is divided into the following:
Character LENGTH
CONCAT
LOWER
Case Character SUBSTR
UPPER manipulation manipulation INSTR
INITCAP LPAD|RPAD
TRIM
Figure 6.7.2 Types of character functions
Example 1:
Let’s understand it by displaying the faculty_id, faculty_name, and salary of those employees
whose last_name in a database table is 'Prasad'. If we tried to match the last_name as
'PRASAD', no record would be found since the data is case sensitive, but if we apply the
UPPER() or LOWER() function, it can help us in matching records.
Example 2
If we want to display formatted data where a message with the first letter in caps, first_name,
and last_name in the upper case by matching the last_name as 'rana', we have to make the
following SQL statement
Character-manipulation functions are used to extract, change, format, or alter in some way a
character string. One or more characters or words are passed into the function and the
function will then perform its functionality on the input character strings and return the
changed, extracted, counted, or altered value.
CONCAT() Function: is used to concatenate or join two values together to display the data.
Values could be expression or column as shown below:
SUBSTR() Function: is used to extract the part of the string as per specified length. It has
two variants depending on the number of passed arguments. The arguments are
(character String, starting position, length).
The Length argument is optional, and if omitted, returns all characters to the end of the
string.
** Here substring of given length 3 is extracted starting from 2nd position in first_name column**
Output:
FIRST_NAME Part Of Firstname
Ram am
Laxman axm
Ayush Yus
…………
** Here, substring is extracted starting from 2nd position till the end. **
Output:
FIRST_NAME Till End
Ram am
Laxman axman
Ayush yush
……
LENGTH(): The Length function is used to find the number of characters in a string. It
returns the numeric value as an output.
SQL Statement | Oracle
SELECT first_name, salary, length(designation) FROM faculty WHERE
length(last_name)=6;
Output:
FIRST_NAME SALARY LENGTH(DESIGNATION)
Ram 54000 19
Laxman 72000 19
Grish 62000 19
………
Explanation: In the above example, only those rows returned where the length of designation
is greater than 8. In filtered records, first_name & last_name is concatenated, the first
occurrence of ‘a’ is returned using the INSTR() function and substring is extracted using the
SUBSTR() function from the faculty table.
Number functions accept the numeric input and return numeric values. To understand the
functions under this section, we will use the dummy table dual provided by Oracle. SQL Server
and MySQL RDBMSs do not require the dual table for expressions. Different RDBMS may
generate different decimal places in the output, but values will be the same.
ROUND(): The Round function rounds the value to the n decimal values. If n is not
specified, there won't be any decimal places. If n is negative, numbers to the left of the
decimal point are rounded.
TRUNC(): The Trunc function truncates the value to the n decimal places. If n is omitted,
then n defaults to zero.
Syntax: TRUNC(column|expression,n)
The table is given below to summarize the number functions for a quick review:
Functions Results
ROUND(456.65.1) 456.7
ROUND(446.56,-2) 400
TRUNC(456.65,1) 456.6
TRUNC(446.56,-1) 440
MOD(131,11) 10
ABS(-4563.0) 4563
Since a database store dates as a number so we can perform an arithmetic calculation with
dates in the following manner:
o Add or subtract a number to or from the date for the resultant date. Output is
a new date.
o We can find the number of days between any two dates by doing subtraction.
Output is the number that shows the number of days between two dates.
o We can add hours to any specific date by dividing the number of hours by 24.
o SYSDATE is a system-defined variable in Oracle RDBMS that returns the current
system date and timestamp.
There are some predefined DATE functions. Let's understand each in the below section.
MONTHS_BETWEEN(): is used to find the number of months between two dates.
Syntax: MONTHS_BETWEEN(date1,date2),If date 1 is later than date 2 then result is
positive else negative. We can apply the ABS function also to make the result positive.
ADD_MONTHS(): is used to add or subtract the number of calendar months to the given
date.
Syntax: ADD_MONTHS(date,n), n must be an integer and cannot be negative
NEXT_DAY(): finds the date of the next specified day of the week.
Syntax: NEXT_DAY(date,'char'), the value of char may be a number representing a day or
character string.
LAST_DAY(): returns the last date of the month.
Syntax: LAST_DAY(date)
ROUND(): returns the date rounded to the specified format.
Syntax: ROUND(date [,'fmt']), the date is rounded to the unit specified by the format 'fmt'.
If 'fmt' is omitted, then the date is rounded to the nearest day.
TRUNC(): returns the date truncated to the specified
Syntax: Trunc(date [,'fmt']), format returns date with the time portion of the day
truncated to the unit specified by the format 'fmt'. If 'fmt' is omitted, then the date is
truncated to the nearest day.
……
Note: Due to the limitation of rows in the existing case study SMS, we will now use another
table emp or DUAL table to understand the following two types of function, i.e.,
GENERALfunction and CONVERSION function. Emp table is shown below for reference and
providedin the script under Additional Tables.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839 KING PRESIDEN - 17-Nov-81 5000 - 10
7698 BLAKE MANAGER 7839 01-May-81 2850 - 30
7782 CLARK MANAGER 7839 09-Jun-81 2450 - 10
7566 JONES MANAGER 7839 02-Apr-81 2975 - 20
7788 SCOTT ANALYST 7566 19-Apr-87 3000 - 20
…….
6.7.4 GENERAL Functions
GENERAL functions are used to deal with NULL values. These functions are listed under a
single row function that returns a single value for a row in the result set. The general functions
work with any data type. GENERAL functions are provided by ORACLE are:
NVL,NVL2,NULLIF,COALESCE
NVL(): used to convert NULL value to an actual value. The NVL function takes two
arguments as its input. If the first argument is NULL, it returns the second argument;
otherwise, it returns the first argument.
Syntax: NVL(column|expression1,expression2)
In syntax:
column| expression 1: is the input or source value that contains NULL values.
expression2: is the target value that will replace the NULL in a given source or input.
Remember both arguments are of the same data type as shown in the table below:
Data Type Conversion
NUMBER NVL(numeric_column,9)
DATE NVL(date_column,'24-FEB-21')
CHAR or VARCHAR NVL(char_varchar_col, 'string or character')
Similarly, if any character or date column has NULL values, then the character or date value
can be replaced.
NULLIF(): The NULLIF function compares the two expressions and returns NULL if they are
equal; otherwise, it returns the first expression.
Syntax: NULLIF(expr1, expr2), if both expressions are equal, then NULL is returned;
otherwise, expr1 will return.
Conditional expressions are expressed using two methods, CASE and DECODE. Here we will
discuss the CASE method. DECODE is specific to ORACLE only.
CASE Expression: provides the way of implementing IF-THEN-ELSE logic within the SQL
statement. The CASE expression complies with ANSI SQL.
Syntax | Oracle
CASE WHEN comparision_expr1 THEN return_expr1
[WHEN comparision_expr2 THEN return_expr2
WHEN comparision_exprn THEN return_exprn
ELSE else_expr]
END AS colum_aliasing
Conversion functions are used to convert the data type of values from one to another. There
are two ways to data type conversions as discussed below:
Implicit data type conversion: automatically convert the data type. Oracle server
automatically converts the following data type:
FROM TO
VARCHAR or CHAR NUMBER
VARCHAR or CHAR DATE
NUMBER VARCHAR
DATE VARCHAR
Explicit data type conversion: ORACLE provides three functions to convert a value from one
data type to another, as shown in Fig. 6.7.3 below:
TO_NUMBER TO_DATE TO_CHAR TO_CHAR
TO_CHAR(): converts the source value of number or date data type in the specified format
of source into character.
Syntax: TO_CHAR(date|number|column,[ format]) converts a number or date value to a
VARCHAR character string with the specified format.
There are different elements of dates in terms of YEAR, MONTH & DAY as shown in the
summary table below:
Elements of Date Results
YYYY Full years in numbers
YEAR Year spelled out (in English)
MM Two-digit value for month
MONTH Full name of month
MON Three-letter abbreviation of the month
DY Three-letter abbreviation of the day of the week
DAY Full name of the day of the week
DD Numeric day of the month
Explanation: In the above example, the system date is used, and converting date value into
specified format is projected using DUAL table.
Explanation: In the above example, a number value is used and how to convert numeric value
as per specified format is projected using DUAL table.
TO_DATE(): converts string with specified format into default date format.
Syntax: To_date(character,[format]) converts a character string representing a date to a date
value according to the specified format. If format argument is omitted, then the default
format is DD-MON-YY.
Explanation: In the above example, the date data type value is used, and how to convert the
date value as per the specified format is projected using the DUAL table.
Aggregate/Group functions operate on sets of rows and return a single result per group.
These sets may comprise the entire table as shown in Figure 1 or the table is split into groups
as shown in Figure 2.
Aggregate functions can appear in select lists and ORDER BY and HAVING clauses. However,
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.
Note:
DISTINCT makes the function consider only non-duplicate values
ALL makes it consider all values, including duplicates. Default is ALL and therefore need
not be specified.
The datatypes for the functions with expr as an argument may be CHAR, VARCHAR2,
NUMBER or DATE.
All aggregate functions ignore null values. Use NVL(), NVL2() or COALESCE() function to
substitute a value for null values.
AVG () Function
Syntax | Oracle | SQL Server | MySQL
SELECT [column_list,], AVG(aggregate_expression)
FROM table_name
[WHERE condition]
[GROUP BY column(s)];
Parameters or Arguments:
expression1, expression2: Expressions that are not encapsulated within the AVG function
must be included in the GROUP BY clause at the end of the SQL statement.
aggregate_expression: This is the column or expression that will be averaged.
Tables: The tables that you wish to retrieve records from. There must be at least one table
listed in the FROM clause.
WHERE conditions: Optional. These are conditions that must be met for the records to be
selected.
AVG () Function | Example with a single field
SQL Statement | Oracle | SQL Server | MySQL
SELECT AVG (salary) AS "Average Salary"
FROM faculty;
Output:
AVG(SALARY)
67593.75
In this AVG function example, we have aliased the AVG (SALARY) expression as "Average
Salary"
COUNT () Function
Syntax | Oracle | SQL Server | MySQL
SELECT [column_list], COUNT (aggregate_expression)
FROM table_name
[WHERE condition]
[GROUP BY column(s)];
MAX () Function
Syntax | Oracle | SQL Server | MySQL
SELECT [column_list], MAX(aggregate_expression)
FROM table_name
[WHERE condition]
[GROUP BY column(s)];
MIN () Function
Syntax | Oracle | SQL Server | MySQL
SELECT [column_list], MIN(aggregate_expression)
FROM table_name
[WHERE condition]
[GROUP BY column(s)];
SUM () Function
Syntax | Oracle | SQL Server | MySQL
SELECT [column_list], SUM(aggregate_expression)
FROM table_name
[WHERE condition(s)]
[GROUP BY column]
[ORDER BY column(s)];
Standard deviation is defined as the square root of the variance. STDDEV operates on a group
of rows and returns one row of output. Null values are ignored by the STDDEV() function. You
can use the DISTINCT keyword to exclude duplicate entries.
STDDEV () Function
Syntax | Oracle
SELECT [column_list], STDDEV(aggregate_expression)
FROM table_name
[WHERE condition(s)]
[GROUP BY column]
[ORDER BY column(s)];
Deviation In Increment
1371
VARIANCE () Function
Syntax | Oracle
SELECT [column_list], VARIANCE(aggregate_expression)
FROM table_name
[WHERE condition(s)]
[GROUP BY column]
[ORDER BY column(s)];
Variance In Salary
188094105.1
Variance In Increment
1880941
Note:
Any column or expression in the SELECT list that is not an aggregate function must be in the
GROUP BY clause.
The comma operator (,) is used to combine records from two or more tables in a database.
Suppose there are two tables T1 having x no of columns, m no of records, and table T2 having
y no of columns, n no of records. After applying the comma operator, x+y no of column and
m*n no of records will be in the resultant table. To filter some desired records from the
resultant table, we can use the WHERE clause.
Example: Suppose we would like to display COURSE information along with DEPARTMENT
information. This means we have to combine two tables using a comma operator.
Course Department
course_id course_name course_credit dept_id dept_id dept_name dept_location
KCS301 Data Structure 3 D01 D01 CS Aryabhatta Block
KCS401 DBMS 3 D02 D02 CSE Aryabhatta Block
KME502 Machine Design 2 D05 D03 IT Aryabhatta Block
KEC301 Digital Electronics 2 D03 D04 CSE-DS Bhabha Block
KAS101 Physics 3 D02 D05 ME Ramanujan Block
In the above output, the orange box showing the COURSE table, and the green box showing
the DEPARTMENT table. In the resultant table, some records are not valid as per our case
study, e.g., Record no 1, physics course is mapping with CS department, but our case study
physics course is only running in CSE department and many more invalid records. We are
getting these records because of the comma operator working. Comma operator maps each
record of the first table with each record of the second table.
Output:
The SQL Joins clause is used to combine records from two or more tables in a database. A
JOIN is a means for combining fields from two tables by using values common to each one.
To apply the JOIN on two relations, there must be one common attribute. In addition, the
common attribute must have the same data type and size in both relations; the name of the
attribute may be or may not be the same.
For example, we have to display the COURSE information (course id, course_name,
course_credit, dept_id) along with their DEPARTMENT information (dept_id, dept_name,
depat_location). We can do this by combing the columns from the following tables.
Course Department
course_id course_name course_credit dept_id dept_id dept_name dept_location
KCS301 Data Structure 3 D01 D01 CS Aryabhatta Block
KCS401 DBMS 3 D02 D02 CSE Aryabhatta Block
KME502 Machine Design 2 D05 D03 IT Aryabhatta Block
KEC301 Digital Electronics 2 D03 D04 CSE-DS Bhabha Block
KAS101 Physics 3 D02 D05 ME Ramanujan Block
To combine the columns as per requirement, from these two tables, we can use SQL Joins.
Different types of SQL Joins are:
NATURAL JOIN
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
SELF JOIN
NATURAL JOIN: The NATURAL JOIN keyword selects all rows from both the tables as long as
the condition satisfies. We apply NATURAL JOIN when common attributes in both relations
have the same name and the same data type and size. This keyword will create the result-
set by combining all rows from both the tables where the condition satisfies, i.e., the
common field's value will be the same.
Syntax | Oracle
SELECT table1.column1, table1.column2, table2.column1,…
FROM table1 NATURAL JOIN table2;
Example: Display COURSE Information along with DEPARTMENT Information with NATURAL
JOIN.
Explanation: In the above output, we have applied NATURAL JOIN. When the dept_id of
COURSE table is the same as the dept_id of the DEPARTMENT table, that record will be
displayed.
INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies. This keyword will create the result-set by combining all rows from both
the tables where the condition satisfies, i.e., the common field's value will be the same.
Example 1: Display COURSE Information along with DEPARTMENT Information with INNER
JOIN.
SQL Statement | Oracle | SQL Server | MySQL
SELECT course.course_id, course.course_name, course.course_credit, course.dept_id,
department.dept_id,department.dept_name,department.dept_location
FROM course INNER JOIN department ON course.dept_id = department.dept_id;
Output:
course_id course_name course_credit dept_id dept_id dept_name dept_location
KAS101 Physics 3 D02 D02 CSE Aryabhatta Block
KCS301 Data Structure 3 D01 D01 CS Aryabhatta Block
KCS401 DBMS 3 D02 D02 CSE Aryabhatta Block
KEC301 Digital Electron 2 D03 D03 IT Aryabhatta Block
KME502 Machine Desig 2 D05 D05 ME Ramanujan Block
Explanation: In the above output, we have applied INNER JOIN. Orange box showing COURSE
table and green box showing DEPARTMENT table. When the dept_id of COURSE table is the
same as the dept_id of the DEPARTMENT table, that record will be displayed.
Output:
dept_id dept_name section_name max_capacity student_nos
D01 CS A 70 63
D01 CS B 70 67
D02 CSE A 70 65
D04 CSE-DS A 70 NULL
D03 IT A 70 70
D05 ME A 60 32
D05 ME B 60 31
LEFT JOIN: The LEFT JOIN returns all rows from the left table, even if there are no matches
in the right table. This means that a left join returns all the values from the left table, plus
matched values from the right table or NULL in case of no matching join predicate.
Example: Let us assume one scenario in our case study. Mini Projects are allocated to the
students. Some mini projects are not assigned because it is not mandatory to choose a mini
project for every student. Display the student’s information (roll_no, first_name, dept_id)
with their allocated mini project details (miniproj_id, miniproj_name, domain, subject,
description)
In this example, we have to display all student’s information with allocated mini-project
details.
MINI_PROJECT
miniproj_id miniproj_name domain subject description roll_no(FK)
STUDENT
roll_no first_name middle_ name last_ name dob gender house_no street_name city state pincode dept_id (FK) section_name(FK)
190006876 Nikhil NULL Gupta 22-07-2000 Male G/67 Shahi Road Shimla Himachal Pradesh 223344 D02 A
191006345 Satynder Kumar Bhatia 15-06-2001 Male B/35 Kavi Nagar Ghaziabad Uttar Pradesh 200100 D01 B
191106654 Anmol Lal Ranjan 20-02-2002 Male A/987 Main Market Mysore Karnataka 102030 D05 A
191106660 Amit NULL Singh 14-01-2002 Male C/21 Main Market Raj Nagar Ghaziabad 200101 D03 A
191306279 Ram Kumar Singh 21-04-2002 Male 34/45 Civil Line Allahabad Uttar Pradesh 201016 D02 A
191306280 Aayushi NULL Mishra 12-03-2002 Female 243 Shahdra Delhi Delhi 110063 D01 A
191306286 Swati NULL Srivastava 19-12-2001 Female 456 Rajpur Road Dehradun Uttarakhand 114455 D02 A
Output:
roll_no first_name dept_id (FK) miniproj_id miniproj_name description
AI is used to construct models to
190006876 Nikhil D02 MP003 Tumor detection System
predict any kind of tumor
Malware has become a big risk in
191006345 Satynder D01 MP002 Malware detection System
todays time
It is a text analysis technique that uses
191106654 Anmol D05 MP005 Opinion Mining computational linguistics and natural
language processing
Development of a new crawler
191106660 Amit D03 MP006 Design of crawler system algorithm for mining social data from
internet.
191306279 Ram D02 NULL NULL NULL
It seeks to predict the rating a user
191306280 Aayushi D01 MP001 Recommender System
would give to an item.
The management of the flow of goods
191306286 Swati D02 MP004 Supply chain management
and services
Explanation: In the above output, all students are shown with only allocated mini-project
details. One student (RAM) has not chosen any mini-project.
RIGHT JOIN: The RIGHT JOIN returns all rows from the right table, even if there are no
matches in the left table. This means that a right join returns all the values from the right
table, plus matched values from the left table or NULL in case of no matching join predicate.
Example: Let us assume one scenario in our case study. Mini projects are allocated to the
students. It may be the possibility that some mini projects are unallocated. Display all mini-
project details (miniproject_id, miniproject_name, domain, description) with their allocated
with student’s details (roll_no, first_name, dept_id).
Output:
miniproj_id miniproj_name domain description roll_no first_name
It seeks to predict the rating a user
MP001 Recommender System Artificial Intelligence 191306280 Aayushi
would give to an item.
Malware has become a big risk in
MP002 Malware detection System Network Security 191006345 Satynder
todays time
AI is used to construct models to
MP003 Tumor detection System Artificial Intelligence 190006876 Nikhil
predict any kind of tumor
The management of the flow of goods
MP004 Supply chain management Blockchain 191306286 Swati
and services
It is a text analysis technique that uses
MP005 Opinion Mining Artificial Intelligence computational linguistics and natural 191106654 Anmol
language processing
Development of a new crawler
MP006 Design of crawler system Web Mining algorithm for mining social data from 191106660 Amit
internet.
Design Stock Market Prediction Designing Machine Learning Classifier
MP007 Artificial Intelligence NULL NULL
Classifier for Stock Market Prediction
Explanation: In the above output, all mini-projects are shown. One project is not allocated for
any students.
FULL JOIN: The SQL FULL JOIN combines the results of both left and right outer joins.
The joined table will contain all records from both the tables and fill in NULLs for missing
matches on either side.
Syntax | Oracle | SQL Server
SELECT table1.column1, table2.column2... FROM table1 FULL JOIN table2 ON
join_predicate;
Example: Let us assume one scenario in our case study. Mini projects are allocated with
students. However, some mini projects are not assigned because every student doesn’t need
to choose a mini-project. So It may be the possibility that some mini projects are unallocated.
Therefore, display all the mini-project details (miniproj_id, miniproj_name, domain,
description) along with all student’s details (roll_no, first_name, dept_id).
Output:
miniproj_id miniproj_name domain description roll_no first_name
AI is used to construct models to predict any
MP003 Tumor detection System Artificial Intelligence 190006876 Nikhil
kind of tumor
MP002 Malware detection System Network Security Malware has become a big risk in todays time 191006345 Satynder
It is a text analysis technique that uses
MP005 Opinion Mining Artificial Intelligence computational linguistics and natural language 191106654 Anmol
processing
Development of a new crawler algorithm for
MP006 Design of crawler system Web Mining 191106660 Amit
mining social data from internet.
NULL NULL NULL NULL 191306279 Ram
It seeks to predict the rating a user would give
MP001 Recommender System Artificial Intelligence 191306280 Aayushi
to an item.
The management of the flow of goods and
MP004 Supply chain management Blockchain 191306286 Swati
services
Design Stock Market Designing Machine Learning Classifier for Stock
MP007 Artificial Intelligence NULL NULL
Prediction Classifier Market Prediction
SELF JOIN: A self-join allows you to join a table to itself. It is useful for querying hierarchical
data or comparing rows within the same table.
A self-join uses the inner join or left join clause. Because the query that uses self-join
references the same table, the table alias assigns different names to the same table within
the query.
Example: Suppose we have to find the second highest salary college paid by the college.
There can be multiple queries possible to find the above result. For example, one way can be
using SELF JOIN.
If we see the records of faculties and apply SELF JOIN on it, we will get the 2nd highest salary.
faculty_id first_name designation doj salary
1123 Ashutosh Assistant Professor 12-06-2010 76000
1231 Ram Assistant Professor 17-12-2010 54000
1386 Shyam Assistant Professor 03-04-2011 57000
1497 Mala Associate Professor 04-06-2011 70300
1695 Sneha Associate Professor 06-09-2011 76000
1711 Elina Assistant Professor 17-10-2011 55000
1734 Nidhi Assistant Professor 06-12-2011 56000
1762 Ankur Assistant Professor 23-03-2012 56000
1793 Babita Associate Professor 17-08-2012 70000
1859 Divya Professor 17-09-2012 77000
1987 Ayush Professor 13-11-2012 100125
1998 Ashutosh Assistant Professor 20-03-2013 57000
2345 Gagan Assistant Professor 10-10-2013 52000
2765 Grish Assistant Professor 01-12-2013 62000
3768 Aakash Associate Professor 04-04-2014 63000
9765 Laxman Associate Professor 19-09-2018 72000
A subquery answers multiple-part questions. For example, to determine who works in Ayush's
department, you can first use a subquery to determine the department in which Ayush works.
You can then answer the original question with the parent SELECT statement. A subquery in
the FROM clause of a SELECT statement is also called an inline view. A subquery in the WHERE
clause of a SELECT statement is also called a nested subquery.
A subquery can contain another subquery. Oracle Database imposes no limit on the number
of subquery levels in the FROM clause of the top-level query.
Let’s take this example of knowing the faculties working in Ayush’s department.
Inner query to know Ayush’s Department:
SELECT dept_id FROM faculty WHERE first_name='Ayush;
So we have divided to use a subquery as a better option than the query used in two parts. By
using a subquery, we can nest the first query inside the second one, as shown in the following
statement:
Explanation: The inner query will retrieve the department id D03, and the outer query will
retrieve the first name of all faculties working in the same department.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Types of subqueries:
A single row subquery returns zero or one row to the outer SQL statement. You can place a
subquery in a WHERE clause, a HAVING clause, or a FROM clause of a SELECT statement.
Example 1: Find the name of faculty who is getting maximum salary along with their
department id and the salary
Example 2: Display the name and department of the faculties who have the designation as
'Professor' using IN operator
The ANY operator compares a value to each value returned in the subquery.<ANY means less
than the maximum, >ANY means more than the minimum, and =ANY is equivalent to IN
operator
Example 3: Display the list of faculty who are not 'Assistant Professor' and whose salary is less
than any 'Assistant Professor' using ANY Operator.
SQL Statement | Oracle | SQL Server | MySQL
The ALL operator compares a value to every value returned in the subquery. Here >ALL means
that greater the maximum value and <ALL means that the minimum value.
Example 4: Display the list of faculties who are not 'Associate Professor' and whose salary is
less than all Associate Professors using ALL operator.
Eleven faculty members are not Associate Professor and eight faculty out of these eleven have
less salary than all Associate Professors.
Scalar Subquery
A scalar subquery expression is a subquery that returns exactly one column value from one
row. The value of the scalar subquery expression is the value of the select list item of the
subquery. If the subquery returns 0 rows, then the value of the scalar subquery expression is
NULL.
Example 5: Find the name of employees for whom salary is greater than the average salary
Example 6: Write a query that displays the faculty details which get the maximum salary in
their respective departments.
Nested Subquery
A subquery that is placed in the WHERE clause of a SELECT statement is also called nested
subquery. There is no limit on the number of subqueries that could be placed in the FROM
clause of a SELECT statement, but we could nest up to 255 levels of subqueries in
the WHERE clause.
Example 7: Find the name of faculty who have offices in 'Aryabhatta block'
Syntax | Oracle
SELECT column1, column2, ....
FROM table1 outer
WHERE column1 operator
(SELECT column1, column2
FROM table2
WHERE expr1 =
outer.expr2);
Example 8: Find all faculties who earn more than the average salary in their department.
SQL Statement | Oracle
SELECT last_name, salary, dept_id
FROM faculty outer
WHERE salary >
(SELECT AVG(salary)
FROM faculty
WHERE dept_id =
outer.dept_id);
Output
Example 9: Update the faculty record by substituting its department id from the department
name.
Correlated Update
SQL Statement | Oracle
UPDATE faculty f
SET dept_id = (SELECT dept_name
FROM department d
WHERE f.dept_id =
d.dept_id);
SELECT faculty_id, first_name, dept_id
FROM faculty;
Output:
Correlated Delete
Syntax | Oracle
DELETE FROM table1 alias1
WHERE column1 operator
(SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
Example 10: Delete the records of all faculty who have offices in Aryabhatta block
SQL Statement | Oracle
DELETE FROM faculty f
WHERE dept_id =
(SELECT dept_id
FROM department d
WHERE (f.dept_id = d.dept_id and d.dept_location='Aryabhatta
Block'));
SELECT faculty_id, dept_id FROM faculty;
Output: (Output may vary depending on the data)
NOTE: You can also use the ANY and ALL operator in a correlated subquery.
The Set operators are used to combine two or more SELECT queries into a single result.
Queries containing set operators are called compound queries.
Here are the four SQL set operators with their respective returns
Operator Returns
UNION All distinct rows selected by either query
UNION ALL All rows selected by either query, including all duplicates
INTERSECT All distinct rows selected by both queries
MINUS All distinct rows selected by the first query but not the second
UNION Operator
The UNION operator is used to combine the results of two or more SELECT statements.
However, it will eliminate duplicate rows from its result set.
Syntax | Oracle | SQL Server | MySQL
SELECT [column_came, ...] FROM [table1]
UNION
SELECT [column_name, ...] FROM [table2]
Example 1
If we want to display all faculty members who have their designation as 'Assistant Professor'
we have to make the following SELECT statement.
Output:
It will display nine records in the result set, as shown below.
FACULTY_ID FIRST_NAME DESIGNATION
1231 Ram Assistant Professor
2765 Grish Assistant Professor
1734 Nidhi Assistant Professor
1386 Shyam Assistant Professor
2345 Gagan Assistant Professor
1123 Ashutosh Assistant Professor
1762 Ankur Assistant Professor
1998 Ashutosh Assistant Professor
1711 Elina Assistant Professor
If we want to display all faculty members who have their gender as 'Female' we have to make
the following SELECT statement.
If we see here, these two records are present in both result sets.
1734 Nidhi Assistant Professor
1711 Elina Assistant Professor
If we want to find the set of all faculties having designation as 'Assistant Professor' or gender
as 'Female', we write the following compound SQL queries.
Example 2: To find the students who have a phone number or having 'Technical' hobby or
both, we write the SQL statement
SELECT roll_no
FROM student_phone_no
UNION
SELECT roll_no
FROM student_hobby
WHERE student_hobby.hobby='Technical';
OUTPUT of Query 1 OUTPUT of Query 2
ROLL_NO ROLL_NO
190006876 190006876
190006876 191106654
191006345 191106660
191106654 UNION 191306280
191106660 191306286
191306280
191306280
191306286
ROLL_NO
190006876
191006345
191106654
191106660
191306280
191306286
Example
Now it displays 15 records since UNION ALL does not remove duplicate records.
FACULTY_ID FIRST_NAME DESIGNATION
1231 Ram Assistant Professor
2765 Grish Assistant Professor
1734 Nidhi Assistant Professor
1386 Shyam Assistant Professor
2345 Gagan Assistant Professor
1123 Ashutosh Assistant Professor
1762 Ankur Assistant Professor
1998 Ashutosh Assistant Professor
1711 Elina Assistant Professor
1734 Nidhi Assistant Professor
1497 Mala Associate Professor
1695 Sneha Associate Professor
1793 Babita Associate Professor
1859 Divya Professor
1711 Elina Assistant Professor
INTERSECT Operator
The INTERSECT operator is used to combine two SELECT statements, but it only returns the
records which are common from both SELECT statements.
Example
To find the set of all 'Female' faculties having designation as 'Assistant Professor', we have to
write the following compound query.
SQL Statement | Oracle | SQL Server | MySQL
SELECT faculty_id ,first_name, designation FROM faculty
WHERE designation = 'Assistant Professor'
INTERSECT
SELECT faculty_id,first_name, designation FROM faculty
WHERE gender = 'Female';
Output:
FACULTY_ID FIRST_NAME DESIGNATION
1711 Elina Assistant Professor
1734 Nidhi Assistant Professor
MINUS Operator
The MINUS operator combines two SELECT statements and returns only those in the final
result, which belongs to the first result set but not in the second result set.
Example
To find the set of all faculties with designation as 'Assistant Professor' but not 'Female'
SQL Statement | Oracle
SELECT faculty_id ,first_name, designation FROM faculty
WHERE designation = 'Assistant Professor'
MINUS
SELECT faculty_id,first_name, designation FROM faculty
WHERE gender = 'Female';
Output:
Just as a function (in programming) can provide abstraction, so can a database view. In
another parallel with functions, database users can manipulate nested views; thus, one view
can aggregate data from other views. Without the use of views, the normalization of
databases above the second normal form would become much more difficult. Views can make
it easier to create lossless join decomposition.
Views can provide advantages over tables:
Views can represent a subset of the data contained in a table. Consequently, a view
can limit the degree of exposure of the underlying tables to the outer world.
Views can join and simplify multiple tables into a single virtual table
Views can act as aggregated tables, where the database engine aggregates data (sum,
average, etc.) and presents the calculated results as part of the data.
Views can hide the complexity of data. For example, a view could appear as
faculty_d01 or student_cse, transparently partitioning the actual underlying table.
Views take very little space to store; the database contains only the definition of a
view, not a copy of all the data.
Creating Views
You can create views using the CREATE VIEW statement. Each view is defined by a query that
references base tables or other views.
Execute the SQL statement to create the view. Once the view is created, we can use it as a
normal table. For example, if we run a SELECT query on the view faculty_d01, it will display
all the faculty members of dept_id as 'D01'.
Altering a View
If you want to make some changes to any existing view, you can use ALTER VIEW statement.
For example, if you want to alter the view, student_cse to have only Female students of the
'CSE' department, we can write the following SQL statement.
If we display records in the student_cse view; you will get only one record
ROLL_NO FIRST_NAME GENDER DEPT_NAME
191306286 Swati Female CSE
Dropping a View
If you want to remove a view from the database, you can drop it using the DROP VIEW
statement.
Inline View
An inline view is not a real view but a subquery in the FROM clause of a SELECT statement.
Consider the following SELECT statement:
Syntax | Oracle
SELECT column_list FROM table_name;
In the FROM clause, you can specify a table from which you want to query data. Besides a
table, you can use a subquery as shown in the following example:
Syntax | Oracle
SELECT column_list FROM (SELECT * FROM table_name);
The subquery specified in the FROM clause of a query is called an inline view. Because an
inline view can replace a table in a query, it is also called a derived table.
In the above query for each row returned by a query, the ROWNUM pseudo-column returns
a number indicating the order in which Oracle selects the row from a table or set of joined
rows. The first row selected has a ROWNUM of 1, the second has 2, and so on. In the preceding
example, the ROWNUM values are those of the top-level SELECT statement, so they are
generated after the rows have already been ordered by the salary of the faculty in the
subquery.
Output
Explanation: In this output, first, the inline view returns all faculty sorted by the salary of
faculty in descending order, and then the outer query retrieves the first five rows from the
inline view.
In this example, the inline view returns the department id list and the highest salary paid in
each department. The outer query joins the inline view with the department table to get the
department name.
Output:
Output:
Example 4: Increase the salary of all faculty of the 'CSE' Department by 15%.
SQL Statement | Oracle
UPDATE
(
SELECT salary
FROM faculty f INNER JOIN department d using(dept_id)
WHERE d.dept_name='CSE'
)
SET salary = salary*1.15
Output
4 row(s) updated.
Example 5: Delete the records of all faculties who are working as an 'Assistant Professor' in
the 'CSE' Department.
Output:
1 row(s) deleted.
The table is the primary storage for storing data and information in RDBMS. A table is a
collection of related data entries and consists of columns and rows.
A view is a virtual table whose contents are defined by a query. Unless indexed, a view does
not exist as a stored set of data values in a database. Advantages over the table are
We can combine columns/rows from multiple tables or another view and have a
consolidated view.
Views can be used as security mechanisms by letting users access data through the
view without granting the users permissions to access the view's underlying base
tables directly.
It acts as an abstract layer to downstream systems, so any change in the schema is not
exposed and hence the downstream system does not get affected.
Sometimes we require some column in a table that should have auto-generated sequence
numbers, which are not filled by us but automatically generated and filled by the RDBMS
itself. Such values are in sequences and the columns which contain such values are known as
identity columns or auto-incremented columns. A sequence number is a value generated right
before the record is inserted into a table.
Different RDBMS provide different syntaxes to create the identity columns. It is generally
defined at the time of the creation of the table but can also be used separately by some
RDBMS.
Syntax | Oracle
CREATE TABLE table_name(
identity_column_name NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
column_name datatype(size) constraints,
...
);
Syntax | MySQL
CREATE TABLE table_name(
identity_column_name INT AUTO_INCREMENT PRIMARY KEY,
column_name datatype(size) constraints,
…
);
For example, we want to create a table quiz with some questions to be used for an
examination. The quiz number will be generated automatically.
If we add some sample questions and see the records, the RDBMS will automatically generate
the sequence number as a quiz number.
Output:
QUIZNO QUESTION
1 Who is known as Father of Computers
2 Who is known as founder of C Language
3 Who is known as founder of Java
When we delete a record using the DELETE statement, the RDBMS automatically takes the
next sequence number and the deleted sequence number is not used.
For example, if I delete the record having quizno as 3 and insert a new record, its quizno will
be 4.
If you delete all the records from a table, even then, RDBMS remembers the last sequence
value and starts with the next value and does not start with sequence number 1.
If you want to remove all records from a table and restart the sequence number again from
1, you need to use the TRUNCATE statement. TRUNCATE TABLE statement removes all the
records and resets the sequence to 1.
After creating a user, you need to decide which actions the user can do in the database. The
user may be provided several forms of authorization to perform those actions on parts of the
database.
The users may also be granted authorizations on the database schema, allowing them to
create, modify, or drop relations.
The various authorizations will be allowed by providing the privileges to the user through the
GRANT command. A privilege is a right to execute an SQL statement or a right to access an
object of another user.
For the types mentioned above of authorizations, Oracle defines two main types of privileges:
1. System Privileges
2. Object Privileges
System Privileges
System privileges determine what a user can do in the database. They mainly allow users to
add or modify schema objects in the database, such as creating tables, creating views, and
removing tablespaces.
To grant one or more privileges to a user, you use the GRANT statement.
In the given syntax, "system_privileges" specify the system privileges you want to assign to a
"user_name" after the GRANT keyword. If you assign more than one privilege, you use a
comma-separated list of privileges.
Examples:
CREATE SESSION Privilege:
After creating the user, to make the newuser enable to log in to the Oracle database, it is
required to provide the CREATE SESSION system privilege to the newuser by using the
GRANT command.
Syntax | Oracle
GRANT {object_privileges}
TO user
Examples:
SELECT Privilege
If the user 'UserX' executes the Select query on the table created by 'UserY', the system will
throw an error: table or view does not exist because the 'UserX' does not have privileges to
execute select query on the table.
The privilege to execute Select query on the table belongs to 'UserY' is given by using the
following syntax by the 'UserX' as:
After this command, the user 'UserX' will be able to execute the SELECT command on
'table_name' created by 'UserY'.
INSERT | UPDATE Privilege
To allow 'UserX' to INSERT and UPDATE tuples in the 'table_name', 'UserY' need to grant the
INSERT and UPDATE object privilege to 'UserX'.
The insert/update authorization may be given either on all attributes of the relation or on
only some. In that case, the list of attributes on which insert/update authorization is granted
optionally appears in parentheses immediately after the insert/update keyword.
To revoke an authorization, we use the revoke statement. The syntax is almost identical to
that of Grant:
Syntax |Oracle
Example:
To revoke select, insert, and update authorization from 'UserX' on 'table_name', we use the
following command:
SQL> REVOKE SELECT, INSERT, UPDATE ON table_name FROM UserX
To revoke all system privileges from 'UserX', you can use the following statement:
SQL> REVOKE ALL PRIVILEGES FROM UserX
Note:
We can use the REVOKE statement to revoke only privileges that are granted directly with a
GRANT statement. In other words, you cannot use the REVOKE statement to revoke privileges
that are granted through the operating system or roles.
Concept of Transaction
1. COMMIT
2. SAVEPOINT
3. ROLLBACK
COMMIT;
BEGIN TRANSACTION
UPDATE STUDENT SET FIRST_NAME='Mohit' WHERE FIRST_NAME='Nikhil';
COMMIT;
Syntax | Oracle
SAVEPOINT savepoint_name;
ROLLBACK TO savepoint-name;
Example of SAVEPOINT and ROLLBACK
Following is the table Department
Department
dept_id dept_name dept_location
D01 CS Aryabhatta Block
D02 CSE Aryabhatta Block
D03 IT Aryabhatta Block
D04 CSE-DS Bhabha Block
D05 ME Ramanujan Block
SAVEPOINT A;
INSERT INTO department VALUES ('D07','ECE','BHABHA BLOCK');
SAVEPOINT B;
INSERT INTO department VALUES ('D08','EN','RAMAN BLOCK');
SAVEPOINT C;
INSERT INTO department VALUES ('D09','CSE-AI','ARYABHATTA BLOCK');
Explanation: In the above example, we have inserted three values in the department table
and created the savepoints named A, B, and C after each statement. After the execution of
the statements, three records will be inserted in the department table, along with the
creation of three savepoints.
Now, if we execute the ROLLBACK statement to roll back the state of data to the savepoint B
SQL Statement | Oracle
ROLLBACK TO B;
SELECT * FROM department;
After executing the above statement, the database will be rollbacked till savepoint B, which
means the last two inserted records after savepoint B in the department table will be
removed.
The output table will be:
Department
dept_id dept_name dept_location
D01 CS Aryabhatta Block
D02 CSE Aryabhatta Block
D03 IT Aryabhatta Block
D04 CSE-DS Bhabha Block
D05 ME Ramanujan Block
D07 ECE BHABHA Block
ROLLBACK TO A;
SELECT * FROM department;
After executing the above statement, the database will be rollbacked till savepoint A, which
means the data insert after savepoint A in the department table will be removed.
CSV stands for comma-separated values. It is one of the standard data formats used by many
programming languages like Java and Python to generate various reports.
This section will help you export your tabular data managed by different RDBMS to CSV
format, which can be beneficial while creating an application. We can open such files into text
editors like Notepad.
The simplest way to create a CSV file in MySQL is via SELECT Statement. After selecting the
data, we need to save the data in CSV format into a file that has to be placed in some specific
folder specified by the MySQL RDBMS.
To see the folder name where you can export the files, we need to execute the following SQL
statement.
SHOW VARIABLES LIKE "secure_file_priv";
Here, secure_file_prev is a predefined system variable that contains the folder name where
we can export the data.
Variable_name Value
secure_file_priv C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
Now we can export the result set of any SELECT query using the following SQL Statement.
Remember to add one more \ in the path wherever you have \ to since to print a \ we need
to use the Escape Sequence Character \\.
USE SMS;
SELECT * INTO OUTFILE 'C:\\ProgramData\\MySQL\\MySQL Server
8.0\\Uploads\\department.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM department;
Now you can use the file present in the given folder 'C:\ProgramData\MySQL\MySQL Server
8.0\Uploads\' or a similar folder in your machine.
Now select your database, e.g., SMS and choose the table to which data need to be exported,
e.g., department table.
Define the folder and file name where you want to export the data using Browse… button
Click on the Next button.
6.17 Conclusion
In this unit, we have studied how to install the RDBMS and manage our relational database
using SQL. Management of database means creating a database with appropriate privileges,
to create the structure of relations using DDL commands like CREATE, ALTER, DROP,
TRUNCATE, RENAME, COMMENT, manipulations of records using DML commands like
INSERT, SELECT, DELETE, UPDATE, allowing or not allowing user permissions on relations using
DCL like GRANT, REVOKE and basics of TCL commands like ROLLBACK, COMMIT, SAVEPOINT.
In this unit, we have seen how SQL Arithmetic operators, Comparison Operators, SET
Operators work in queries, various Single-row and multi-row SQL functions, including
conversion functions like TO_DATE, TO_CHAR, TO_NUMBER.
We have also studied how to fetch records with multiple tables using nested queries,
subqueries, and various types of joins like NATURAL JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN,
FULL JOIN, and SELF JOIN. In the last, we have implemented VIEW and SEQUENCES in RDBMS.
In the next unit, we will see all concepts related to Advance SQL like Stored Procedures,
Triggers, Cursors.
Practice Problems
Subquery
1. Find the names of faculty who are working in the same department in which Gagan
Kumar Verma is working.
2. Find the status of research project/s in which Ram Mohan Prasad is working.
3. Find the names of students who have registered for mini-project.
4. Find the names of faculty who are working in more than two research projects.
5. Find the number of students registered in each course of a department.
6. Create a report that displays the faculty id, last name, and salary of all faculty who
earn more than the average salary. Sort the results in ascending order of salary.
7. Write a query that displays the faculty id and last name of all employees who are
working in a department with any employee whose last name includes letter t.
8. Find the first name and salary of all faculty who are reporting to head of the
department Ayush Giri.
9. Find the names of students who are taking courses of faculty Mr. Ram Mohan
Prasad.
10. Find the department in which a faculty has joined most recently.
11. What is the difference between WHERE clause and HAVING clause?
12. Why do we need to use the same columns that are selected in the SELECT list in the
GROUP BY Clause?
13. Why do we need to have columns that are not part of GROUP BY Clause as part of
aggregate function?
14. Will fixing the error by adding the columns to Group By clause, result in correct
output?
15. In SQL, what’s the difference between the having clause and the group by
statement?
16. Write a query in SQL to find the number of faculty in each department along with the
department id.
17. Write a query in SQL to find the sum of the allotment of salary amount of all
departments.
18. Write a SQL query to display the average salary amount in each department along
with their id.
19. Write a SQL query to find the number of faculty getting salary more than or equal to
Rs.60000.
20. Write a SQL query to find the number of faculty teaching a course and number of
students registered in that course.
21. Write a SQL query to find the number of facultyof each department who are working
in any research project.
22. Write a SQL query to find the first name, faculty id and department of allfaculty who
are working in more than two research projects.
23. Write a SQL query to display the name and id of faculty who has maximum
experience.
24. Write a SQL query to find the first nameand department of all faculty who have
completed any research project.
25. Write a SQL query to find the student names and their departments who have
registered in mini projects of Artificial Intelligence domain.
26. Display all the details of student which belongs to Dept_id = ‘D01’.
27. Display name of all male students.
28. List the faculty of Dept_id = ‘D01’ or ‘D03’.
29. List the faculty who have experience more than 5 years.
30. List the details of faculty those have salary greater than 70,000/-.
31. Find out the name of students whose name begins with ‘A’ and ‘S’.
32. List out the name and salary of faculty whose salary is not in the range of 50,000 and
72,000.
33. Find out the name of faculty that have ‘I’ anywhere in their name.
34. List the name of faculty and position that is without HOD.
35. Find out the name of employee that have ‘I’ as third character in their name.
36. List the faculty working under Hod_id 1231, 9765, 2765.
37. Find out the salary of both Ram and Nidhi.
38. List the faculty whose 10% of salary is equal to year of joining.
39. List the faculty whose name is having a character set ‘ii’ together.
40. List the faculty who joined in the month of JAN.
41. Compute daily salary of faculty Nidhi.
42. List those faculty whose salary is even value.
43. List the name of faculty whose salary is ending with 00.
References:
https://docs.oracle.com/database/121/SQLRF/functions003.htm#SQLRF20035
https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj32693.html
https://www.techonthenet.com/oracle/functions/avg.php
https://docs.oracle.com/cd/B19306_01/server.102/b14231/views.htm#i1006127
https://www.folkstalk.com/2012/01/oracle-general-function-examples.html
https://www.tutorialscampus.com/sql/conversion-functions.htm
ORCl10g_1.pdf
http://www.mathcs.emory.edu/~cheung/Courses/554/Syllabus/5-query-opt/convert4.html
SQL Correlated Subqueries - GeeksforGeeks
https://docs.oracle.com/cd/E57185_01/HIRUG/ch03s06s03.html
https://www.w3resource.com/sql/subqueries/corelated-subqueries-using-aliases.php
https://www.tutorialspoint.com/sql/sql-sub-queries.htm
The Basics of Inline View in Oracle, https://www.oracletutorial.com/oracle-view/inline-view-in-
oracle/
https://docs.oracle.com/cd/B12037_01/server.101/b10759/pseudocolumns008.htm
DBMS Question Bank
Unit 2 (2 Marks)
Q1 What is the difference between DROP and DELETE command?
Q2 What are different Integrity Constraints?
Q3 What is Equi-Join in database?
Q4 Define Super Key, Candidate Key, Primary Key and Foreign Key.
Q5 Explain different Features of SQL.
Unit 2 (5 Marks)
Q1 Define join and explain its different types.
Q2 Suppose we have the following relational database:
employee (person name, street, city)
works(person name, company name, salary)
company (company name, city)
(a) Find the names of all employees who live in city ‘Miami’.
(b) Find the names of all employees whose salary is greater than $100,000.
(c) Find the names of all employees who live in ‘Miami’ and whose salary is greater than
$100,000.
(d) Find the names of all employees who work for “First Bank Corporation”.
(e) Find the names and cities of residence of all employees who work for “First Bank
Corporation”.
(f) Find the names, street address, and cities of residence of all employees who work for
“First Bank Corporation” and earn more than $10,000.
Q3 Suppose we have the following relational database:
branch(branch name, branch city, assets)
customer(customer name, customer street, custromer city)
loan(loan number, branch name, amount)
borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(custromer name, account number)
(a) Underline appropriate primary keys. Primary keys are bold font.
branch(branch name, branch city, assets)
customer(customer name, customer street, customer city) ** customer names may not be
unique**
loan(loan number, branch name, amount) ** assume that loan numbers are across all
branches borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(customer name, account number)
(b) Given your choice of primary keys, identify appropriate foreign keys. Draw a relation
diagram to indicate foreign keys. try having the students draw a box for each schema and
then draw the relation diagram for the foreign keys as is done on the diagram on the first
page.
branch(branch name, branch city, assets)
customer(customer name, customer street, customer city) ** names may not be unique**
loan(loan number, branch name, amount) ** assume that loan numbers are across all
branches borrower(customer name, loan number)
account(account number, branch name, balance)
depositor(customer name, account number)
Foreign Keys: loan and account have foreign key branch name for branch. borrower has
foreign key loan number for loan. depositor has foreign key account number for account.
(c) Find the names of all branches located in ‘Chicago’.
(d) Find the names of all borrowers who have a loan in branch ’Down-town’.
(e) Find all loan numbers with a loan value greater than $10, 000.
(f) Find the names of all depositors who have an account with a value greater than $6000.
Q4 Describe mapping constraints with its types.
Q5 Notown Records has decided to store information about musicians who perform on its
albums (as well as other company data) in a database. The company has wisely chosen to hire
you as a database designer (at your usual consulting fee of $2,500/day).
Each musician that records at Notown has an SSN, a name, an address, and a phone number.
Poorly paid musicians often share the same address, and no address has more than one phone.
Each instrument that is used in songs recorded at Notown has a name (e.g., guitar,
synthesizer, flute) and a musical key (e.g., C, B-flat, E-flat).
Each album that is recorded on the Notown label has a title, a copyright date, a format (e.g.,
CD or MC), and an album identifier.
Each song recorded at Notown has a title and an author.
Each musician may play several instruments, and a given instrument may be played by
several musicians. Each album has a number of songs on it, but no song may appear on more
than one album.
Each song is performed by one or more musicians, and a musician may perform a number of
songs.
Each album has exactly one musician who acts as its producer.
A musician may produce several albums, of course.
Design a conceptual schema for Notown and draw an ER diagram for your schema. The
following information describes the situation that the Notown database must model. Be sure
to indicate all key and cardinality constraints and any assumptions that you make. Identify
any constraints that you are unable to capture in the ER diagram and briefly explain why you
could not express them.
Unit 2 (7 / 10 Marks)
Q1. Consider a schema R(A, B, C, D) and functional dependencies A -> B and C -> D. Then
the decomposition of R into R1 (A, B) and R2(C, D) is
(a) dependency preserving and lossless join
(b) lossless join but not dependency preserving
(c) dependency preserving but not lossless join
(d) not dependency preserving and not lossless join
Explain in detail why and how?
Q2 Consider the following relational schema:
SELECT S.sname
FROM Suppliers S
WHERE S.sid NOT IN (SELECT C.sid FROM Catalog C
WHERE C.pid NOT IN (SELECT P.pid FROM Parts P
WHERE P.color<> 'blue'))
Assume that relations corresponding to the above schema are not empty. Which one of the
following is the correct interpretation of the above query?
(a) Find the names of all suppliers who have supplied a non-blue part.
(b) Find the names of all suppliers who have not supplied a non-blue part.
(c) Find the names of all suppliers who have supplied only blue parts.
(d) Find the names of all suppliers who have not supplied only blue parts.
Q3 What are the main tasks performed by DBA? Explain Role and Responsibilities of DBA.
Q4. What is a join and explains different types of joins with the help of example.
Q5 The relation book (title, price) contains the titles and prices of different books. Assuming
that no two books have the same price, what does the following SQL query list?
GATE 2005
Select title
From book as B
Where (Select count(*)
from book as T
Where T.price > B.price) < 5