Vous êtes sur la page 1sur 22

INF3710 : Database Systems

Introduction to Structured Query


Language (SQL)
Fall 2017

Fabio Petrillo
Chargé de Cours This work is licensed under a Creative
Commons Attribution-NonCommercial-
ShareAlike 3.0 Unported License
The Structured Query Language (SQL)
● The SQL language is the most common query language, and may be
considered one of the major reasons for the commercial success of relational
databases. De facto standard for relational databases
● SQL language provides a higher-level declarative language interface, so
the user only specifies what the result is to be, leaving the actual
optimization and decisions on how to execute the query to the DBMS
● SQL is a comprehensive database language: It has statements for data
definitions, queries, and updates. Hence, it is both a DDL and a DML.
● SQL is flexible, expressive and powerful
● Because the SQL specification is very large, we give a description of the most
important features.
● You must master SQL!
SQL History
● IBM SEQUEL language developed as part of System R project at the IBM
San Jose Research Laboratory
● Renamed Structured Query Language (SQL)
● ANSI and ISO standard SQL:
○ SQL-86,SQL-89,SQL-92,SQL:1999,
○ SQL:2003
● Commercial systems offer most, if not all, SQL-92 features, plus varying
feature sets from later standards and special proprietary features.
The Structured Query Language (SQL)
● SQL is a nonprocedural and declarative language; you specify what
information you require, rather than how to get it.
● SQL does not require you to specify the access methods to the data.
● SQL is essentially free-format, which means that parts of statements do not
have to be typed at particular locations on the screen.
● The command structure consists of standard English words such as CREATE
TABLE, INSERT, SELECT
● Most components of an SQL statement are case-insensitive
● Example of SQL query
Data Definition Language (DDL)
The SQL data-definition language (DDL) allows the specification of information
about relations, including:

● The Database
● The schema for each relation.
● The domain of values associated with each attribute.
● Integrity constraints
● The set of indices to be maintained for each relations.
● Security and authorization information for each relation.
● The physical storage structure of each relation on disk.
SQL Data Definition Commands
Creating and Dropping Database Schema
● Create schema

CREATE SCHEMA [ Name | AUTHORIZATION Creatorldentifier ]

if the creator of a schema SqlTests is Smith , the SQL statement is:

CREATE SCHEMA SqlTests AUTHORIZATION Smith ;

● Dropping schema

DROP SCHEMA Name [RESTRICT | CASCADE]


Common SQL Data Types
Creating a Table (CREATE TABLE)
Creating a Table (Example)
CREATE TABLE employee (
employeeId INTEGER NOT NULL,
assurance VARCHAR (9) NOT NULL,
name VARCHAR (30) NOT NULL,
salary DECIMAL(9,2),
PRIMARY KEY (employeeId),
UNIQUE (assurance))
Data manipulation language (DML)
● SQL includes commands to insert, update, delete, and retrieve data within the
database tables.
SQL Data Manipulation Commands
SQL Data Manipulation Commands (continued)
Adding Table Rows - INSERT
SQL requires the use of the INSERT command to enter data into a table. The
INSERT command’s basic syntax looks like this:

INSERT INTO tablename VALUES (value1, value2, ... , valuen)

Examples:

INSERT INTO Employee VALUES (1, '123','Jonh',20.0)

INSERT INTO Employee (employeeId, name, assurance)


VALUES (2, 'Mary','124')
Listing Table Rows - SELECT
The SELECT command is used to list the contents of a table. The syntax of the
SELECT command is as follows:

SELECT [DISTINCT | ALL] {* | [columnExpression [ AS newName]] [, . . .]}


FROM TableName [alias] [, . . .]
[WHERE condition]
[GROUP BY columnList]
[HAVING condition]
[ORDER BY columnList]
Listing Table Rows - SELECT
1) Retrieve all columns, all rows

SELECT * FROM employee

2) List employee name where salary > 5

SELECT name FROM employee WHERE salary > 5


Updating Table Rows (UPDATE)
The UPDATE statement allows the contents of existing rows in a named table to
be changed. The format of the command is:

UPDATE TableName
SET columnName1 = dataValue1 [, columnName2 = dataValue2 . . .]
[WHERE searchCondition]

Example

UPDATE employee SET salary = salary+100 WHERE assurance = '123'


Deleting data from the database (DELETE)
The DELETE statement allows rows to be deleted from a named table. The format
of the command is:

DELETE FROM TableName [WHERE searchCondition]

Example

DELETE FROM Employee WHERE assurance = '124'


View mechanism
● View is, in essence, some subset of the database
View mechanism
● Views provide a level of security: Views can be set up to exclude data that
some users should not see.
● Views provide a mechanism to customize the appearance of the
database: For example, the Contracts Department may wish to call the
monthly rent field ( rent ) by the more obvious name, Monthly Rent.
● A view can present a consistent, unchanging picture of the structure of the
database
● A view helps provide the program–data independence
Creating a View
The format of the CREATE VIEW statement is:

CREATE VIEW ViewName [( newColumnName [, . . . ])] AS subselect

Example:

CREATE VIEW director AS


SELECT * FROM employee WHERE salary >= 100

SELECT * FROM director


Next course
Course review

Vous aimerez peut-être aussi