Vous êtes sur la page 1sur 39

UNIT III.

DATABASE and SQL


WHAT IS A DATABASE?
A data base is collection of a logically related data. By data we mean known facts that can be
recorded and that have implicit meaning. E.g. consider a telephone directory (database) which
consists of numbers and addresses of the people (data) or your college students’ register
(database) which contains registration number, name, course and address of the students (data).

WHAT IS A DBMS?
A database management system (DBMS) is a collection of software programs that enables
user to create and maintain a database. Thus DBMS is a general purpose software system that
facilitates the process of defining, constructing, and manipulating databases for various
databases.
Defining a database involves specifying the types of data (e.g. Numeric, alphanumeric etc.),
structures, and rules for the data (e.g. age of employee should be between 18 and 60, Salary of
the employee is numeric etc.) to be stored in the database.
Constructing the database refers to the process of actually storing data on some physical
medium (e.g. Hard disk).
Manipulating a database includes such functions as querying the database to retrieve
specific data, updating the database to reflect changes and using the database to generate report.
E.g. New telephone numbers are added, old ones are deleted and changed numbers are updated
in the telephone directory. You may also use directory to locate all the heart surgeons in Mumbai
(Example of report generation).

DATA HIERARCHY:-

Data element or field:-


Data element is known facts with implicit meaning. It consists of combination of letters
(A,B,C, …Z), numeric digits (0,1,2,…,9) or special characters (e.g. _, $, ., & etc.). E.g. name of
the students consists of letters, roll number consists of digits.

Record:-
A record is a collection of related fields. For example, a student record may consist of roll
number, Name of the students, address, class etc.

File:-
A File is a collection of related records. E.g. A student file contains records of all the
students.

Database:-
A database is organized as a collection of related files. E.g. College Database would consist
of a student file (containing records of the students), Course file (storing information about the
all the courses i.e. collection of records of courses), Instructor file (collection of records about
teachers) etc.

Thus, we can say that database is organized collection of files. A file is collection of related
records and records are collection of related data elements.

ADVANTAGES OF DATABASE SYSTEM:-


Some of the advantages of storing the data in a database are given below:
• Redundancy can be controlled: - Before database system was introduced, data were
stored and managed using traditional file processing system. In traditional file processing
system, the application programs were designed as per the need of user and data was stored
in a flat files i.e. stream of data elements. The application programs would access the data
and also include the description of the files and data it is accessing (called as Data
Dependence). E.g. In a college, Office would create file for storing student information and
course information and use application programs to maintain these records. Similarly,
Examination section would also store information about students and their performance and
use application programs to generate examination reports. Thus, there would be duplication
of student information at two places. The duplication of similar information at more than
one place is called Data Redundancy. The data redundancy results in wastage of memory.
Database system maintains only one copy of a centralized database thereby avoiding
redundancy.

• Data inconsistency is removed:-


Data redundancy may also result into data inconsistency. Data Inconsistency occurs due to
partial updation or erroneous data entry. E.g. Student has changed his name and informed
the college office that updated his record. However, office failed to communicate this to
exam department and thus the student may get his examination report card bearing old
name. The same student registration number now reflects different names at two different
places causing inconsistency. As database system maintains only one copy of database; the
changes made by one user are visible to other users.

• Data can be shared:-


The data stored at centralized location can easily be shared by different programs accessing
data.
• Standards can be reinforced:-
Database is developed after taking into consideration requirements of all the users across the
organization. Thus the standards related to different names and formats of data elements,
display structure, terminology etc. can be enforced.

• Unauthorized Access can be restricted: -


The security rules can be defined to check unauthorized access to some data. The users can
be given permissions to view, modify or add data selectively. E.g. financial data of the
organization will be available only to selected few.
• Integrity Constraints can be enforced:-
Integrity refers to correctness and accuracy of data stored in the database. Centralized
control of the data enables defining constraints on the data. E.g. Salary of the employee
cannot be zero; age of the current employee cannot exceed 60 years etc.
• Flexibility:-
The new database files can be added or structures of the existing files can be modified
without violating existing integrity constraints as per new requirements.
• Conflicting Requirements can be balanced:-
The knowledge of the overall requirements of the system enable creation of database design
that is most appropriate for the organization.

COMPONENTS OF A DATABASE:
The DBMS performs the necessary operations on the stored database by using various
components. The major components of DBMS that combine to manipulate the database and
provide the services are as follows:
• Transaction Management
A transaction is a logical unit of work. It is sequence of operations that transforms the
database from one state to the other. If a transaction executes some updates and failure
occurs, it either executes in its entirety or is completely cancelled. The component that
provides this facility is called the transaction manager. The commit transaction and the
rollback transaction are the operations that can be used to make the changes permanent or to
retain the original state or the database. E.g. A transaction involving transfer of amount from
say account A to account B consists of two operations namely withdrawal from account A
and depositing the amount in account B. In order that transfer to be successful, withdrawal
and deposit operations should both occur.
• Concurrency Control
The basic goal of database concurrency control is to ensure that concurrent transactions
(transactions occurring at the same time) operate correctly without any loss of database
consistency. For example, in an airways reservation system, where the database is accessed
by several operators, only one operator should gain access to one seat, at a time, for
assignment to a passenger. So, maintaining the consistency of the shared data should be the
goal of an ideal concurrency management mechanism.
• Recovery Management
The recovery management system of the DBMS is responsible from recovery from hardware
or software failures. For example, if the computer system fails amidst a complex upgrade
program, the recovery management system takes care of the restoring database to the state it
was in prior to the program execution. In addition, it also ensures that the program starts from
the point it was interrupted.
• Security Management
In DBMS, there is always a need to protect a part of the database from other users. For
example, in a college, a student should not be given access to that part of the database that
has the information about the teachers’ salary or the funds available with the institution.
Security mechanisms of a DBMS are responsible for protecting the data against unauthorized
access. The access privileges given to the users monitor and control the level of access each
user gets and the operations that each one can perform on the data.
• Language Interface
A database system provides languages for specifying the database structure and to frame
database queries. The Data Definition Language (DDL) is used to specify the structure by a
set of definitions. Queries are used to manipulate data stored in a database. Manipulation
involves operations such as information retrieval, insertion, deletion or modification. The
language that enables users to access or manipulate data is called a Data Manipulation
Language (DML).
The support of the DDL and DML languages facilitates the users in performing complex jobs
without giving a thought to the physical implementation.
• Storage Management
Databases need a large amount of storage space. The main memory of the computer is not
capable of storing this much information. Therefore, the information is stored on disks.
Since, the transfer of data to and from disks takes longer; the database system simplifies and
facilitates quick access to data.
A storage manager is a program that provides the interface between the data stored in the
database and the application program and the queries given to the system. It interfaces with
the operating system to access the physical storage and is responsible for data storage,
retrieval, and updation.
• Data Catalog Management
Data Catalog or Data Dictionary is a system database that contains metadata—that is, data
about data. This database is referred before the data are read or modified ion the database
system. It contains information about data, relationships, constraints, etc. and can be queried
to get information about the database structure.

RELATIONSHIPS AMONG DIFFERENT RECORDS:-


