Dbms r23 Unit 2 More Information Notes
Dbms r23 Unit 2 More Information Notes
2.1 Introduction
Relational Model was proposed by E.F Codd to model data in the form of relations or tables. After
designing the conceptual model of database using ER diagram, we need to convert the conceptual
model in the relational model which can be implemented using any RDBMS (Relational Data Base
Management System) like SQL, MY SQL etc.
The relational model is very simple and elegant; a database is a collection of one or more relations,
where each relation is a table with rows and columns.
This simple tabular representation enables even new users to understand the contents of a
database, and it permits the use of simple, high-level languages to query the data.
ROLL_NO
1
Null values: The value which is not known or unavailable is called NULL VALUE. It is
represented by blank space.
Cardinality: The number of tuples are present in the relation is called as its cardinality.
Mr.D.ESWARAIAH, M.Tech., 2
Relation
A relation is defined as a set of tuples and attributes.
A relation consists of Relation schema and relation instance.
Relation schema: A relation schema represents the name of the relation with its attributes.
Ex: STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is Relation schema for
STUDENT.
Relation instance: The set of tuples of a relation at a particular instance of a time is called
Relation Instance.
An instance of „Employee „relation
2.5 Constraints
On modeling the design of the relational data base, we can put some rules(conditions) like
what values are allowed to be inserted in the relation
Constraints are the rules enforced on the data columns of a table. These are used to limit the
type of data that can go in to a table
This Ensure the accuracy and reliability of the data in the database. Constraints could be
either on a column level on a table level.
Mr.D.ESWARAIAH, M.Tech., 3
1. Not Null:
Null represents a record where data may be missing data or data for that record may be optional.
Once not null is applied to a particular column, you cannot enter null values to that column.
A not null constraint cannot be applied at table level.
Mr.D.ESWARAIAH, M.Tech., 4
Example:
Create table EMPLOYEE (id int Not null, name varchar Not null, Age int
not null, address char (25), salary decimal (18,2), primary key(id));
In the above example we have applied not null on three columns id, name and age which
means whenever a record is entered using insert statement all three columns should contain a
value other than null.
We have two other columns address and salary, where not null is not applied which means
that you can leave the row as empty.
2. Unique:
Some times we need to maintain only. Unique data in the column of a database table, this is
possible by using a Unique constraint.
Example:
Create table PERSONS (id int unique, last_name varchar (25) not null,
First name varchar (25), age int);
In the above example, as we have used unique constraint on ID column we are not supposed
to enter the data that is already present, simply no two ID values are same.
Mr.D.ESWARAIAH, M.Tech., 5
3. Default:
When a column is specified as default with same value then all the rows will use the same
value i.e., each and every time while entering the data we need not enter that value.
But default column value can be customised i.e., it can be over ridden when inserting a data
for that row based on the requirement.
Example:
Create table EMPLOYEE (id int Not null, last_name varchar (25) Not null,
first_name varchar (25), Age int, city varchar (25) Default Hyderabad);
As a result, whenever you insert a new row each time you need not enter a value for this
default column that is entering a column value for a default column is optional.
4. Check:
Check constraint ensures that the data entered by the user for that column is within the range
of values or possible values specified.
Example: Create table STUDENT (id int, name varchar (25), age int,
check(age>=18));
As we have used a check constraint as (age>=18) which means value entered by user for this
age column while inserting the data must be less than or equal to 18.
Mr.D.ESWARAIAH, M.Tech., 6
5. Primary Key:
A primary key is a constraint in a table which uniquely identifies each row record in a
database table by enabling one or more column in the table as primary key.
A particular column is made as a primary key column by using the primary key keyword
followed by the column name.
Example:
Create table EMP (ID int, name varchar (20), age int, course varchar
(10), Primary key (ID));
Here we have used the primary key on ID column then ID column must contain unique
values i.e., one ID cannot be used for another student.
6. Foreign Key:
The foreign key constraint is a column or list of columns which points to the primary key
column of another table.
The main purpose of the foreign key is only those values are allowed in the present table that
will match to the primary key column of another table.
From the above two tables, COURSE_ID is a primary key of the table STUDENT_MARKS and also
behaves as a foreign key as it is same in STUDENT_DETAILS and STUDENT_MARKS.
Mr.D.ESWARAIAH, M.Tech., 7
Example:
(Reference Table)
Create table CUSTOMER1 (id int, name varchar (25), course varchar (10),
primary key (ID));
(Child table)
These constraints are used to ensure the uniqueness of each record or row in the data
table.
Entity Integrity constraints says that no primary key can take NULL VALUE,
since using primary key we identify each tuple uniquely in a relation .
Example:
Explanation:
In the above relation, EID is made primary key, and the primary key
can‟t take NULL values but in the 3 rd tuple, the primary key is NULL, so
it is violating Entity integrity constraints.
The referential integrity constraint is specified between two relations or tables and used
to maintain the consistency among the tuples in two relations.
This constraint is enforced through foreign key, when an attribute in the foreign key of
relation R1 have the same domain as primary key of relation R2, then the foreign key of
R1 is said to reference or refer to the primary key of relation R2.
The values of the foreign key in a tuple of relation R1 can either take the values of the
primary key for some tuple in Relation R2, or can take NULL values, but can‟t be empty.
Mr.D.ESWARAIAH, M.Tech., 8
Explanation:
In the above, DNO of the first relation is the foreign key and DNO in the second relation is the
primary key
DNO=22 in the foreign key of the first relation is not available in the second relation so, since
DNO=22 is not defined in the primary key of the second relation therefore Referential
integrity constraints is violated here.
SQL stands for Structure Query Language it is used for storing and managing data in
relational database management system.
It is standard language for relational database system. It enables a user to create, read, update
and delete relational databases and tables.
All the RDBMS like MYSQL, Oracle, MA access and SQL Server use SQL as their standard
database language.
SQL allows users to Query the database in a number of ways using statements like common
English.
Rules: SQL follows following rules
Mr.D.ESWARAIAH, M.Tech., 9
Mr.D.ESWARAIAH, M.Tech., 10
2. Data Manipulation Language (DML): used to update, store and retrieve data from tables.
3. Data Control Language (DCL): used to control the access of database created using
DDL and DML.
Every column is required to have a name and data type in the database table.
SQL DATA
TYPES
Binary data Numeric data Extract String data Date data type
type type numeric data type
type
1. BINARY DATATYPES:
There are three types of binary data types which are given below
Mr.D.ESWARAIAH, M.Tech., 11
2. NUMERIC DATATYPE:
DATA TYPE FROM TO DESCRIPTION
5. STRING DATATYPE:
DATA TYPE DESCRIPTION
Char It has a maximum length of 8000 characters. It contains fixed-length non-
Unicode characters.
Varchar It has a maximum length of 8000 characters. It contains variable-length
non-Unicode characters.
Text It has a maximum length of 2,147,483,647 characters. It contains variable-
length non-Unicode characters.
Mr.D.ESWARAIAH, M.Tech., 12
1. Create table: SQL create table is used to create a table in the database. To define the
table, you should define the name of the table and also define its column and column‟s data
type.
SYNTAX:
“column2” “datatype”,
“column3” “datatype”,
….
“column N” “datatype”);
EXAMPLE:
SQL > create table employee (emp_id int, emp_name varchar (25), phone_no int,
address char (30));
If you create the table successfully, you can verify the table by looking at the message by
the sql server. else you can use DESC command as follows
Mr.D.ESWARAIAH, M.Tech., 13
2. ALTER TABLE:
SYNTAX:
EXAMPLE:
3. DROP TABLE:
SYNTAX :
EXAMPLE:
1. Insert:
SQL insert statement is a sql query. It is used to insert a single multiple records in a table.
Syntax:
Mr.D.ESWARAIAH, M.Tech., 14
NAME ID CITY
Alekhya 501 Hyderabad
Deepti 502 Guntur
Ramya 503 Nellore
2. Update:
The SQL Commands update are used to modify the data that is already in the database.
SQL Update statement is used to change the data of records held by tables which rows is to
be update, it is decided by condition to specify condition, we use “WHERE” clause.
The update statement can be written in following form:
Syntax:
Update table_name set column_name=expression where condition;
Example:
Let‟s take an example: here we are going to update an entry in the table.
NAME ID CITY
Alekhya 501 Hyderabad
Deepti 502 Guntur
Rasi 503 Nellore
3. Delete:
The SQL delete statement is used to delete rows from a table.
Generally, delete statement removes one or more records from a table.
Syntax:
Example:
Mr.D.ESWARAIAH, M.Tech., 15
NAME ID CITY
Deepti 502 Guntur
Rasi 503 Nellore
2.18 Basic SQL querying (select and project) using where clause:
The following are the various SQL clauses:
SQL Clause
1. Group by:
SQL group by statement is used to arrange identical data into groups.
The group by statement is used with the SQL select statement.
The group by statement follows the WHERE clause in a SELECT statement and precedes
the ORDER BY clause.
Syntax:
Select column from table_name where column group by column, order by
column;
Example:
Select company count (*) from product group by company;
Mr.D.ESWARAIAH, M.Tech., 16
Output:
Com 1 2
Com 2 3
Com 3 5
2. Having clause:
Having clause is used to specify a search condition for a group or an aggregate.
Having clause is used in a group by clause, if you are not using group by clause then you can
use having function like a where clause.
Syntax:
Select column1, column2 from table_name
Where conditions
Having conditions
Example:
select company count (*) from product
Group by company
Output:
Com 3 5
Com 2 2
3. Order by clause:
The order by clause sorts the result _set in ascending or descending order.
Syntax:
Where condition
Sample table:
Mr.D.ESWARAIAH, M.Tech., 17
Example:
Output:
NAME ID CITY
Alekhya 501 Hyderabad
Deepti 502 Guntur
Rasi 503 Nellore
Syntax:
Select column1, column2, …………column from table_name where[condition];
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
SQL operators:
SQL statements generally contain some reserved words or characters that are used to
perform operations such as arithmetic and logical operations etc. Their reserved words are
known as operators.
+ Addition
- Subtraction
/ Division
* Multiplication
Mr.D.ESWARAIAH, M.Tech., 18
% modulus
1. Addition (+):
It is used to perform addition operation on data items.
Sample table:
EMP_ID EMP_NAME SALARY
1 Alex 25000
2 John 55000
3 Daniel 52000
4 Sam 12312
Output:
EMP_ID EMP_NAME SALARY SALARY+100
1 Alex 25000 25100
2 John 55000 55100
3 Daniel 52000 52100
4 Sam 12312 12412
Here we have done addition of 100 to each emp‟s salary.
2. Subtraction (-):
It is used to perform subtraction on the data items.
Example:
Select emp_id, emp_name, salary, salary-100 as “salary-100” from
subtraction;
EMP_ID EMP_NAME SALARY SALARY-100
1 Alex 25000 24900
2 John 55000 54900
3 Daniel 52000 51900
4 Sam 90000 89900
Here we have done subtraction of 100 for each emp‟s salary.
3. Division (/):
The division function is used to integer division (x is divided by y).an integer value is
returned.
Example:
Select emp_id, emp_name, salary, salary/100 as “salary/100” from
division;
Mr.D.ESWARAIAH, M.Tech., 19
4. Multiplication (*):
It is used to perform multiplication of data items.
Select emp_id, emp_name, salary, salary*100 as “salary*100” from
multiplication;
5. Modulus (%):
It is used to get remainder when one data is divided by another.
Select emp_id, emp_name, salary, salary%25000 as “salary%25000” from
modulus;
Output:
EMP_ID EMP_NAME SALARY SALARY%25000
1 Alex 25000 0
2 John 55000 5000
3 Daniel 52000 2000
4 Sam 90000 15000
Here we have done modulus operation to each emp‟s salary.
The following example finds all employees where salaries are greater than the 5000 and
less than 7000.
Select first_name, last_name, salary from employees where
salary>5000 AND salary<7000 order by salary;
Output:
FIRST_NAME LAST_NAME SALARY
John Wesley 6000
Eden Daniel 6000
Luis Popp 6900
Shanta Suji 6500
Mr.D.ESWARAIAH, M.Tech., 20
2. ALL:
The ALL operator compares a value to all values in another value set.
The following example finds all employees whose salaries are greater than all salaries
of employees.
EX:
select first_name, last_name, salary from employees where salary>=ALL
(select salary from employees where department_id =8) order by salary
DESC;
Output:
FIRST_NAME LAST_NAME SALARY
Steven King 24000
John Russel 17000
Neena Kochhar 14000
3. ANY:
The ANY operator compares a value to any value in a set ascending to condition.
The following example statement finds all employees whose salaries are greater than the average
salary of every department.
EX:
select first_name, last_name, salary from employees where salary >ANY
(select avg (salary) from employees‟ group by department_id) order by
first_name, last_name;
Output:
FIRST_NAME LAST_NAME SALARY
Alexander Hunold 9000.00
Charles Johnson 6200.00
David Austin 4800.00
Eden Flip 9000.00
4. Between:
The between operator searches for values that are within a set of values.
For example, the following statement finds all employees where salaries are between
9000 and 12000.
EX:
Output:
FIRST_NAME LAST_NAME SALARY
Alexander Hunold 9000.00
Den Richards 10000.00
Nancy Prince 12000.00
Mr.D.ESWARAIAH, M.Tech., 21
5. IN:
The IN operator compares a value to list of specified values. The IN operator return true if
compared value matches at least one value in the list.
The following statement finds all employees who work in department _id 8 or 9.
EX:
Output:
FIRST_NAME LAST_NAME DEPARTMENT_ID
John Russel 8
Jack Livingstone 8
Steven King 9
Neena Kochhar 9
6. Exists:
The EXISTS operator tests if a sub query contains any rows.
For example, the following statement finds all employees who have dependents.
select first_name, last_name from employees where EXISTS (select 1
from dependent d where d.employee_id=e.employee_id);
FIRST_NAME LAST_NAME
Steven King
Neena Kochhar
Alexander Hunold
Mr.D.ESWARAIAH, M.Tech., 22
Mr.D.ESWARAIAH, M.Tech., 23
dual;
Output: 05-DEC-2021.
ADD_MONTHS: This function returns a date after adding data with specified no of months.
Output: 31-MAR-17.
Output: 05-MAR-22.
Output: 05-DEC-2021.
NEXT_DAY: This function represents both day and date and returns the day of the next given day.
Output: 07-DEC-21.
Output: 31-DEC-21.
Mr.D.ESWARAIAH, M.Tech., 24
Output: -4.
ROUND: It gives the nearest value or round off value for the argument pass. (or) It returns a date
rounded to a specific unit of measure.
Output: 01-JAN-22.
TRUNC: This function returns the date with the time(co-efficient) portion of the date truncated to
the unit specified.
Output: 01-DEC-21.
TO_DATE: This function converts date which is in the character string to a date value.
Output: 01-JAN-17.
Output: 05 12 2021.
LEAST: This function displays the oldest date present in the argument list.
Output: 01-MAR-21.
GREATEST: This function displays the latest date present in the argument list.
Output: 28-DEC-21.
1. Count ()
2. Sum ()
3. Avg ()
Mr.D.ESWARAIAH, M.Tech., 25
4. Max ()
5. Min ()
From table_name
Where condition);
2. Sum (): It will add/ sum all the column values in the query.
From table_name
Where condition);
3. Avg (): Avg function used to calculate average values of the set of rows.
From table_name
Where condition);
4. Max (): This function is used to find maximum value from the set of values.
From table_name
Where condition);
5. Min (): This function is used to find minimum value from the set of values.
From table_name
Where condition);
Mr.D.ESWARAIAH, M.Tech., 26
Numeric functions are used to perform operations on numbers and return numbers.
OUTPUT: 243.5
4. CEIL (): It returns the smallest integer value that is a greater than or equal to a
number. EX: select CEIL (25.77) from dual;
OUTPUT: 26
Mr.D.ESWARAIAH, M.Tech., 27
5. FLOOR (): It returns the largest integer value that is a less than or equal to a number.
EX: select FLOOR (25.75) from dual;
OUTPUT: 25
6. TRUNCATE (): This does not work for SQL server. It returns the truncated to 2 places right of
the decimal point.
EX: select TRUNCATE (7.53635, 2) from dual;
OUTPUT: 7.53
7. MOD (): It returns the remainder when two numbers are divided.
EX: select MOD (55,2) from dual;
OUTPUT: 1.
8. ROUND (): This function rounds the given value to given number of digits of precision.
EX: select ROUND (14.5262,2) from dual;
OUTPUT: 14.53.
9. POWER (): This function gives the value of m raised to the power of
n. EX: select POWER (4,9) from dual;
OUTPUT: 262144.
10. SQRT (): This function gives the square root of the given value n.
EX: Select SQRT (576) from dual;
OUTPUT: 24.
11. LEAST (): This function returns least integer from given set of integers.
OUTPUT: 1.
12. GREATEST (): This function returns greatest integer from given set of integers.
OUTPUT: 22
Mr.D.ESWARAIAH, M.Tech., 28
String Functions are used to perform an operation on input string and return
the output string. Following are the string functions
1. CONCAT (): This function is used to add two words (or) strings.
3. LOWER (): This function is used to convert the given string into
OUTPUT: database
4. UPPER (): This function is used to convert the lowercase string into
OUTPUT: DATABASE
5. LPAD (): This function is used to make the given string of the given size by adding the given
OUTPUT: 00system
6. RPAD (): This function is used to make the given string as long as the given size by adding the
given symbol on the right.
OUTPUT: system00
7. LTRIM (): This function is used to cut the given substring from the original string.
Mr.D.ESWARAIAH, M.Tech., 29
OUTPUT: base
8. RTRIM (): This function is used to cut the given substring from the original string.
OUTPUT: data.
9. INITCAP (): This function returns the string with first letter of each word starts with uppercase.
10. LENGTH (): Tis function returns the length of the given string.
OUTPUT: 11.
11. SUBSTR (): This function returns a portion of a string beginning at the character
OUTPUT: AM.
12. TRANSLATE (): This function returns a string after replacing some set of characters into
another set. EX: select TRANSLATE („Delhi is the capital of India‟,‟i‟,‟a‟) from
Review Questions:
Mr.D.ESWARAIAH, M.Tech., 30
Mr.D.ESWARAIAH, M.Tech., 31
References:
Mr.D.ESWARAIAH, M.Tech., 32