elmasri 6e ch04 updated -...

57
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL

Upload: truongthuan

Post on 03-Apr-2018

257 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 4

Basic SQL

Page 2: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Chapter 4 Outline

� SQL Data Definition and Data Types

� Specifying Constraints in SQL

� Basic Retrieval Queries in SQL

� INSERT, DELETE, and UPDATE Statements

in SQL

� Additional Features of SQL

Page 3: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Basic SQL

� SQL language

� Considered one of the major reasons for the

commercial success of relational databases

� SQL

� Structured Query Language

� Statements for data definitions, queries, and

updates (both DDL and DML)

� Core specification

� Plus specialized extensions

Page 4: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

SQL Data Definition and Data

Types� Terminology:

� Table, row, and column used for relational

model terms relation, tuple, and attribute

� CREATE statement

� Main SQL command for data definition

Page 5: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Schema and Catalog Concepts

in SQL� SQL schema

� Identified by a schema name

� Includes an authorization identifier and

descriptors for each element

� Schema elements include

� Tables, constraints, views, domains, and other

constructs

� Each statement in SQL ends with a semicolon

Page 6: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Schema and Catalog Concepts

in SQL (cont’d.)� CREATE SCHEMA statement

� CREATE SCHEMA COMPANY AUTHORIZATION

‘Jsmith’;

� Catalog

� Named collection of schemas in an SQL

environment

� SQL environment

� Installation of an SQL-compliant RDBMS on a

computer system

Page 7: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The CREATE TABLE Command

in SQL� Specify a new relation

� Provide name

� Specify attributes and initial constraints

� Can optionally specify schema:

� CREATE TABLE COMPANY.EMPLOYEE ...

or

� CREATE TABLE EMPLOYEE ...

Page 8: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The CREATE TABLE Command

in SQL (cont’d.)� Base tables (base relations)

� Relation and its tuples are actually created and

stored as a file by the DBMS

� Virtual relations

� Created through the CREATE VIEW statement

Page 9: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 10: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 11: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The CREATE TABLE Command

in SQL (cont’d.)� Some foreign keys may cause errors

� Specified either via:

• Circular references

• Or because they refer to a table that has not yet been created

Page 12: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Attribute Data Types and

Domains in SQL� Basic data types

� Numeric data types

• Integer numbers: INTEGER, INT, and SMALLINT

• Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION

� Character-string data types

• Fixed length: CHAR(n), CHARACTER(n)

• Varying length: VARCHAR(n), CHAR

VARYING(n), CHARACTER VARYING(n)

Page 13: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Attribute Data Types and

Domains in SQL (cont’d.)� Bit-string data types

• Fixed length: BIT(n)

• Varying length: BIT VARYING(n)

� Boolean data type

• Values of TRUE or FALSE or NULL

� DATE data type

• Ten positions

• Components are YEAR, MONTH, and DAY in the form

YYYY-MM-DD

Page 14: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Attribute Data Types and

Domains in SQL (cont’d.)� Additional data types

� Timestamp data type (TIMESTAMP)

• Includes the DATE and TIME fields

• Plus a minimum of six positions for decimal fractions of seconds

• Optional WITH TIME ZONE qualifier

� INTERVAL data type

• Specifies a relative value that can be used to

increment or decrement an absolute value of a date, time, or timestamp

Page 15: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Data Type: TIMESTAMP

• TIMESTAMP:

24-JAN-2012 08.00.05.993847 AM

• TIMESTAMP(0): -- Oracle

24-JAN-2012 08.00.05 AM

Page 16: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Functions that Return Current DateTime

Datetime GETDATE()

Datetime CURRENT_TIMESTAMP()

Page 17: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Getting the current system date

and time : DATATIMESELECT SYSDATETIME() ,

SYSDATETIMEOFFSET() ,

SYSUTCDATETIME() ,

CURRENT_TIMESTAMP ,

GETDATE() ,

GETUTCDATE();

SYSDATETIME() 2007-04-30 13:10:02.0474381

SYSDATETIMEOFFSET()2007-04-30 13:10:02.0474381 -07:00

SYSUTCDATETIME() 2007-04-30 20:10:02.0474381

CURRENT_TIMESTAMP 2007-04-30 13:10:02.047

GETDATE() 2007-04-30 13:10:02.047

GETUTCDATE() 2007-04-30 20:10:02.047

Page 18: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Getting the current system: date

SELECT CONVERT (date, SYSDATETIME()) ,

CONVERT (date, SYSDATETIMEOFFSET()) ,

CONVERT (date, SYSUTCDATETIME()) ,

CONVERT (date, CURRENT_TIMESTAMP) ,