A record in the database may be associated with one or more records in the database. These
associations are known as relationships. E.g. Student enrolls for a course. Thus a student record
is associated with a course record for which he has enrolled. The relationships can be of different
types: one-to-one type, one-to-many type, many-to-one type or many-to-many type.
In One-to-One relationship each record is associated with only one record of other type.
E.g. a housing loan record can be associated with one house.
In one-to-many type of relationship a record can be associated with many records of other
type. E.g. A department record (parent record) is associated with all the records of employees
(Child Record) working for that department.
In many-to-one type of relationship many records can be associated with a record of other
type. E.g. many students can work on one group project. Thus, records of all the students
working on the project are associated with one project record.
In many-to-many type of relationship, many records of one type can be associated with one
record of other type and vice versa. E.g. A teacher teaches many students and a student is taught
by many teachers. Thus, a student record is associated with many records of teachers and vice
versa.

RELATIONAL DATABASE MANAGEMENT SYSTEM (RDBMS):-


In a relational database system, a database is organized as the collection of a two dimensional
tables. Each table is known as relation and represents a file. E.g. College database consists of
tables of student, instructor, course and Department. Consider a sample database as given
below:-

Student
Regn. No Name Course Address Year of
Admission
08118 Rajesh Deshpande C001 Andheri 2008
09120 Sachin Deshmukh M002 Bandra 2009
09114 Saket Kadam I001 Parle 2009

Instructor
Instr. No Name Course Dept Qualification
1001 Prof. Padhye BMS Management MMS
1003 Prof. Ms. Shah BCom Commerce MCom
1005 Prof. Joshi BCom Accountancy CA
1006 Prof. Dsouza BSc Botany MSc

Course
Course Id Course Name Year of Introduction
C001 BCom 1989
M002 BMS 2002
S001 BSc(CS) 2008
I001 BSc(IT) 2001

A column of each table a particular characteristic of a person or a thing or an event or a


phenomena etc. e.g. Student number is a characteristic of student. Hence, it is also called a field
or attribute. Each row of a table is collection of related fields and known as a tuple or record
in RDBMS terminology.

Primary Key, Candidate Key and Foreign Key:-


If you observe college database carefully, you will realize in each table there is at least one
column which has distinct value for each record. E.g. CourseId column in course table, Regn.no
in Student table. Such a column which distinctly identifies each record from all other records is
called primary key. In table, there may other columns which can also satisfy the same condition.
E.g. Course Name in Course table. Such columns are called candidate keys. We can say that
out of many candidate keys in a table; we may choose one as a primary key. Sometimes, a single
column may not be enough to identify each record distinctly and we will have to use a
combination of columns as a primary key. Consider the table project having columns
ProjectName, EmpName, NumHrs, StartDate and EndDate. Here ProjectName denotes Name of
the project; EmpName denotes name of the employee; NumHrs denotes number of hours
employee worked on each project. Startdate and EndDate denote the day employee began and
ended his work on the project. Since an employee can work on many projects and Project can
have many employees working; both the columns will contain duplicate values. However, the
combination of ProjectName and EmpName will be distinct for each row. Thus, primary key in
this table consists of two columns: - ProjectName and EmpName. Such primary key containing
more than one column is called Composite Key.
in above tables, Course column of Student table is related to CourseId column of Course table
i.e. each student row will have a value for Course column which matches with one of the values
of CourseId column of Course table. Such a column is called Foreign Key. Foreign key sets up
the association between two tables. We can say foreign key is such a column in a table that is
associated with the primary key of other table. Thus, Course column in Student table is the
foreign key as it is related to the CourseId column which is the primary key in Course table.

NON RELATIONAL DATABASE SYSTEMS:-

Hierarchical Database System:-

1. In a hierarchical database system, records are logically organized into a hierarchy of


relationships representing inverted tree.
2. For Example, consider university database. University has department. Each department
runs several courses. Each course has several instructors and students. Thus, this information
can be represented in hierarchical manner as shown in the figure.
3. All records in the hierarchy are called nodes. Each node is related to other in a parent-
child relationship. Each parent can have one or more child records. However, no child can
have more than one parent records. E.g. Department can have several courses but no two
courses can be run by two departments. Thus, hierarchical database structure implements
one-to-one and one-to-many relationships.
4. The top record parent record is called root node. E.g. Department is a root node. Records
in the database are always accessed through root node. I.e. to access information of any
course we access department record to which it belongs to. Similarly, to access student
record, we access department first and then course to which student is enrolled.

Physics Psychology
Root Record (Departments)

M.Sc. M.A. Ph. D. Children of Root parent


(Courses Record)
Prof.
Prof. Children of
Courses
Prof. Rao Prof. Joshi Naik
Bose
Dsouza (Instructors Records)

Advantages:-
• This database structure design is conceptually simple and easy to implement.
• The speed of processing data is fast especially for batch operations like payroll
processing.
• The child record cannot exist without parent thus promoting data integrity.
Disadvantages:-
• This database structure design is rigid.
• The database structure does not support ad hoc queries.
• It does not support many-to-many relationships.
• The child record cannot be added without member records and parent record can not be
deleted without deleting all the child records.

Network Database System:-

1. In a Network database system, records are logically viewed as sets.


2. Each set is composed of an owner record and one or more member records. A member
record can also belong to more than one set having different owner records. Thus, Network
database structure supports one-to-one, one-to-many and many-to-many relationships.
3. For Example, consider Company database. Company has department. Each department
undertakes several projects. Each project has several employees. An employee may work on
more than one project. Thus, this information can be represented in Network structure as
shown in the figure.

Web Databse
Des. Soln. Owner Record (Departments)

W0001 D0002 D0003 Member of Department


(Project Record)

Ms.sham M. Member of
Projects
Mr. Raje Mr. Subbu
a Krishnan
(Employee Records)

Advantages:-
• This network database structure design is conceptually simple and easy to design.
• It can handle all the possible relationship types.
• The record can be easily accessed in a set. If an application access one owner record; all
the corresponding member records are accessed easily.
• The member record cannot exist without owner thus data integrity is promoted.

Disadvantages:-
• The records are accessed one at a time; thus for accessing records familiarity of internal
data structure is required.
• The changes made in database leads to changes in application programs accessing the
data.
DIFFERENT LEVELS OF ABSTRACTION (VIEWS OF DATA):-
A database system must provide user with a conceptual view of the database that is user
of the database need not be aware about physical implementation and internal complexities of a
database. The database system achieves this by providing three layers or views of data. The user
needs to deal only with the external layer. The different views of data are:-

• External Or User View:-


 It is the highest level of data abstraction.
 It includes a part of database that a particular user is interested in and hides the
rest of the database from the user.
 It is described by means of external schema.
 For example, from Bank database, each user access data of his or her accounts. In
Company database, HR department users may access records of employees and
attendance where as marketing department may access employee records and his sales
figures. Thus, same database has different views for different users.

• Conceptual or Logical Level :-


 It describes the whole database for a community of users.
 It includes all database entities (Physical objects like employee, books etc. or
Logical objects like Department, Loan, and Account etc.), their relationships and
constraints.
 It is defined by means of the conceptual schema.
 For example, a logical view of a library database includes data about books,
members, publishers, and relationships among these entities like books issued to a
member, books published by a publisher etc. and constraints such as a number of books
that can be issued to the member, number of days books can be kept etc.

• Internal or physical Level :-


 It is the lowest level of data abstraction.
 It describes how data is stored and how it will be accessed.
 For example, at physical level, an employee record consists of EmpNo which is
numeric, EmpName is a string of characters and Salary is a real number.

External External
View -1 View -n

Conceptual view or Logical Level

Internal view or physical level

Stored
Database
Fig. Data Abstraction

STRUCTURED QUERY LANGUAGE (SQL)

