0% found this document useful (0 votes)
48 views29 pages

Lec 2.3

Uploaded by

obivmous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views29 pages

Lec 2.3

Uploaded by

obivmous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Module 08

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Outline
Module 08: Introduction to SQL/1
History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table
Partha Pratim Das
Data
Manipulation Department of Computer Science and Engineering
Language (DML):
Query Structure
Indian Institute of Technology, Kharagpur
Select Clause
Where Clause
[email protected]
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.1


Module Recap PPD

Module 08

Partha Pratim • Introduced relational algebra


Das
• Familiarized with the operators of relational algebra
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.2


Module Objectives PPD

Module 08

Partha Pratim • To understand relational query language


Das
• To understand data definition and basic query structure
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.3


Module Outline PPD

Module 08

Partha Pratim • History of SQL


Das
• Data Definition Language (DDL)
Objectives &
Outline • Data Manipulation Language (DML): Query Structure
Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.4


History of SQL PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
History of SQL
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.5


History of Query Language PPD

Module 08

Partha Pratim • IBM developed Structured English Query Language (SEQUEL) as part of System R
Das
project. Renamed Structured Query Language (SQL: pronounced still as SEQUEL)
Objectives &
Outline • ANSI and ISO standard SQL:
Outline
SQL-86 First formalized by ANSI
History of SQL SQL-89 + Integrity Constraints
Data Definition SQL-92 Major revision (ISO/IEC 9075 standard), De-facto Industry Standard
Language (DDL) SQL:1999 + Regular Expression Matching, Recursive Queries, Triggers, Support for Procedural and
Create Table
Control Flow Statements, Nonscalar types (Arrays), and Some OO features (structured
Integrity Constraints
types), Embedding SQL in Java (SQL/OLB), and Embedding Java in SQL (SQL/JRT)
Update Table
SQL:2003 + XML features (SQL/XML), Window Functions, Standardized Sequences, and Columns
Data with Auto-generated Values (identity columns)
Manipulation
Language (DML): SQL:2006 + Ways of importing and storing XML data in an SQL database, manipulating it within
Query Structure the database, and publishing both XML and conventional SQL-data in XML form
Select Clause SQL:2008 Legalizes ORDER BY outside Cursor Definitions
Where Clause + INSTEAD OF Triggers, TRUNCATE Statement, and FETCH Clause
From Clause
SQL:2011 + Temporal Data (PERIOD FOR)
Module Summary Enhancements for Window Functions and FETCH Clause
SQL:2016 + Row Pattern Matching, Polymorphic Table Functions, and JSON
SQL:2019 + Multidimensional Arrays (MDarray type and operators)

Database Management Systems Partha Pratim Das 08.6


History of Query Language (2): Compliance PPD

Module 08

Partha Pratim • SQL is the de facto industry standard today for relational or structred data systems
Das
• Commercial systems as well as open systems may be fully or partially compliant to one
Objectives &
Outline or more standards from SQL-92 onward
Outline
• Not all examples here may work on your particular system. Check your system’s SQL
History of SQL
documentation
Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.7


History of Query Language (3): Alternatives PPD

Module 08

Partha Pratim • There aren’t any alternatives to SQL for speaking to relational databases (that is, SQL
Das
as a protocol), but there are many alternatives to writing SQL in the applications
Objectives &
Outline • These alternatives have been implemented in the form of frontends for working with
Outline relational databases. Some examples of a frontend include (for a section of languages):
History of SQL
◦ SchemeQL and CLSQL, which are probably the most flexible, owing to their Lisp
Data Definition
Language (DDL) heritage, but they also look like a lot more like SQL than other frontends
Create Table
Integrity Constraints
◦ LINQ (in .Net)
Update Table ◦ ScalaQL and ScalaQuery (in Scala)
Data
Manipulation
◦ SqlStatement, ActiveRecord and many others in Ruby
Language (DML):
Query Structure
◦ HaskellDB
Select Clause ◦ ...the list goes on for many other languages.
Where Clause
From Clause

Module Summary

Source: What are good alternatives to SQL (the language)?

Database Management Systems Partha Pratim Das 08.8


History of Query Language (4): Derivatives PPD

Module 08

Partha Pratim • There are several query languages that are derived from or inspired by SQL. Of these,
Das
the most popular and effective is SPARQL.
Objectives &
Outline ◦ SPARQL (pronounced sparkle, a recursive acronym for SPARQL Protocol and RDF
Outline Query Language) is an RDF query language
History of SQL . A semantic query language for databases - able to retrieve and manipulate data
Data Definition
Language (DDL) stored in Resource Description Framework (RDF) format.
Create Table . It has been standardized by the W3C Consortium as key technology of the
Integrity Constraints
Update Table semantic web
Data . Versions:
Manipulation
Language (DML):
Query Structure
− SPARQL 1.0 (January 2008)
Select Clause − SPARQL 1.1 (March, 2013)
Where Clause
From Clause . Used as the query languages for several NoSQL systems - particularly the Graph
Module Summary Databases that use RDF as store

Database Management Systems Partha Pratim Das 08.9


Data Definition Language (DDL) PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Data Definition Language (DDL)
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.10