CONVERT (date, GETDATE()) ,

CONVERT (date, GETUTCDATE());

SYSDATETIME() 2007-05-03

SYSDATETIMEOFFSET() 2007-05-03

SYSUTCDATETIME() 2007-05-04

CURRENT_TIMESTAMP 2007-05-03

GETDATE() 2007-05-03

GETUTCDATE() 2007-05-04

Page 19: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Getting the current system: time

SELECT CONVERT (time, SYSDATETIME()) ,

CONVERT (time, SYSDATETIMEOFFSET()) ,

CONVERT (time, SYSUTCDATETIME()) ,

CONVERT (time, CURRENT_TIMESTAMP) ,

CONVERT (time, GETDATE()) ,

CONVERT (time, GETUTCDATE());

SYSDATETIME() 13:18:45.3490361

SYSDATETIMEOFFSET()13:18:45.3490361

SYSUTCDATETIME() 20:18:45.3490361

CURRENT_TIMESTAMP 13:18:45.3470000

GETDATE() 13:18:45.3470000

GETUTCDATE() 20:18:45.3470000

Page 20: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

MSDN References for Data Type

http://msdn.microsoft.com/en-us/library/ms188383.aspx

http://msdn.microsoft.com/en-us/library/ms716506(v=vs.85).aspx

Page 21: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Attribute Data Types and

Domains in SQL (cont’d.)� Domain

� Name used with the attribute specification

� Makes it easier to change the data type for a

domain that is used by numerous attributes

� Improves schema readability

� Example:

• CREATE DOMAIN SSN_TYPE AS CHAR(9);

Page 22: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Constraints in SQL

� Basic constraints:

� Key and referential integrity constraints

� Restrictions on attribute domains and NULLs

� Constraints on individual tuples within a

relation

Page 23: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Attribute Constraints

and Attribute Defaults� NOT NULL

� NULL is not permitted for a particular attribute

� Default value

� DEFAULT <value>

� CHECK clause

� Dnumber INT NOT NULL CHECK (Dnumber

> 0 AND Dnumber < 21);

Page 24: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 25: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Key and Referential

Integrity Constraints� PRIMARY KEY clause

� Specifies one or more attributes that make up

the primary key of a relation

� Dnumber INT PRIMARY KEY;

� UNIQUE clause

� Specifies alternate (secondary) keys

� Dname VARCHAR(15) UNIQUE;

Page 26: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Key and Referential

Integrity Constraints (cont’d.)� FOREIGN KEY clause

� Default operation: reject update on violation

� Attach referential triggered action clause

• Options include SET NULL, CASCADE, and SET DEFAULT

• Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE

• CASCADE option suitable for “relationship” relations

Page 27: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Giving Names to Constraints

� Keyword CONSTRAINT

� Name a constraint

� Useful for later altering

Page 28: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Constraints on Tuples

Using CHECK� CHECK clauses at the end of a CREATE TABLE statement

� Apply to each tuple individually

� CHECK (Dept_create_date <=

Mgr_start_date);

Page 29: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Schema Change Statements in

SQL� Schema evolution commands

� Can be done while the database is operational

� Does not require recompilation of the database

schema

Page 30: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The DROP Command

� DROP command

� Used to drop named schema elements, such

as tables, domains, or constraint

� Drop behavior options:

� CASCADE and RESTRICT

� Example:

� DROP SCHEMA COMPANY CASCADE;

Page 31: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The ALTER Command

� Alter table actions include:

� Adding or dropping a column (attribute)

� Changing a column definition

� Adding or dropping table constraints

� Example:

� ALTER TABLE COMPANY.EMPLOYEE ADD

COLUMN Job VARCHAR(12);

� To drop a column

� Choose either CASCADE or RESTRICT

Page 32: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The ALTER Command (cont’d.)

� Change constraints specified on a table

� Add or drop a named constraint

Page 33: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Basic Retrieval Queries in SQL

� SELECT statement

� One basic statement for retrieving information

from a database

� SQL allows a table to have two or more tuples that are identical in all their attribute values

� Unlike relational model

� Multiset or bag behavior

Page 34: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The SELECT-FROM-WHERE

Structure of Basic SQL Queries� Basic form of the SELECT statement:

Page 35: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The SELECT-FROM-WHERE

Structure of Basic SQL Queries (cont’d.)

� Logical comparison operators

� =, <, <=, >, >=, and <>

� Projection attributes

� Attributes whose values are to be retrieved

� Selection condition

� Boolean condition that must be true for any

retrieved tuple

Page 36: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 37: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 38: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Ambiguous Attribute Names

� Same name can be used for two (or more) attributes

� As long as the attributes are in different

relations

