RDBMS Unit - Iii 2023
RDBMS Unit - Iii 2023
RDBMS Unit - Iii 2023
STRUCTURES QUERY LANGUAGE (SQL): Meaning – SQL commands - Data Definition Language
- Data Manipulation Language - Data Control Language - Transaction Control Language - Queries
using Order by – Where - Group by - Nested Queries. Joins – Views – Sequences - Indexes and
Synonyms - Table Handling
Introduction to SQL
SQL is a language which is used to interact with relational database management system.
Q1)What is a database
Database is a organized collection of data. For example a database of a college would be having a
collection of data such as –
1. Personal records of Students
2. Student’s performance history
3. Teacher’s data
4. Financial department data etc.
A database management system is a software application which is used for managing different
databases. It helps us to create and manage database. With the help of DBMS we take care following
tasks –
1. Data Security
2. Data Backup
3. Manages huge amount of data
4. Data export & import
5. Serving multiple concurrent database requests
6. Gives us a way to manage the data using programming languages.
Types of Databases
Non-relational databases:
Data is not organized in form of tables. Data is stored in form of key & value pairs. The examples of
non-relational databases are: JSON & XML.
In relational database, data is organized in form of tables. A table contains rows and columns of
data. Table has a unique key to identify each row of the table.
SQL is used to interact with relational databases. We often refer relational database as SQL
database.
• 1970 - Dr. Edgar F. "Ted" Codd described a relational model for databases.
• 1974 - Structured Query Language appeared.
• 1978 - IBM released a product called System/R.
• 1986 - IBM developed the prototype of a relational database, which is standardized by ANSI.
• 1989- First ever version launched of SQL
• 1999 - SQL 3 launched with features like triggers, object-orientation, etc.
• SQL2003- window functions, XML-related features, etc.
• SQL2006- Support for XML Query Language
• SQL2011-improved support for temporal databases
Q 4)What is a Query ?
A) A Query is a set of instruction given to the database management system, which tells RDBMS
what information you would like to get from the database. For e.g. to fetch the employee name from
the database table EMPLOYEE, we write the SQL Query like this:
The DATE data type accepts date values. No parameter are required
when declaring a DATE datatype. DATE values should be specified in the
form: MM-DD-YY.
Syntax:-
<columnname> DATE
Example:-
DOB Date
Date
Create table TABLENAME(Rno number(10), name varchar(10),DOJ date,JoiningTime
Time);
I. CREATE
II. ALTER
III. DROP
IV. TRUNCATE
V. RENAME
I.CREATE
The "create" command is used to create Tables. It defines each column of the table uniquely. In SQL
each column has minimum of 3 attributes.
1. Name
2. Data type
3. Size(size means column width)
Each column name is separated from the other by a comma. Finally the SQL statement is terminated
with a semicolon;
Syntax:-
create table TABLENAME(columnName1 datatype(size), columnName2
datatype(size)....... columnNameN datatype (size));
Example:-
Create table STUDENT (rno number(10), name varchar(20), age
number(10));
The above query creates a table with the name "STUDENT’’ and with the given column
names(rno,name,age)
II. ALTER
ALTER command is used for altering the table structure, such as,
• to add a column to existing table
• to rename any existing column
• to change datatype of any column or to modify its size.
• to drop a column from the table.
Example
ALTER TABLE student ADD(address VARCHAR(200));
The above command will add a new column address to the table student, which will hold data of
type varchar of length 200.
III. TRUNCATE
TRUNCATE command removes all the records from a table. But this command will not destroy the
table's structure. When we use TRUNCATE command on a table its (auto-increment) primary key is
also initialized.
Syntax,
TRUNCATE TABLE table_name;
Example
TRUNCATE TABLE student;
The above query will delete all the records from the table student.
IV.DROP
DROP command completely removes a table from the database. This command will also destroy the
table structure and the data stored in it.
Syntax,
DROP TABLE table_name;
Example
DROP TABLE student;
The above query will delete the Student table completely. It can also be used on Databases, to
delete the complete database. For example, to drop a database,
V .RENAME
This command is used to set a new name for any existing table.
Syntax
RENAME TABLE old_table_name to new_table_name;
Example
RENAME TABLE student to students_info;
The above query will rename the table student to students_info.
I. INSERT
II. UPDATE
III. DELETE
Syntax:-
Insert into TABLENAME values(data1,data2,data3,………);
Example:-
Insert into STUDENT values(101,’RAM’,35);
We can use the INSERT command to insert values for only some specific columns of a row. We can
specify the column names along with the values to be inserted like this,
The above SQL query will only insert id and name values in the newly inserted record.
II.UPDATE
UPDATE command is used to update any record of data in a table. Following is its general syntax,
WHERE is used to add a condition to any SQL query, we will soon study about it in detail.
As you can see, we have used age = age + 1 to increment the value of age by 1.
III.DELETE
DELETE command is used to delete data from a table.
Syntax:
A special character asterisk * is used to address all the data(belonging to all columns) in a
query. SELECT statement uses * character to retrieve all records from a table, for all the columns.
Select a particular record based on a condition: We can use the WHERE clause to set a
condition,
SELECT * FROM student WHERE name = 'Abhi';
103 Abhi 17 Rohtak
you can also perform simple mathematical operations on the data too using the SELECT query to
fetch data from table.
ROLLBACK
COMMIT;
UPDATE class SET name ='Abhijit'WHERE id ='5';
SAVEPOINT A;
INSERTINTO class VALUES(6,'Chris');
SAVEPOINT B;
INSERTINTO class VALUES(7,'Bravo');
SAVEPOINT C;
SELECT*FROM class;
NOTE: SELECT statement is used to show the data stored in the table.
The resultant table will look like,
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
Now let's use the ROLLBACK command to roll back the state of data to the savepoint B.
ROLLBACK TO B;
Example:
Consider the following table-STUDENT
Rno. Name Age Address
1 Ram 18 Hyderabad
2 Ali 17 Secunderabad
3 John 16 Chennai
4 Goutham 15 Mumbai
2.LIKE CLAUSE
LIKE clause is used in the condition in SQL query with the WHERE clause. LIKE clause compares
data with an expression using wildcard operators to match pattern given in the condition.
There are two wildcard operators that are used in LIKE clause.
Example 1
SELECT * FROM Student WHERE s_name LIKE 'A%';
The above query will return all records where s_name starts with character 'A'.
s_id s_Name age
101 Adam 15
102 Alex 18
103 Abhi 17
Example 2
SELECT*FROM Student WHERE s_name LIKE'_d%';
The above query will return all records from Student table where s_name contain 'd' as second
character.
s_id s_Name age
101 Adam 15
3. ORDER BY Clause
Order by clause is used with SELECT statement ,and it is used to sort the data in ascending (or)
descending order. SQL sorts query results in ascending order by default.To sort data in descending
order DESC keyword is used with Order by clause
Syntax
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;
Example: 1
SQL>Select * from EMPLOYEE order by salary; Eid Name Salary
The above query will return result in ascending order of the salary. 2 Srivani 3000
1 Ram 5000
3 Ganesh 9000
Example: 2
SQL>Select * from EMPLOYEE order by salary DESC;
Eid Name Salary
The above query will return result in ascending order of the 3 Ganesh 9000
salary. 1 Ram 5000
2 Srivani 3000
4.Group by Clause
Group by clause is used to group the results of a SELECT query based on one or more columns. It
is also used with SQL functions to group the result from one or more tables.
DEPT SALARY
SQL>Select dept, sum(salary)from EMP GROUP BY dept;
Computers 81000
Commerce 76000
5. HAVING Clause
Having clause is used with SQL Queries to give more precise condition for a statement. It is used to
mention condition in Group by based SQL queries, just like WHERE clause is used
with SELECT query.
SYNTAX
Shweta K /Swapna A Page 15
RDBMS UNIT III BCOM III SEM
SELECT column, function(column) FROM table_name WHERE column condition
GROUP BY column HAVING function(column) condition
The HAVING clause must follow the GROUP BY clause in a query and must also precedes the
ORDER BY clause if used.
Example
EID NAME DEPT SALARY
101 Ram Electronics 55000
103 Dhanvi Aeronautics 35000
102 Srivani Electronics 20000
104 Ganesh Info Tech 30000
If you want to select the department that has total salary paid for its employees more than 25000,
the SQL query would be like;
When WHERE, GROUP BY and HAVING clauses are used together in a SELECT statement, the
WHERE clause is processed first, then the rows that are returned after the WHERE clause is
executed are grouped based on the GROUP BY clause.
Creating a VIEW
Syntax
Operations on view
(1)Displaying the rows from the view:
Syntax:
Select * from view_name;
Example:
Select * from sale_view;
(2)Updating views:-
Syntax:
Update viewname set condition where condition;
Example:
Update sale_view set customer=’Adam’ where oid=11;
Read-Only VIEW:We can create a view with read-only option to restrict access to the view.
Syntax
SNo. String
Functions
2. Scalar Functions 1 Upper( ) Converts the given column to capital letters.
2 Lower( ) Converts the given column to small lettres
3 initcap( ) Makes first letter capital for all text values in
the column.
4 Concat( ) The CONCAT function combines two or more
Strings into one string.
5 Length( ) The LENGTH( ) Function returns Number of
character in string.
6 Round() used to round a numeric field to number of nearest
integer
1. AVG() Function: Average returns average value after calculating it from values in a numeric
column.
syntax
SELECT AVG(column_name) FROM table_name
Example
SELECT avg(salary) from Emp;
avg(salary)
8200
2.COUNT() Function: Count returns the number of rows present in the table either based on
some condition or without condition.
syntax,
SELECT COUNT(column_name) FROM table-name;
Example
SELECT COUNT(name) FROM Emp WHERE salary = 8000;
count(name)
2
3. COUNT(distinct): DISTINCT clause eliminates the repetitive appearance of the same data.
Example
SELECT COUNT(DISTINCT salary) FROM emp;
o/p
count(distinct salary)
4
4.MAX() Function
MAX function returns maximum value from selected column of the table.
Syntax
SELECT MAX(column_name) from table-name;
Example
5.MIN() Function: MIN function returns minimum value from a selected column of the table.
Syntax
SELECT MIN(column_name) from table-name;
Example
SELECT MIN(salary) FROM emp;
MIN(salary)
6000
6.SUM() Function
SUM function returns total sum of a selected columns numeric values.
Example
SELECT SUM(salary) FROM emp;
SUM(salary)
41000
Scalar Functions
Scalar functions return a single value from an input value. Following are some frequently used
Scalar Functions in SQL.
1.UPPER() Function
UPPER function is used to convert value of string column to Uppercase characters.
Syntax
SELECT UPPER(column_name) from table-name;
Example
SELECT UPPER(name) FROM emp;
UPPER(name)
ANU
SHANE
ROHAN
SCOTT
TIGER
2.LOWER() Function
LOWER function is used to convert value of string columns to Lower case characters.
Syntax
SELECT LOWER(column_name) FROM table-name;
Example
SELECT LCASE(name) FROM emp;
LOWER(name)
anu
shane
rohan
scott
tiger
3.Initcap() Function :The initcap( ) function is used to make the first letter capital values of the
selected fields in the given column:
Syntax:
4. Concat () Function:The CONCAT function combines two or more strings into one string.
Syntax:
Select CONCAT(column1,column2) from TABLENAME;
Example:
Select CONCAT(eno,name) from employee;
OUTPUT:-
CONCAT(ENO,'NAME')
101Anu
102Shane
103Rohan
104Scott
105Tiger
ROUND(salary)
9001
8001
6000
10000
8000
1. INNER JOIN:
Syntax:
Example:
This example would return all rows from the CUSTOMER and ORDER tables where there is
matching cid values in both the CUSTOMERS and ORDER tables
Output:
cid orderid orderdate
10 4 24/10/2016
20 2 27/09/2016
40 1 18/08/2016
50 3 22/09/2016
Syntax:
Example:
20 2 27/09/2016
30 NULL NULL
40 1 18/08/2016
50 3 22/09/2016
60 NULL NULL
Right join
Syntax:
Example:
Output:
Orderid Orderdate Cid
5 10/10/2016 NULL
4 24/10/2016 10
2 27/9/2016 20
1 18/8/2016 40
3 22/9/2016 50
Syntax:
Example:
Sequence is also some what similar to AUTO_INCREMENT but it has some additional features too.
Creating a Sequence
Syntax
ID NAME
ID NAME
1 abhi
2 adam
4 alex
5 anu
Once you use nextval the sequence will increment even if you don't Insert any record into the table.
Example:
Select ColumnName, From TABLENAME Where ColumnName Operator (Select ColumnName from
TABLENAME);
Example:-