Database Concepts Final
Database Concepts Final
DATA-
Data is a collection of raw facts which have not been processed to reveal useful information.
DATA PROCESSING-
Eg;
DBMS-
→ Stands for database management system
→ A database is an organized collection of data that has been arranged in an systematic order.
→ Database Management System is a software that enables users to create and maintain databases. The
popular DBMSs are MySQL, PostgreSQL, Microsoft Access, Oracle, Microsoft SQL Server, DB2 and Sybase.
Properties :-
→ A database is a representation of some aspect of the real world also called miniworld. Whenever there
are changes in this miniworld they are also reflected in the database.
→ It is designed, built and populated with data for specific purpose.
→ It can be of any size and complexity.
→ It can be maintained manually or it may be computerized.
→ It produce flexible reports, both on screen and on paper, that make it easy to comprehend the information
stored in the database.
Advantages :-
→ Overcomes data redundancy (Data Redundancy: Same information is stored in more than one file. This
would result in wastage of space.)
→ Data inconsistency could be ignored (Data Inconsistency: If a file is updated then all the files containing
similar information must be updated else it would result in inconsistency of data.)
→ Helps in data integration (Lack of Data Integration: As data files are independent, accessing information
out of multiple files becomes very difficult. )
→ Improved availability ( Same information is made available to different users by helping by sharing of
Information Technology Page 1
→ Improved availability ( Same information is made available to different users by helping by sharing of
information.)
→ User friendly ( It is easy to access, modify and delete the data and reduces the dependency of users on
computers.)
→ Improved Security (Though there is improvement in the availability of information to users, it may also be
required to restrict the access to confidential information. By making use of passwords and controlling
users' database access rights, the DBA can provide security to the database.)
Limitations Of DBMS :-
→ High Cost: The cost of implementing a DBMS system is very high.
→ Time Consuming: It is also a very time-consuming process which involves analyzing user requirements,
designing the database specifications, writing application programs and then also providing training.
→ Security and Recovery Overheads: Unauthorized access to a database can lead to threat to the individual
or organization depending on the data stored.
→ Digital literacy is required: One should have proper knowledge to interact with DBMS to take its best
possible use.
DBMS Environment :-
Operations on Database :-
→ Defining the Database: It involves specifying the data type of data that will be stored in the database and
also any constraints on that data.
→ Populating the Database: It involves storing the data on some storage medium that is controlled by DBMS.
→ Manipulating the Database: It involves modifying the database, retrieving data or querying the database,
generating reports from the database etc.
→ Sharing the Database: Allow multiple users to access the database at the same time.
→ Protecting the Database: It enables protection of the database from software/ hardware failures and
unauthorized access.
→ Maintaining the Database: It is easy to adapt to the changing requirements. Some examples of DBMS are –
MySQL, Oracle, DB2, IMS, IDS etc.
RDBMS-
→ A database that stores data in separate tables that are related through the use of a common column is
called a Relational database.
→ Examples of RDBMS are Oracle, MySQL, IBM DB2.
Terminologies-
→ Domain is pool of values or the collection (set) of possible values from which the value for a column is
derived. OR The data type of values in each column is called the Domain.
→ A row is called a Tuple/Record.
→ A column is called an Attribute/Feild.
→ A table is called as a Relation.
→ The number of attributes in a relation is called the Degree of a relation.
→ The number of rows in a relation is called the Cardinality of a relation.
Eg;
Keys-
A column or a combination of columns which can be used to identify one or more rows (tuples) in a table is
called a key of the table.
TYPES-
→ Primary Key: The group of one or more columns used to uniquely identify each row of a relation is called
its Primary Key.
→ Candidate Key: A column or a group of columns which can be used as the primary key of a relation is
called a Candidate key because it is one of the candidates available to be the primary key of the relation.
→ Alternate Key: A candidate key of a table which is not selected as the primary key is called its Alternate
Information Technology Page 4
→ Alternate Key: A candidate key of a table which is not selected as the primary key is called its Alternate
Key.
→ Foreign Key: A primary key of a base table when used in some other table is called as Foreign Key.
→ Super key: It is a set of one or more attributes that can uniquely identify a row in a table. Every relation
must have at least one super key which is the combination of all attributes in a relation.
NOTE: Key is the minimal super key, which means it is that super key of a relation from which if any
attribute is removed then it no longer remains a super key.
• For example the superkey {Name, Employee_ID, Gender}is not a key as we can remove Name and Gender
from this combination and then what is left {Employee_ID} is still a Superkey. Now {Employee_ID} is a key
as it is a superkey as well as no more removals are possible.
• A relation may have more than one key. Consider the relation PERSON with the following schema: PERSON
(Aadhar_number, PAN, Voter_ID_cardno, Name, Date_of_birth, Address). This relation has three keys
namely : {Aadhar_number}, {PAN}, {Voter_ID_no} as every individual in India has a unique Aadhar
card number, PAN as well as Voter ID card number.
Characteristics of Relations-
→ Ordering of tuples is not important in a Relation.
→ The ordering of attributes is also unimportant.
→ No two tuples of relation should be identical i.e. given any pair of two tuples, value in at least one column
must be different.
→ The value in each tuple is an atomic value (indivisible).
→ If the value of an attribute in a tuple is not known or not applicable or not available, a special value
called null is used to represent them.
Constraints-
Constraints, are restrictions on the values, stored in a database based on the requirements.
TYPES-
→ Domain Constraint:
• It specifies that the value of every attribute in each tuple must be from the domain of that attribute
• For example, the Employee_ID must be a 4-digit number. Hence a value such as “12321” or “A234”
violates the domain constraint as the former is not 4-digit long and the latter contains an alphabet.
→ Null Value Constraint:
• Makes sure that NULLs are not accepted in the specified column
• Sometimes it is required that certain attributes cannot have null values. For example, if every EMPLOYEE
must have a valid name then the Name attribute is constrained to be NOT NULL.
→ Entity Integrity Constraint:
• Sets a column or a group of columns as a primary key of the table. Therefore, NULLs and Duplicate values
in this column are not accepted.
• This constraint specifies that primary key of a relation cannot have null value. The reason behind this
constraint is that we know primary key contains no duplicates. However if we allow null values for a
Information Technology Page 5
constraint is that we know primary key contains no duplicates. However if we allow null values for a
primary key then there can be multiple tuples for which primary key is having null values. This would
imply that we are allowing duplicate values (NULL) for a primary key which itself violates the definition of
primary key
→ Referential Integrity Constraint:
• Data will be accepted in this column, if the same data value exists in a column in another related table.
This other related table name and column name are specified while creating the foreign key constraint.
• This constraint is specified between two relations. If foreign key acts in a relation R1 to another relation
R2 then R1 is called the referencing relation and R2 is called referenced relation, and a referential
integrity constraint holds from R1 to R2. The main purpose of this constraint is to check that data entered
in one relation is consistent with the data entered in another relation.
→ Unique Key:
• Make sure that duplicate values in the specified column are not accepted.
→ Default:
• If a user has not entered a value for an attribute, then default value specified while creating the table is
used.
• For example, if a teacher's salary has not been entered, then by default the database should store 40000
assuming that the minimum salary given to every teacher is Rs. 40000.
→ Check:
• In order to restrict the values of an attribute within a range, CHECK constraint may be used.
• For example Dept_No of any teacher must not exceed 110. This can be specified as follows: 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,
Date_of_Birth DATE, Dept_No INTEGER CHECK (Dept_No<=110)
);
Self-Referencing Tables-
→ A foreign key constraint can reference columns within the same table. These tables are called as self-
referencing tables.
→ For example, consider a table Employee that contains five columns: Employee_ID, Name, Age, Salary and
Manager_ID. Because the manager is also an employee, there is a foreign key relationship between the
Manager_ID and Employee_ID
SQL-
→ Stands for Structured Query Language.
→ SQL is a language that is used to manage data stored in a RDBMS.
→ It comprises of a Data Definition Language (DDL), Data Manipulation Language (DML) and Transaction
Information Technology Page 6
→ It comprises of a Data Definition Language (DDL), Data Manipulation Language (DML) and Transaction
Control Language (TCL).
Characteristics Of SQL-
→ It requires no cost or payment for its usage.
→ MySQL has superior speed, is easy to use and is reliable.
→ MySQL uses a standard form of the well-known ANSI-SQL standards.
→ MySQL is a platform independent application which works on many operating systems like Windows, UNIX,
LINUX etc. and has compatibility with many languages including JAVA , C++, PHP, PERL, etc.
→ MySQL is an easy to install RDBMS and is capable of handling large data sets.
Data Types-
→ Using a Database
Syntax :
USE <table name> ;
→ Insert Command
Synatx:
INSERT INTO <table name> (<column name>)
VALUES (value 1, value 2, value3, ……….) ;
→ Select Command
Syntax:
SELECT <column name> FROM <table>;
→ Distinct Command
Syntax:
SELECT DISTINCT <column name> FROM <table>;
→ WHERE Clause
Syntax:
SELECT <column name> FROM <table name> WHERE <condition>;
AGGREGATE FUNCTION-
Sometimes it is required to apply certain mathematical functions on group of values in a database. Such
functions are called Aggregate Functions.
→ COUNT- It counts the numbers of tuples in the result of the query.
→ 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.
NOTE-
* *
Command to display all the columns : symbol is used to display all the columns of the table. is the wild
Information Technology Page 9
* *
• Command to display all the columns : symbol is used to display all the columns of the table. is the wild
card character ( means ALL) that is used to display names of all the columns.
• CASCADE is used to remove any entry from both the parent and child tables at the same time.
Create a database of all the teachers working in a school. This database should include the Teacher
relation (schema given below):
Teacher (Teacher_ID, First_Name, Last_Name, Gender, Date_of_Birth, Salary, Dept_No)
To create the above relations in SQL, following CREATE TABLE command is used:
CREATE TABLE Teacher
(
Teacher_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
First name of the Teacher cannot be NULL. Hence NOT NULL constraint can be specified in this case.
Information Technology Page 10
First name of the Teacher cannot be NULL. Hence NOT NULL constraint can be specified in this case.
CREATE TABLE Teacher
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
If a teacher's salary has not been entered, then by default the database should store 40000 assuming
that the minimum salary given to every teacher is Rs.40000.
CREATE TABLE Teacher
(
Teacher_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
Check/Specify Dept_No of any teacher must not exceed 110.
CREATE TABLE Teacher
(
Teacher_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER CHECK (Dept_NO < = 110)
);
If Teacher_ID attribute of the Teacher relation is the PRIMARY KEY then it can be specified as
CREATE TABLE TEACHER
(
Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),