DBMS Unit4
DBMS Unit4
SQL stands for Structured Query Language which is a computer language for storing,
manipulating and retrieving data stored in a relational database. SQL was developed in the 1970s by
IBM Computer Scientists.SQL is a programming language for Relational Databases. It is designed
over relational algebra and tuple relational calculus. SQL comes as a package with all major
distributions of RDBMS.
SQL Advantages:
1.Allows users to access data in the relational database management systems.
2.Allows users to describe the data.
3.Allows users to define the data in a database and manipulate that data.
4.Allows users to create and drop databases and tables.
5.Allows users to create view, stored procedure, functions in a database.
6.Allows users to set permissions on tables, procedures and views.
Relation instance − A finite set of tuples in the relational database system represents relation instance.
Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and their
names.
Relation key − Each row has one or more attributes, known as relation key, which can identify the
row in the relation (table) uniquely.
Query is a set of SQL statements used to make changes in the tables of the database
The inner query is called subquery which is placed in the round parentheses after the WHERE clause,
HAVING clause, or FROM clause.
CREATE object allows users to create the specified object in their own schema.
CREATE ANY object allows users to create the specified object in any schema.
The above rules also apply for ALTER and DROP system privileges.
Few of the object privileges are listed below:
Object Privileges Description
INSERT allows users to insert rows into a table.
SELECT allows users to select data from a database object.
UPDATE allows user to update data in a table.
EXECUTE allows user to execute a stored procedure or a function.
Roles: Roles are a collection of privileges or access rights. When there are many users in a database it
becomes difficult to grant or revoke privileges to users. Therefore, if you define roles, you can grant or
revoke privileges to users, thereby automatically granting or revoking privileges. You can either
create Roles or use the system roles pre-defined by oracle.
Some of the privileges granted to the system roles are as given below:
System Role Privileges Granted to the Role
CREATE TABLE, CREATE VIEW, CREATE SYNONYM, CREATE
CONNECT
SEQUENCE, CREATE SESSION etc.
CREATE PROCEDURE, CREATE SEQUENCE, CREATE TABLE, CREATE
RESOURCE TRIGGER etc. The primary usage of the RESOURCE role is to restrict access to
database objects.
DBA ALL SYSTEM PRIVILEGES
Creating Roles:
The Syntax to create a role is:
CREATE ROLE role_name [IDENTIFIED BY password];
For Example: To create a role called "developer" with password as "pwd",the code will be as follows
CREATE ROLE testing [IDENTIFIED BY pwd];
It's easier to GRANT or REVOKE privileges to the users through a role rather than assigning a
privilege directly to every user. If a role is identified by a password, then, when you GRANT or
REVOKE privileges to the role, you definitely have to identify it with the password.
We can GRANT or REVOKE privilege to a role as below.
For example: To grant CREATE TABLE privilege to a user by creating a testing role:
First, create a testing Role
CREATE ROLE testing
Second, grant a CREATE TABLE privilege to the ROLE testing. You can add more privileges to the
ROLE.
GRANT CREATE TABLE TO testing;
Third, grant the role to a user.
GRANT testing TO user1;
To revoke a CREATE TABLE privilege from testing ROLE, you can write:
REVOKE CREATE TABLE FROM testing;
The Syntax to drop a role from the database is as below:
DROP ROLE role_name;
For example: To drop a role called developer, you can write:
DROP ROLE testing;
Aggregate function:
1)SUM :It is used to return the sum of the values of a numeric type column.
SQL> SELECT SUM(MARK1) FROM STUDENT;
2)AVG :It is used to return the average value of the column.
SQL> SELECT AVG(MARK1) FROM STUDENT;
3)MIN :It is used to return the minimum value of an expression.
SQL>SELECT MIN(TOTAL)FROM STUDENT;
4)MAX : It is used to return the maximum value of an expression.
ORDER BY: It is used to sort the data in ascending or descending order, based on one or more
columns.
SQL>SELECT * FORM STUDENT ORDER BY NAME;
SQL>SELECT * FROM STUDENT ORDER BY NAME DESC; (for descending order)
GROUP BY ,HAVING: The GROUP BY clause , group rows based on the distinct values that exists
for the specified columns.
SQL>SELECT COURSE,COUNT(*) FROM STUDENT GROUP BY COURSE HAVING
COURSE=’COMMERCE’;
Distinct is a keyword used with SELECT command to avoid duplication of rows in the selection.
A View is a virtual table that consists of columns from one or more tables. Though it is similar to a
table ,it is not physically stored in the database.
CREATE VIEW EMP1 AS SELECT * FROM EMP WHERE JOB = ‘CLERK’;
Like:
This Operator applies only to text fields .It searches a character field to see a part of it matches a
string supplied with LIKE.
SELECT * FROM EMP WHERE ENAME LIKE 'S%';
The above query displays details of employees whose name starts with the letter ‘S’
SQL Logical Operators
1.ALL
The ALL operator is used to compare a value to all values in another value set.
2.AND
The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
3.ANY
The ANY operator is used to compare a value to any applicable value in the list as per the condition.
4.BETWEEN
The BETWEEN operator is used to search for values that are within a set of values, given the
minimum value and the maximum value.
5.EXISTS
The EXISTS operator is used to search for the presence of a row in a specified table that meets a
certain criterion.
6.IN
The IN operator is used to compare a value to a list of literal values that have been specified
7.LIKE
The LIKE operator is used to compare a value to similar values using wildcard operators.
8.NOT
The NOT operator reverses the meaning of the logical operator with which it is used. Eg: NOT
EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
9.OR
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
10.IS NULL
The NULL operator is used to compare a value with a NULL value.
11.UNIQUE
The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
SQL Constraints:
Constraints are the rules enforced on data columns on table. These are used to limit the type of data
that can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints could be column level or table level. Column level constraints are applied only to one
column where as table level constraints are applied to the whole table.
Following are commonly used constraints available in SQL:
▪ NOT NULL Constraint: Ensures that a column cannot have NULL value.
▪ DEFAULT Constraint: Provides a default value for a column when none is specified.
▪ UNIQUE Constraint: Ensures that all values in a column are different.
▪ PRIMARY Key: Uniquely identified each rows/records in a database table.
▪ FOREIGN Key: Uniquely identified a rows/records in any another database table.
▪ CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain
conditions.
Join in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set
of data. SQL Join is used for combining column from two or more tables by using values common to
both tables. Join Keyword is used in SQL queries for joining two or more tables. Minimum required
condition for joining table, is (n-1) where n, is number of tables. A table can also join to itself known
as, Self Join.
Types of Join
The following are the types of JOIN that we can use in SQL.
Cross join
Inner join
1.Natural join
Outer
1.Left outer join
2.Right outer join
3.Full outer join
Cross JOIN or Cartesian Product
This type of JOIN returns the Cartesian product of rows from the tables in Join. It will return a table
which consists of records which combines each row from the first table with each row of the second
table.
Cross JOIN Syntax is,
SELECT column-name-list from table-name1 CROSS JOIN table-name2;
INNER Join or EQUI Join
This is a simple JOIN in which the result is based on matched data as per the equality condition
specified in the query.
Student Class
Regno Name
Regno Place
100 A
100 Calicut
101 B
101 Malappuram
102 C
102 Thrissur
103 D
The resulting table is
Reg Na Reg Place
no me no
100 A 100 Calicut
101 B 101 Malappuram
102 C 102 Thrissur