� Must qualify the attribute name with the

relation name to prevent ambiguity

Page 39: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aliasing, Renaming, and Tuple

Variables� Aliases or tuple variables

� Declare alternative relation names E and S

� EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd,

Addr, Sex, Sal, Sssn, Dno)

Page 40: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Unspecified WHERE Clause

and Use of the Asterisk� Missing WHERE clause

� Indicates no condition on tuple selection

� CROSS PRODUCT

� All possible tuple combinations

Page 41: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Unspecified WHERE Clause

and Use of the Asterisk (cont’d.)� Specify an asterisk (*)

� Retrieve all the attribute values of the selected

tuples

Page 42: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Tables as Sets in SQL

� SQL does not automatically eliminate duplicate tuples in query results

� Use the keyword DISTINCT in the SELECT

clause

� Only distinct tuples should remain in the result

Page 43: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Tables as Sets in SQL (cont’d.)

� Set operations

� UNION, EXCEPT (difference), INTERSECT

� Corresponding multiset operations: UNION

ALL, EXCEPT ALL, INTERSECT ALL)

Page 44: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Substring Pattern Matching and

Arithmetic Operators� LIKE comparison operator

� Used for string pattern matching

� % replaces an arbitrary number of zero or

more characters

� underscore (_) replaces a single character

� Standard arithmetic operators:

� Addition (+), subtraction (–), multiplication (*),

and division (/)

� BETWEEN comparison operator

Page 45: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Ordering of Query Results

� Use ORDER BY clause

� Keyword DESC to see result in a descending

order of values

� Keyword ASC to specify ascending order

explicitly

� ORDER BY D.Dname DESC, E.Lname ASC,

E.Fname ASC

Page 46: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Discussion and Summary

of Basic SQL Retrieval Queries

Page 47: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

INSERT, DELETE, and UPDATE

Statements in SQL� Three commands used to modify the

database:

� INSERT, DELETE, and UPDATE

Page 48: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The INSERT Command

� Specify the relation name and a list of values for the tuple

Page 49: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The DELETE Command� Removes tuples from a relation

� Includes a WHERE clause to select the tuples to

be deleted

� Without Where Clause, all the tuples will be

deleted

Page 50: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Join Delete

DELETE FROMTable1 /*Target table */

FROM Table1 AS T1

INNER JOIN Table2 AS T2

ON T1.ID = T2.ID /*Source table*/

WHERE T2.Sales > 2500000.00;

Page 51: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Join Delete

DELETE FROMSales.SalesPersonQuotaHistory

FROM Sales.SalesPersonQuotaHistory AS spqh

INNER JOIN Sales.SalesPerson AS sp

ON spqh.BusinessEntityID = sp.BusinessEntityID

WHERE sp.SalesYTD > 2500000.00;

Page 52: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The UPDATE Command� Modify attribute values of one or more

selected tuples

� Additional SET clause in the UPDATE

command � Specifies attributes to be modified and new values

� W/O Where Clause, all the values of Plocation will be changed to ‘Bellaire’, Dnum to 5.

Page 53: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Join Update

UPDATE Table2 /*Target table*/

SET Table2.ColB = Table2.ColB + Table1.ColB

FROM Table2 INNER JOIN Table1 /*Source table*/

ON (Table2.ColA =Table1.ColA);

Page 54: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Merge (Into)

MERGE member_Table AS Target

USING someOtherTable AS Source

ON target.mt_member = source.mt_memberAND source.mt_member = 0 AND source.mt_topic = 110

WHEN MATCHED THEN

UPDATE

SET mt_notes = 'test'

WHEN NOT MATCHED THEN

INSERT (mt_member, mt_topic, mt_notes) VALUES (0, 110, 'test') ;

Page 55: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Additional Features of SQL

� Techniques for specifying complex retrieval queries

� Writing programs in various programming languages that include SQL statements

� Set of commands for specifying physical database design parameters, file structures for relations, and access paths

� Transaction control commands

Page 56: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Additional Features of SQL

(cont’d.)� Specifying the granting and revoking of

privileges to users

� Constructs for creating triggers

� Enhanced relational systems known as object-relational

� New technologies such as XML and OLAP

Page 57: Elmasri 6e Ch04 Updated - csuohio.edueecs.csuohio.edu/~sschung/cis430/Elmasri_6e_Ch04_Updated...Basic data types Numeric data types • Integer numbers: INTEGER , INT , and SMALLINT

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Summary

� SQL

� Comprehensive language

� Data definition, queries, updates, constraint

specification, and view definition

� Covered in Chapter 4:

� Data definition commands for creating tables

� Commands for constraint specification

� Simple retrieval queries

� Database update commands