SlideShare a Scribd company logo
1 of 17
Relational Database Management System
Nadar Saraswathi College of Arts & Science
Submitted by
N. Nagapandiyammal
M. Sc (CS)
Content
• Basic Structure
• Set Operations
• Complex Queries
• Joined Relations
• Data-Definition Language(DDL)
Basic Structure
 SQL is based on set and relational operations with certain
modifications and enhancements
 A typical SQL query has the form
select A1,A2, ..., An
From r1,r2, ..., rm
Where P
 A is represent attributes
 r is represent relations
 P is a predicate
 This query is equivalent to the relational algebra expression:
ΠA1,A2, ..., An(P(r1 x r2 x...x rm))
 The result of an SQL query is a relation.
Set Operations
 The set operations union, intersect, and except operate on
relations and correspond to the relational algebra operations
[,, and −.
 Each of the above operations automatically eliminates
duplicates; to retain all duplicates use the corresponding
multiset versions union all, intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it
occurs:
m + n times in r union all s
min(m, n) times in r intersect all s
max(0,m−n) times in r except all s
 Find all customers who have a loan, an account, or both:
(Select customer-name from depositor)
union
(select customer-name from borrower)
 Find all customers who have both a loan and an account
(select customer- name from depositor)
intersect
(select customer-name from borrower)
 Find all customers who have an account but no loan.
(select customer-name from depositor)
except
(select customer-name from borrower)
Complex Queries
• Complex queries are often hard or impossible to write as a
single SQL block or a union / intersection / difference of
SQL blocks.
• Following are some very important Complex SQL Queries
Examples with answers. I have tried to explain each and
every query in detail so that everyone will get idea of how it
is executed step-by-step. Following are some Complex SQL
Queries Examples with answers in detail.
1.Query to find Second Highest Salary of Employee?
Answer:
Select distinct Salary from Employee e1
where 2=Select count(distinct Salary) from
Employee e2 where e1.salary<=e2.salary;
2.Query to find duplicate rows in table?
Answer:
Select * from Employee a where rowid <>( select
max(rowid) from Employee b where
a.Employee_num=b.Employee_num);
Joined Relations
o Join operations take two relations and return as a result another
relation.
o These additional operations are typically used as sub query
expressions in the from clause.
o Join condition – defines which tuple in the two relations match, and
what attributes are present in the result of the join.
o Join type – defines how tuple in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
Join Types
inner join
left outer join
right outer join
full outer join
Join Conditions
natural
on <predicate>
using (A1, A2, . . .,
An)
Joined Relations – Datasets for Examples
 Relation loan
 Relation borrower
branch-name loan-number amount
Downtown L-170 3000
Redwood L-230 4000
Perry ridge L-260 1700
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-260
Joined Relations – Examples
 loan inner join borrower on
loan. loan-number =borrower. loan-number
 loan left outer join borrower on
loan. loan-number=borrower. loan-number
 loan natural inner join borrower
 loan natural right outer join borrower
 loan full outer join borrower using (loan-number)
Find all customers who have either an account or a loan (but
not both) at the bank.
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null
Data Definition Language (DDL)
Allows the specification of not only a set of relations but
also information about each relation, including:
 The schema for each relation.
 The domain of values associated with each attribute.
 Integrity constraints.
 The set of indices to be maintained for each relation.
 Security and authorization information for each relation.
 The physical storage structure of each relation on disk.
Domain Types in SQL
 char(n). Fixed length character string, with user-
specified length n.
 varchar(n). Variable length character strings, with user-
specified maximum length n.
 int. Integer (a finite subset of the integers that is
machine-dependent).
 smallint. Small integer (a machine-dependent subset of
the integer domain type).
 numeric( p, d). Fixed point number, with user-specified
precision of p digits, with n digits to the right of decimal
point.
Domain Types in SQL (Cont.)
• real, double precision. Floating point and double-
precision floating point numbers, with machine-dependent
precision.
• float(n). Floating point number, with user-specified
precision of at least n digits.
• date. Dates, containing a (4 digit) year, month and date.
• time. Time of day, in hours, minutes and seconds.
– Null values are allowed in all the domain types. Declaring
an attribute to be not null prohibits null values for that
attribute.
– create domain construct in SQL-92 creates user-defined
domain types
create domain person-name char(20) not null
Create Table Construct
• An SQL relation is defined using the create table
command:
create table r (A1 D1, A2 D2, . . . , An Dn,
<integrity-constraint l >,
. . .,
<integrity-constraint>)
– r is the name of the relation
– each Ai is an attribute name in the schema of relation r
– Di is the data type of values in the domain of attribute Ai
Integrity Constraints In Create Table
 not null
 primary key (A1, . . ., An)
 check (P), where P is a predicate
Example: Declare branch-name as the primary key for
branch and ensure that the values of assets are non-negative.
create table branch
(branch-name char(15) not null,
branch-city char(30),
assets integer,
primary key (branch-name),
check (assets >= 0))
 primary key declaration on an attribute automatically
ensures not null in SQL-92
Drop and Alter Table Constructs
 The drop table command deletes all information about
the dropped relation from the database.
 The alter table command is used to add attributes to an
existing relation. All tuples in the relation are assigned null
as the value for the new attribute. The form of the alter
table command is
 alter table r add A D
 where A is the name of the attribute be added to relation r
and D is the domain of A.
 The alter table command can also be used to drop
attributes of a relation
 alter table r drop A
 where A is the name of an attribute of relation r.
The End

More Related Content

What's hot

FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLEVraj Patel
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational modelATS SBGI MIRAJ
 
Sv data types and sv interface usage in uvm
Sv data types and sv interface usage in uvmSv data types and sv interface usage in uvm
Sv data types and sv interface usage in uvmHARINATH REDDY
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Eddyzulham Mahluzydde
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]Bashir Rezaie
 
