• Non ci sono risultati.

DatabasesSQL language: basicsElena Baralisand Tania Cerquitelli©2013 Politecnico di Torino1

N/A
N/A
Protected

Academic year: 2021

Condividi "DatabasesSQL language: basicsElena Baralisand Tania Cerquitelli©2013 Politecnico di Torino1"

Copied!
4
0
0

Testo completo

(1)

Databases SQL language: basics

Elena Baralis and Tania Cerquitelli

©2013 Politecnico di Torino 1

D B M G D B

M G Databases

Unit 3 Unit 3

SQL language: basics SQL language: basics

D B M G

2

SQL language: basics

Introduction

The SELECT statement: basics Nested queries

Set operators Update commands Table management

D B M G D B

M G

SQL language: basics

Introduction Introduction

D B M G

4

Introduction

Introduction to the SQL language Language commands

Notation

D B M G D B

M G Introduction

Introduction to the SQL language Introduction to the SQL language

D B M G

6

The SQL language

A language for managing relational databases Structured Query Language

SQL provides commands to

define the schema of a relational database read and write data

define the schema of derived tables definire user access priviliges manage transactions

(2)

Databases SQL language: basics

Elena Baralis and Tania Cerquitelli

©2013 Politecnico di Torino 2

D B M G

7

The SQL language

SQL is a set-levellanguage operators are applied to relations the result is always a relation SQL is a declarative language

it describes what to doand not how to do it it has a higher level of abstraction compared to traditional programming languages

D B M G

8

The SQL language

The language may be used in two ways interactive

compiled

a host language encapsulates the SQL commands SQL commands can be distinguished from the host language commands by means of appropriate syntactical devices

D B M G D B

M G Introduction

Language commands Language commands

D B M G

10

The SQL language

It can be subdivided into

DML (Data Manipulation Language) language for querying and updating the data DDL (Data Definition Language)

language for defining the database structure

D B M G

11

Data Manipulation Language

To query a database in order to extract the data of interest

SELECT

To modify a database instance

insertion of new information into a table INSERT

update of the information in the database UPDATE

deletion of obsolete data DELETE

D B M G

12

Data Definition Language

To define a database schema

creation, modification and deletion of tables CREATE, ALTER, DROP TABLE

To define derived tables

creation, modification and deletion of tables whose content is obtained from other database tables

CREATE, ALTER, DROP VIEW

To define complementary data structures for efficiently retrieving the data

creation and deletion of indices CREATE, DROP INDEX

(3)

Databases SQL language: basics

Elena Baralis and Tania Cerquitelli

©2013 Politecnico di Torino 3

D B M G

13

Data Definition Language

To define user access privileges

grant and revocation of privileges on resources GRANT, REVOKE

To define transactions termination of a transaction

COMMIT, ROLLBACK

D B M G

14

Evolution of the SQL standard

Informal

name Official name Features

Basic SQL SQL-86 Basic constructs

SQL-89 Referential integrity

SQL-2 SQL-92

Relational model Several new constructs 3 levels: entry, intermediate, full

SQL-3

SQL:1999

Object-relational model Organized into multiple parts Triggers, external functions, …

SQL:2003

Extensions to the object model Removal of unused constructs New parts: SQL/JRT, SQL/XML

D B M G D B

M G Introduzione

Notation Notation

D B M G

16

Syntax of SQL commands

Notation

language keywords upper case, dark blue color variable terms

italic font

D B M G

17

Syntax of SQL commands

Grammar

angle brackets < >

to isolate a syntactic term square brackets [ ]

to mark that the enclosed term is optional braces { }

to mark that the enclosed term may not appear or may repeated an arbitrary number of items vertical bar |

to mark that the term must be chosen among those separated by the bars

D B M G

18

Example database

Supplier and part DB

table P: it describes the available products primary key: PId

table S: it describes the suppliers primary key: SId

table SP: it describes supplies, by relating each product to the suppliers that provide it

primary key: (SId, PId)

(4)

Databases SQL language: basics

Elena Baralis and Tania Cerquitelli

©2013 Politecnico di Torino 4

D B M G

19

Instance of the example database

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

P

D B M G

20

Instance of the example database

S

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B M G

21

Instance of the example database

SP

SId PId Qty

S1 P1 300

S1 P2 200

S1 P3 400

S1 P4 200

S1 P5 100

S1 P6 100

S2 P1 300

S2 P2 400

S3 P2 200

S4 P3 200

S4 P4 300

S4 P5 400

Riferimenti

Documenti correlati

Integrity constraints are checked after each SQL command that may cause their violation Insert or update operations on the referencing table that violate the constraints are

The view is updatable CREATE VIEW SUPPLIER_LONDON AS SELECT SId, SName, #Employees, City FROM S.

The database is in a new (final) correct state The changes to the data executed by the transaction

If an SQL command returns a single row it is sufficient to specify in which host language variable the result of the command shall be stored If an SQL command returns a table (i.e.,

if Rossi has propagated the privilege to other users, the privilege is revoked in cascade if Rossi has created a view using the SELECT privilege, the view is removed. REVOKE

Operations carried out to execute the query reading of the accessory physical structure to recover the physical locations of the records corresponding to Residence=Como

Find the codes and the names of the pilots who are qualified to fly on an aircraft that can cover distances greater than 5,000 km (MaximumRange&gt;=5,000). AIRCRAFT (AId,

Find the codes of the sailors who have never booked a red boat SAILOR (SId, SName, Expertise, DateofBirth). BOOKING (SId, BId, Date) BOAT(Bid,