SQL- An introduction:-
SQL is the language used by RDBMS for database interaction. It enables users to define
access and manage relational database. All the tasks related to relational data management such
as creating tables, retrieving information from the tables, deleting them, granting access to users
and so on – can be easily performed by SQL. SQL has standard set of commands used for data
management.
SQL is a nonprocedural query language i.e. user should only specify what information is
required from the database and not how to get that information.

SQL Statements:-
SQL statements can be used to create, modify and delete the tables, query that data in the
tables, insert data into table, modify and delete data, control access to the data and so on. An
SQL statement is a simple set of instructions to the RDBMS to perform an action. It contains
reserved words and has a specific structure.

SQL statements can be classified as:-


1. Data Definition Language (DDL):-
The DDL statements allow user to create, modify and delete database objects such as tables,
indexes, views etc. The commands used are CREATE, DROP and ALTER. The example of
DDL statements are CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX,
DROP VIEW etc.
2. Data Manipulation Language (DML):-
The DML statements are used to add, alter data to and delete data from database. The DML
statements are INSERT, UPDATE and DELETE.
3. Data Query Language:-
SQL provides a statement SELECT to retrieve data from the database. SELECT statement
can used to retrieve data from one or many tables.
4. Data Control Language:-
The data control statements are used to restrict user access to the database. These commands
are related to the security issues, that is, determining who has the access to the database
objects and what operations can be performed on them. The DCL commands are GRANT
and REVOKE.
5. Transaction Control Statements:-
All the changes made by DML statements in a database are managed by transaction
statements. A transaction is a set of DML statements which are performed as a single unit.
The transaction control statements are COMMIT, ROLLBACK, SAVEPOINT and SET
TRANSACTION.

SQL DATA TYPES:-


SQL allows user to store different types of data such as integer, characters etc, in columns of the
table. The SQL data types are:-

Data Types Description Examples


 CHARACTER (n) Fixed-length character strings Names of the Employees,
 CHAR (n) where n specifies the length. n Designations.
should be greater than zero.
 CHARACTER VARYING Variable-length character string Name of the Departments,
(n) whose maximum length is ‘n’. n Locations.
 CHAR VARYING (n) should be a positive integer
 VARCHAR (n)

 INTEGER Integer Numbers. BIGINT can be Employee Number, order


 INT used to store very large integers. numbers, Department
 BIGINT number etc.
 SMALLINT Small Integer Number IDs, Item Number
 BIT (n) Fixed length string of exactly ’n’ Marital Status
bits. ‘n’ is positive integer
 BIT VARYING (n) Variable length string of bits up to
‘n’ bits. ‘n’ is a positive integer,
 DECIMAL (p, s) A Decimal Number. ‘p’ is the Salary, Amount
 NUMERIC(p, s) precision representing length of
decimal number including
decimal point and ‘s’ is the scale
representing number of digits
after decimal point. Both
‘p’ and ‘s’ are positive integers.
‘p’ should be non zero. ‘q’ can be
zero and can never exceed ‘p’.
 FLOAT(p) Floating point numbers
 REAL Low-precision Floating Point
Number
 DOUBLE PRECISION High-Precision floating number
 DATE Stores a date like June 17, 2009 Date of Birth
 TIME Stores a time of a day like 11.30 In Time , Out Time of an
p.m. Employee
 TIMESTAMP Stores a specific instance. A
combination of date and time,
with a precision of nanoseconds.
CONSTANTS:-

• Numeric constants:-
Integer and decimal constants are written as ordinary numbers in SQL statements, with an
optional leading sign.
e.g. 33 +123 33.50 2000.65
Please note no comma would be used in numeric constants.

Floating point constants are specified using E notations.

e.g. 1.5E5 2.5E -7


The E stands for “times ten to the power of”. Thus, 1.5E5 is 1.5 X 105 .

• String Constants:-
The string constants are a sequence of characters. We need to enclose string constants in a
pair of single quotes.

e. g. ‘HELLO’ ‘VALIA COLLEGE’ ‘MUMBAI’

Consider the following SQL statement which adds one row to Employee table.

INSERT INTO EMPLOYEE (ENUM, ENAME, DEPTNO, DESG, DOB, SALARY) VALUES
(2,’YOGESH’, 10,’COORDINATOR’,’03-MAY-72’, 20000).

Please note no comma in numeric values and quotes around string constants and date. Also,
note date value is expressed as dd-mmm-yy.

BASIC SELECT QUERIES:-

The basic form of SQL query is:-


SELECT <attribute list>
FROM <table list>
WHERE <condition>
Where,
• <attribute list> is a comma separated list of attributes to be retrieved.
• <table list> is a comma separated list of tables to be accessed.
• <condition> is a Boolean expression specifying which rows to be retrieved. It is optional
in case all the rows are to be retrieved.
Note: - All SQL queries end with semicolon. (;)

QUERIES:-
• Retrieving all the columns:-
Query :-
Display employee table.
SQL:-
SELECT * FROM EMPLOYEE;
output:-

EmpId EmpName Desg Dept Salary DOB DOJ City MgrId


1 Sachin Vice President 1 50000 1973-04-24 1998-01-01 Mumbai NULL
2 Virender Sr. Exec 2 40000 1980-08-23 2003-02-03 Delhi 1
3 Saurav Sr. Exec 3 45000 1971-08-21 1996-07-01 Kolkata 1
4 Rahul Asst. Vice President 1 45000 1973-01-06 1996-07-01 Bengaluru 1
5 Yuvraj Exec 3 35000 1982-01-23 2000-09-08 Punjab 3
6 Dinesh Exec 2 25000 1986-09-09 2007-09-03 Chennai 2

• Retrieving all the columns:-


Query :-
Display Department table.

SQL:-
SELECT * FROM DEPARTMENT;

output:-
DeptNo DeptNam Location
e
1 Marketing Mumbai
2 Accounts Pune
3 HRM Delhi
4 Production Chennai

• Retrieving multiple columns:-


Query :-
List Employees, their salaries and designations.

SQL:-
SELECT EmpName, Salary, Desg FROM EMPLOYEE;

Output:-

EmpName Salary Desg


--------------- --------------------- --------------------
Sachin 50000 Vice President
Virender 40000 Sr. Exec
Saurav 45000 Sr. Exec
Rahul 45000 Asst. Vice President
Yuvraj 35000 Exec
Dinesh 25000 Exec

Note:-
1. While retrieving multiple columns, specify each column name.
2. A comma must separate field names.
3. The columns will be displayed in the specified order.
• Retrieving calculated columns:-
Query:-
List Employees and their Bonuses. The bonus is calculated as 10% of the salary.

SQL:-
SELECT EmpName, 0.1*Salary As Bonus FROM EMPLOYEE;

EmpName Bonus
---------- -----------
Sachin 5000.0000
Virender 4000.0000
Saurav 4500.0000
Rahul 4500.0000
Yuvraj 3500.0000
Dinesh 2500.0000

Explanation:-
• 0.1* Salary is a calculated column. It is arithmetic expression.
• Since calculated columns are displayed without headings, we may use alias for a column
name. In the above example we have used Bonus as the alias.
• The alias is used for the purpose of display only.
• The calculated column does not have any impact on the original table.

Arithmetic Expressions are formed using arithmetic operators.

Arithmetic Operators:-
Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
Unary (+,-) Used to denote positive
or negative sign
Arithmetic operators always return value. E.g. 3 + 4 will result into 7.
Arithmetic operators are used to form expressions. E.g. x +30, 1.1*salary, X *2 + y / 10 etc.

• Retrieving Distinct Rows:-


Query :-
List different designations employees held.

SQL:-
SELECT DISTINCT Desg FROM EMPLOYEE;

