0% found this document useful (0 votes)
2 views

Unit-4 dbms

The document provides an introduction to Structured Query Language (SQL), detailing its significance in managing data within Relational Database Management Systems (RDBMS). It covers SQL's history, advantages, data types, operators, and commands, including Data Definition Language (DDL) and Data Manipulation Language (DML). The document also explains the syntax and usage of various SQL commands for creating, modifying, and querying databases.

Uploaded by

adars251
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-4 dbms

The document provides an introduction to Structured Query Language (SQL), detailing its significance in managing data within Relational Database Management Systems (RDBMS). It covers SQL's history, advantages, data types, operators, and commands, including Data Definition Language (DDL) and Data Manipulation Language (DML). The document also explains the syntax and usage of various SQL commands for creating, modifying, and querying databases.

Uploaded by

adars251
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.

,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 widely popular because it offers the following advantages:

Allows users to access data in the relational database management systems.


Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows embedding within other languages using SQL modules, libraries & pre- compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.

History of SQL Standard:

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

Dept. of Computer Science Page 1


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

1997 Oracle8: First Web-enabled database


1998-- Oracle 8i provides database/Internet integration and moves away from
client/server model
Oracle 8i-1999 Oracle8i: First internet ready database
Oracle 9i- Introduced in 2001.
Oracle 10g- Introduced in 2003 – Oracle 10g: first Grid computing database Oracle
11g- Introduced in 2007.
Oracle 12c- Introduced in 2013 –here ‗ c ‘ standard for cloud computing. Oracle
18c- Introduced in 2018.
Oracle 19c- Introduced in 2019.
Oracle 21c- Introduced in 2020.

Some examples of DBMS software include Microsoft SQL Server, Oracle Corporation‘s
Oracle, MySQL AB‘s MySQL and IBM‘s DB2.

Data Types in SQL


When we create a table we must specify a data type for each of its columns. These data types define
the domain of values that each column can take. Oracle provides a number of built-in data types as well
as several categories for user-defined types that can be used as data types. Some of the built-in data types
are string data type to store characters, number data type to store numerical value, and date and time data
type to store when the event happened (history, date of birth, etc.).

CHARACTER [(length)] or CHAR [(length)]


VARCHAR (length)
BOOLEAN
NUMBER
DATE

CHARACTER [(length)] or CHAR [(length)]

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'

VARCHAR vs. VARCHAR2 :


The VARCHAR data type behaves like VARCHAR2 data type in the current version of Oracle.
The varchar2 datatype supports a variable length character string. It also stores alphanumaric values. The
size for this datatype ranges from 1-4000 bytes. In order to justify the above statement let us create a table
CHAMPION, which refers to Wimbledon

Dept. of Computer Science Page 2


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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

Dept. of Computer Science Page 3


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

/ 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:

Dept. of Computer Science Page 4


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

 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.

Data Definition Language (DDL)


DDL statements are used to alter/modify a database or table structure and schema. These
statements handle the design and storage of database objects.
DDL commands:

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:

Dept. of Computer Science Page 5


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

CREATE TABLE Persons ( PersonID number(5), LastName varchar(10), FirstName varchar(10),


Address varchar(15) );

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.

Syntax: DESC tablename;


Ex: Desc Persons;
2. ALTER TABLE Statement:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
To add a column in a table, use the following
Syntax: ALTER TABLE table_name ADD column_name datatype;

Ex: SQL> ALTER TABLE person ADD city varchar (10);

To delete a column in a table, use the following


syntax: ALTER TABLE table_name DROP COLUMN column_name;
Ex: SQL> ALTER TABLE PERSON DROP CITY ;
To change the data type of a column in a table
syntax: ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Ex: ALTER TABLE MODIFY COLUMN ADDRESS CHAR(10);
3. DROP TABLE Statement:
The DROP TABLE statement is used to delete an existing table in a database.
Syntax: drop table tablename;
Ex: DROP TABLE persons;
If this statement is successfully executed then the message ―table dropped‖ would be displayed.
4. Rename TABLE Statement:
Rename command is used to rename the database object. We can rename the table name as
follows:
Syntax: RENAME TABLE old_name to new_name;
Ex: RENAME TABLE Persons to citizens;

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;

Dept. of Computer Science Page 6


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

Data Manipulation Language(DML)


The data manipulation language is used to add, update, and delete data in the database. The SQL
command INSERT is used to add data into the database, the SQL command UPDATE is used to modify
the data in the database, and the SQL command DELETE is used to delete data in the database, the SQL
command SELECT is used to describe the data from the database. Here the term database refers to the
table.

1.Insert
2.Update
3.Delete
4.select

1. Insert Command:

Adding a New Row to the Table

The INSERT command is to add new row to the table.

Syntax: INSERT INTO table name VALUES (‗column1-name‗, ‗column2-name‗. . .


columnN-name);
Ex: Ex: Insert into persons(1234,‗RAO‗,‗RAMANA‗,‗KURNOOL‗);
Inserting specified column values to a table
Syntax: INSERT INTO table name(COL1,COL2,…) VALUES (‗column1-name‗, ‗column2-
name‗. . . columnN-name);
Ex: Insert into persons(person_id,last_name,addr) values (1234,‗RAMANA‗,‗KURNOOL‗);
Inserting rows at run time
INSERT INTO table name VALUES (‗&column1-name‗, ‗&column2-name‗. . . &columnN-name);
NOTE: Here & is used to accept values at runtime Character, date type values has to
represent in ‗ ‗ (single quotes)
Ex: Insert into persons(&person_id,‗&last_name‗,‗&first_name‗,‗&addr‗);

2. UPDATE Command:
The data in the table can be updated by using UPDATE command.
The syntax of the UPDATE command is:

