slide1 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
SQL PowerPoint Presentation
Download Presentation
SQL

Loading in 2 Seconds...

play fullscreen
1 / 15

SQL - PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on

SQL. Sangeeta Devadiga CS157A, Fall 2006. Outline. Background Data Definition Basic Structure Set Operation. Background. IBM developed the original version named sequel in early 1970’s Sequel language has evolved into SQL SQL (Structured Query Language) Versions of SQL

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

SQL


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

SQL

Sangeeta Devadiga

CS157A, Fall 2006

outline
Outline
  • Background
  • Data Definition
  • Basic Structure
  • Set Operation
background
Background
  • IBM developed the original version

named sequel in early 1970’s

  • Sequel language has evolved into SQL
  • SQL (Structured Query Language)

Versions of SQL

SQL 92

SQL 99

SQL 2003 (latest version)

parts of sql
Parts of SQL
  • Data Definition Language (DDL)
  • Data manipulation Language (DML)
  • Integrity
  • View Definition
  • Transaction control
  • Embedded SQL and Dynamic SQL
  • Authorization
basic domain types
Basic Domain Types
  • char(n): A fixed length character string with user specifed length n. character can be used instead.
  • varchar(n): A variable length character string with user specified max length n. charactervarying is equivalent.
  • int: An Integer, the full form integer is equivalent.
  • smallint: A small integer, a subset of integer domain type
  • numeric(p,d): A fixed point number with user specified precision. E.g: numeric(3,1) allows 31.5 to be defined precisely
  • real, double precision: Floating-point and double precision floating-point numbers with machine-dependent precision.
  • float(n): A floating point number, with precision of at least n digits.
basic schema definition in sql
Basic Schema Definition in SQL

We create SQL relation using the create table command

create table r(A1D1 , A2D2, ….. , AnDn ,

(integrity constraint1),

………. ,

(integrity constraintk))

r  is the name of relation

A1….An  are the names of attributes

D1…Dn  are the types of values in the domain

examples create table
Examples create table

Example 1:

create tablecustomer

(customer_name char(20),

customer_street char(30),

customer_city char(30),

primary key (customer_name))

example 2
Example 2

create tableaccount

(account_number char(10),

branch_name char(15),

balance numeric (12, 2),

primary key (account_number))

basic sql query structure
Basic SQL Query Structure
  • SQL is based on set and relational operations with some modification and enhancement.
  • SQL query has the form

selectA1,A2, … ,An

from r1, r2, …. ,rm

whereP

      • A1 is a attribute
      • r1 represents a relation
      • P is a predicate
  • Equivalent Query:

 A1, A2, … , An(P (r1X r2X …… X rm))

  • The result of a SQL query is a relation
example select clause
Example select Clause
  • selectbranch_name

fromloan

  • In relational Algebra, the query would be

branch_name(loan)

  • SQL allows duplicates in query result
    • use distinct if no duplicates in result
    • use all if duplicates required in result
  • selectdistinctbranch_name

from loan (result has distinct branch names)

  • selectallbranch_name

from loan (result may have duplicates)

where clause
Where Clause
  • Corresponds to the selection predicate of relational algebra
  • To find loan numbers for loans made at San Jose

branch with loan amounts greater than $500

selectloan_number

fromloan

wherebranch_name = “San Jose” and amount > $500

  • Comparison result can be combined with logical connectives and, or, and not
  • SQL includes between comparison operator
  • To find loan numbers between amt. 900 and 10,000

select loan_number

from loan

whereamount between 900 and 10000

from clause
From Clause
  • Corresponds to Cartesian product operation of relational algebra
  • Example:

To find name, loan number, amount of all

customers having loan at San Jose branch.

selectcustomer_name, loan_number, amount

from borrower , loan

where borrower.loan_number=loan.loan_number and

branch_name = “San Jose”

rename
Rename
  • SQL allows renaming relations and attributes using as clause
  • Example:

To find name, loan_number, amount

of all customers and rename column

loan_number as loan_id.

selectcustomer_name, loan_number as loan_id, amount

from borrower , loan

where borrower.loan_number=loan. loan_number

set operation
Set Operation
  • The set operations union, intersect and except corresponds to U,, - respectivelyof relational algebra.
  • Each of the above operation automatically eliminates duplicates
  • To retain all duplicates use union all,

intersect all, except all

examples set operations
Examples Set Operations
  • Find all customers who have a loan, a account or both:

selectcustomer_namefromdepositor

union

selectcustomer_namefromborrower

  • Find all customers who have both loan and an account:

selectcustomer_namefromdepositor

intersect

selectcustomer_namefromborrower

  • Find all customers who have an account, but no loan:

selectcustomer_namefromdepositor

except

selectcustomer_namefromborrower