Output:-

Desg
--------------------
Asst. Vice President
Exec
Sr. Exec
Vice President

Note:-
The keyword DISTINCT removes duplicate rows. Thus, though EMPLOYEE table has 6
rows; query displays only 4 rows as 2 designations Exec. and Sr. Exec. are repeated in the
table.

• Retrieving Specific Rows (Using WHERE clause):-


Query :-
List all the employees earning more than 40000.

SQL:-

SELECT EmpName, Salary


FROM EMPLOYEE
WHERE Salary > 40000;

Output:-

EmpName Salary
-------------- --------
Sachin 50000
Saurav 45000
Rahul 45000

Explanation:-
The WHERE clause specifies a condition and all the rows satisfying that condition are displayed.
The conditions are formed using relational operators and logical operators.

• Relational Operators:-

Operators Meaning
= Equality
!=, <> In equality
> Greater than
>= Greater than or Equal to
< Less than
<= Less than or Equal to
Relational operators are used to form a simple condition. Relational operators are used to
compare two quantities and return TRUE or FALSE. E.g. salary > 20000 will return true for
salary is greater than 20000. Relational operators can be used to compare values, expressions etc.
e.g. (x *10) <= 20, dept=’MKTG’, 1.1 * Salary = 20000 etc.

Consider the following example.


• Query :-
List all the employees residing in ‘Mumbai’.

SQL:-

SELECT EmpName
FROM EMPLOYEE
WHERE city= ‘Mumbai’;

Output:-
EmpName
-------
Sachin

• Logical Operators:-
A logical operators are used to form a complex condition which returns TRUE or FALSE. A
complex condition is formed by combining two or more simple conditions using logical
operator/s.
Operators Meaning
AND Returns true if both the simple conditions return true.
OR Returns true if either of the simple conditions return true.
NOT Return true if the condition is false else return false.

Consider the following example:


A=10, B=15 and C=20, then
A>B AND B>C = false AND false = false
A<B AND B<C = true AND true = true
A<B AND B>C = true AND false =false
A<B OR B>C = true OR false =true
A<B OR B<C = true OR true = true
A>B OR B>C = false OR false = false
NOT (A>B) = NOT (false) =true
NOT (B<C) = NOT (true) =false

Let us now see some examples using logical operators.

• Query:-
List all the employees of department number 2 and earning more than 30000.

SQL:-
SELECT EmpName, Salary
FROM EMPLOYEE
WHERE Dept=2 AND Salary > 30000;

Output:-
EmpName Salary
-------------- -------------
Virender 40000.0000

• Query:-
List all the employees of department number 2 or earning more than 45000.

SQL:-
SELECT EmpName, Dept, Salary
FROM EMPLOYEE
WHERE Dept=2 OR Salary > 45000;

Output:-
EmpName Dept Salary
------------------------- ----------- ---------------------
Sachin 1 50000.0000
Virender 2 40000.0000
Dinesh 2 25000.0000

THE IN OPERATOR:-
The IN operator is used to select rows with a value for specified column matching with any
value in a given set of values.
Consider following examples:-

Query:-
Display Employees working in department number 2 or 3.
SQL:-
SELECT empname, dept FROM employee WHERE Dept IN (2, 3);
Output:-
empname dept
----------------------- -----------
Virender 2
Saurav 3
Yuvraj 3
Dinesh 2

Query:-
Display Employees working in metros.
SQL:-
SELECT empname, city FROM employee
WHERE city IN ('Mumbai', 'Delhi', 'Kolkata', 'Chennai');
Output:-
empname city
----------- -------
Sachin Mumbai
Virender Delhi
Saurav Kolkata
Dinesh Chennai

Note:- We also have NOT IN operator which is opposite of IN. for example, to display
employees not working in dept 1 or 3 we can write following SQL query:-
SELECT empname FROM employee WHERE dept NOT IN (1,3).

THE BETWEEN... AND OPERATOR:-


The BETWEEN … AND operator is used to select rows with values for specified column
falling within a range of given values.
Consider following example:-

Query:-
Display Employees earning more than or equal to 30000 and less than or equal to 50000.
Or
Display Employees earning between 30000 and 50000; both inclusive.
SQL:-
SELECT empname, salary FROM employee
WHERE salary BETWEEN 30000 AND 50000;
Output:-
empname salary
----------------------- ---------------------
Sachin 50000.0000
Virender 40000.0000
Saurav 45000.0000
Rahul 45000.0000
Yuvraj 35000.0000

Note: - We also have NOT BETWEEN … AND operator which is opposite of BETWEEN …
AND. E.g. Display the employees not earning between 10000 and 20000.
We can write following SQL query:-
SELECT empname, salary FROM employee
WHERE salary BETWEEN 30000 AND 50000;

THE LIKE OPERATOR (PATTERN MATCHING):-


The LIKE operator is used for matching rows with a pattern of string containing wild
card characters. Wild card characters used are ‘%’ and’_’(underscore). The ‘%’ denotes multiple
optional characters in a pattern where as ‘_’ denotes a single mandatory character.
Consider the following examples:-
Query:-
Display employees with a name beginning with ‘s’.
SQL:-
SELECT empname FROM employee WHERE empname LIKE 'S%';
Output:-
empname
--------
Sachin
Saurav

Examples:-

Pattern Explanation Values


‘_o%’ A pattern with a second character as ‘o’ ‘bon’, ’john’, ’Bo’,
followed by any number of optional ’Collin’
characters
‘_a_’ A pattern with compulsory three characters ‘Ram’,’Sam’
With a second character is ‘a’.

If a pattern contains wild card character then the special meaning of the wild card character can
be suppressed using ESCAPE clause. E.g. if we want to retrieve email addresses containing ‘_’
stored as EmailId column of Employee table.

We will write the query as:


SELECT EmpName, EmailId FROM Employee
WHERE EmailID LIKE ‘%\_%’ ESCAPE ‘\’;

Note: - ‘\’ preceding wildcard in combination with ESCAPE clause suppresses the meaning of
wildcard character and allows matching with a pattern containing wildcard characters.

THE IS NULL AND IS NOT NULL OPERATOR:-


Sometimes, the row does not contain a value for a particular column. E.g. We store Email of
Employees in EMPLOYEE table. However, some employees may not have email ids and hence,
for all such employees we use NULL data type. NULL is a special data value. NULL is neither a
blank nor an empty string and cannot be compared with a use of relational operator. SQL
provides special operators IS NULL and IS NOT NULL to handle NULL. For example:- To list
all the employees who are not having email ids , we write following query.

SELECT empname FROM employee WHERE email_id IS NULL;

IS NOT NULL operator is opposite of NULL. For example, to display all the employees who are
allocated project; assuming Pno is a column in Employee table holding Id of the project
employee is allocated. We write following query.

SELECT empname FROM employee WHERE Pno IS NOT NULL;

ORDERING THE OUTPUT (ORDER BY CLAUSE):-


The ORDER BY clause is used to view the result in a particular order i.e. either
ascending or descending order for a specified column.
Consider the following examples:-
Query:-
Display the employees in ascending order of their names .
SQL:-
SELECT empname FROM employee ORDER BY empname;
Output:-
empname
---------
Dinesh
Rahul
Sachin
Saurav
Virender
Yuvraj

