M4 SQL 2019
M4 SQL 2019
SQL evolution
§ SQL- 86/89
§ SQL- 92 - SQL2
§ SQL- 99/03 - SQL3
(includes object relational features)
And the evolution continues .....
§ Authorization
Specifies how to restrict a user / set of users to access only
certain parts of data, perform only certain types of queries etc.
UNIQUE (B1,B2,…,Bk)
specifies that {B1,B2,…,Bk} is a candidate key for the table
There can be more than one UNIQUE constraint but only one
PRIMARY KEY constraint for a table.
Using subqueries
§ makes the main query easy to understand / formulate
§ sometimes makes it more efficient also
• sub query result can be computed once and
used many times.
• not the case with all subqueries.
§ IN is equivalent to = ANY
NOT IN is equivalent to
< > ALL
As SQL does not have universal quantifier, we will rewrite the query this way:
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS2300’ and
sem = ‘odd’ and year = ‘2019’ ) UNION
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS2320’ and
sem = ‘odd’ and year = ‘2019’ );
Equivalent to:
(SELECT rollNo
FROM enrollment
WHERE (courseId = ‘CS2300’ or courseID = ‘CS2320’)
and sem = ‘odd’ and year = ‘2019’ )
Prof P Sreenivasa Kumar 34
Department of CS&E, IITM
Example using INTERSECTION
Obtain the roll numbers of students who are currently
enrolled for both CS230 and CS232 Courses.
(select rollNo
from enrollment
where courseId = ‘CS2300’ and
sem = ‘odd’ and year = ‘2019’ )
INTERSECT
(select rollNo
from enrollment
where courseId = ‘CS2320’ and
sem = ‘odd’ and year = ‘2019’;
(SELECT rollNo
FROM enrollment
WHERE sem = ‘odd’ and year = ‘2019’)
EXCEPT
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS2300’ and
sem = ‘odd’ and year = ‘2019’);
Data analysis
§ to get info on summary and trends in certain attributes
§ need for computing aggregate values for data
§ total value, average value etc
§ SUM ( [DISTINCT]A):
computes the sum of (distinct) values in column A
§ COUNT ( [DISTINCT]A):
computes the number of (distinct) values in column A or no.
of tuples in result
Select f.name
from professor as f, department as d
where f.deptNo = d.deptId and
d.name = ‘CSE’;
Join types:
1. inner join (default):
from (r1 inner join r2 on <predicate>)
use of just ‘join’ is equivalent to ‘inner join’
2. left outer join:
from (r1 left outer join r2 on <predicate>)
3. right outer join:
from (r1 right outer join r2 on <predicate>)
4. full outer join:
from (r1 full outer join r2 on <predicate>)
Prof P Sreenivasa Kumar 49
Department of CS&E, IITM
Natural join
The adjective ‘natural’ can be used with any of the join types to
specify natural join.
REMARKS
• Specifying join operation explicitly goes against the spirit of
declarative style of query specification
• But the queries may be easier to understand
• The feature is to be used judiciously
If the details of a new CSE professor are entered into professor table,
the above view gets updated automatically
Prof P Sreenivasa Kumar 52
Department of CS&E, IITM
Queries on Views
Once created a view can be used in queries just like any other
table.
select name
from profAft2K
where name like ‘Ram%’;
§ Querying is allowed
update professors
set phone = ‘9444422605’
where deptNo = (select deptId
from department
where name = ‘CSE’);
§ Use of ‘null’ to test for a null value, if the attribute can take null
e.g., Obtain roll numbers of students who
don’t have phone numbers
select rollNo
from student
where phoneNumber is null;
select name
from professor
where startYear between 1980 and 1990;
Three Tier System Architectures – also possible – details left out here
Data transfer –
takes place through specially declared HL variables
SQL Commands
CONNECT TO <serverName> AS <connName>
AUTHORIZATION <uName, passWd>
DISCONNECT <connName>
Prof P Sreenivasa Kumar 72
Department of CS&E, IITM
Embedded SQL Statements – An example
Suppose we collect data through user interface into variables
rollNo, studName, degree, year, sex, deptNo, advisor
DB Language Approach
+ No impedance mismatch
- Programmers need to learn a new language; apps not portable
Prof P Sreenivasa Kumar 80
Department of CS&E, IITM