Unit 3 Database Management
Unit 3 Database Management
Unit 3 Database Management
DATABASE MANAGEMENT
Database Concepts
A database (DB) is an organized collection of interrelated data
stored together to serve multiple application
DBMS
1
Oracle, MS SQL Server, MySQL, Sybase, PostgreSQL, SQLite
Data models define how data is connected to each other and how
they are processed and stored inside the system.
The basic structure of a relational model is tables. So, the tables are
also called relations in the relational model. Example: In this
example, we have a Department Table.
2
DEPTNO DNAME LOCATION
10 HR NEW YORK
20 ACCOUNTS BRAZIL
30 SALES CANADA
40 IT INDIA
Concept of Domain
Relation/Table
In the Relational model the, relations are saved in the table format.
It is stored along with its entities. A table has two properties rows
and columns. Rows represent records and columns represent
attributes.
Tuple
Attribute
Degree
3
The total number of attributes in the relation is called the degree
of the relation/table.
Cardinality
Cardinality.
Keys
Primary Key
Candidate Key
Alternate Key
5
Foreign Key
EMPLOYEE TABLE
EMPNO ENAME GENDER DEPTNO SALARY COMM
DEPARTMENT TABLE
10 HR NEW YORK
20 ACCOUNTS BRAZIL
30 SALES CANADA
40 IT INDIA
From the Above table definition we can observe that the DEPTNO column
of EMPLOYEE table is deriving its values from DEPTNO of table
DEPARTMENT. So we can say that the DEPTNO of EMPLOYEE table is a
foreign key whose value is dependent upon the Primary key column
DEPTNO of table DEPARTMENT
6
Quick Revision
Table- Customer
Table- Transaction
7
Structured Query Language (SQL)
SQL Features
Allows creating/modifying a database’s structure
Querying database
8
Advantages of using SQL
Ease of use
No coding required
Portable
Not case sensitive
Reliable
Freedom of data abstraction
Standard for database connectivity
My SQL
MySQL is freely available open source RDBMS
Can be downloaded from www.mysql.org
In MySQL information is stored in Tables.
Provides features that support secure environment for storing,
maintaining and accessing data.
It is fast, reliable, scalable alternative to many of the commercial
RDBMS today.
Create and supported by MySQL AB, a company based in Sweden.
This company is now subsidiary of Sun Microsystems. On April 2009
Oracle Corp acquires Sun Microsystems.
The chief inventor of MySQL was Michael Widenius (a.k.a Monty).
MySQL has been named after Monty’s daughter My. The logo of
MySQL is dolphin and name of that dolphin is ‘Sakila’
9
Data Definition Language(DDL )
DDL is abbreviation of Data Definition Language.
It deals with the structure of the database.
It is used to create, remove and modify the structure of database
objects in database. It provides following commands
CREATE – Creates objects in the database
ALTER – Alters objects of the database
DROP – Deletes objects of the database
10
Numeric Data Types
Data type Description
INT Numbers without decimal. Store up to 11 digits. -2147483648 to
/NUMBER 2147483647
DECIMAL It is used to store exact numeric value that preserves exact precision
for e.g. money data in accounting system.
DECIMAL(P,D) means P no. of significant digits (1-65), D represent no.
of digit after decimal(0-30), for e.g DECIMAL(6,2) means 4 digit
before decimal and 2 digit after decimal. Max will be 9999.99
YEAR(M) To store only year part of data where M may be 2 or 4 i.e. year in 2
digit like 18 or 4 digit like 2018
String Types
Data type Description
CHAR(M) Fixed length string between 1 and 255. it always occupy M size for
each data for example if size is CHAR(20) and we store value
‘MOBILE’ , although the size of MOBILE is 6 but in a table it will
occupy 20 size with space padded at right side for remaining place.
Mostly use in the case where the data to be insert is of fixed size
like Grade (A,B,C,..) or Employee code as E001, E002, etc. In this
case CHAR will give better performance than varchar
VARCHAR(M) Variable length string between 1 and 65535 (from MySQL 5.0.3) ,
earlier it was 255. It takes size as per the data entered for
example with VARCHAR (20) if the data entered is MOBILE then it
will take only 6 byte. It is useful for the data like name, address
11
where the number of character to be entered is not fixed.
Quick Revision
Table- Library
Bid Name Author Price Mem_name Issue_Date Status
B01 Wings of Fire A.P.J 550 Sarita 2018-05-20 Returned
Abdul
Kalam
Look at the table library and identify the correct datatype for
all the columns
One Computer Shopkeeper wants to create table to store records
related to the material in his shop. Help him in identifying the
columns with their datatypes.
12
SQL Commands
DDL Commands
TO CREATE A TABLE
CREATE TABLE is used to create a table in a database.
Syntax:
Create Table TableName
(ColumnName1 datatype(size) constraint,
13
ColumnName2 datatype(size) constraint,…..);
Example:
Create Table Employee
(empno int primary key,
name varchar(20) not null,
dept varchar(20) unique,
salary int check(salary>=5000));
Different Constraints:
Primary Key
It ensures unique value in any column, also forces data
entry mandatory. Only one primary key can be applied
in one table
Unique
It also allows unique value in any column but it allows
NULL values and can be applied to n times
Not Null
It will make data entry mandatory for applied column
i.e. NULL will not be allowed
Default
It allows to specify any value which will be auto-
matically inserted in applied column if we not specify
applied column at the time of data entry using INSERT
CHECK
14
It allows to specify range of values that can be
entered in applied column like salary must be greater
than 5000; marks must be greater than 0 or dept must
be in given list of values etc.
TO MODIFY A TABLE
ALTER TABLE command is used to modify the
structure of the table. It performs the following
operations:
a) Adding new column in existing table
b) Dropping existing column from table
c) Modifying column definition in table
a) Adding new column in existing table
Syntax:
Alter Table TableName add
( ColumnName1 datatype(size) constraint);
Example:
Alter Table Employee add
( designation varchar(15) not null);
b) Dropping existing column from table
Syntax:
Alter Table TableName drop ColumnName;
Example:
Alter Table Employee drop salary;
15
c) Modifying column definition in table
Syntax:
Alter Table TableName
modify (ColumnName datatype(size) ) ;
Example:
Alter Table Employee modify (designation varchar(20));
16
Quick Revision
Table- Library
Bid Name Author Price Mem_name Issue_Date Status
B01 Wings of Fire A.P.J 550 Sarita 2018-05-20 Returned
Abdul
Kalam
Write the commands to perform following operations:
Create the above table, set Bid as Primary key.
Add a new column to store no of pages
Modify the range of Author, increase it by 5 characters.
Remove a column named
Remove the column named price.
17
SQL Operators
Relational Operators
SQL is providing various relational operators to apply
conditions in different commands. Such as
<, >, <= ,>=, <> (not equal to), =
Example :
salary>2000
dept =’Sales’
rollno =121 etc.
Logical Operators
SQL is also providing various logical operators to merge
different relational expressions to apply conditions in
different commands. Such as
AND, OR, NOT
Example :
salary>2000 and salary< 50000
dept =’Sales’ or designation=’manager’ etc.
DML Commands
b) In all columns.
Syntax:
Insert Into table_name VALUES(value1, value2, value3, ..);
Example :
Insert into Employee values (2,’dipanker’,’IT’,20000);
Quick Revision
Table: PharmaDB
RxID DrugID DrugName Price PharmacyName Location
R1000 5476 Amlodipine 100.00 RxPharmacy Pitampura
R1001 2345 Paracetamol 15.00 RajMedicos Bahadurgarh
R1002 1236 Nebistar 60.00 MyChemist RajGarden
R1003 6512 VitaPlus 150.00 MyChemist Gurgaon
R1004 5631 Levocitrezine 110.00 RxPharmacy SouthExtension
21
22