Query:-
Display the employees in descending order of their salaries.
SQL:-
SELECT empname, salary FROM employee ORDER BY salary DESC;
Output:-
empname salary
----------------------- ---------------------
Sachin 50000.0000
Saurav 45000.0000
Rahul 45000.0000
Virender 40000.0000
Yuvraj 35000.0000
Dinesh 25000.0000
Note: - We need not mention ascending order as it is by default. However, descending order need
to be explicitly mentioned by using keyword DESC following column name. We can have
multiple sort levels. Consider the following example.
SQL:-
SELECT empname, dept, salary FROM employee ORDER BY Dept,salary DESC;
Output:-
empname dept salary
----------------------- ----------- ---------------------
Sachin 1 50000.0000
Rahul 1 45000.0000
Virender 2 40000.0000
Dinesh 2 25000.0000
Saurav 3 45000.0000
Yuvraj 3 35000.0000
Explanation: - The result is sorted in ascending order of departments first and for the rows with a
same department number; the secondary sorting is done in the descending order of salary.

SET OPERATIONS:-
The set operators are used to combine results of two or more select statements. The operators
are:-
Union:-
It combines the results of two select statements eliminating duplicates.
Intersect:-
It returns only those rows which are common to both the set of rows returned by two
select statements.
Except:-
It returns those rows which exists in a set of rows returned by first select statement but do
not exist in a set of rows returned by second select.

Consider the following example:-

Let us say customer_name column in the Deposit table stores customers having account in the
bank and customer_name in the Loan table stores customers having loan in the bank.
The customer_ name columns of the two tables are as given below:

Deposit Loan

Customer_name Customer_name
Sachin Dhoni
Rahul Harbhajan
Saurav Rahul
Yuvraj Saurav

Query:-
Display customers having an account, a loan or both in the bank.
SQL:-
SELECT customer_name FROM deposit;
Union
SELECT customer_name FROM loan;
Output:-
Customer_name
Sachin
Rahul
Saurav
Yuvraj
Dhoni
Harbhajan

Query:-
Display customers having a loan and an account in the bank.
SQL:-
SELECT customer_name FROM deposit;
Intersect
SELECT customer_name FROM loan;
Output:-
Customer_name
Rahul
Saurav

Query:-
Display customers having an account but not loan in the bank.
SQL:-
SELECT customer_name FROM deposit;
Except
SELECT customer_name FROM loan;
Output:-
Customer_name
Sachin
Yuvraj

AGGREGATE FUNCTIONS:-
Aggregate functions, also known as group functions perform operations on a group of rows and
return a single value. The different aggregate functions are:-
Aggregate function Result
SUM([ALL | DISTINCT] expression) Total of the all the values or only distinct in the
numeric expression
AVG([ALL | DISTINCT] expression) Average of all the values or only distinct in the
numeric expression
COUNT([ALL | DISTINCT] expression) Number of all the values or distinct values in the
expression
COUNT(*) Number of selected rows
MAX(expression) Highest value in the expression
MIN(expression) Lowest value in the expression
Note: -
• SUM, AVG, COUNT, MAX, and MIN ignore null values; COUNT (*) does not.
• Expression can be a series of values or column name.

Consider the following example:-


Query: -
Display minimum salary, maximum salary and average of salaries paid to Employees.
SQL:-
Select max (salary) as MAXSAL, min (salary) as MINSAL, avg (salary) as AVGSAL
from employee;
Output:-
MAXSAL MINSAL AVGSAL
-------------- ------------ -------------
50000.0000 25000 .0000 40000.0000

Query: -
Display number of employees and total of salaries paid to Employees working in
department number 1.
SQL:-
SELECT COUNT (EmpName) as EMPS, SUM (salary) as TOTSAL
FROM employee WHERE dept=1;

Output:-
EMPS TOTSAL
----------- ---------------------
2 95000

When aggregate functions are used in a select list, the select list can contain only:
• Aggregate functions.
• Grouping columns from a GROUP BY clause.
• An expression that returns the same value for every row in the result set, such as a
constant.

GROUP BY clause:-
The GROUP BY clause is used to group the rows on one or more columns. For example, if
we wish to see number of employees working for each department, we group employees on
departments. The group by clause is always used in conjunction with aggregate functions like
SUM, AVG, COUNT, MAX and MIN.

Consider the following examples:-


Query:-
For each department, display the number of employees working and total salary paid.

SQL:-
SELECT Dept, COUNT (empid) AS EMPNUM, SUM (salary) AS TOTSAL
FROM Employee GROUP BY dept;

Output:-
Dept EMPNUM TOTSAL
----------- ----------- ---------------------
1 2 95000.0000
2 2 65000.0000
3 2 80000.0000

Query:-
For each department, display the maximum, minimum and average salary paid.

SQL:-
SELECT Dept, MIN (salary) AS MINSAL, MAX (Salary) As MAXSAL,
AVG (Salary) As AVGSAL
FROM Employee
GROUP BY Dept;

Output:-
Dept MINSAL MAXSAL AVGSAL
----------- --------------------- -------------------- -------------------
1 45000.0000 50000.0000 47500.0000
2 25000.0000 40000.0000 32500.0000
3 35000.0000 45000.0000 40000.0000

Note: - When GROUP BY clause is used, the SELECT clause can only specify columns used
for grouping and aggregate functions

HAVING clause can be used along with GROUP BY to filter specific groups. In other
words, HAVING is to GROUPS is what WHERE is to ROWS.
Consider the following example.

Query:-
For each department with minimum 2 employees, display the maximum, minimum and
average salary paid.

SQL:-
SELECT Dept, MIN (salary) AS MINSAL, MAX (Salary) AS MAXSAL,
AVG (Salary) AS AVGSAL
FROM Employee
GROUP BY Dept
HAVING COUNT (empid) > 1;

MULTITABLE QUERIES:-
Sometimes, we need to access data from more than one table. Consider the following example.
“List the employees and their departments”. Now, Employee table has employee names but does
not have department names. However, it has dept column (Foreign Key) which holds the
department numbers and related to DeptNo of Department column. The Department table has
department names. Thus, we need to access Employee and Department table both. There is a link
between two tables. The dept column of Employee table contains department number of an
employee which matches with one of the values in DeptNo column of department table. The
query can be written as:-

SQL: - SELECT EmpName, DeptName


FROM EMPLOYEE, DEPARTMENT
WHERE Dept = DeptNo;
This query combines rows from both the tables by matching the contents of related columns.
This process is called joining the tables. The resulting table containing data from both the tables
is called join. Since we are including the rows matching for related columns it is called simple
join or Equi-join.
The output for the above query is:-
EmpName DeptName
-------- --------------
Sachin Marketing
Virender Accounts
Saurav HRM
Rahul Marketing
Yuvraj HRM
Dinesh Accounts

When we combine the data from two tables Employee and Department by creating join, the
resultant table looks like below:-
Consider the following query which displays all the columns from both the tables.
SELECT * FROM EMPLOYEE, DEPARTMENT;

OUTPUT:-
EmpID EmpNam Desg Dept Salary DOB DOJ City MgrID DeptNo DeptName Location
e
1 Sachin Vice 1 50000 1973-04- 1998-01-01 Mumbai NULL 1 Marketing Mumbai
President 24
2 Virender Sr. Exec 2 40000 1980-08- 2003-02-03 Delhi 1 2 Accounts Pune
23
3 Saurav Sr. Exec 3 45000 1971-08- 1996-07-01 Kolkata 1 3 HRM Delhi
21
4 Rahul Asst. Vice 1 45000 1973-01- 1996-07-01 Bengalur 1 1 Marketing Mumbai
President 06 u
5 Yuvraj Exec 3 35000 1982-01- 2000-09-08 Punjab 3 3 HRM Delhi
23
6 Dinesh Exec 2 25000 1986-09- 2007-09-03 Chennai 2 2 Accounts Pune
09