Relational database intro for marketers
Relational database intro for marketersRelational database intro for marketers
Relational database intro for marketersSteve Finlay
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries InformationNishant Munjal
 
Database Assignment
Database AssignmentDatabase Assignment
Database AssignmentJayed Imran
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data ModelSmriti Jain
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints4 the relational data model and relational database constraints
4 the relational data model and relational database constraintsKumar
 
Relational Model - An Introduction
Relational Model - An IntroductionRelational Model - An Introduction
Relational Model - An IntroductionRajeev Srivastava
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATELShashi Patel
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...Mustafa Kamel Mohammadi
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Referential integrity
Referential integrityReferential integrity
Referential integrityJubin Raju
 

What's hot (20)

FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
Sv data types and sv interface usage in uvm
Sv data types and sv interface usage in uvmSv data types and sv interface usage in uvm
Sv data types and sv interface usage in uvm
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2
 
Normalisation
NormalisationNormalisation
Normalisation
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]
 
Relational database intro for marketers
Relational database intro for marketersRelational database intro for marketers
Relational database intro for marketers
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
PPT
PPTPPT
PPT
 
Database Assignment
Database AssignmentDatabase Assignment
Database Assignment
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
4 the relational data model and relational database constraints
4 the relational data model and relational database constraints4 the relational data model and relational database constraints
4 the relational data model and relational database constraints
 
Relational Model - An Introduction
Relational Model - An IntroductionRelational Model - An Introduction
Relational Model - An Introduction
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Referential integrity
Referential integrityReferential integrity
Referential integrity
 
Expression tree
Expression treeExpression tree
Expression tree
 
Dbms ER Model
Dbms ER ModelDbms ER Model
Dbms ER Model
 

Similar to RDBMS

Similar to RDBMS (20)

Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptx
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
3.ppt
3.ppt3.ppt
3.ppt
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
ch3
ch3ch3
ch3
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Ch2
Ch2Ch2
Ch2
 
