Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
191 views

Chapter4-Introduction To SQL

This document provides an overview of structured query language (SQL) for database systems. It discusses the basic commands and functions of SQL for data administration and manipulation. These include creating tables, indexes and views, as well as inserting, modifying, deleting and retrieving data. The key SQL commands covered are SELECT, INSERT, UPDATE, DELETE, COMMIT and ROLLBACK. The document also discusses SQL data types, constraints, indexes and advanced query techniques.

Uploaded by

tinishdharan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

Chapter4-Introduction To SQL

This document provides an overview of structured query language (SQL) for database systems. It discusses the basic commands and functions of SQL for data administration and manipulation. These include creating tables, indexes and views, as well as inserting, modifying, deleting and retrieving data. The key SQL commands covered are SELECT, INSERT, UPDATE, DELETE, COMMIT and ROLLBACK. The document also discusses SQL data types, constraints, indexes and advanced query techniques.

Uploaded by

tinishdharan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Database Systems: Design,

Implementation, and
Management
Eighth Edition

Structured Query Language (SQL)


Objectives

• In this chapter, you will learn:


– The basic commands and functions of SQL
– How to use SQL for data administration (to
create tables, indexes, and views)
– How to use SQL for data manipulation (to add,
modify, delete, and retrieve data)
– How to use SQL to query a database for useful
information

Database Systems, 8th Edition 2


Introduction to SQL

• SQL functions fit into two broad categories:


– Data definition language
– Data manipulation language
• Basic command set has vocabulary of less than
100 words
• American National Standards Institute (ANSI)
prescribes a standard SQL
• Several SQL dialects exist

Database Systems, 8th Edition 3


Data Definition Commands

• The database model


– In this chapter, a simple database with these
tables is used to illustrate commands:
• CUSTOMER
• INVOICE
• LINE
• PRODUCT
• VENDOR
– Focus on PRODUCT and VENDOR tables

Database Systems, 8th Edition 4


Database Systems, 8th Edition 5
Creating the Database

• Two tasks must be completed:


– Create database structure
– Create tables that will hold end-user data
• First task:
– RDBMS creates physical files that will hold
database
– Differs substantially from one RDBMS to another

Database Systems, 8th Edition 6


The Database Schema

• Authentication
– DBMS verifies that only registered users are
able to access database
– Log on to RDBMS using user ID and password
created by database administrator
• Schema
– Group of database objects that are related to
each other

Database Systems, 8th Edition 7


Data Types

• Data type selection is usually dictated by nature


of data and by intended use
• Supported data types:
– Number(L,D), Integer, Smallint, Decimal(L,D)
– Char(L), Varchar(L), Varchar2(L)
– Date, Time, Timestamp
– Real, Double, Float
– Interval day to hour
– Many other types
Database Systems, 8th Edition 8
Creating Table Structures

• Use one line per column (attribute) definition


• Use spaces to line up attribute characteristics
and constraints
• Table and attribute names are capitalized
• NOT NULL specification
• UNIQUE specification

Database Systems, 8th Edition 9


Creating Table Structures (continued)

• Primary key attributes contain both a NOT


NULL and a UNIQUE specification
• RDBMS will automatically enforce referential
integrity for foreign keys
• Command sequence ends with semicolon

Database Systems, 8th Edition 10


SQL Constraints

• NOT NULL constraint


– Ensures that column does not accept nulls
• UNIQUE constraint
– Ensures that all values in column are unique
• DEFAULT constraint
– Assigns value to attribute when a new row is
added to table
• CHECK constraint
– Validates data when attribute value is entered
Database Systems, 8th Edition 11
SQL Indexes

• When primary key is declared, DBMS


automatically creates unique index
• Often need additional indexes
• Using CREATE INDEX command, SQL indexes
can be created on basis of any selected
attribute
• Composite index
– Index based on two or more attributes
– Often used to prevent data duplication

Database Systems, 8th Edition 12


Data Manipulation Commands

• INSERT
• SELECT
• COMMIT
• UPDATE
• ROLLBACK
• DELETE

Database Systems, 8th Edition 13


Adding Table Rows

• INSERT
– Used to enter data into table
– Syntax:
• INSERT INTO columnname
VALUES (value1, value2, … , valueN);

Database Systems, 8th Edition 14


Adding Table Rows (continued)

• When entering values, notice that:


– Row contents are entered between parentheses
– Character and date values are entered between
apostrophes
– Numerical entries are not enclosed in
apostrophes
– Attribute entries are separated by commas
– A value is required for each column
• Use NULL for unknown values

Database Systems, 8th Edition 15


Saving Table Changes

• Changes made to table contents are not


physically saved on disk until:
– Database is closed
– Program is closed
– COMMIT command is used
• Syntax:
– COMMIT [WORK];
• Will permanently save any changes made to
any table in the database
Database Systems, 8th Edition 16
Listing Table Rows

• SELECT
– Used to list contents of table
– Syntax:
• SELECT columnlist
• FROM tablename;
• Columnlist represents one or more attributes,
separated by commas
• Asterisk can be used as wildcard character to
list all attributes

Database Systems, 8th Edition 17


Updating Table Rows

• UPDATE
– Modify data in a table
– Syntax:
UPDATE tablename
SET columnname = expression [, columnname =
expression]
[WHERE conditionlist];
• If more than one attribute is to be updated in
row, separate corrections with commas

Database Systems, 8th Edition 18


Restoring Table Contents

• ROLLBACK
– Undoes changes since last COMMIT
– Brings data back to pre-change values
• Syntax:
– ROLLBACK;
• COMMIT and ROLLBACK only work with
commands to add, modify, or delete table rows

Database Systems, 8th Edition 19


Deleting Table Rows

• DELETE
– Deletes a table row
– Syntax:
DELETE FROM tablename
[WHERE conditionlist ];
• WHERE condition is optional
• If WHERE condition is not specified, all rows
from specified table will be deleted

Database Systems, 8th Edition 20


Inserting Table Rows with a
SELECT Subquery
• INSERT
– Inserts multiple rows from another table (source)
– Uses SELECT subquery
– Subquery: query embedded (or nested) inside
another query
– Subquery executed first
– Syntax:
INSERT INTO tablename SELECT columnlist
FROM tablename;

Database Systems, 8th Edition 21


SELECT Queries

• Fine-tune SELECT command by adding


restrictions to search criteria using:
– Conditional restrictions
– Arithmetic operators
– Logical operators
– Special operators

Database Systems, 8th Edition 22


Selecting Rows with
Conditional Restrictions
• Select partial table contents by placing
restrictions on rows to be included in output
– Add conditional restrictions to SELECT
statement, using WHERE clause
• Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;

Database Systems, 8th Edition 23


Deleting a Table from the Database

• DROP
– Deletes table from database
– Syntax:
DROP TABLE tablename;
• Can drop a table only if it is not the “one” side
of any relationship
– Otherwise RDBMS generates an error message
– Foreign key integrity violation

Database Systems, 8th Edition 24


Advanced SELECT Queries

• Logical operators work well in the query


environment
• SQL provides useful functions that:
– Count
– Find minimum and maximum values
– Calculate averages, etc.
• SQL allows user to limit queries to:
– Entries having no duplicates
– Entries whose duplicates may be grouped
Database Systems, 8th Edition 25

You might also like