Data Definition Language (DDL)

Module 08

Partha Pratim
The SQL data-definition language (DDL) allows the specification of information about
Das relations, including:
Objectives &
Outline
• The Schema for each Relation
Outline • The Domain of values associated with each Attribute
History of SQL
• Integrity Constraints
Data Definition
Language (DDL)
• And, as we will see later, also other information such as
Create Table
Integrity Constraints ◦ The set of Indices to be maintained for each relations
Update Table

Data
◦ Security and Authorization information for each relation
Manipulation
Language (DML):
◦ The Physical Storage Structure of each relation on disk
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.11


Domain Types in SQL

Module 08

Partha Pratim • char(n). Fixed length character string, with user-specified length n
Das
• varchar(n). Variable length character strings, with user-specified maximum length n
Objectives &
Outline • int. Integer (a finite subset of the integers that is machine-dependent)
Outline

History of SQL
• smallint(n). Small integer (a machine-dependent subset of the integer domain type)
Data Definition • numeric(p, d). Fixed point number, with user-specified precision of p digits, with d
Language (DDL)
Create Table
digits to the right of decimal point. (ex., numeric(3, 1), allows 44.5 to be stores
Integrity Constraints
Update Table
exactly, but not 444.5 or 0.32)
Data • real, double precision. Floating point and double-precision floating point numbers,
Manipulation
Language (DML): with machine-dependent precision
Query Structure
Select Clause • float(n). Floating point number, with user-specified precision of at least n digits
Where Clause
From Clause • More are covered in Chapter 4
Module Summary

Database Management Systems Partha Pratim Das 08.12


Schema Diagram for University Database PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.13


Create Table Construct PPD

Module 08
• An SQL relation is defined using the create table command:
Partha Pratim
Das create table r (A1 D1 , A2 D2 , . . . , An Dn ),
Objectives &
(integrity -constraint1 ),
Outline
...
Outline
(integrity -constraintk ));
History of SQL

Data Definition
◦ r is the name of the relation
Language (DDL) ◦ each Ai is an attribute name in the schema of relation r
Create Table
Integrity Constraints ◦ Di is the data type of values in the domain of attribute Ai
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.14


Create Table Construct (2) PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline create table instructor (


History of SQL ID char(5),
Data Definition
Language (DDL) name varchar(20)
Create Table
Integrity Constraints dept name varchar(20)
Update Table

Data salary numeric(8, 2));


Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.15


Create Table Construct (3): Integrity Constraints PPD

Module 08
• not null
Partha Pratim
Das • primary key (A1 , . . . , An )
Objectives &
Outline
• foreign key (Am , . . . , An ) references r
Outline
create table instructor ( create table instructor (
History of SQL

Data Definition ID char(5), ID char(5),


Language (DDL)
Create Table name varchar(20) name varchar(20) not null,
Integrity Constraints
Update Table
dept name varchar(20) dept name varchar(20),
Data
Manipulation salary numeric(8, 2)); salary numeric(8, 2),
Language (DML):
Query Structure primary key (ID),
Select Clause
Where Clause
foreign key (dept name) references department));
From Clause

Module Summary
primary key declaration on an attribute automatically ensures not null

Database Management Systems Partha Pratim Das 08.16


University Schema PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.17


Create Table Construct (4): More Relations PPD

create table student ( create table takes (


Module 08
ID varchar(5), ID varchar(5),
Partha Pratim
Das
name varchar(20) not null, course id varchar(8), sec id varchar(8),
Objectives & dept name varchar(20), semester varchar(6), year numeric(4, 0),
Outline

Outline tot cred numeric(3, 0), grade varchar(2),


History of SQL primary key (ID), primary key (ID, course id, sec id, semester, year ),
Data Definition
Language (DDL)
foreign key (dept name) foreign key (ID) references student
Create Table references department);
Integrity Constraints
foreign key (course id, sec id, semester, year )
Update Table
create table course ( references section);
Data course id varchar(8), • Note: sec id can be dropped from primary key above,
Manipulation
Language (DML): title varchar(50), to ensure a student cannot be registered for two
Query Structure sections of the same course in the same semester
Select Clause dept name varchar(20),
Where Clause
From Clause credits numeric(2, 0),
Module Summary primary key (course id),
foreign key (dept name)
references department);
Database Management Systems Partha Pratim Das 08.18
Update Tables

Module 08 • Insert (DML command)


Partha Pratim
Das
◦ insert into instructor values (‘10211’, ‘Smith’, ‘Biology’, 66000);
Objectives &
• Delete (DML command)
Outline
◦ Remove all tuples from the student relation
Outline
delete from student
History of SQL

Data Definition
• Drop Table (DDL command)
Language (DDL)
Create Table
◦ drop table r
Integrity Constraints
Update Table
• Alter (DDL command)
Data ◦ alter table r add A D
Manipulation
Language (DML): . Where A is the name of the attribute to be added to relation r and D is the domain of A
Query Structure . All existing tuples in the relation are assigned null as the value for the new attribute
Select Clause
Where Clause ◦ alter table r drop A
From Clause
. Where A is the name of an attribute of relation r
Module Summary . Dropping of attributes not supported by many databases

