Database Overview
Database Overview
2
Database: What
• Database
– is collection of related data and its metadata organized in a structured format
– for optimized information management
3
Database Management System
- manages interaction between end users and database
4
Database System Environment
Hardware
Software
- OS
- DBMS
- Applications
People
Procedures
Data
5
Database: Why
• Purpose of Database
– Optimizes data management
– Transforms data into information
7
Evolution of Data Models
• Timeline
File-based
Hierarchical
Object-oriented
Network
Relational Web-based
Entity-Relationship
8
Database: Historical Roots
• Manual File System
– to keep track of data
– used tagged file folders in a filing cabinet
– organized according to expected use
• e.g. file per customer
– easy to create, but hard to
• locate data
• aggregate/summarize data
9
File System: Example
10
File System: Weakness
• Weakness
– “Islands of data” in scattered file systems.
• Problems
– Duplication
• same data may be stored in multiple files
– Inconsistency
• same data may be stored by different names in different format
– Rigidity
• requires customized programming to implement any changes
• cannot do ad-hoc queries
• Implications
– Waste of space
– Data inaccuracies
– High overhead of data manipulation and maintenance
11
Database System vs. File System
12
File System: Problem Case
CUSTOMER file AGENT file SALES file
13
Hierarchical Database
• Background
– Developed to manage large amount of data for complex manufacturing
projects
– e.g., Information Management System (IMS)
• IBM-Rockwell joint venture
• clustered related data together
• hierarchically associated data clusters using pointers
14
Hierarchical Database: Example
15
Hierarchical Database: Pros & Cons
• Advantages
– Conceptual simplicity
• groups of data could be related to each other
• related data could be viewed together
– Centralization of data
• reduced redundancy and promoted consistency
• Disadvantages
– Limited representation of data relationships
• did not allow Many-to-Many (M:N) relations
– Complex implementation
• required in-depth knowledge of physical data storage
– Structural Dependence
• data access requires physical storage path
– Lack of Standards
• limited portability
16
Network Database
• Objectives
– Represent more complex data relationships
– Improve database performance
– Impose a database standard
17
Network Database: Example
18
Network Database: Pros & Cons
• Advantages
– More data relationship types
– More efficient and flexible data access
• “network” vs. “tree” path traversal
– Conformance to standards
• enhanced database administration and portability
• Disadvantages
– System complexity
• require familiarity with the internal structure for data access
– Lack of structural independence
• small structural changes require significant program changes
19
Relational Database
• Problems with legacy database systems
– Required excessive effort to maintain
• Data manipulation (programs) too dependent on physical file structure
– Hard to manipulate by end-users
• No capacity for ad-hoc query (must rely on DB programmers).
20
Relational Database: Example
Provides a logical “human-level” view of the data and associations
among groups of data (i.e., tables)
21
Relational Database: Pros & Cons
• Advantages
– Structural independence
• Separation of database design and physical data storage/access
• Easier database design, implementation, management, and use
– Ad hoc query capability with Structured Query Language (SQL)
• SQL translates user queries to codes
• Disadvantages
– Substantial hardware and system software overhead
• more complex system
– Poor design and implementation is made easy
• ease-of-use allows careless use of RDBMS
22
Normalization:
Normalization is a technique which is used to organize the data in the database. It is a systematic
approach to remove the data redundancy.
23
Example
24
Second Normal Form/2nd Normal Form:
1.It is in First normal form
2.There should not be any partial dependency of any column on primary key.
Here We will see that there is composite key as{ Employee No,Department
No}.Employee No is dependent on Employee Name and Department is dependent on
Department No.We can split the above table into 2 different tables:
25
26
Third Normal Form/3rd Normal Form:
The database is in Third normal form if it satisfies following conditions:
Consider a relation R(ABC), where A, B and C are the attributes of the relation R.
A Transitive dependency exists when you have the following functional
dependency pattern,A→B and B→C; therefore, A→C.
27
Book→Author: Here, the Book attribute determines the Author attribute. If you know
the book's name, you can learn the author's name.
28
S511 Session 2, IU-SLIS 29
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987
30
Types of SQL Commands
categories of commands used in SQL to perform various functions
Create - to create a database and its objects like (table, index, views, store procedure)
ALTER - alters the structure of the existing database
DROP - delete objects from the database
TRUNCATE - remove all records from a table
COMMENT - add comments to the data dictionary
31
RENAME - rename an object
Oracle Datatypes
Fixed-length character data of length size bytes or Fixed for every row in the table (with trailing blanks);
Char characters maximum size is 2000 bytes per row
VARCHAR2 Variable-length character data Variable for each row, up to 4000 bytes per row
NVARCHAR2 Variable-length Unicode character data The upper limit is 4000 bytes per row
NCLOB Unicode national character set (NCHAR) data. Up to 232 - 1 bytes, or 4 gigabytes.
NUMBER (p, s) Variable-length numeric dataVariable-length numeric The maximum space required for a
data. Maximum precision p and/or scale s is 38. given column is 21 bytes per row
DATE Fixed-length date and time data Fixed at 7 bytes for each row
INTERVAL YEAR A period of time, represented as years and months. Fixed at 5 bytes.
(precision) TO
MONTH
34
Creating Table With Existing Table
CREATE TABLE EMPLOYEES1( EMPLOYEE_ID INT NOT NULL auto_increment PRIMARY KEY,
LAST_NAME VARCHAR(40),
FIRST_NAME varchar(40),
SALARY decimal(10,2),
DOB DATE)
Insert Into Employees1(first_name,last_name,salary,dob) values('Babjee','Reddy',3000,'2004-
10-20')
35
Create Table with Default value
Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table
37
Not Null Constraint
38
Primary Key
Create Table Employees_PK(
Id int Primary Key,
Employee_Name varchar(40),
Salary decimal
);
Insert into Employees_PK Values(1,'Babjee',30000);
Insert into Employees_PK Values(1,'Babjee',30000);
Insert into Employees_PK Values(null,'Babjee',30000);
39
Create Table Employees_PK2(
Id int,
Employee_Name varchar(40),
Salary decimal ,
constraint emp_k primary key(id,Employee_name)
);
Insert into Employees_PK2 Values(1,'Babjee',30000);Insert
into Employees_PK2 Values(1,'Babjee',30000);
Insert into Employees_PK2 Values(1,'Naveen',30000);
40
Unique Constraint
42
Foreign Key
Create table Supplier(
Supplier_id int Primary key,
Supplier_name varchar(40)
);
Insert Into Supplier value(1,'Abc Company');
Insert Into Supplier value(2,'Bca Company');
INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'), ('polo
shirt','small');
SET
A SET is a string object that can have zero or more values, each of which must be chosen from
a list of permitted values specified when the table is created
INSERT INTO myset (col) VALUES -> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Adding Column
Alter table Employees add pincode int;
Dropping column
Alter table Employees drop column address2
Rename Table
alter table employees1 rename to employee_new1
45
Rename Column
Alter table employees CHANGE city new_city varchar(40);
Adding Constraints
46
Drop Command
Truncate Myset1
47
DML Commands
48
Update command
49
SELECT STATE MENT
select * from emp;
Comarision Operator
51
Logical Operators
select * from emp where (deptno=10 and sal >4000) or (deptno=20 and sal <4000)
Order By class
52
Functions
MySQL has many built-in functions.
Mysql contains string, numeric, date, and some advanced functions in MySQL.
String functions
INSERT Inserts a string within a string at the specified position and for a certain number
of characters
SELECT INSERT("Bigems.com", 1, 6, "Example");
INSTR Returns the position of the first occurrence of a string in another string
select instr('Babjee Reddy ponnam',' ')
REPLACE Replaces all occurrences of a substring within a string, with a new substring
SELECT REPLACE("SQL Tutorial", "SQL", "HTML");
56
Number functions
POW Returns the value of a number raised to the power of another number
select pow(10,2)
58
Date Function
ADDDATE Adds a time/date interval to a date and then returns the date
select ADDDATE("2015-10-01",interval 10 DAY)
SELECT ADDDATE(curdate(),interval 10 DAY)
60
%r Time in 12-hour format hh:mm:ss AM or PM
%S Seconds with leading zero 00,01,…59
%T Time in 24-hour format hh:mm:ss
%U Week number with leading zero when the first day of week is Sunday e.g., 00,01,02…53
%u Week number with leading zero when the first day of week is Monday e.g., 00,01,02…53
61
DATE_SUB Subtracts a time/date interval from a date and then returns the date
SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY);
LAST_DAY Extracts the last day of the month for a given date
select last_day(current_time())
QUARTER Returns the quarter of the year for a given date value
select quarter(now())
64
MySQL Advanced Functions
CASE Goes through conditions and return a value when the first condition is met
select empno,ename,sal,deptno,
case when deptno =10 then "Accounts"
when deptno =20 then "IT"
else "Admin"
end
from emp
65
COALESCE Returns the first non-null value in a list
SELECT COALESCE(NULL, NULL, NULL, 'Biges.com', NULL, 'Example.com');
CURRENT_USER Returns the user name and host name for the MySQL account that the
server used to authenticate the current client
select current_user()
NULLIF Compares two expressions and returns NULL if they are equal. Otherwise, the first
expression is returned
select nullif(ename,'smith') from emp
66
Ifnull
select ifnull(comm,0),comm from emp
SESSION_USER Returns the current MySQL user name and host name
select session_user()
SYSTEM_USER Returns the current MySQL user name and host name
select system_user()
USER Returns the current MySQL user name and host name
select user()
67
GROUP FUNCTIONS
SELECT DEPTNO,
MAX(SAL) AS MAX_SAL,
MIN(SAL) AS MIN_SAL,
AVG(SAL) AS AVG_SAL,
SUM(SAL) AS SUM_SAL,
COUNT(SAL) AS NO_OF_EMP
FROM EMP
GROUP BY DEPTNO
SELECT JOB,DEPTNO,
MAX(SAL) AS MAX_SAL,
MIN(SAL) AS MIN_SAL,
AVG(SAL) AS AVG_SAL,
SUM(SAL) AS SUM_SAL,
COUNT(SAL) AS NO_OF_EMP
FROM EMP
GROUP BY DEPTNO,JOB
68
WITH ROLLOUP
SELECT DEPTNO,
SUM(SAL) AS SUM_SAL
FROM EMP
GROUP BY DEPTNO WITH ROLLUP
SELECT
JOB,DEPTNO,
SUM(SAL) AS SUM_SAL
FROM EMP
GROUP BY DEPTNO,JOB
WITH ROLLUP
SELECT
if(grouping(deptno),"Alldeptno",deptno) as deptno,
if(grouping(job),"All jobs",job) as jobs ,
SUM(SAL) AS SUM_SAL
FROM EMP
GROUP BY DEPTNO,job WITH ROLLUP
69
HAVING CLASS
SELECT
DEPTNO,
MAX(SAL) AS MAX_SAL,
MIN(SAL) AS MIN_SAL,
AVG(SAL) AS AVG_SAL,
SUM(SAL) AS SUM_SAL,
COUNT(SAL) AS NO_OF_EMP
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(SAL) >4
70
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from
the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records
from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
CROSS JOIN
NATURAL JOIN
SELF JOIN
NO EQUIE JOIN 71
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of
the associated tables
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
EMP.DEPTNO,
DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
INNER JOIN
EMP.DEPTNO=DEPT.DEPTNO
72
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
EMP.DEPTNO,
DNAME
FROM EMP
INNER JOIN DEPT
ON EMP.DEPTNO=DEPT.DEPTNO
73
INNER JOIN WITH ALIES
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
E.DEPTNO,
DNAME
FROM EMP E
INNER JOIN DEPT D
ON E.DEPTNO=D.DEPTNO
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
E.DEPTNO,
DNAME
FROM EMP E
74
JOIN DEPT D ON E.DEPTNO=D.DEPTNO
RIGHT OUTER JOIN
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
D.DEPTNO,
DNAME
FROM EMP E
RIGHT JOIN DEPT D
ON E.DEPTNO=D.DEPTNO
75
LEFT OUTER JOIN
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
E.DEPTNO,
DNAME
FROM EMP E
LEFT JOIN DEPT D
ON E.DEPTNO=D.DEPTNO
76
FULL OUTER JOIN
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
E.DEPTNO,
DNAME
FROM EMP E
FULL JOIN DEPT D
ON E.DEPTNO=D.DEPTNO
NATURAL JOIN
SELECT
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO,
DNAME
FROM EMP NATURAL JOIN DEPT
77
SELF JOIN
SELECT
E.EMPNO,
E.ENAME,
E.JOB,
E.MGR,
M.ENAME AS MANAGER
FROM EMP E, EMP MWHERE E.MGR =M.EMPNO
NO EQUI JOIN
SELECT
EMPNO,
ENAME,
SAL,
GRADE
FROM
EMP E, SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL
SELECT
DNAME,
MAX(SAL) AS MAX_SAL,
MIN(SAL) AS MIN_SAL,
AVG(SAL) AS AVG_SAL,
SUM(SAL) AS SUM_SAL,
COUNT(SAL) AS NO_OF_EMP
FROM EMP ,DEPT
WHERE EMP.DEPTNO =DEPT.DEPTNO
GROUP BY DNAME;
78
SET OPERATORS
79
SUB QUERY
SELECT * FROM EMP WHERE SAL > (SELECT SAL FROM EMP WHERE ENAME='SMITH')
SELECT * FROM DEPT WHERE DEPTNO NOT IN (SELECT DEPTNO FROM EMP)
SELECT DEPTNO, AVG(SAL) AS AVG_SAL FROM EMP GROUP BY DEPTNO HAVING AVG(SAL)
>= ALL(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO)
SELECT DEPTNO, AVG(SAL) AS AVG_SAL FROM EMP GROUP BY DEPTNO HAVING AVG(SAL)
>= ANY(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO)
80
SELECT * FROM EMP WHERE EXISTS (SELECT DEPTNO FROM DEPT WHERE DEPTNO =EMP.DEPTNO)
SELECT * FROM DEPT WHERE NOT EXISTS (SELECT DEPTNO FROM EMP WHERE DEPTNO =DEPT.DEPTNO)
SELECT EMPNO, Sal FROM Emp e WHERE 2=(SELECT COUNT(DISTINCT Sal) FROM Emp p WHERE e.SaL<=p.Sal)
VIEWS
81
create view emp_view1
as select
empno,
ename,
job,
mgr,
hiredate,
sal
from emp;
82
create view emp_view3
as select
empno as employee_no,
ename as employee_name,
job employee_job,
mgr as manager,
hiredate,
sal
from emp ;
min(sal) as min_sal,
avg(sal) as avg_sal,
sum(sal) as sum_sal,
count(sal) as employee_count
from emp,dept
where emp.deptno =dept.deptno;
83
Commit/rollback
SET autocommit=0;
START TRANSACTION;
insert into dept values (60,'prod','Chennai');
select * from dept;rollback ;
Savepoint
START TRANSACTION;
savepoint a;
insert into dept values (60,'prod','Chennai');
savepoint b;
update dept set dname ='abc' where deptno=10;
select * from dept;
rollback to savepoint b;
84
GRANT AND REVOKE
85
Procedures
delimiter //
86
Stored procedure with input and output parameter
87
Inout parameter
SET @counter = 1;
CALL set_counter(@counter,1);
CALL set_counter(@counter,1);
CALL set_counter(@counter,5);
SELECT @counter;
88
Procedure with if condition
delimiter $$
CREATE PROCEDURE
updateempsal(in p_empno int(11), out p_saltype varchar(20))
BEGIN
DECLARE salary double;
SELECT sal INTO salary FROM emp WHERE empno = p_empno;
IF salary > 4000 THEN
SET p_saltype = 'Good';
ELSEIF salary > 3000 THEN
SET p_saltype = 'Fair';
else
SET p_saltype = 'Poor';
END IF;
END$$
89
Case statement
delimiter $$
CREATE PROCEDURE
empcase( in p_empno int(11), out p_saltype varchar(20))
BEGIN
DECLARE salary double;
SELECT sal INTO salary FROM emp WHERE empno = p_empno;
90
While loop
DELIMITER $$
CREATE PROCEDURE test_loop()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = 1;
SET str = '';
WHILE x <= 5 DO
SET str = CONCAT(str,x,',');
SET x = x + 1;
END WHILE;
SELECT str;
END$$
DELIMITER ;
91
FUNCTIONS
DELIMITER $$
CREATE FUNCTION
EMPFUN(p_creditLimit double) RETURNS VARCHAR(10) DETERMINISTIC
BEGIN
DECLARE lvl varchar(10);
IF p_creditLimit > 4000 THEN
SET lvl = 'Good';
ELSEIF p_creditLimit > 3000 THEN
SET lvl = 'FAIR';
else
SET lvl = 'POOR';
END IF;
RETURN (lvl);
END $$
delimiter ;
92
93
94
95
96
97
98
99
100
101