The tables Employee and Department are as given earlier.

SQL CONSIDERATIONS FOR MULTITABLE QUERIES:-


There may be a possibility where tables used in join may have same column names. When we
refer to such column DBMS has no way to know which table to be referred for one table or
other. E.g. Let us say there is city column in Department table denoting city in which department
is located. You want to display employees along with city in which they work. You write query
as below:-
SELECT EmpName, city
FROM Employee, Department
WHERE Dept = DeptNo;
There is confusion as city column exists in both the tables. The DBMS cannot decide whether
you want to display city of residence from Employee table or workplace from Department
table. To overcome such an ambiguity, we use qualified column names. The qualified column
names are formed by preceding column name by a table name. For example, the above query can
be written as:-
SELECT EmpName, Department.city
FROM Employee, Department
WHERE Dept = DeptNo;

Specifying the qualified column names can sometimes be clumsy, especially when tables have
longer names. You can use alias for the table to use shortened table names. The above query can
also be written as
SELECT EmpName, D.city
FROM Employee, Department D
WHERE Dept = DeptNo;
Here, letter “D” denotes alias for the table Department.

Consider some examples for joins.


Query:-
Display employees working in either accounts or marketing.
SQL:-

SELECT E.EmpName, D.deptname


FROM Employee E, Department D
WHERE E.Dept = D.DeptNo AND D.deptname IN ('accounts', 'marketing');
OUTPUT:-

EmpName DeptName
Sachin Marketing
Virender Accounts
Rahul Marketing
Dinesh Accounts

Query:-
Display employees residing in the same city as the department they work for.
SQL:-

SELECT E.EmpName, E.city


FROM Employee E, Department D
WHERE E.Dept = D.DeptNo AND E.city=D.Location;
OUTPUT:-

EmpName DeptName
Sachin Mumbai

SELF JOIN:-
Self join is used when a same table is referred twice. For example, consider the following
query:
“List employees and their managers.”

We know that mgrid column holds the information about manager for each employee.
Actually, managers are also employee themselves, thus mgrid column for each employee is
nothing but EmpId of Employee who is manager of this employee. Thus, for each employee in
the table we pick up mgrid and again locate this mgrid in EmpId column to find out
corresponding manager. Hence, for each employee we are referring Employee table twice.
To understand how SQL solve this problem imagine if there were two identical copies of
Employee table, one named EMPS, containing data about employees and other MGRS holding
data about managers. The MgrId column of EMPS is a foreign key related to EmpId of MGRS
table and the following table would work.

SELECT EMPS.empname, MGRS.empname


FROM EMPS, MGRS
WHERE EMPS.mgrid=MGRS.Empid;

SQL uses exactly this “imaginary duplicate table” approach to join a table to itself.
Instead of actually duplicating contents, SQL allows you to simply refer table by different
names, called table aliases (as discussed earlier). Thus, the Query “List employees and their
managers” would have following solution.

SELECT EMPS.empname, MGRS.empname AS Managers


FROM EMPLOYEE EMPS, EMPLOYEE MGRS
WHERE EMPS.mgrid=MGRS.Empid;

Here is the output:-

EmpName Managers
Virender Sachin
Saurav Sachin
Rahul Sachin
Yuvraj Saurav
Dinesh Virender

Let us consider one more example:-


Query:-
“List all the managers located in Mumbai or delhi.”
SQL:-
SELECT DISTINCT MGRS.empname as Managers, MGRS.City
FROM EMPLOYEE EMPS, EMPLOYEE MGRS
WHERE EMPS.mgrid=MGRS.Empid and MGRS.city in ('mumbai','Delhi');
Output:-
Managers City
Sachin Mumbai
Virender Delhi
USING SUBQUERY:-
A subquery is a query within a query. The subquery can reside in the SELECT clause,
FROM clause, WHERE clause, GROUP BY clause or HAVING clause. Consider the following
example.
Query:-
“List all employees working in Mumbai or Pune.”
SQL:-
SELECT empname
FROM Employee
WHERE Dept IN (SELECT DeptNo FROM Department WHERE location
= 'MUMBAI' OR location='PUNE');

Output:-
empname
Sachin
Virender
Rahul
Dinesh

HOW SUBQUERY WORKS?


The subquery returns the department numbers of departments located in Mumbai or Pune.
For each row of employee table, dept column value is matched with the values returned by the
subquery and if it matches with any of the values then Empname is returned.

SUBQUERY SEARCH CONDITIONS:-


A subquery usually appears as part of a search condition in the condition in the WHERE or
HAVING clause. Following search conditions are used in SQL.

• Subquery comparison test: - Compares the value of an expression with a single value
returned by a subquery. The subquery comparison test is performed using relational
operators like =, <>, <, <=, >, >=.

E,g. List employees working in ‘accounts’ department.


SQL. - SELECT empname FROM Employee
WHERE Dept = (SELECT Deptno FROM Department
WHERE Deptname=’Accounts’);

• The Set Membership Test (The IN operator or NOT IN operator):- Checks whether
the value of the expression matches one of the set of values produced by a subquery.

E.g. List departments having employees residing in metros.


SQL:-
SELECT Deptname
FROM Department
WHERE Deptno IN
(SELECT Dept FROM Employee WHERE
City IN ('Mumbai','Delhi','Chennai'));

Output:-
Deptname
Marketing
Accounts

• Existence Test: - Tests whether a subquery produces any rows of query results.

E.g. List departments having employees who earn more than average salary.

SQL:-
SELECT Deptname
FROM Department
WHERE EXISTS
(SELECT * FROM Employee WHERE
Department.DeptNo= Dept AND salary >
(Select AVG (Salary) FROM Employee));
Output:-
Deptname
-----------
Marketing
HRM

• Quantified Tests (ANY and ALL):-


ANY and ALL operators are used in conjunction with relational operators
(=, <>, <, <=,>,>=) to compare a value in row of a table with a set of values produced by
a subquery.
Consider following examples.
E.g. Display employees who are earning more than at least one employee of
marketing department.
SQL:-
SELECT empname FROM Employee
WHERE salary > ANY (SELECT salary FROM Employee
WHERE dept = (SELECT DeptNo
FROM Department
WHERE DeptName='marketing'));
E.g. Display employees who are earning greater than all the employees of
accounts department.
SQL:-
SELECT empname FROM Employee
WHERE salary > ALL (SELECT salary FROM Employee
WHERE dept = (SELECT DeptNo
FROM Department
WHERE Deptname= ‘Accounts’));
NESTED SUBQUERY is a subquery within another subquery. The queries mentioned in the
above two examples are nested subqueries. Let us consider one more example.
Query:-
List Employees who are earning more than the average salary of employees working in
the HRM department.
SQL:-
SELECT empname FROM Employee
WHERE salary > (SELECT AVG (salary) FROM Employee
WHERE dept = (SELECT DeptNo
FROM Department
WHERE Deptname= ‘HRM’));

Output:-
empname
--------
Sachin
Saurav
Rahul

CORRELATED SUBQUERY:-
A correlated subquery is the subquery which uses reference of outer table. Consider the
following examples.

Query:-
List Employees who are earning more than the average salary of employees working in
their department.
SQL:-
SELECT Empname FROM Employee E
WHERE E.salary > (SELECT AVG (salary)
FROM Employee WHERE dept= E.dept);
Output:-
Empname
Sachin
Virender
Saurav