Database Management Systems Partha Pratim Das 08.19


Data Manipulation Language (DML): Query Structure PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Data Manipulation Language (DML):
Select Clause
Where Clause
From Clause
Query Structure
Module Summary

Database Management Systems Partha Pratim Das 08.20


Basic Query Structure

Module 08
• A typical SQL query has the form:
Partha Pratim
Das select A1 , A2 , . . . , An ,
Objectives &
from r1 , r2 , ..., rm
Outline where P
Outline
◦ Ai represents an attribute from ri ’s
History of SQL
◦ ri represents a relation
Data Definition
Language (DDL) ◦ P is a predicate
Create Table
Integrity Constraints • The result of an SQL query is a relation
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.21


Select Clause

Module 08
• The select clause lists the attributes desired in the result of a query
Partha Pratim
Das ◦ Corresponds to the projection operation of the relational algebra
Objectives & • Example: find the names of all instructors:
Outline

Outline
select name,
History of SQL
from instructor
Data Definition • NOTE: SQL names are case insensitive (that is, you may use upper-case or lower-case
Language (DDL)
Create Table letters)
Integrity Constraints
Update Table ◦ Name ≡ NAME ≡ name
Data ◦ Some people use upper case wherever we use bold font
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.22


Select Clause (2)

Module 08
• SQL allows duplicates in relations as well as in query results!!!
Partha Pratim
Das • To force the elimination of duplicates, insert the keyword distinct after select
Objectives &
Outline
• Find the department names of all instructors, and remove duplicates
Outline
select distinct dept name
History of SQL
from instructor
Data Definition
Language (DDL)
• The keyword all specifies that duplicates should not be removed
Create Table select all dept name
Integrity Constraints
Update Table
from instructor
Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.23


Select Clause (3)

Module 08
• An asterisk in the select clause denotes all attributes
Partha Pratim
Das select *
Objectives &
from instructor
Outline
• An attribute can be a literal with no from clause
Outline
select ’437’
History of SQL

Data Definition
◦ Results is a table with one column and a single row with value ’437’
Language (DDL)
Create Table
◦ Can give the column a name using:
Integrity Constraints select ’437’ as FOO
Update Table

Data • An attribute can be a literal with from clause


Manipulation
Language (DML): select ’A’
Query Structure
Select Clause
from instructor
Where Clause
From Clause
◦ Result is a table with one column and N rows (number of tuples in the instructors
Module Summary
table), each row with value ’A’

Database Management Systems Partha Pratim Das 08.24


Select Clause (4)

Module 08 The select clause can contain arithmetic expressions involving the operation, +, –, *, and
Partha Pratim /, and operating on constants or attributes of tuples
Das

Objectives &
• The query:
Outline select ID, name, salary/12
Outline from instructor
History of SQL

Data Definition
• Would return a relation that is the same as the instructor relation, except that the
Language (DDL) value of the attribute salary is divided by 12
Create Table
Integrity Constraints • Can rename “salary /12” using the as clause:
Update Table

Data
select ID, name, salary/12 as monthly salary
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.25


Where Clause

Module 08
• The where clause specifies conditions that the result must satisfy
Partha Pratim
Das ◦ Corresponds to the selection predicate of the relational algebra
Objectives & • To find all instructors in Comp. Sci. dept
Outline

Outline
select name
History of SQL
from instructor
Data Definition
where dept name = ’Comp. Sci.’
Language (DDL)
Create Table • Comparison results can be combined using the logical connectives and, or, and not
Integrity Constraints
Update Table ◦ To find all instructors in Comp. Sci. dept with salary > 80000
Data select name
Manipulation
Language (DML): from instructor
Query Structure
Select Clause
where dept name = ’Comp. Sci.’ and salary > 80000
Where Clause
From Clause • Comparisons can be applied to results of arithmetic expressions
Module Summary

Database Management Systems Partha Pratim Das 08.26


From Clause

Module 08
• The from clause lists the relations involved in the query
Partha Pratim
Das ◦ Corresponds to the Cartesian product operation of the relational algebra
Objectives & • Find the Cartesian product instructor X teaches
Outline

Outline
select *
History of SQL
from instructor , teaches
Data Definition ◦ Generates every possible instructor-teaches pair, with all attributes from both
Language (DDL)
Create Table
relations
Integrity Constraints
Update Table
◦ For common attributes (for example, ID), the attributes in the resulting table are
Data
renamed using the relation name (for example, instructor.ID)
Manipulation
Language (DML): • Cartesian product not very useful directly, but useful combined with where-clause
Query Structure
Select Clause
condition (selection operation in relational algebra)
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.27


Cartesian Product

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.28


Module Summary PPD

Module 08

Partha Pratim • Introduced relational query language


Das
• Familiarized with data definition and basic query structure
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure Slides used in this presentation are borrowed from http://db-book.com/ with kind
Select Clause
Where Clause
permission of the authors.
From Clause Edited and new slides are marked with “PPD”.
Module Summary

Database Management Systems Partha Pratim Das 08.29

You might also like