Professional Documents
Culture Documents
Unit 2 DBMS
Unit 2 DBMS
Unit 2 DBMS
Unit-2
Syllabus: Relational Model: Introduction to relational model, concepts of domain, attribute, tuple,
relation, importance of null values, constraints (Domain, Key constraints, integrity constraints) and their
importance BASIC SQL: Simple Database schema, data types, table definitions (create, alter), different
DML Operations (insert, delete, update), basic SQL querying (select and project) using where clause,
arithmetic & logical operations, SQL functions (Date and Time, Numeric, String conversion)
……………………………………………………………………………………………………
RelationalModel:
Relational model can represent as a table with columns and rows. Each row is known as a tuple.
Each table of the column has a name or attribute. It is widely used data model for data storage
and processing. This model is simple and has a capability to process data with storage
efficiency.
Key Terms:
Tables: In this model relations are saved in the form of tables. It has tuples and fields.
Domain: It contains a set of atomic values that an attribute can take.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must have a
domain, dom(Ai)
Relational instance: In the relational database system, the relational instance is represented by a
finite set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify the
row in the relation uniquely.
Properties of Relations:
Example:
One-to-many:
In one-to-many mapping, an entity in E1 is associated with any number of entities in E2, and an
entity in E2 is associated with at most one entity in E1.
Example:
Many-to-one:
In one-to-many mapping, an entity in E1 is associated with at most one entity in E2, and an
entity in E2 is associated with any number of entities in E1.
Example:
Many-to-many:
In many-to-many mapping, an entity in E1 is associated with any number of entities in E2, and
an entity in E2 is associated with any number of entities in E1.
Example:
Keys:
Keys play an important role in the relational database. It is used to uniquely identify any record
or row of data from the table. It is also used to establish and identify relationships between
tables.
Example: In Student table, ID is used as a key because it is unique for each student. In
PERSON table, passport_number, license_number, SSN are keys since they are unique for each
person.
Types of key:
1. Primary key:
It is the first key which is used to identify one and only one instance of an entity uniquely.
Example: In the EMPLOYEE table, ID can be primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary key since they are also unique.
For each entity, selection of the primary key is based on requirement and developers.
2. Candidate key:
A candidate key is an attribute or set of an attribute which can uniquely identify a tuple.
The remaining attributes except for primary key are considered as a candidate key. The
candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. Rest of the
attributes like SSN, Passport_Number, and License_Number, etc. are considered as a
candidate key.
3. Super Key:
Super key is a set of an attribute which can uniquely identify a tuple.
Super key is a superset of a candidate key.
For example: In the above EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME)
the name of two employees can be the same, but their EMPLYEE_ID can't be the same.
Hence, this combination can also be a key.
The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc.
4. Foreign key:
Foreign keys are the column of the table which is used to point to the primary key of another
table.
For Example: In a company, every employee works in a specific department, and employee
and department are two different entities. So we can't store the information of the department
in the employee table. That's why we link these two tables through the primary key of one
table.
We add the primary key of the DEPARTMENT table, Department_Id as a new attribute in
the EMPLOYEE table.
Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables are
related.
5. Composite Key:
When we create keys on more than one column then that key is known as Composite key.
For Example: student which has two columns as sid and srefno and we make primary key
on those two columns. Then this key is known as Composite key.
Attribute:
The attribute is used to describe the property of an entity. Eclipse is used to represent an
attribute.
For Example: id, age, contact number, name, etc. can be attributes of a student.
1. Simple attribute:
Simple attributes are atomic values, which cannot be divided further. For example, a student's
phone number is an atomic value of 10 digits.
2. Key Attribute:
The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.
3. Composite Attribute:
An attribute that composed of many other attributes is known as a composite attribute. The
composite attribute is represented by an ellipse, and those ellipses are connected with an ellipse.
4. Multivalued Attribute:
An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute. For example, a student can
have more than one phone number.
5. Derived Attribute:
An attribute that can be derived from other attribute is known as a derived attribute. It can be
represented by a dashed ellipse. For example, A person's age changes over time and can be
derived from another attribute like Date of birth.
Here, NOT NULL signifies that column should always accept an explicit value of the given
data type.
A field with a NULL value is the one that has been left blank during the record creation.
Example:
Select id, name, age, address, salary
from customers
where salary is not null;
Output:
Integrity Constraints:
Integrity constraints are a set of rules. It is used to maintain the quality of information.
Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints:
Domain constraints can be defined as the definition of a valid set of values for an attribute.
The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain.
Example:
Example:
Example:
4. Key constraints:
Keys are the entity set that is used to identify an entity within its entity set uniquely.
An entity set can have multiple keys, but out of which one key will be the primary key.
A primary key can contain a unique value in the relational table.
Example:
Database Schema:
A database schema is the skeleton structure that represents the logical view of the entire
database.
It defines how the data is organized and how the relations among them are associated. It
formulates all the constraints that are to be applied on the data.
A database schema defines its entities and the relationship among them.
It contains a descriptive detail of the database, which can be depicted by means of schema
diagrams.
It’s the database designers who design the schema to help programmers understand the
database and make it useful.
CHAR(Size) It is used to specify a fixed length string that can contain numbers,
letters, and special characters. Its size can be 0 to 255 characters.
Default is 1.
VARCHAR(Size) It is used to specify a variable length string that can contain
numbers, letters, and special characters. Its size can be from 0 to
65535 characters.
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size
parameter specifies the column length in the bytes. Default is 1.
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size
parameter specifies the maximum column length in bytes.
TEXT(Size) It holds a string that can contain a maximum length of 255
characters.
TINYTEXT It holds a string with a maximum length of 255 characters.
MEDIUMTEXT It holds a string with a maximum length of 16,777,215.
LONGTEXT It holds a string with a maximum length of 4,294,967,295
characters.
ENUM(val1, val2, It is used when a string object having only one value, chosen from a
val3,...) list of possible values. It contains 65535 values in an ENUM list. If
you insert a value that is not in the list, a blank value will be
inserted.
SET( It is used to specify a string that can have 0 or more values, chosen
val1,val2,val3,....) from a list of possible values. You can list up to 64 values at one
time in a SET list.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to
65,535 bytes.
BIT(Size) It is used for a bit-value type. The number of bits per value is specified
in size. Its size can be 1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -2147483648
to 2147483647 and unsigned range varies from 0 to 4294967295. The
size parameter specifies the max display width that is 255.
INTEGER(size) It is equal to INT(size).
FLOAT(size, d) It is used to specify a floating point number. Its size parameter specifies
the total number of digits. The number of digits after the decimal point is
specified by d parameter.
FLOAT(p) It is used to specify a floating point number. MySQL used p parameter to
determine whether to use FLOAT or DOUBLE. If p is between 0 to24,
the data type becomes FLOAT (). If p is from 25 to 53, the data type
becomes DOUBLE().
DOUBLE(size, d) It is a normal size floating point number. Its size parameter specifies the
total number of digits. The number of digits after the decimal is specified
by d parameter.
DECIMAL(size, d) It is used to specify a fixed point number. Its size parameter specifies the
total number of digits. The number of digits after the decimal parameter
is specified by d parameter. The maximum value for the size is 65, and
the default value is 10. The maximum value for d is 30, and the default
value is 0.
DEC(size, d) It is equal to DECIMAL(size, d).
BOOL It is used to specify Boolean values true and false. Zero is considered as
false, and nonzero values are considered as true.
3. MySQL Date and Time Data Types:
SQL Languages:
Database languages can be used to read, store and update the data in the database.
Types of Database Language:
DML stands for Data Manipulation Language. It is used for accessing and manipulating data in
a database.
Here are some tasks that come under DML:
1. Select: It is used to retrieve data from a database.
2. Insert: It is used to insert data into a table.
3. Update: It is used to update existing data within a table.
4. Delete: It is used to delete all records from a table.
Data Control Language (DCL):
It is used to retrieve the stored or saved data. The DCL execution is transactional. It also has
rollback parameters. (But in Oracle database, the execution of data control language does not
have the feature of rolling back.)
Here are some tasks that come under DCL:
1. Grant: It is used to give user access privileges to a database.
2. Revoke: It is used to take back permissions from the user.
Transaction Control Language (TCL):
TCL is used to run the changes made by the DML statement. TCL can be grouped into a logical
transaction.
Here are some tasks that come under TCL:
• Commit: It is used to save the transaction on the database.
• Rollback: It is used to restore the database to original since the last Commit.
SQL Queries:
If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table.
Syntax: insert into table_name
values(value1,value2,value3,…..valueN);
Output:
3. Drop: it is used to delete both the structure and records stored in the table.
Syntax: drop table table_name;
Example: drop table student;
4. Alter: it is used to add, delete, or modify columns in an existing table and also used to add
and drop various constraints on an existing table.
a) Syntax for Add Column: ALTER TABLE table_name
ADD column_name datatype;;
Example: alter table student add smarks integer(20);
b) Syntax for Drop Column: ALTER TABLE table_name
Drop column column_name datatype;;
Example: alter table student drop column smarks;
c) Syntax for Alter/Modify Column: ALTER TABLE table_name
ALTER COLUMN column_name datatype;
(And)
6. Truncate: it is used to delete the data inside a table, but not the table itself.
Syntax: truncate table table_name;
Example: truncate table student;
Customers Table:
9. Select: it is used to select select data from a database. The data returned is stored in a result
table, called the result-set.
Syntax: SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example: select sid,sname,sdepat
From student
Where sid=’505’;
If you want to select all the fields available in the table
Syntax: Select * from student;
SQL Operators:
SQL statements generally contain some reserved words or characters that are used to perform
operations such as comparison and arithmetical operations etc. These reserved words or
characters are known as operators.
Generally there are three types of operators in SQL:
1. SQL Arithmetic Operators
2. SQL Comparison Operators
3. SQL Logical Operators
Operator Description
ALL to compare a value to all values in another value set.
AND operator allows the existence of multiple conditions in an SQL statement.
ANY to compare a value to any applicable value in the list as per the condition.
BETWEEN to search for values, that are within a set of values
IN to compare a value to that specified list value
NOT reverse the meaning of any logical operator
OR to combine multiple conditions in SQL statements
EXISTS to search for the presence of a row in a specified table
LIKE to compare a value to similar values using wildcard operator
1. COUNT():
• COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table.
• COUNT(*) considers duplicate and Null.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST:
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Com1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
2. SUM():
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Syntax: SUM()
or
SUM( [ALL|DISTINCT] expression )
Example: SELECT SUM(COST)
FROM PRODUCT_MAST;
Output:
670
Example: SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
Output: 320
3. AVG():
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax: AVG()
or
AVG( [ALL|DISTINCT] expression )
Example: SELECT AVG(COST)
FROM PRODUCT_MAST;
Output: 67.00
4. MAX():
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax: MAX()
or
MAX( [ALL|DISTINCT] expression )
Syntax: MIN()
or
MIN( [ALL|DISTINCT] expression )
Example: SELECT MIN(RATE)
FROM PRODUCT_MAST;
Output: 10
Scalar functions:
These functions are based on user input, these too returns single value.
1 UCASE()
2 LCASE()
3 MID()
4 LEN()
5 ROUND()
6 NOW()
7 FORMAT()
Example: Students-Table
Example:
SELECT UCASE(NAME) FROM Students;
Output:
NAME
HARSH
SURESH
PRATIK
DHANRAJ
RAM
NAME
harsh
suresh
pratik
dhanraj
ram
3. MID(): The MID() function extracts texts from the text field.
Syntax:
SELECT MID(column_name,start,length) AS some_name FROM table_name;
Example:
SELECT MID(NAME,1,4) FROM Students;
Output:
NAME
HARS
SURE
PRAT
DHAN
RAM
4. LEN(): The LEN() function returns the length of the value in a text field.
Syntax:
SELECT LENGTH(column_name) FROM table_name;
Example:
SELECT LENGTH(NAME) FROM Students;
Output:
NAME
5. ROUND():
The ROUND() function is used to round a numeric field to the number of decimals specified.
Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
Example:
SELECT ROUND(MARKS,0) FROM Students;
Output:
MARKS
90
50
80
95
85
6. NOW(): The NOW() function returns the current system date and time.
Syntax:
NAME DateTime
NAME Date
HARSH 2017-01-13
SURESH 2017-01-13
PRATIK 2017-01-13
DHANRAJ 2017-01-13
RAM 2017-01-13
Relational Algebra:
Relational algebra is a procedural query language. It gives a step by step process to obtain the
result of the query.
Operations:
1. Select Operation:
• The select operation selects tuples that satisfy a given predicate.
• It is denoted by sigma (σ).
Notation: σ p(r)
Where
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and NOT.
Example:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
2. Project Operation:
• This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes are eliminated from the table.
• It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example:
∏ Loan_No,Amount (Loan)
Output:
LOAN_NO AMOUNT
L-17 1000
L-23 2000
L-15 1500
L-14 1500
L-13 500
L-11 900
L-16 1300
This returns the system date and time in the form of a string. In this case, it will be ‘ 08-01-2018
12:28:34’.
2. MONTHS_BETWEEN(x,y):
This function takes two values namely x and y which are in the form of months. It returns the
number of months between x and y.
Example: SELECT MONTHS_BETWEEN (SYSDATE, EMP_JOIN_DATE) FROM EMP;
Consider the Employee joining date as 1-January-2018 and the system date as 1-August-2018.
Therefore the above returns 7.
3. ADD_MONTHS(d,n):
This function gives the same day as d, n number of months away. The value of n can be positive
or negative.
Example: SELECT SYSDATE, ADD_MONTHS (SYSDATE,2) FROM DUAL;
This function will return the sysdate and the date 2 months after the sysdate i.e. ‘1-August-2018’
and ‘1-October-2018’.
4. LAST_DAY(d):
This function returns the last day of the month for the specific month d provided in the function.
Example: SELECT SYSDATE, LAST_DAY (SYSDATE) FROM DUAL;
This returns the system date and the last day of the particular month for the system date i.e. ‘1-
August-2018’ and ‘31-August-2018’.
2. To_Number():
The TO_NUMBER function converts a character value to a numeric datatype. If the string being
converted contains nonnumeric characters, the function returns an error.
Syntax: To_Number(string, format) from table_name;
Prepared by M.ANILKUMAR Page 34
DATABASE MANAGEMENT SYSTEM