34
Structured Query Language (SQL)

Structured Query Language ( SQL)

  • Upload
    ramona

  • View
    75

  • Download
    1

Embed Size (px)

DESCRIPTION

Structured Query Language ( SQL). Objectives of SQL. Ideally, a database language should allow a user to: create the database and table structures; perform basic data management tasks, such as the insertion, modification, and deletion of data from the tables; - PowerPoint PPT Presentation

Citation preview

Page 1: Structured Query Language  ( SQL)

Structured Query Language

(SQL)

Page 2: Structured Query Language  ( SQL)

Objectives of SQLIdeally, a database language should allow a user to: create the database and table structures; perform basic data management tasks, such as the

insertion, modification, and deletion of data from the tables;

perform both simple and complex queries. perform these tasks with minimal user effort, and its

command structure and syntax must be relatively easy to learn.

must be portable: that is, it must conform to some recognized standard can use the same command structure and syntax when move from one DBMS to another

Page 3: Structured Query Language  ( SQL)

SQL is an example of a transform-oriented language, or a language designed to transform input tables into required output tables.

The ISO SQL standard has two major components:◦ Data Definition Language (DDL) for defining the

database structure and controlling access to the data; to create the database structure and the access mechanisms

◦ Data Manipulation Language (DML) for retrieving and updating data. to populate and query the tables

Page 4: Structured Query Language  ( SQL)

Writing SQL commands An SQL statement consists of reserved words and

user-defined words. Reserved words are a fixed part of the SQL

language and have a fixed meaning. They must be spelled exactly as required and

cannot be split across lines. User-defined words are made up by the user

(according to certain syntax rules), and represent the names of various database objects such as tables, columns, views, indexes, and so on.

Most components of an SQL statement are case insensitive

Page 5: Structured Query Language  ( SQL)

The Data Definition Language (DDL): This subset of SQL supports the creation, deletion, and modification of definitions for tables and views.

The Data Manipulation Language (DML): This subset of SQL allows users to pose queries and to insert, delete, and modify rows.

Triggers: actions executed by the DBMS whenever changes to the database meet conditions specified in the trigger.

The SQL language

Page 6: Structured Query Language  ( SQL)

Data manipulation SQL DML statements:

◦ SELECT to query data in the database;◦ INSERT to insert data into a table;◦ UPDATE to update data in a table;◦ DELETE to delete data from a table

Page 7: Structured Query Language  ( SQL)

SELECT [DISTINCT] select-listFROM from-listWHERE qualification

THE FORM OF A BASIC SQL QUERY

Page 8: Structured Query Language  ( SQL)

a SELECT clause : species columns to be retained in the result,

a FROM clause : species a cross-product of tables.

The optional WHERE clause : selection conditions on the tables mentioned in the FROM clause.

Page 9: Structured Query Language  ( SQL)

Queries: SELECT The purpose : is to retrieve and display data from one or

more database tables. The form:

◦ columnExpression represents a column name or an expression;◦ newName is the column name as a display heading;◦ TableName is the name of an existing database table or view ◦ alias is an optional abbreviation for TableName

Page 10: Structured Query Language  ( SQL)

The sequence of processing in a SELECT statement is:

SELECT specifies which columns are to appear in the output;

FROM specifies the table or tables to be used; WHERE filters the rows subject to some condition; GROUP BY forms groups of rows with the same

column value; HAVING filters the groups subject to some

condition; ORDER BY specifies the order of the output.

Page 11: Structured Query Language  ( SQL)

Example:

Page 12: Structured Query Language  ( SQL)

Sailors Reserves

Boats

Page 13: Structured Query Language  ( SQL)

Find the names and ages of all sailors.

Page 14: Structured Query Language  ( SQL)

Find the names and ages of all sailors.

Page 15: Structured Query Language  ( SQL)

Find all sailors with a rating above 7

Example :

Page 16: Structured Query Language  ( SQL)

Find all sailors with a rating above 7

Page 17: Structured Query Language  ( SQL)

The from-list in the FROM clause is a list of table names.◦ A table name can be followed by a range variable

The select-list is a list of column names of tables named in the from list.◦ Column names can be prefixed by a range variable.

The qualification in the in the WHERE clause is a boolean combination of conditions of the form expression op expression◦ Op : comparison expression◦ An expression is a column name, a constant or an (arithmetic

or string) expression The DISTINCT keyword is optional.

◦ Indicate that the table computed as an answer to this query should not contain duplicate.

The syntax of a basic SQL query

Page 18: Structured Query Language  ( SQL)

1. Compute the cross-product of the tables in the from-list.

2. Delete those rows in the cross-product that fail the qualification conditions.

3. Delete all columns that do not appear in the select-list.

4. If DISTINCT is specified, eliminate duplicate rows.

The Conceptual evaluation strategy:

Page 19: Structured Query Language  ( SQL)

Find the names of sailors who have reserved boat number 103

Page 20: Structured Query Language  ( SQL)

Find the names of sailors who have reserved boat number 103

Page 21: Structured Query Language  ( SQL)

Explanation:

ReservesSailors

Reserves X Sailors

Page 22: Structured Query Language  ( SQL)

The answer :

Page 23: Structured Query Language  ( SQL)

Find the names of sailors who have reserved a red boat

Page 24: Structured Query Language  ( SQL)

Find the Sids of sailors who have reserved a red boat

Page 25: Structured Query Language  ( SQL)

Find the colors of boats reserved by Lubber

Page 26: Structured Query Language  ( SQL)

Find the colors of boats reserved by Lubber

Page 27: Structured Query Language  ( SQL)

Find the names of sailors who have reserved at least one boat

Page 28: Structured Query Language  ( SQL)

Find the names of sailors who have reserved at least one boat

Page 29: Structured Query Language  ( SQL)

Find the names of sailors who have reserved a red or a green boat.

UNION, INTERSECT, AND EXCEPT

Page 30: Structured Query Language  ( SQL)

Find the names of sailors who have reserved a red or a green boat.

Page 31: Structured Query Language  ( SQL)

Find the names of sailors who have reserved both a red and a green boat.

Page 32: Structured Query Language  ( SQL)

Find the names of sailors who have reserved both a red and a green boat.

Page 33: Structured Query Language  ( SQL)

Find the sids of all sailors who have reserved red boats but not green boats

Page 34: Structured Query Language  ( SQL)

Find the sids of all sailors who have reserved red boats but not green boats