DBMS Unit-2.pdf
DBMS Unit-2.pdfDBMS Unit-2.pdf
DBMS Unit-2.pdf
 
6 integrity and security
6 integrity and security6 integrity and security
6 integrity and security
 
Ch4
Ch4Ch4
Ch4
 
Relational model
Relational modelRelational model
Relational model
 
relational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptrelational model in Database Management.ppt.ppt
relational model in Database Management.ppt.ppt
 
SQL PPT.ppt
SQL PPT.pptSQL PPT.ppt
SQL PPT.ppt
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
 
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
 

More from NilaNila16

Basic Block Scheduling
Basic Block SchedulingBasic Block Scheduling
Basic Block SchedulingNilaNila16
 
Affine Array Indexes
Affine Array IndexesAffine Array Indexes
Affine Array IndexesNilaNila16
 
Software Engineering
Software EngineeringSoftware Engineering
Software EngineeringNilaNila16
 
Web Programming
Web ProgrammingWeb Programming
Web ProgrammingNilaNila16
 
MapReduce Paradigm
MapReduce ParadigmMapReduce Paradigm
MapReduce ParadigmNilaNila16
 
Hadoop Distributed File System
Hadoop Distributed File SystemHadoop Distributed File System
Hadoop Distributed File SystemNilaNila16
 
Operating system
Operating systemOperating system
Operating systemNilaNila16
 
Linear Block Codes
Linear Block CodesLinear Block Codes
Linear Block CodesNilaNila16
 
Applications of graph theory
                      Applications of graph theory                      Applications of graph theory
Applications of graph theoryNilaNila16
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence RelationNilaNila16
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 

More from NilaNila16 (14)

Basic Block Scheduling
Basic Block SchedulingBasic Block Scheduling
Basic Block Scheduling
 
Affine Array Indexes
Affine Array IndexesAffine Array Indexes
Affine Array Indexes
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
 
MapReduce Paradigm
MapReduce ParadigmMapReduce Paradigm
MapReduce Paradigm
 
Hadoop Distributed File System
Hadoop Distributed File SystemHadoop Distributed File System
Hadoop Distributed File System
 
Data Mining
Data MiningData Mining
Data Mining
 
Operating system
Operating systemOperating system
Operating system
 
Linear Block Codes
Linear Block CodesLinear Block Codes
Linear Block Codes
 
Applications of graph theory
                      Applications of graph theory                      Applications of graph theory
Applications of graph theory
 
Hasse Diagram
Hasse DiagramHasse Diagram
Hasse Diagram
 
Fuzzy set
Fuzzy set Fuzzy set
Fuzzy set
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