Query:-
List Employees residing in the same city in which their department is located.
SQL:-
SELECT E.empname FROM Employee E
WHERE E.city = (SELECT Location FROM Department WHERE deptno= E.dept);

Output:-
Empname
Sachin
Explanation:-
For each row of the outer table (Employee), the subquery is executed once and in case search
condition is satisfied then row is returned.

SUBQUERIES IN THE HAVING CLAUSE:-


You can also use the subquery in the HAVING clause. When a subquery is used in the
HAVING clause, it works as a part of the row group selection performed by the HAVING
clause.
Consider the following example.

Query:-
List departments with average salaries are more than average salary paid to all the
employees.
SQL:-
SELECT D.Deptname
FROM Department D, Employee E
WHERE E.Dept=D.Deptno
GROUP BY D.Deptname
HAVING AVG (E.Salary) > (SELECT AVG (Salary) FROM EMPLOYEE);

Output:-
Deptname
Marketing

CREATING A DATABASE:-
In a large mainframe or enterprise level network installation, the corporate database
administrator is solely responsible for creating new databases. On smaller workgroup DBMS
installations, individual user may be allowed to create their own databases. On personal
computer, the user is database administrator and creates his own database. Different DBMS
products like SQL Server, Oracle, Informix, and DB2 use different approach for creating
database. However, majority of the DBMS software like SQL server and Oracle now offers
CREATE DATABASE and DROP DATABASE statements to create and destroy databases
respectively.
Consider the following example of creating database. This example creates a database
called Sales. Because neither MB nor KB is specified in the SIZE parameter for the Sales_dat
file, it defaults to MB and is allocated in megabytes. The Sales_log file is allocated in megabytes
because the MB suffix is explicitly stated in the SIZE parameter. The FILENAME parameter
defines name of the file in which data would be stored. MAXSIZE defines maximum size of the
file.
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )

CREATING A TABLE:-
We need to create tables in a relational database to store the data. The SQL provides
CREATE TABLE statement to create tables. The CREATE TABLE defines all the columns in
the table and each column definition specifies following:
• Column Name:- Every column in the table must have a unique name, however different
tables can have same column names.
• Data Type: - Data Type for the column specified the type of data a column can hold. E.g.
VARCHAR, INT etc.
• Constraint: - Used optionally to specify the restriction on the content of each column.

Consider the Simple create table statement for creating Employee table and its columns.
CREATE TABLE Employee
(EmpId int ,
EmpName varchar (30),
Desg char(20),
Dept int ,
Salary money,
DOB datetime,
DOJ datetime,
City varchar(20),
MgrId int);

Sometimes, we need to restrict the values allowed in the table. We can use constraint to
achieve this. The constraints are placed either at column level to apply restriction on the values
in the column or at the table level defined after the definition of last column and involve
restricting the values of the multiple columns simultaneously. The constraints defined at the
column level are:-

• The PRIMARY KEY Constraint:-


Specifies that the column is the primary key i.e. it has distinct value for each row and entry in
each row is mandatory. In the above table definition we can make EmpId as PRIMARY KEY
by defining column EmpId as:-
EmpId int PRIMARY KEY
A Composite primary key has to be defined at the table level. Consider the following
example which defines a combination of PartId and OrdId as a primary key for Order table.
Constraint pk PRIMARY KEY (PartId, OrdId)
Note: - The table can have only one PRIMARY KEY.
• The FOREIGN KEY Constraint:-
A FOREIGN KEY constraint is used to reference another table. Foreign keys can be single-
column keys or multicolumn keys. This example shows a single-column FOREIGN KEY
constraint on the employee table that references the DEPARTMENT table. Only the
REFERENCES clause is required for defining a single column foreign key.
Dept int REFERENCES Dept (DeptNo)

You can also explicitly use the FOREIGN KEY clause and restate the column attribute. Note that
the column name does not have to be the same in both tables.

FOREIGN KEY (Dept) REFERENCES Department (DeptNo)

Multicolumn key constraints are created as table constraints. Let us say in a database, the sales
table includes a multicolumn PRIMARY KEY. This example shows how to reference this key
from another table; an explicit constraint name is optional.

CONSTRAINT FK_sales_backorder FOREIGN KEY (stor_id, ord_num, title_id)


REFERENCES sales (stor_id, ord_num, title_id),

• The NOT NULL Constraint:-


The NOT NULL constraint specifies that the column can not have a NULL value that is each
row of the table must have a value for this column. Consider the following example which
makes it mandatory for user to enter value for an employee name.

Empname varchar (30) NOT NULL

• The DEFAULT Constraint:-


the default constraint is used to supply value when no value is supplied in either INSERT or
UPDATE statement. For example, if user does not insert city for the employee, the following
DEFAULT clause will supply ‘Mumbai’ for such a row.
City DEFAULT ('Mumbai')

In addition to constants, DEFAULT definitions can include functions. Use this example to
get the current date for an entry in DOJ column:

DOJ datetime DEFAULT (getdate())

• The UNIQUE Constraint:-


The UNIQUE constraint specifies that the value entered in the row should be unique
however, unlike PRIMARY KEY it also allows NULL value.

pan_number varchar(20) UNIQUE

• The CHECK Constraint:-


It is used to specify user defined constraint on the column or table. The Check specifier is
followed by a condition in a parenthesis which must evaluate to true. Consider the following
examples:-
To restrict user to enter only ‘Mumbai’, ‘Delhi’ or ‘Chennai’ in city column. We define the
city column in the following way.

City varchar (30) check (city IN (‘Mumbai’, ‘Delhi’, ‘Chennai’))

The following example shows complete definition of Employee table including constraints.

CREATE TABLE Employee


(EmpId int Primary key,
EmpName varchar (30) NOT NULL,
Desg char(20),
Dept int REFERENCES Dept (Deptno),
Salary money CHECK (salary >0),
DOB datetime NOT NULL,
DOJ datetime NOT NULL,
City varchar(20) CHECK (city IN (‘mumbai’, ‘delhi’, ‘chennai’)),
MgrId int);

REMOVING A TABLE (DROP TABLE):-


The table can be deleted from the database by using DROP TABLE statement followed by
table name. The employee table can be deleted as follows:-
DROP TABLE Employee;

CHANGING A TABLE DEFINITION (ALTER TABLE):-


Alter table command is used to:
• Add a column to the table or Drop a column from a table,
• Change default value for the column.
• Add or Drop constraints on the table or columns.

Consider the following examples:-


1) Add a phone number to the employee table.

ALTER TABLE Employee


ADD phone_number char(10);

2) Delete a city column from Employee table.

ALTER TABLE Employee


DROP city;

3) Make a desg column of Employee table s foreign key for the newly created designation
table.

ALTER TABLE Employee


ADD CONSTRAINT FK_DSG
FOREIGN KEY (Desg)
REFERENCES Desgname (Designation);
4) Suppose we have to change primary key of Department table from DeptNo to DeptName,
we can write following queries. First, we drop the foreign key constraint from Employee
table

ALTER TABLE Employee


DROP CONSTRAINT FK
FOREIGN KEY (Dept) REFERENCES Department(DeptNo);

ALTER TABLE Department


DROP PRIMARY KEY (DeptNo);

ADDING DATA TO THE DATABASE (INSERT Statement):-


The INSERT statement is used to add a row to a table. The INSERT statement may take
various forms as shown below.

• INSERT INTO tablename VALUES (value1, value2,…)

E,g. INSERT INTO Employee VALUES (7,’Harbhajan’,’Exe’,4,20000,’02-Mar-1983’,’13-Jun-


2004’,’Punjab’,4);