UPDATE table name SET Columnname1=value, Columnname2=value,..


ColumnnameN=value, WHERE condition;

Ex: To change the addr of all the persons to NANDYAL


UPDATE persons set addr=‗NANDYAL‗;
Ex: To change first_name of 1234 from ‗RAMANA‗ TO ‗RAM‗
UPDATE persons set FIRST_NAME=‗RAM‗ WHERE PERSON_ID=1234;

3. Delete Command:
The DELETE command in SQL is used to delete row(s) from the table.

Dept. of Computer Science Page 7


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

The syntax of DELETE command is


DELETE FROM tablename WHERE condition;
Ex: TO delete all rows from a table
DELETE FROM PERSONS;
Ex: to delete a particular row
DELETE from PERSONS where person_id=1234;
4. Select Command:
The SELECT command is the most commonly used command in SQL. It allows database users
to retrieve the specific information they desire from an operational database. Select command is also
referred as DQL(Data Query Language) or DRL(Data Retrieval Language) command.
Syntax:
SELECT columnlist FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ]
[HAVING conditionlist ]
[ORDER BY columnlist [ASC | DESC] ] ;

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.

GROUPING THE RESULT OF QUERY (GROUP BY CLAUSE)


In SQL, by default all rows in a table are treated as one group. The GROUP BY clause is used to
divide the rows in a table into smaller group. Once the groups made, then group functions like SUM,
AVG, MAX and MAX can used to return summary information for each group.
The Group By clause is used with SELECT statement to combine a group of rows based on the values
of a particular column or expression. SQL groups the result after it retrieves the rows from a table.
Syntax:
SELECT columnlist FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ];

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;

Dept. of Computer Science Page 8


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

THE HAVING CLAUSE


Having clause is used to filter data based on the group functions. It is used to specify which
groups are to be displayed, that is, restrict the groups that you return on the basis of group function.
Simply, conditional retrieval of rows from a grouped result is possible with having clause.
This is similar to WHERE condition but is used with group functions. Group functions cannot
be used WHERE clause but can be used in HAVING clause.
Syntax:
SELECT columnlist FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ]
[HAVING conditionlist ];

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;

Order by clause(Ordering the Result of a Query)


The order of rows returned in a query result is understand. SQL uses the ORDER BY clause to
impose an order on the result of a query. The ORDER BY clause is used with SELECT statement to sort
results either ascending or descending order. Oracle sorts query results in ascending order by default.
Syntax:
SELECT columnlist FROM tablelist
[WHERE conditionlist ]
[ORDER BY columnlist [ASC | DESC] ] ;
Ex:
If you want to sort the emp table by salary of the employee, the query would be like:
SELECT ENAME, SAL FROM EMP
ORDER BY SAL;
If you want to sort the emp table by names of the employee, the query would be like:
SELECT * FROM EMP
ORDER BY ENAME;
If you want to sort the emp table by salary of the employee in descending order, the
query would be like:
SELECT ENAME, SAL FROM EMP
ORDER BY SAL DESC;

Dept. of Computer Science Page 9


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

Data Control Language(DCL)


DCL commands are used to grant and take back authority from any database user. Here are
some commands that come under DCL:
1.Grant
2.Revoke

1. Grant: It is used to give user access privileges to a database.

Syntax: Grant create,select, update on my_table to some_user, another_user;

Ex: grant create view to scott;

2. Revoke: It is used to take back permissions from the user.

Syntax: Revoke create,select, update on my_table from user1, user2;

Ex: Revoke create view from scott;

Transaction Control Language(TCL)

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;

Dept. of Computer Science Page 10


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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) ) ;

Dept. of Computer Science Page 11


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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;

Dept. of Computer Science Page 12


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

Sno Sname percentage


101 Ramesh 89.45
102 Raju 55.80
103 Rakesh 69.25
104 Rohan 87.33
105 Revanth 75.62
The result contains the same number of columns as the student table, but only a subset of its rows.

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.

Ex: select sno from student;

Sno Sname percentage


101 Ramesh 89.45
102 Raju 55.80
103 Rakesh 69.25
104 Rohan 87.33
105 Revanth 75.62

The result contains the same number of rows as the student table, but it projects only a subset of the
columns in the table.

Table Modification Commands


All changes in the table structure are made by using the ALTER TABLE command, followed
by a keyword that produces the specific change you want to make. Three options are available: ADD,
MODIFY, and DROP. You use ADD to add a column, MODIFY to change column characteristics, and
DROP to delete a column from a table. Most
RDBMSs do not allow you to delete a column (unless the column does not contain any values) because
such an action might delete crucial data that are used by other tables.
ALTER TABLE tablename {ADD | MODIFY} ( columnname
datatype [ {ADD | MODIFY} columnname datatype] ) ;

Dept. of Computer Science Page 13


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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;

Dept. of Computer Science Page 15


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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

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

Dept. of Computer Science Page 16


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

Common Syntax for Set Operations:


SELECT column_name FROM table1
SET_OPERATOR
SELECT column_name FROM table2;

1.UNION:

oUNION will be used to combine the result of two select statements.


oDuplicate rows will be eliminated from the results obtained after performing the UNION
operation.

Ex: Display the different designations in Department 20 and 30 of Emp table.


Select Job from emp where deptno=20
Union
Select Job from emp where deptno=30;
2. UNION ALL:
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Ex: Display the different designations in Department 20 and 30 of Emp table without removing
duplicate values.
Select Job from emp where deptno=20
Union all
Select Job from emp where deptno=30;

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);

Dept. of Computer Science Page 17


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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

Dept. of Computer Science Page 18


St. Joseph’s Degree College, Sunkesula Road, Knl DBMS(IIBSc.,IIISemester)

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)

Dept. of Computer Science Page 19

You might also like