DATABASE
DATABASE
What is data?
Data is collection of characters, numbers and other symbols that represents values
of some situations or variables
What is Database?
Collection of logically related data that has been recorded, organized and made
available for searching is called database.
Properties of database:
It represents real world accepts.
It is well structured and populated with specific data.
It can be managed manually or automatically.
It is secured and durable(data is never lost)
Banking
Online shopping
Inventory management
Loan and investment
Reservation system and many more
DBMS
DBMS provides many operations e.g. creating a database, sorting in the database, updating an
existing database, delete from the database.
DBMS is a say system that enables you to store, modify and retrieve data in an organized way. It
also provides security to the database.
As the name suggests, the database management system consists of two parts.
They are:
1. Database
2. Management System
To find out what database is, we have to start from data, which is the basic building block of
any DBMS.
Data: Facts, figures, statistics etc. having no particular meaning (e.g. 1, SONIKA, 19 etc).
Record: Collection of related data items, e.g. in the above example the three data items had
no meaning. But if we organize them in the following way, then they collectively represent
meaningful information.
1 SONIKA 19
2 SAKSHI 18
3 MANJU 21
Fields, Attributes or Domains
Tuples or Records
1. Database:
Table-1 Table-2
Table-3 Table-4
YEAR HOSTEL
ROLL NO YEAR
1 1997 2001 H1
2 2001
2006 H2
3 2006
We now have a collection of 4 tables. They can be called a “related collection” because we can
clearly find out that there are some common attributes existing in a selected pair of tables.
Because of these common attributes we may combine the data of two or more tables together
to find out the complete details of a student. Questions like “Which hostel does the youngest
student live in?” can be answered now,
Application Programs
DBMS Software
Stored
Database &
Its Definition
2. Management System:
A database-management system (DBMS) is a collection of interrelated data and a set of
programs to access those data. This is a collection of related data with an implicit meaning and
hence is a database.
The collection of data, usually referred to as the database, contains information relevant to an
enterprise. The primary goal of a DBMS is to provide a way to store and retrieve database
information that is both convenient and efficient. By data, we mean known facts that can be
recorded and that have implicit meaning.
The management system is important because without the existence of some kind of rules and
regulations it is not possible to maintain the database. We have to select the particular
attributes which should be included in a particular table; the common attributes to create
relationship between two tables; if a new record has to be inserted or deleted then which tables
should have to be handled etc. These issues must be resolved by having some kind of rules to
follow in order to maintain the integrity of the database.
Database systems are designed to manage large bodies of information. Management of data
involves both defining structures for storage of information and providing mechanisms for the
manipulation of information. In addition, the database system must ensure the safety of the
information stored, despite system crashes or attempts at unauthorized access.
View of Data
A major purpose of a database system is to provide users with an abstract view of the data.
That is, the system hides certain details of how the data are stored and maintained.
Data Abstraction
Many database-system users are not computer trained developers hide the complexity from
users through several levels of abstraction, to simplify users’ interactions with the system:
1. High Cost:
The cost of implementing a DBMS system is very high. It is also a very time
consuming process which involves user requirements, designing the database
specifications, writing application programs and then also providing training.
2. Security and Recovery Overheads:
Unauthorized access to a database can lead to Database
Management Applications threat to the individual or organization depending on the
data stored
Also the data must be regularly backed up to prevent its loss due to fire,
earthquakes, etc.
For example, consider EMPLOYEE table in each row in this table represents facts about
a particular employee. The column (attributes) names – Name, Employee_ID, Gender,
Salary and Date_of_Birth specify how to interpret the data in each row.
Employee Table
In relational model:---
EMPLOYEE
table is a relation.
The domain of a database attribute is the set of all the possible values that attribute
may contain. In order to specify a domain, we specify the data type of that attribute.
Following are the domain of attributes of the EMPLOYEE relation:
(e) Date_of_Birth – Should have a valid date, month and year. The birth year of the
employee must be greater than 1985. Also the format should be dd-mm-yyyy.
The degree of the EMPLOYEE relation is 5 as there are five attributes in this relation.
The cardinality of the EMPLOYEE relation is 3 as there are three tuples in this relation.
RDBMS Constraints:---
NOTNULL: A column cannot have NULL values where NULL means missing / unknown
/ not applicable value.
Key Constraint:
(i) Super key:-- Super key is a set of attributes in a relation, for which no two
tuples in a relation state have the same combination of values.
Every relation must have at least one super key which is the combination
of all attributes in a relation.
NOTE- Gender and Salary attributes is not a super key because both these attributes have
identical value for employees Radhika Mehta and Sarika yadav
(ii) Candidate key:-- minimal super keys are called candidate keys.
Candidate key allow us to identify uniquely a tuple in the relation.
(iii) Primary Key:-- One of the candidate keys may be designated as Primary
key. Primary key is used to identify tuples in a relation. If a relation has
many candidate keys it is preferable to choose that one as primary key
which has least number of attributes.
Primary key allow us to identify uniquely a attributes in the relation. and
not null.
(iv) Foreign key:---The attributes which refers to value of an attribute as
primary key in another table is called Foreign key.
SQL commands
SQL commands used to define and modify a database.
Help of SQL commands we are create database, create relations and delete database,add
attributes , remove attributes and change schema
Command of SQL
Commands
Help of DDL the made schema of DB (Table). In other words we can say the create the table
by DDL.
1. Create commands.
2. Alter commands.
3. Drop commands.
1. Create commands:
Creating Database:
Syntax
Example
After creating a database StudentAttendance we need to define relation in this database and
specify attributes for each relation along with data type and constraint.
Syntax
Example
Program:
);
OUTPUT
Students
2. Alter commands:
We need to add /remove an attribute or to modify the data type of an existing attribute or
to add constraint in attribute of the table by using the alter command.
“Alter TABLE statement is used to make changes in the structure of a table like
adding, removing or changing data type of columns.
Adding a column:
We may need to add an additional attribute in a table. it can be using the ADD
attribute statement.
Syntax:
ALTER TABLE Table name ADD <attribute name 1 , attribute name 2>
EXAMPLE:
ALTER TABLE Table name Add primary key ( admission date, roll no );
Syntax
ALTER TABLE Table name Add FOREIGN KEY (attribute name) REFERENCES
referenced Table name (attribute name);
Syntax:
3. DROP commands:
We can use a DROP command to remove a database or a table permanently from the
system.
DROP command is used to delete tables.
Data manipulation using a database means either insert of new data removal of exiting
data or modification of existing data in the database.
For ex. The insert statement add the first record in the table.
INSERT INTO Students VALUES (101,"vikash yadav", "alwar", 9521081323 );
2. SELECT COMMAND:
The SQL command SELECT is used to retrieve data from the table in a database and
output is also displayed in tabular form.
There are various ways in which the SELECT command can be used.
To select all the data available in a table we use the following SELECT statement.
SELECT FROM Table_name;
Consider the following tables in the School Database for all the queries that follow:
PROGRAM
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(30) NOT NULL
);
CREATE TABLE Teacher
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Year_of_Birth DATE,
Dept_No INTEGER,
CONSTRAINT TEACHER_PK PRIMARY KEY (Teacher_ID),
CONSTRAINT TEACHER_FK FOREIGN KEY (Dept_No) REFERENCES
Department (Dept_ID)
);
The Teacher table refers Department table to keep track of the department to which a teacher
belongs.
Assume that the following data has been inserted in the Department tables using the Insert
commands:
Department
Dept_ID Dept_Name
1 Chemistry
2 Computer
Science
3 English
4 Physics
5 Biology
6 Commerce
7 Mathematics
8 Economics
Assume that the following data has been inserted in the Teacher tables using the Insert
commands:
TEACHER
In this query we have to specify all the attributes in the SELECT clause.
NOTE -- use asterisk <Star> (*), which means select all the attributes in table.
Query (PROGRAM)
SELECT *
FROM Teacher
WHERE Teacher_ID=101;
Result:
Query (PROGRAM)
SELECT First_Name, Salary,Dept_No
FROM Teacher
WHERE Teacher_ID=105;
Result:
Query:
SELECT First_Name,Last_Name
FROM Teacher
Result:
First_Name Last_Name
Subhash Chndr
Mahipal Ji
Imran Khan
Vikash Yadav
Program
FROM Teacher
Result:
NOTE : Thus Boolean operations AND,OR can also be used in the WHERE clause.
Query:
To retrieve names of all the teachers and the names and numbers of their respective
departments.
Note That the above query requires two tables – Teacher and Department.
Query:
This query we have not specified any WHERE clause, this query will combine each row in
Teacher table with each row of Department table resulting in product of two tables. This is
called as Cross Join.
Now if we have to combine each teacher with his/her respective department, there should be a
connecting column between the two tables which can be used to join them. Thus Dept_No
(Teacher table) and Dept_ID (Department table) can be used to join the two tables. This type
of join will result in joining rows from Teacher and Department table.
Query:
WHERE Dept_ID=Dept_No;
In SQL, duplicate tuples can appear more than once in a table and in the result of a query. However if
the requirement is to list distinct values of an attribute then this can be done by using the keyword –
'DISTINCT'.
For example, query will list all the Department numbers corresponding to departments having male
teachers.
SELECT Dept_No
FROM Teacher
Dept_No
8
5
7
1
4
2
3
6
2
7
In the above result, 2, 7 is appearing twice which is not required as the query is to find only the
department numbers.
Hence we can use DISTINCT keyword in the SELECT clause so that there is no repetition in
the result.
FROM Teacher
Dept_No
8
5
7
1
4
2
3
6
Renaming attributes(cloumn):
In case of we want to rename any column while display the output it can be done by using the
keyword ‘AS’.
Exampale:
FROM Department;
OUTPUT:
Dept_ID SUBJECT_NAME
1 Chemistry
2 Computer
Science
3 English
4 Physics
5 Biology
6 Commerce
7 Mathematics
8 Economics
% replaces zero or more number of random characters and _ replaces a single character.
Query: To retrieve names of all the teachers starting from letter 'A'.
FROM Teacher
Output
First_Name Last_Name Dept_No
Ashok Sir 5
Anand Nehara 2
Abbas Alli 7
Query:
To retrieve names of all the teachers having 5 characters in the first name and starting with
'M'
Program:
SELECT First_Name
FROM Teacher
Output:
First_Name
Manoj
Query: To retrieve names of all the teachers having at least 7 characters in the first name.
Program(syntax):
SELECT Last_Name
FROM Teacher
Output:
Last_Name
Chndr
Ji
Soni
Sorting data in Dbms:
Sorting data can be done by using the clause – ORDER BY followed by the attributes which
needs to be sorted.
For ascending order the keyword ASC and for descending order the keyword DESC is used
Program:
SELECT First_Name
FROM Teacher
ORDER BY First_Name;
Output (Result):
----------------
Query: To list the names of all the Departments in the descending order of their names.
syntx
SELECT Dept_Name
FROM Department
Result:
------------
Query:
SELECT First_Name
FROM Teacher
Output
-----------------
Query:
Program
FROM Department
WHERE Dept_ID IN (Select Dept_No FROM Teacher WHERE Gender = 'F');
Output: -----------
Nested query: Query within another query is called Nested query. The outer one is called
outer query and the inner one is called as inner query.
Aggregate Functions:
When apply mathematical functions on group of values in a database. Such functions are
called Aggregate Functions.
SUM – It finds the sum of all the values for a selected attribute which has numeric data type.
MAX –It finds the maximum value out of all the values for a selected attribute which has
numeric data type.
MIN - It finds the minimum value out of all the values for a selected attribute which has numeric
data type.
AVG – It finds the average value of all the values for a selected attribute which has numeric
data type.
Program-
FROM Teacher;
Result: ________
FROM Teacher;
Result:_________
SELECT COUNT(Salary)
FROM Teacher
Result: _________
FROM Teacher
WHERE Dept_No = 3;
Result: __________
Arithmetic operators:-
For example if we want to display Teacher name, current salary and a 10% increase in the
salary for those teachers who belongs to Department number 7, the SELECT statement can be
written:
FROM Teacher
WHERE Dept_No = 7;
Result:_____
3. Update Command:
This command is used to update the attribute values of one or more tuples in a table.
Syntax :
SET Attribute
WHERE Condition ;
Program-
UPDATE Teacher
SET Salary=60000
WHERE Teacher_ID=105;
OUTPUT:______________
Query : if we want to Update increase the salary of a Teacher Priya mam by 8000.
Program-
UPDATE Teacher
SET Salary=Salary+8000
WHERE First_Name="Priya";
4. DELETE COMMAND :
DELETE command is used to delete/ remove one or more records (Tuple) from a Table.
Syntax:
DELETE
WHERE Condition;
DELETE
FROM Teacher
WHERE Teacher_ID=105;
NOTE: If the WHERE clause is missing then it will delete all the tuples in a table :