RDBMS

  • 1. Relational Database Management System Nadar Saraswathi College of Arts & Science Submitted by N. Nagapandiyammal M. Sc (CS)
  • 2. Content • Basic Structure • Set Operations • Complex Queries • Joined Relations • Data-Definition Language(DDL)
  • 3. Basic Structure  SQL is based on set and relational operations with certain modifications and enhancements  A typical SQL query has the form select A1,A2, ..., An From r1,r2, ..., rm Where P  A is represent attributes  r is represent relations  P is a predicate  This query is equivalent to the relational algebra expression: ΠA1,A2, ..., An(P(r1 x r2 x...x rm))  The result of an SQL query is a relation.
  • 4. Set Operations  The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations [,, and −.  Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m, n) times in r intersect all s max(0,m−n) times in r except all s
  • 5.  Find all customers who have a loan, an account, or both: (Select customer-name from depositor) union (select customer-name from borrower)  Find all customers who have both a loan and an account (select customer- name from depositor) intersect (select customer-name from borrower)  Find all customers who have an account but no loan. (select customer-name from depositor) except (select customer-name from borrower)
  • 6. Complex Queries • Complex queries are often hard or impossible to write as a single SQL block or a union / intersection / difference of SQL blocks. • Following are some very important Complex SQL Queries Examples with answers. I have tried to explain each and every query in detail so that everyone will get idea of how it is executed step-by-step. Following are some Complex SQL Queries Examples with answers in detail.
  • 7. 1.Query to find Second Highest Salary of Employee? Answer: Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from Employee e2 where e1.salary<=e2.salary; 2.Query to find duplicate rows in table? Answer: Select * from Employee a where rowid <>( select max(rowid) from Employee b where a.Employee_num=b.Employee_num);
  • 8. Joined Relations o Join operations take two relations and return as a result another relation. o These additional operations are typically used as sub query expressions in the from clause. o Join condition – defines which tuple in the two relations match, and what attributes are present in the result of the join. o Join type – defines how tuple in each relation that do not match any tuple in the other relation (based on the join condition) are treated. Join Types inner join left outer join right outer join full outer join Join Conditions natural on <predicate> using (A1, A2, . . ., An)
  • 9. Joined Relations – Datasets for Examples  Relation loan  Relation borrower branch-name loan-number amount Downtown L-170 3000 Redwood L-230 4000 Perry ridge L-260 1700 customer-name loan-number Jones L-170 Smith L-230 Hayes L-260
  • 10. Joined Relations – Examples  loan inner join borrower on loan. loan-number =borrower. loan-number  loan left outer join borrower on loan. loan-number=borrower. loan-number  loan natural inner join borrower  loan natural right outer join borrower  loan full outer join borrower using (loan-number) Find all customers who have either an account or a loan (but not both) at the bank. select customer-name from (depositor natural full outer join borrower) where account-number is null or loan-number is null
  • 11. Data Definition Language (DDL) Allows the specification of not only a set of relations but also information about each relation, including:  The schema for each relation.  The domain of values associated with each attribute.  Integrity constraints.  The set of indices to be maintained for each relation.  Security and authorization information for each relation.  The physical storage structure of each relation on disk.
  • 12. Domain Types in SQL  char(n). Fixed length character string, with user- specified length n.  varchar(n). Variable length character strings, with user- specified maximum length n.  int. Integer (a finite subset of the integers that is machine-dependent).  smallint. Small integer (a machine-dependent subset of the integer domain type).  numeric( p, d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point.
  • 13. Domain Types in SQL (Cont.) • real, double precision. Floating point and double- precision floating point numbers, with machine-dependent precision. • float(n). Floating point number, with user-specified precision of at least n digits. • date. Dates, containing a (4 digit) year, month and date. • time. Time of day, in hours, minutes and seconds. – Null values are allowed in all the domain types. Declaring an attribute to be not null prohibits null values for that attribute. – create domain construct in SQL-92 creates user-defined domain types create domain person-name char(20) not null
  • 14. Create Table Construct • An SQL relation is defined using the create table command: create table r (A1 D1, A2 D2, . . . , An Dn, <integrity-constraint l >, . . ., <integrity-constraint>) – r is the name of the relation – each Ai is an attribute name in the schema of relation r – Di is the data type of values in the domain of attribute Ai
  • 15. Integrity Constraints In Create Table  not null  primary key (A1, . . ., An)  check (P), where P is a predicate Example: Declare branch-name as the primary key for branch and ensure that the values of assets are non-negative. create table branch (branch-name char(15) not null, branch-city char(30), assets integer, primary key (branch-name), check (assets >= 0))  primary key declaration on an attribute automatically ensures not null in SQL-92
  • 16. Drop and Alter Table Constructs  The drop table command deletes all information about the dropped relation from the database.  The alter table command is used to add attributes to an existing relation. All tuples in the relation are assigned null as the value for the new attribute. The form of the alter table command is  alter table r add A D  where A is the name of the attribute be added to relation r and D is the domain of A.  The alter table command can also be used to drop attributes of a relation  alter table r drop A  where A is the name of an attribute of relation r.