0% found this document useful (0 votes)
6 views

Database Concepts Final

Uploaded by

railavina23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Database Concepts Final

Uploaded by

railavina23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Database Concepts

Monday, May 13, 2024 7:33 PM

 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.

Information Technology Page 2


 Characteristics of DBMS :-
→ Self-describing Nature of a Database System: DBMS contains not only the database but also the
description of the data that it stores. This description of data is called meta-data. Meta-data is stored in a
database catalogue or data dictionary. It contains the structure of the data and also the constraints that
are imposed on the data.
→ Insulation Between Programs and Data: Since the definition of data is stored separately in a DBMS, any
change in the structure of data would be done in the catalogue and hence programs which access this data
need not be modified. This property is called Program-Data Independence.
→ Sharing of Data: A multiuser environment allows multiple users to access the database simultaneously.
Thus a DBMS must include concurrency/ coexistence control software to allow simultaneous access of data
in the database without any inconsistency problems.

 Types of DBMS Users :-


→ End Users: Users who use the database for querying, modifying and generating reports as per their needs.
They are not concerned about the working and designing of the database. They simply use the DBMS to get
their task done.
→ Database Administrator (DBA): As the name implies, the DBA administers the database and the DBMS. The
DBA is responsible for authoring access, monitoring its use, providing technical support, acquiring
software and hardware resources.
→ Application Programmers: Application programmers write application programs to interact with the
database. These programs are written in high level languages and SQL to interact with the database.
→ System Analyst: System analyst determines the requirements of the end users and then develops
specifications to meet these requirements. A system analyst plays a major role in the database design and
all the technical, economic and feasibility aspects.

 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.

Information Technology Page 3


→ The number of rows in a relation is called the Cardinality of a relation.
→ Relation Schema defines the design and structure of the relation like it consists of relation name, set of
attributes/column names.
→ Relation State represents a relation with all the tuples at any particular point of time.

Eg;

• EMPLOYEE table is a relation.


• There are three tuples in EMPLOYEE relation.
• Name, Employee_ID, Gender, Salary, Date_of_Birth are attributes.
• The domain is a set of atomic (or indivisible) values. 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:
(a) Name – Set of character strings representing names of persons.
(b) Employee_ID–Set of 4-digit numbers
(c) Gender – male or female
(d) Salary – Number
(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.
• Relation Schema – EMPLOYEE (Name, Employee_ID, Gender, Salary, Date_of_Birth)
• Relation State –

 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.

 Categories of SQL Commands-


→ Data Definition Language (DDL) Commands
• The DDL part of SQL permits database tables to be created or deleted.
• It also defines indices (keys), specifies links between tables, and imposes constraints on tables.
• Examples of DDL commands in SQL are:
CREATE DATABASE - creates a new database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
→ Data Manipulation Language (DML) Commands
• The query and update commands form the DML part of SQL
• Examples of DDL commands are:
SELECT - extracts data from a table
UPDATE - updates data in a table
DELETE - deletes data from a table
INSERT INTO - inserts new data into a table
→ Transaction Control Language (TCL)
• It deals with transactions within a database and also maintain consistency of the database.
GRANT - it gives users access privileges to the database
REVOKE - it is used to remove granted permissions

 Data Types-

Information Technology Page 7


Data Types-

 Types of SQL Commands-

→ CREATE DATABASE command


Syntax :
CREATE DATABASE <table name> ;

→ Using a Database
Syntax :
USE <table name> ;

→ CREATE TABLE command


This command is used to create a new table or relation.
Syntax:
CREATE TABLE <table name>
(
<column1><data type> [constraint] ,
<column2><data type> [constraint],
<column3><data type> [constraint]
);

→ Viewing tables in a database


Syntax :
SHOW TABLES;

→ Viewing the structure of the table


Syntax:
DESCRIBE <table name>; OR
DESC <table name>;

Information Technology Page 8


→ Modifying the structure of the table
Syntax:
ALTER TABLE <table name> ADD/DROP <column name><data type> ;

→ Deleting the structure of the table


Syntax:
DROP TABLE <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.

SQL SYNTAX FOR DIFFERENT CONDITIONS:

 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),

Information Technology Page 11


Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
 The primary key of the TEACHER relation comprises of Teacher_ID and Date_of_Birth.
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,
PRIMARY KEY (Teacher_ID, Date_of_Birth)
);
 To insert a tuple in the Teacher table
INSERT INTO Teacher (First_Name, Last_Name, Gender, Teacher_ID, Date_of_Birth, Dept_No, Salary)
" "" " ' '
VALUES ( Shanaya , Batra , 'F', 101, 1984-08-11 , 1, 50000);
 In the Teacher table, we want to update the Salary of teacher with Teacher_ID=101 to 55000.
UPDATE Teacher
SET Salary=55000
WHERE Teacher_ID=101;
 Delete the tuple for Teacher with ID=101
DELETE FROM Teacher
WHERE Teacher_ID=101;

Information Technology Page 12


 Retrieve all the information about Teacher with ID=101


Syntax:
SELECT*
FROM Teacher
WHERE Teacher_ID=101;
Result:

 To find the names of all teachers earning more than 50000.


Syntax:
SELECT First_Name,Last_Name
FROM Teacher
WHERE salary > 50000;
Result:

 To display Teacher_ID,First_Name,Last_Name and Dept_No of teachers who belongs to department number


4 or 7.
Syntax:
SELECT Teacher_ID,First_Name,Last_Name, Dept_No
FROM Teacher
WHERE Dept_No = 4 OR Dept_No = 7;
Result :

Information Technology Page 13


Result :

Information Technology Page 14

You might also like