You will have to mention all the values for the columns and these values should be in the
order of columns in the table. In case of NULL value you write NULL keyword. Note strings
and dates are enclosed in the quotes.

• INSERT INTO tablename (col1, col2…) VALUES (val1, val2 …)

e.g. INSERT INTO employee(Empid, Empname, dept, desg, salary, dob, doj, city)
VALUES (8,'Yogesh',4,'VP',50000,'03-May-1972','01-Aug-2002','Mumbai');

When you supply column list in the INSERT clause the order of columns can be different
than in the table. However, values supplied should be in the same order as that of columns. Also,
we may drop the columns allowing NULL values. As you can see in the query the dept column
appears before desg thus, the order of appearance of columns is different than the table and also,
the city column is dropped.

• INSERT INTO table (col1, col2 …) <SQL query>

e.g. Copy the employees of marketing department, their designations and salaries in MktEmp
table.

E,g, INSERT INTO MktgEmp (Empname,desg, salary)


SELECT E.Empname, E.Desg, E.Salary
FROM Employee E, Department D
WHERE E.Dept = D.Deptno AND D.Deptname='marketing');

Note: - the MktgEmp table must preexist before execution of this query.

DELETING DATA FROM DATABASE (DELETE Statement):-


The DELETE statement is used to remove data from the table. The DELELE statement takes
following forms.
• DELETE FROM <table>

E.g. Delete employee from employee table.


SQL: - DELETE FROM Employee;

• DELETE FROM <table> WHERE <condition>

E.g. Delete employees residing in Mumbai.


SQL:- DELETE FROM Employee WHERE city = ‘Mumbai’;

E.g. Delete Employees working in Accounts or Production departments.

SQL: - DELETE FROM Employee


WHERE Dept IN (SELECT DeptNo FROM Department
WHERE Deptname IN (‘Accounts’,’Production’));

MODIFYING THE DATA IN THE DATABASE (UPDATE Statement):-


The UPDATE statement is used to modify the data in the table. The UPDATE statement
takes following forms.
• UPDATE <table>
SET column1=value1 [, column2 = value2 …]

E.g. Give all the employees raise of 10% in the salary.


SQL: - UPDATE Employee SET Salary= (Salary + 0.01*Salary);

• UPDATE <table>
SET column1=value1 [, column2 = value2 …]
WHERE <condition>

E.g. Employee ‘Sachin’ has migrated from ’Mumbai’ to ‘Delhi’ and he is transferred to
Department number 4. .
SQL: - UPDATE Employee
SET City= ‘Delhi’, Dept=4
WHERE Empname=’Sachin’;

E.g. Promote executives working in Accounts department to Senior Executive category.

SQL: - UPDATE Employee


SET Desg= ‘Sr. Exec’
WHERE Desg= ‘Exec’ AND Dept IN (SELECT DeptNo FROM Department
WHERE Deptname =‘Accounts’);

BUILT IN FUNCTIONS:-

Functions Returns Examples


CAST(value As Data type) The value converted to specific data CAST (dob As varchar)
type
LEN ( string_expression ) Returns the number of characters, of LEN(“Hello”) =5
the given string expression,
excluding trailing blanks.
LEFT ( character_expression , Returns the part of a character string LEFT('abcdefg',2) = ab
integer_expression ) starting at a specified number of
characters from the left.
RIGHT ( character_expression, Returns the part of a character string RIGHT(‘HELLO’, 2)=LO
integer_expression ) starting a specified number of
integer_expression characters from
the right.
LOWER ( character_expression ) Returns a character expression after LOWER(‘HELLO’)=hello
converting uppercase character data
to lowercase.
UPPER ( character_expression ) Returns a character expression with UPPER(‘hello’)=HELLO
lowercase character data converted
to uppercase.
LTRIM ( character_expression ) Returns a character expression after LTRIM( ‘ hi’)=hi
removing leading blanks.
RTRIM ( character_expression) Returns a character string after RTRIM(‘hi ‘)=hi
truncating all trailing blanks.
REVERSE ( Returns the reverse of a character REVERSE(‘dog’)=god
character_expression) expression.

SUBSTRING ( expression , start , Returns part of a string starting SUBSTRING('abcdef', 2, 3)=


length ) from start position extracting bcd
number of characters mentioned
by length
MONTH ( date ) Returns an integer that represents MONTH('03/12/1998')
the month part of a specified date. =3
DAY ( date ) Returns an integer representing DAY('03/12/1998')=12
the day part of the specified date.
YEAR ( date ) Returns an integer that represents YEAR('03/12/1998')=1998
the year part of a specified date.

Consider the following examples of built in functions:-

1) SQL:-
SELECT LEN (Empname), UPPER (Empname), LOWER (Empname)
FROM EMPLOYEE
OUTPUT:-
6 SACHIN sachin
8 VIRENDER virender
6 SAURAV saurav
5 RAHUL rahul
6 YUVRAJ yuvraj
6 DINESH dinesh
9 HARBHAJAN harbhajan
6 YOGESH yogesh

2) SQL:-
SELECT Empname, RIGHT (Empname, 3), LEFT (Empname, 2)
FROM EMPLOYEE

OUTPUT:-

Sachin hin Sa
Virender der Vi
Saurav rav Sa
Rahul hul Ra
Yuvraj raj Yu
Dinesh esh Di
Harbhaja jan Ha
n
Yogesh esh Yo

3) SQL:-
SELECT Empname, REVERSE (Empname)
FROM EMPLOYEE

OUTPUT:-

Sachin nihcaS
Virender redneriV
Saurav varuaS
Rahul luhaR
Yuvraj jarvuY
Dinesh hseniD
Harbhaja najahbraH
n
Yogesh hsegoY
4) SQL:-
SELECT Deptname, SUBSTRING (Deptname, 2, 4)
FROM Department

OUTPUT:-
Marketing arke
Accounts ccou
HRM RM
Production rodu

5) SQL:-

SELECT DOB, DAY (DOB) As Dy, MONTH (DOB) AS MTH, YEAR (DOB) AS YR
FROM Employee

OUTPUT:-

DOB Dy Mth Yr
1973-04-24 24 4 1973
1980-08-23 23 8 1980
1971-08-21 21 8 1971
1973-01-06 6 1 1973
1982-01-23 23 1 1982
1986-09-09 9 9 1986
1983-03-02 2 3 1983
1972-05-03 3 5 1972

TRANSACTION:-
A transaction is a set of statements that together form a logical unit of work and executed as
all or none i.e. either all the statements are executed or none of them is executed. E.g. Let us say
you are transferring the amount Rs. 1000/- from account A to account B. this transaction
involves two operations – withdrawing Rs. 1000/- from account A and depositing the same
amount in account B. Both these operations must occur for successful completion of transaction.

The different transaction handling statements provided by SQL are:-


• START: - A START statement indicates a beginning of new transaction.
• COMMIT: - A COMMIT statement ends the transaction successfully, making database
changes permanent.
• ROLLBACK: - A ROLLBACK aborts the transaction and cancels all the changes made
by transaction in the database.

Consider following Examples of transaction.


Successful Transaction:- Unsuccessful Transaction:-

START START

UPDATE deposit UPDATE deposit


SET amount= amount – 1000 SET amount= amount – 1000
WHERE accno= 111; WHERE accno= 111;

UPDATE deposit UPDATE deposit


Set amount = amount + 1000 Set amount = amount + 2000
WHERE accno =222; WHERE accno =222;

COMMIT ROLLBACK