Unit-4 dbms
Unit-4 dbms
,IIISemester)
Unit-4
SQL Introduction
Structured Query Language (SQL) is a language used for storing and managing data in
RDBMS. SQL was the first commercial language introduced for E.F Codd's Relational model. Today
almost all RDBMS (MySql, Oracle, Infomix, Sybase, MS Access) uses SQL as the standard database
language. SQL is used to perform all type of data operations in RDBMS.
SQL is a tool for organizing, managing, and retrieving data stored by a computer database. The
name "SQL" is an abbreviation for Structured Query Language. SQL is the standard language for
Relation Database System. All relational database management systems like MySQL, MS Access,
Oracle, Sybase, Informix, DB2, postgres and SQL Server uses SQL as standard database language.
The name SQL is presently expanded as Structured Query Language. Originally, SQL was
called SEQUEL (Structured English QUEry Language) and was designed and implemented at IBM
Research as the interface for an experimental relational database system called SYSTEM R. SQL is now
the standard language for commercial relational DBMSs.
History:
1970 -- Dr. Edgar Frank "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
1974 -- Structured Query Language appeared.
1978 -- IBM worked to develop Codd's ideas and released a product named System/R.
1986 -- IBM developed the first prototype of relational database and standardized by ANSI. The
first relational database was released by Relational Software and its later becoming Oracle.
1987-- ISO SQL1 standard ratified
1992 --ANSI SQL2 standard ratified
1997 --IBM DB2 UDB unifies DB2 architecture across IBM and other vendor
platforms
1998-- Microsoft SQL Server 7 provides enterprise-level database support for
Windows NT
Some examples of DBMS software include Microsoft SQL Server, Oracle Corporation‘s
Oracle, MySQL AB‘s MySQL and IBM‘s DB2.
The CHARACTER data type accepts character strings, including Unicode, of a fixed length.
The length of the character string should be specified in the data type declaration; for example,
CHARACTER (n) where n represents the desired length of the character string. If no length is specified
during the declaration, the default length is 1.
The minimum length of the CHARACTER data type is 1 and it can have a maximum length up
to the table page size. Character strings that are larger than the page size of the table can be stored as a
Character Large Object (CLOB).
CHAR(10) or CHARACTER(10)
EX: 'Race car','RACECAR','24865'
Champions. The attributes of the table CHAMPION are Name, Nation, and Year (the year in which the
sportsman has won the title).
For our example, let us use the data type VARCHAR for the attribute Name and VARCHAR2
for the data type Nation.
BOOLEAN Data type: The BOOLEAN data type supports the storage of two values: TRUE or
FALSE. No parameters are required when declaring a BOOLEAN data type.
NUMBER Data type: The NUMBER datatype stores zero, positive, and negative fixed and floating
point numbers. The syntax to store fixed-point number is NUMBER (p, q) where ―p‖ is the total
number of digits and ―q‖ is the number of digits to the right of decimal point. The syntax to specify an
integer is NUMBER (p).
DATEData type The DATE data type is used to store the date and time information. For each DATE
value, Oracle stores the century, year, month, date, hour, minute, and second information. The ANSI date
literal contains no time portion, and must be specified in YYYY-MM-DD format where Y stands for
Year, M for month, and D for date.
LOB Data type: Multimedia data like sound, picture, and video need more storage space. The LOB data
types such as BLOB, CLOB, and BFILE allows us to store large block of data.
BLOBData type The BLOB data type stores unstructured binary data in the database. BLOBs can store
up to 4 GB of binary data.
CLOB Data type The CLOB data type can store up to 4 GB of character data in the database.
BFILE Data type The BFILE data type stores unstructured binary data in operating system files outside
the database. A BFILE can store up to 4 GB of data.
SQL Operators
Operators are the foundation of any programming language. We can define operators as symbols that
help us to perform specific mathematical and logical computations on operands. In other words, we can
say that an operator operates the operands. SQL operators have three different categories.
Arithmetic operator
Comparison operator
Logical operator
Arithmetic operators:
We can use various arithmetic operators on the data stored in the tables. Arithmetic Operators
are:
Operator Description
+ The addition is used to perform an addition operation on the data values.
- This operator is used for the subtraction of the data values.
* This operator is used for multiply data values
/ This operator works with the ‗ALL‘ keyword and it calculates division
operations.
% Modulus is used to get the remainder when data is divided by another.
Comparison operators:
Another important operator in SQL is a comparison operator, which is used to compare one
expression‘s value to other expressions. SQL supports different types of the comparison
operator, which is described below:
Operator Description
= Equal to
< Less than
> Greater than
>= Greater than Equal to
<= Less than Equal to
< > or != Not Equal to
Logical operators:
The Logical operators are those that are true or false. They return true or false values to combine one or
more true or false values.
Operator description
AND For the row to be selected all specified conditions must be true OR For
the row to be selected at least one of the conditions must be true NOT For the row
to be selected the specified condition must be false
Special operators:
Operator description
LIKE Matching a pattern from a column. Column value is similar to specified
character(s).
IN Checking a value in a set. Column value is equal to any one of a specified set of
values.
BETWEEN Checking a value within a range. Column value is between two values,
including the end values specified in the range
SQL COMMANDS
SQL commands are instructions, coded into SQL statements, which are used to
communicate with the database to perform specific tasks, work, functions and queries with data.
SQL commands can be used not only for searching the database but also to perform various other
functions like, for example, you can create tables, add data to tables, or modify data, drop the table,
set permissions for users. SQL commands are grouped into four major categories depending on their
functionality:
Data Definition Language (DDL) - These SQL commands are used for creating, modifying, and
dropping the structure of database objects. The commands are CREATE, ALTER, DROP,
RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing, retrieving,
modifying, and deleting data. These Data Manipulation Language commands are: SELECT,
INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing changes
affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing security to
database objects. These commands are GRANT and REVOKE.
1.Create
2.Alter
3.Drop
4.Rename
5.Truncate
1. Create Table Command – The CREATE TABLE command is used to implement the schemas
of individual relations.
Syntax: CREATE TABLE table name (columnname1 data type(size), columnname2 data type(size),
columnnameN data type(size));
The example creates a table called "Persons" that contains five columns: PersonID, LastName,
FirstName, Address:
Description of table: The DESC command returns the attributes (columns) of the table, the datatype
associated with the column, and also any constraint (if any) imposed on the column.
5. TRUNCATE TABLE statement: The TRUNCATE TABLE command deletes all the
rows in the table and recreates the structure of the table.
Syntax: TRUNCATE TABLE tablename;
Ex: TRUNCATE TABLE citizens;
1.Insert
2.Update
3.Delete
4.select
1. Insert Command:
2. UPDATE Command:
The data in the table can be updated by using UPDATE command.
The syntax of the UPDATE command is:
3. Delete Command:
The DELETE command in SQL is used to delete row(s) from the table.
Columnlist Specifies the columns to select. It can be one or more columns of the table. WHERE
clause is used to extract only those records that fulfill a specified criterion.
Group by clause us used to divide the rows in a table into smaller groups.
Having clause is used to filter data based on the group functions.
Order by clause is used to impose an order on the result of a query. Sort the result either ascending or
descending order.
Display the total amount of salary spent on each department, the query would be like:
Select deptno,sum(sal) from emp
group by deptno;
List the average monthly salary for each job type within department:
Select deptnno,job,avg(sal) from emp
group by deptno,job;
If you want to select the department that has total salary paid for its employees more than 10000, the
query would be like:
Select deptno,sum(sal) from emp
Group by deptno
Having sum(sal)>10000;
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
These operations are automatically committed in the database that's why they cannot be used
while creating tables or dropping them.
Here are some commands that come under TCL:
oCOMMIT
oROLLBACK
oSAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
Syntax: ROLLBACK;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back
the entire transaction.
Syntax: SAVEPOINT SAVEPOINT_NAME;
SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified
when a table is created (with the CREATE TABLE statement) or after the table is created (with the
ALTER TABLE statement). Constraints can apply both column level and table level.
We will focus on the following constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
Not Null:
The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL
constraint enforces a field to always contain a value. This means that you cannot insert a new record, or
update a record without adding a value to this field.
The following SQL enforces the "P_Id" column and the "LastName" column to not accept
NULL values:
CREATE TABLE Persons ( P_Id number(3) NOT NULL, LastName varchar(25) NOT NULL,
FirstName varchar(25), Address varchar(25), City varchar(25));
Unique:
The UNIQUE constraint uniquely identifies each record in a database table.The UNIQUE and
PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A
PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
CREATE TABLE Persons ( P_Id number(4) NOT NULL UNIQUE, LastName varchar(25) NOT
NULL, FirstName varchar(25), Address varchar(25), City varchar(25) ) ;
Primary Key:
The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary
keys must contain unique values. A primary key column cannot contain NULL values. Each table
should have a primary key, and each table can have only ONE primary key.
CREATE TABLE Persons ( P_Id number(3) NOT NULL PRIMARY KEY, LastName varchar(25)
NOT NULL, FirstName varchar(25), Address varchar(25), City varchar(25) ) ;
Foreign Key:
The Foreign key or referential integrity constraint identifies any column referencing the Primary
Key in another table. It establishes relationship between two columns in same tables or between different
tables.
The foreign key is defined in the child table, and the table containing the referenced column is
the parent table.
Parent table:
CREATE TABLE Persons ( P_Id number(3) NOT NULL PRIMARY KEY, LastName varchar(25)
NOT NULL, FirstName varchar(25), Address varchar(25), City varchar(25) ) ;
Child table:
Create table occupation(P_Id number(3) references Persons(P_Id),job varchar2(20)); (Or)
Create table occupation(P_Id number(3), job varchar2(20) , foreign key(P_Id) references Persons(P_Id));
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a single column it allows only certain values for this column. If you
define a CHECK constraint on a table it can limit the values in certain columns based on values in other
columns in the row.
CREATE TABLE Persons ( P_Id number(3) NOT NULL CHECK (P_Id>0), LastName varchar(25)
NOT NULL, FirstName varchar(25), Address varchar(25), City varchar(25) ) ;
DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column. The default value will
be added to all new records, if no other value is specified.
The following SQL creates a DEFAULT constraint on the "City" column when the "Persons"
table is created:
CREATE TABLE Persons ( P_Id number(3) NOT NULL, LastName varchar(25) NOT NULL,
FirstName varchar(25), Address varchar(25), City varchar(25) DEFAULT 'KURNOOL' ) ;
Selection Operation
Selection: A select operation selects a subset of rows (records) in a table (relation) that satisfy a selection
condition. The ability to select rows from out of complete result set is called Selection. It involves
conditional filtering and data staging. The subset can range from no rows, if none of the rows satisfy the
selection condition, to all rows in a table.
The selection operation is also known as horizontal partitioning. Select is used to select all
columns of a specific row.
This kind of SELECT statement returns some of the rows and all the columns in a table. Selection
is implemented through the WHERE clause of a SELECT statement, as the following figure
shows.
Ex: Select * from student where sno=104;
Projection Operation
Projection: A project operation selects only certain columns (fields) from a table. The result table has a
subset of the available columns and can include anything from a single column to all available columns.
The Project operation is also known as vertical partitioning. Projection is used to select
specific column or columns.
This kind of SELECT statement returns some of the columns and all the rows in a table.
Projection is implemented through the projection list in the Projection clause of a SELECT
statement, as the following figure shows.
The result contains the same number of rows as the student table, but it projects only a subset of the
columns in the table.
Adding a Column:
You can alter an existing table by adding one or more columns. In the following example, you add the
column named CITY to the STUDENT table.
ALTER TABLE STUDENT
ADD (CITY VARCHAR2(25));
Adding Primary and Foreign Key Designations
When you create a new table based on another table, the new table does not include integrity rules from
the old table.
In particular, there is no primary key. To define the primary key for the new EXAM table, use the
following command:
ALTER TABLE EXAM
ADD PRIMARY KEY (STUD_NO)
ADD FOREIGN KEY (STUD_NO) REFERENCES
STUDENT;
Changing a Column’s Data Type:
Using the ALTER syntax, the (integer) STUD_NO in the STUDENT table can be changed to a
character type by using:
ALTER TABLE PRODUCT
MODIFY (STUD_NO CHAR(5));
Some RDBMSs, such as Oracle, do not let you change data types unless the
column to be changed is empty.
Dropping a Column
Occasionally, you might want to modify a table by deleting a column. Suppose you want to delete the
CITY attribute from the STUDENT table. To accomplish that, you would use the following command:
ALTER TABLE STUDENT
DROP COLUMN CITY;
Dropping Primary and Foreign Key Designations: We can drop the primary or foreign keys by using
following command.
ALTER TABLE EXAM
DROP primary key;
Aggregate functions
Aggregate functions, also called Group Functions are built-in SQL functions that operate on
groups of rows and returns one value for the entire group or table. Aggregate functions are used to
produced summarized results.
Syntax: SELECT function (column) FROM tablename; In
SQL, aggregate functions can be categorized as:
Count( )
Sum( )
Max( )
Min( )
Avg( )
Count( ): This function returns the number of rows in the table.
Ex: Select Count(*) from emp;
Sum( ):This function is used to get the sum of a numeric column. Ex:
Select sum(sal) from emp;
Dept. of Computer Science Page 14
St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)
Max( ): This function is used to get the maximum value from numeric column. Ex:
Select max(sal) from emp;
Min( ): This function is used to get the minimum value from numeric column. Ex:
Select min(sal) from emp;
Avg( ): This function is used to get the average value from numeric column. Ex:
Select avg(sal) from emp;
Join Operation
The ability to combine (Join) tables on common attributes is perhaps the most important
distinction between a relational database and other databases. A join is performed when data are
retrieved from more than one table at a time.
To join tables, you simply list the tables in the FROM clause of the SELECT statement. The
DBMS will create the Cartesian product of every table in the FROM clause. However, to get the correct
result, you must select only the rows in which the common attribute values match. We use the WHERE
clause to indicate the common attributes used to link the tables.
The join condition is generally composed of an equality comparison between the foreign key and
the primary key of related tables.
For example, suppose you want to join the two tables EMP and DEPT. Because DEPTNO is
the foreign key in the EMP table and the primary key in the DEPT table, the link is established on
DEPTNO.
To join the EMP and DEPT tables, you would use the following,
SELECT EMPNO, ENAME, JOB, SAL, DNAME, LOC FROM EMP,DEPT WHERE
EMP.DEPTNO=DEPT.DEPTNO;
When joining three or more tables, you need to specify a join condition for each pair of tables.
The number of join conditions will always be N-1, where N represents the number of tables listed in the
FROM clause.
Recursive Joins/Self Join:
An alias is especially useful when a table must be joined to itself in a recursive query.
For example, suppose you are working with the EMP table. Using the data in the EMP table,
you can generate a list of all employees with their managers' names by joining the EMP table to itself.
The SQL command sequence would look like this:
SELECT E.MGR, M.ENAME, E.EMPNO, E.ENAME FROM EMP E, EMP M WHERE
E.MGR=M.EMPNO;
Cartesian Join:
If a SQL join condition is omitted or if it is invalid the join operation will result in a Cartesian product.
The Cartesian product returns the number of rows equal to the product of all rows in all the tables being
joined.
Ex:
SELECT EMPNO, ENAME, DNAME, LOC FROM EMP, DEPT;
Outer join
An outer join does not require each record in the two joined tables to have a matching record.
The joined table retains each record—even if no other matching record exists. Outer joins subdivide
further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains
the rows from (left, right, or both). No implicit join-notation for outer joins exists in standard SQL.
Left outer join
Left outer join returns all the values from the left table, plus matched values from the right table
If the right table returns one row and the left table returns more than one matching row for it, the values in
the right table will be repeated for each distinct row on the left table.
For example, this allows us to find an employee's department, but still shows the employee(s)
even when they have not been assigned to a department
SELECT * FROM emp, dept WHERE emp.DeptNO = dept.DeptNO (+);
Right outer join
A right outer join (or right join) closely resembles a left outer join, except with the treatment of
the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no
matching row from the "left" table (A) exists, NULL will appear in columns from A for those records
that have no match in B. A right outer join returns all the values from the right table and matched values
from the left table (NULL in case of no matching join predicate).
For example, this allows us to find each employee and his or her department, but still show
departments that have no employees. Below is shown an example of right outer join, with the additional
result row italicized:
SELECT * FROM EMP RIGHT OUTER JOIN dept ON emp.DeptNO = dept.DeptNO;
Set Operations
Set operators combine the results of two component queries into a single result. Queries containing
set operators are called compound queries
There are certain rules which must be followed to perform operations using SET operators in SQL.
Rules are as follows:
1.The number and order of columns must be the same in both tables.
2.Data types must be compatible.
Operator Returns
UNION All distinct rows selected by either query
MINUS All distinct rows selected by the first query but not the
second
1.UNION:
3. INTERSECT:
oIt is used to combine two SELECT statements. The Intersect operation returns the common
rows from both the SELECT statements.
oIt has no duplicates and it arranges the data in ascending order by default. Ex: List the
jobs common to department 20 and 30.
Select Job from emp where deptno=20
Intersect
Select Job from emp where deptno=30;
4. MINUS:
oIt combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
oIt has no duplicates and data arranged in ascending order by default. Ex: List
the jobs unique to department number 20.
Select Job from emp where deptno=20
Minus
Select Job from emp where deptno in (10,30);
View
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a real
table in the database. We can create a view by selecting fields from one or more tables present in the
database. A View can either have all the rows of a table or specific rows based on certain condition.
Syntax for creating a VIEW is:
CREATE VIEW view_name AS SELECT columns FROM table WHERE condition;
Ex:
Create View Emp_View As Select Empno, Ename, Job, Sal From Emp Where Sal>2000 and
job=‘CLERK‘;
This would create a virtual table based on the result set of the select statement. You can now query the
view as follows:
SELECT * FROM EMP_view;
Deleting VIEW:
SQL allows us to delete an existing View. We can delete or drop a View using the DROP statement.
DROP VIEW view_name;
Ex: DROP VIEW EMP_view;
Advantages of a View :
1.Restricting data access –Views provide an additional level of table security by restricting access to a
predetermined set of rows and columns of a table.
2.Hiding data complexity –A view can hide the complexity that exists in a multiple table join.
3.Simplify commands for the user –Views allows the user to select information from multiple tables
without requiring the users to actually know how to perform a join.
4.Multiple view facility –Different views can be created on the same table for different users.
5.Store complex queries –Views can be used to store complex queries.
Nested Queries
A nested query is a query that has another query embedded within it. The embedded query is
called a subquery. A subquery typically appears within the WHERE clause of a query. It can sometimes
appear in the FROM clause or HAVING clause.
There are mainly two types of nested queries:
1.Independent Nested Queries: In independent nested queries, query execution starts
from innermost query to outermost queries. The execution of inner query is independent
of outer query, but the result of inner query is used in execution of outer query. Various
operators like IN, NOT IN, ANY, ALL etc are used in writing independent nested queries.
Special operators used in nested queries:
EXIST Used to check for the existence of values. It
produces a Boolean result(TRUE or False) ANY It
compares the lowest value from the set ALL
It compares the highest value from the set SOME It works
similar to ANY
Ex: List the employee names along with their salaries from emp table, whose salary is greater
than the highest salary of an employee belonging to department number 20.
select ename,sal from emp where sal >ALL(select sal from emp deptno=20);
2.Co-related Nested Queries: A Query called correlated when both the inner and the outer query
are interdependent. For every row processed by the inner query, the outer query is processed as
well the inner query depends on the outer query before it can processed.
Ex: List the employees details from emp table, who earn salary greater than the average
salary for their departments.
Select empno,ename,sal,deptno from emp e where sal>(select avg(sal) from emp where
deptno=e.deptno)