SlideShare a Scribd company logo
1 of 44
Database Systems
Design, Implementation, and Management
Coronel | Morris
11e
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Chapter 7
Introduction to Structured Query
Language (SQL)
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Learning Objectives
 In this chapter, you will learn:
 The basic commands and functions of SQL
 How to use SQL for data administration (to create
tables and indexes)
 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
2
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Structured Query Language (SQL)
 Categories of SQL function
 Data definition language (DDL)
 Data manipulation language (DML)
 Nonprocedural language with basic command
vocabulary set of less than 100 words
 Differences in SQL dialects are minor
3
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Table 7.1 - SQL Data Definition Command
4
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Table 7.2 - SQL Data Manipulation
Commands
5
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 7.1 - The Database Model
6
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Tasks to be Completed Before Using a New
RDBMS
 Create database structure
 RDBMS creates physical files that will hold database
 Differs from one RDBMS to another
 Authentication: Process DBMS uses to verify that
only registered users access the data
 Required for the creation tables
 User should log on to RDBMS using user ID and
password created by database administrator
7
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The Database Schema
 Logical group of database objects related to each
other
 Command
 CREATE SCHEMAAUTHORIZATION {creator};
 Seldom used directly
8
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Common SQL Data Types
• NUMBER(L,D) or NUMERIC(L,D)
Numeric
• CHAR(L)
• VARCHAR(L) or VARCHAR2(L)
Character
• DATE
Date
9
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
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
 Features of table creating command sequence
 NOT NULL specification
 UNIQUE specification
 Syntax to create table
 CREATE TABLE tablename();
10
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Primary Key and Foreign Key
 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
 ANSI SQL allows use of following clauses to cover
CASCADE, SET NULL, or SET DEFAULT
 ON DELETE and ON UPDATE
11
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
SQL Constraints
12
• Ensures that column does not accept nulls
NOT NULL
• Ensures that all values in column are unique
UNIQUE
• Assigns value to attribute when a new row is added to table
DEFAULT
• Validates data when attribute value is entered
CHECK
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
SQL Indexes
 When primary key is declared, DBMS automatically
creates unique index
 Composite index:
 Is based on two or more attributes
 Prevents data duplication
 Syntax to create SQL indexes
 CREATE INDEX indexname ON tablename();
 Syntax to delete an index
 DROP INDEX indexname;
13
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Data Manipulation Commands
14
INSERT: Command to insert data into table
• Syntax - INSERT INTO tablename VALUES();
• Used to add table rows with NULL and NOT NULL
attributes
COMMIT: Command to save changes
• Syntax - COMMIT [WORK];
• Ensures database update integrity
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Data Manipulation Commands
SELECT: Command to list the contents
• Syntax - SELECT columnlist FROM tablename;
• Wildcard character(*): Substitute for other
characters/command
UPDATE: Command to modify data
• Syntax - UPDATE tablename SET columnname =
expression [, columnname = expression] [WHERE
conditionlist];
15
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Data Manipulation Commands
WHERE condition
• Specifies the rows to be selected
ROLLBACK: Command to restore the database
• Syntax - ROLLBACK;
• Undoes the changes since last COMMIT
command
DELETE: Command to delete
• Syntax - DELETE FROM tablename
• [WHERE conditionlist];
16
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Inserting Table Rows with a
SELECT Subquery
 Syntax
 INSERT INTO tablename SELECT columnlist FROM
tablename
 Used to add multiple rows using another table as
source
 SELECT command - Acts as a subquery and is
executed first
 Subquery: Query embedded/nested inside another
query
17
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Selecting Rows Using Conditional
Restrictions
 Following syntax enables to specify which rows to
select
 SELECT columnlist
 FROM tablelist
 [WHERE conditionlist];
 Used to select partial table contents by placing
restrictions on the rows
 Optional WHERE clause
 Adds conditional restrictions to the SELECT statement
18
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Comparison Operators
 Add conditional restrictions on selected table
contents
 Used on:
 Character attributes
 Dates
19
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Table 7.6 - Comparison Operators
20
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Comparison Operators: Computed
Columns and Column Aliases
 SQL accepts any valid expressions/formulas in the
computed columns
 Alias: Alternate name given to a column or table in
any SQL statement to improve the readability
 Computed column, an alias, and date arithmetic can
be used in a single query
21
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Arithmetic operators
 The Rule of Precedence: Establish the order in
which computations are completed
 Perform:
 Operations within parentheses
 Power operations
 Multiplications and divisions
 Additions and subtractions
22
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Table 7.7 - The Arithmetic Operators
23
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 7.12 - Selected PRODUCT Table
Attributes: The logical OR
24
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 7.13 - Selected PRODUCT Table
Attributes: The Logical AND
25
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 7.14 - Selected PRODUCT Table
Attributes: The Logical AND and OR
26
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Special Operators
27
BETWEEN
• Checks whether attribute value is within a range
IS NULL
• Checks whether attribute value is null
LIKE
• Checks whether attribute value matches given string pattern
IN
• Checks whether attribute value matches any value within a value list
EXISTS
• Checks if subquery returns any rows
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Advanced Data Definition Commands
 ALTER TABLE command: To make changes in the
table structure
 Keywords use with the command
 ADD - Adds a column
 MODIFY - Changes column characteristics
 DROP - Deletes a column
 Used to:
 Add table constraints
 Remove table constraints
28
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Changing Column’s Data Type
 ALTER can be used to change data type
 Some RDBMSs do not permit changes to data types
unless column is empty
 Syntax –
 ALTER TABLE tablename MODIFY
(columnname(datatype));
29
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Changing Column’s Data Characteristics
 Use ALTER to change data characteristics
 Changes in column’s characteristics are permitted if
changes do not alter the existing data type
 Syntax
 ALTER TABLE tablename MODIFY
(columnname(characterstic));
30
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Column, Dropping Column
 Adding a column
 Use ALTER and ADD
 Do not include the NOT NULL clause for new
column
 Dropping a column
 Use ALTER and DROP
 Some RDBMSs impose restrictions on the deletion of
an attribute
31
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Advanced Data Updates
 UPDATE command updates only data in existing
rows
 If a relationship is established between entries and
existing columns, the relationship can assign values
to appropriate slots
 Arithmetic operators are useful in data updates
 In Oracle, ROLLBACK command undoes changes
made by last two UPDATE statements
32
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Copying Parts of Tables
 SQL permits copying contents of selected table
columns
 Data need not be reentered manually into newly created
table(s)
 Table structure is created
 Rows are added to new table using rows from another
table
33
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Primary and Foreign Key
Designations
 ALTER TABLE command
 Followed by a keyword that produces the specific
change one wants to make
 Options include ADD, MODIFY, and DROP
 Syntax to add or modify columns
 ALTER TABLE tablename
 {ADD | MODIFY} ( columnname datatype [ {ADD |
MODIFY} columnname datatype] ) ;
 ALTER TABLE tablename
 ADD constraint [ ADD constraint ] ;
34
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Deleting a Table from the Database
• DROP TABLE: Deletes table from database
 Syntax - DROP TABLE tablename;
 Can drop a table only if it is not the one side of any
relationship
 RDBMS generates a foreign key integrity violation
error message if the table is dropped
35
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Additional SELECT Query Keywords
 Logical operators work well in the query
environment
 SQL provides useful functions that:
 Counts
 Find minimum and maximum values
 Calculate averages
 SQL allows user to limit queries to entries:
 Having no duplicates
 Whose duplicates may be grouped
36
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Ordering a Listing
 ORDER BY clause is useful when listing order is
important
 Syntax - SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[ORDER BY columnlist [ASC | DESC]];
 Cascading order sequence: Multilevel ordered
sequence
 Created by listing several attributes after the ORDER
BY clause
37
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Listing Unique Values
 DISTINCT clause: Produces list of values that are
unique
 Syntax - SELECT DISTINCT columnlist
FROM tablelist;
 Access places nulls at the top of the list
 Oracle places it at the bottom
 Placement of nulls does not affect list contents
38
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Table 7.8 - Some Basic SQL Aggerate
Functions
39
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Grouping Data
 Frequency distributions created by GROUP BY
clause within SELECT statement
 Syntax - SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC]];
40
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
HAVING Clause
 Extension of GROUP BY feature
 Applied to output of GROUP BY operation
 Used in conjunction with GROUP BY clause in
second SQL command set
 Similar to WHERE clause in SELECT statement
41
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Joining Database Tables
 Performed when data are retrieved from more than
one table at a time
 Equality comparison between foreign key and primary
key of related tables
 Tables are joined by listing tables in FROM clause of
SELECT statement
 DBMS creates Cartesian product of every table in the
FROM clause
42
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Joining Tables With an Alias
 Alias identifies the source table from which data are
taken
 Any legal table name can be used as alias
 Add alias after table name in FROM clause
 FROM tablename alias
43
©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Recursive Joins
 Recursive query: Table is joined to itself using alias
 Use aliases to differentiate the table from itself
44

More Related Content

Similar to Chapt7.ppt

Similar to Chapt7.ppt (20)

Sql9e ppt ch04
Sql9e ppt ch04 Sql9e ppt ch04
Sql9e ppt ch04
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara University
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Using MySQL in Automated Testing
Using MySQL in Automated TestingUsing MySQL in Automated Testing
Using MySQL in Automated Testing
 
SQL SELECT Statement
 SQL SELECT Statement SQL SELECT Statement
SQL SELECT Statement
 
Ebook11
Ebook11Ebook11
Ebook11
 
Lesson10
Lesson10Lesson10
Lesson10
 
Sql2
Sql2Sql2
Sql2
 
Sql9e ppt ch05
Sql9e ppt ch05 Sql9e ppt ch05
Sql9e ppt ch05
 
Marcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL WorkbenchMarcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL Workbench
 
Sql good practices
Sql good practicesSql good practices
Sql good practices
 
Admin Guiding Query Plans
Admin Guiding Query PlansAdmin Guiding Query Plans
Admin Guiding Query Plans
 
Sql views
Sql viewsSql views
Sql views
 
Sql9e ppt ch07
Sql9e ppt ch07 Sql9e ppt ch07
Sql9e ppt ch07
 
Oracle SQL Developer Reports
Oracle SQL Developer ReportsOracle SQL Developer Reports
Oracle SQL Developer Reports
 
Learn Oracle WebLogic Server 12c Administration
Learn Oracle WebLogic Server 12c AdministrationLearn Oracle WebLogic Server 12c Administration
Learn Oracle WebLogic Server 12c Administration
 
Data virtualization using polybase
Data virtualization using polybaseData virtualization using polybase
Data virtualization using polybase
 
Ebook7
Ebook7Ebook7
Ebook7
 
Sql interview question part 7
Sql interview question part 7Sql interview question part 7
Sql interview question part 7
 
Ohio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQLOhio Linux Fest -- MySQL's NoSQL
Ohio Linux Fest -- MySQL's NoSQL
 

More from ImXaib

ERD introduction in databases model.pptx
ERD introduction in databases model.pptxERD introduction in databases model.pptx
ERD introduction in databases model.pptxImXaib
 
SDA presentation the basics of computer science .pptx
SDA presentation the basics of computer science .pptxSDA presentation the basics of computer science .pptx
SDA presentation the basics of computer science .pptxImXaib
 
terminal a clear presentation on the topic.pptx
terminal a clear presentation on the topic.pptxterminal a clear presentation on the topic.pptx
terminal a clear presentation on the topic.pptxImXaib
 
What is Machine Learning_updated documents.pptx
What is Machine Learning_updated documents.pptxWhat is Machine Learning_updated documents.pptx
What is Machine Learning_updated documents.pptxImXaib
 
Grid Computing and it's applications.PPTX
Grid Computing and it's applications.PPTXGrid Computing and it's applications.PPTX
Grid Computing and it's applications.PPTXImXaib
 
Firewall.pdf
Firewall.pdfFirewall.pdf
Firewall.pdfImXaib
 
4966709.ppt
4966709.ppt4966709.ppt
4966709.pptImXaib
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.pptImXaib
 
Tools.pptx
Tools.pptxTools.pptx
Tools.pptxImXaib
 
lec3_10.ppt
lec3_10.pptlec3_10.ppt
lec3_10.pptImXaib
 
ch12.ppt
ch12.pptch12.ppt
ch12.pptImXaib
 
Fullandparavirtualization.ppt
Fullandparavirtualization.pptFullandparavirtualization.ppt
Fullandparavirtualization.pptImXaib
 
mis9_ch08_ppt.ppt
mis9_ch08_ppt.pptmis9_ch08_ppt.ppt
mis9_ch08_ppt.pptImXaib
 
rooster-ipsecindepth.ppt
rooster-ipsecindepth.pptrooster-ipsecindepth.ppt
rooster-ipsecindepth.pptImXaib
 
Policy formation and enforcement.ppt
Policy formation and enforcement.pptPolicy formation and enforcement.ppt
Policy formation and enforcement.pptImXaib
 
Database schema architecture.ppt
Database schema architecture.pptDatabase schema architecture.ppt
Database schema architecture.pptImXaib
 
Transport layer security.ppt
Transport layer security.pptTransport layer security.ppt
Transport layer security.pptImXaib
 
Trends in DM.pptx
Trends in DM.pptxTrends in DM.pptx
Trends in DM.pptxImXaib
 
AleksandrDoroninSlides.ppt
AleksandrDoroninSlides.pptAleksandrDoroninSlides.ppt
AleksandrDoroninSlides.pptImXaib
 
dm15-visualization-data-mining.ppt
dm15-visualization-data-mining.pptdm15-visualization-data-mining.ppt
dm15-visualization-data-mining.pptImXaib
 

More from ImXaib (20)

ERD introduction in databases model.pptx
ERD introduction in databases model.pptxERD introduction in databases model.pptx
ERD introduction in databases model.pptx
 
SDA presentation the basics of computer science .pptx
SDA presentation the basics of computer science .pptxSDA presentation the basics of computer science .pptx
SDA presentation the basics of computer science .pptx
 
terminal a clear presentation on the topic.pptx
terminal a clear presentation on the topic.pptxterminal a clear presentation on the topic.pptx
terminal a clear presentation on the topic.pptx
 
What is Machine Learning_updated documents.pptx
What is Machine Learning_updated documents.pptxWhat is Machine Learning_updated documents.pptx
What is Machine Learning_updated documents.pptx
 
Grid Computing and it's applications.PPTX
Grid Computing and it's applications.PPTXGrid Computing and it's applications.PPTX
Grid Computing and it's applications.PPTX
 
Firewall.pdf
Firewall.pdfFirewall.pdf
Firewall.pdf
 
4966709.ppt
4966709.ppt4966709.ppt
4966709.ppt
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Tools.pptx
Tools.pptxTools.pptx
Tools.pptx
 
lec3_10.ppt
lec3_10.pptlec3_10.ppt
lec3_10.ppt
 
ch12.ppt
ch12.pptch12.ppt
ch12.ppt
 
Fullandparavirtualization.ppt
Fullandparavirtualization.pptFullandparavirtualization.ppt
Fullandparavirtualization.ppt
 
mis9_ch08_ppt.ppt
mis9_ch08_ppt.pptmis9_ch08_ppt.ppt
mis9_ch08_ppt.ppt
 
rooster-ipsecindepth.ppt
rooster-ipsecindepth.pptrooster-ipsecindepth.ppt
rooster-ipsecindepth.ppt
 
Policy formation and enforcement.ppt
Policy formation and enforcement.pptPolicy formation and enforcement.ppt
Policy formation and enforcement.ppt
 
Database schema architecture.ppt
Database schema architecture.pptDatabase schema architecture.ppt
Database schema architecture.ppt
 
Transport layer security.ppt
Transport layer security.pptTransport layer security.ppt
Transport layer security.ppt
 
Trends in DM.pptx
Trends in DM.pptxTrends in DM.pptx
Trends in DM.pptx
 
AleksandrDoroninSlides.ppt
AleksandrDoroninSlides.pptAleksandrDoroninSlides.ppt
AleksandrDoroninSlides.ppt
 
dm15-visualization-data-mining.ppt
dm15-visualization-data-mining.pptdm15-visualization-data-mining.ppt
dm15-visualization-data-mining.ppt
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Chapt7.ppt

  • 1. Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Chapter 7 Introduction to Structured Query Language (SQL)
  • 2. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Learning Objectives  In this chapter, you will learn:  The basic commands and functions of SQL  How to use SQL for data administration (to create tables and indexes)  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 2
  • 3. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Structured Query Language (SQL)  Categories of SQL function  Data definition language (DDL)  Data manipulation language (DML)  Nonprocedural language with basic command vocabulary set of less than 100 words  Differences in SQL dialects are minor 3
  • 4. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table 7.1 - SQL Data Definition Command 4
  • 5. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table 7.2 - SQL Data Manipulation Commands 5
  • 6. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure 7.1 - The Database Model 6
  • 7. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Tasks to be Completed Before Using a New RDBMS  Create database structure  RDBMS creates physical files that will hold database  Differs from one RDBMS to another  Authentication: Process DBMS uses to verify that only registered users access the data  Required for the creation tables  User should log on to RDBMS using user ID and password created by database administrator 7
  • 8. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Database Schema  Logical group of database objects related to each other  Command  CREATE SCHEMAAUTHORIZATION {creator};  Seldom used directly 8
  • 9. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Common SQL Data Types • NUMBER(L,D) or NUMERIC(L,D) Numeric • CHAR(L) • VARCHAR(L) or VARCHAR2(L) Character • DATE Date 9
  • 10. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 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  Features of table creating command sequence  NOT NULL specification  UNIQUE specification  Syntax to create table  CREATE TABLE tablename(); 10
  • 11. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Primary Key and Foreign Key  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  ANSI SQL allows use of following clauses to cover CASCADE, SET NULL, or SET DEFAULT  ON DELETE and ON UPDATE 11
  • 12. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. SQL Constraints 12 • Ensures that column does not accept nulls NOT NULL • Ensures that all values in column are unique UNIQUE • Assigns value to attribute when a new row is added to table DEFAULT • Validates data when attribute value is entered CHECK
  • 13. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. SQL Indexes  When primary key is declared, DBMS automatically creates unique index  Composite index:  Is based on two or more attributes  Prevents data duplication  Syntax to create SQL indexes  CREATE INDEX indexname ON tablename();  Syntax to delete an index  DROP INDEX indexname; 13
  • 14. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Data Manipulation Commands 14 INSERT: Command to insert data into table • Syntax - INSERT INTO tablename VALUES(); • Used to add table rows with NULL and NOT NULL attributes COMMIT: Command to save changes • Syntax - COMMIT [WORK]; • Ensures database update integrity
  • 15. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Data Manipulation Commands SELECT: Command to list the contents • Syntax - SELECT columnlist FROM tablename; • Wildcard character(*): Substitute for other characters/command UPDATE: Command to modify data • Syntax - UPDATE tablename SET columnname = expression [, columnname = expression] [WHERE conditionlist]; 15
  • 16. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Data Manipulation Commands WHERE condition • Specifies the rows to be selected ROLLBACK: Command to restore the database • Syntax - ROLLBACK; • Undoes the changes since last COMMIT command DELETE: Command to delete • Syntax - DELETE FROM tablename • [WHERE conditionlist]; 16
  • 17. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Inserting Table Rows with a SELECT Subquery  Syntax  INSERT INTO tablename SELECT columnlist FROM tablename  Used to add multiple rows using another table as source  SELECT command - Acts as a subquery and is executed first  Subquery: Query embedded/nested inside another query 17
  • 18. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Selecting Rows Using Conditional Restrictions  Following syntax enables to specify which rows to select  SELECT columnlist  FROM tablelist  [WHERE conditionlist];  Used to select partial table contents by placing restrictions on the rows  Optional WHERE clause  Adds conditional restrictions to the SELECT statement 18
  • 19. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Comparison Operators  Add conditional restrictions on selected table contents  Used on:  Character attributes  Dates 19
  • 20. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table 7.6 - Comparison Operators 20
  • 21. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Comparison Operators: Computed Columns and Column Aliases  SQL accepts any valid expressions/formulas in the computed columns  Alias: Alternate name given to a column or table in any SQL statement to improve the readability  Computed column, an alias, and date arithmetic can be used in a single query 21
  • 22. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Arithmetic operators  The Rule of Precedence: Establish the order in which computations are completed  Perform:  Operations within parentheses  Power operations  Multiplications and divisions  Additions and subtractions 22
  • 23. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table 7.7 - The Arithmetic Operators 23
  • 24. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure 7.12 - Selected PRODUCT Table Attributes: The logical OR 24
  • 25. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure 7.13 - Selected PRODUCT Table Attributes: The Logical AND 25
  • 26. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure 7.14 - Selected PRODUCT Table Attributes: The Logical AND and OR 26
  • 27. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Special Operators 27 BETWEEN • Checks whether attribute value is within a range IS NULL • Checks whether attribute value is null LIKE • Checks whether attribute value matches given string pattern IN • Checks whether attribute value matches any value within a value list EXISTS • Checks if subquery returns any rows
  • 28. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Advanced Data Definition Commands  ALTER TABLE command: To make changes in the table structure  Keywords use with the command  ADD - Adds a column  MODIFY - Changes column characteristics  DROP - Deletes a column  Used to:  Add table constraints  Remove table constraints 28
  • 29. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Changing Column’s Data Type  ALTER can be used to change data type  Some RDBMSs do not permit changes to data types unless column is empty  Syntax –  ALTER TABLE tablename MODIFY (columnname(datatype)); 29
  • 30. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Changing Column’s Data Characteristics  Use ALTER to change data characteristics  Changes in column’s characteristics are permitted if changes do not alter the existing data type  Syntax  ALTER TABLE tablename MODIFY (columnname(characterstic)); 30
  • 31. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Adding Column, Dropping Column  Adding a column  Use ALTER and ADD  Do not include the NOT NULL clause for new column  Dropping a column  Use ALTER and DROP  Some RDBMSs impose restrictions on the deletion of an attribute 31
  • 32. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Advanced Data Updates  UPDATE command updates only data in existing rows  If a relationship is established between entries and existing columns, the relationship can assign values to appropriate slots  Arithmetic operators are useful in data updates  In Oracle, ROLLBACK command undoes changes made by last two UPDATE statements 32
  • 33. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Copying Parts of Tables  SQL permits copying contents of selected table columns  Data need not be reentered manually into newly created table(s)  Table structure is created  Rows are added to new table using rows from another table 33
  • 34. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Adding Primary and Foreign Key Designations  ALTER TABLE command  Followed by a keyword that produces the specific change one wants to make  Options include ADD, MODIFY, and DROP  Syntax to add or modify columns  ALTER TABLE tablename  {ADD | MODIFY} ( columnname datatype [ {ADD | MODIFY} columnname datatype] ) ;  ALTER TABLE tablename  ADD constraint [ ADD constraint ] ; 34
  • 35. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Deleting a Table from the Database • DROP TABLE: Deletes table from database  Syntax - DROP TABLE tablename;  Can drop a table only if it is not the one side of any relationship  RDBMS generates a foreign key integrity violation error message if the table is dropped 35
  • 36. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Additional SELECT Query Keywords  Logical operators work well in the query environment  SQL provides useful functions that:  Counts  Find minimum and maximum values  Calculate averages  SQL allows user to limit queries to entries:  Having no duplicates  Whose duplicates may be grouped 36
  • 37. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Ordering a Listing  ORDER BY clause is useful when listing order is important  Syntax - SELECT columnlist FROM tablelist [WHERE conditionlist] [ORDER BY columnlist [ASC | DESC]];  Cascading order sequence: Multilevel ordered sequence  Created by listing several attributes after the ORDER BY clause 37
  • 38. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Listing Unique Values  DISTINCT clause: Produces list of values that are unique  Syntax - SELECT DISTINCT columnlist FROM tablelist;  Access places nulls at the top of the list  Oracle places it at the bottom  Placement of nulls does not affect list contents 38
  • 39. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table 7.8 - Some Basic SQL Aggerate Functions 39
  • 40. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Grouping Data  Frequency distributions created by GROUP BY clause within SELECT statement  Syntax - SELECT columnlist FROM tablelist [WHERE conditionlist] [GROUP BY columnlist] [HAVING conditionlist] [ORDER BY columnlist [ASC | DESC]]; 40
  • 41. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. HAVING Clause  Extension of GROUP BY feature  Applied to output of GROUP BY operation  Used in conjunction with GROUP BY clause in second SQL command set  Similar to WHERE clause in SELECT statement 41
  • 42. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Joining Database Tables  Performed when data are retrieved from more than one table at a time  Equality comparison between foreign key and primary key of related tables  Tables are joined by listing tables in FROM clause of SELECT statement  DBMS creates Cartesian product of every table in the FROM clause 42
  • 43. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Joining Tables With an Alias  Alias identifies the source table from which data are taken  Any legal table name can be used as alias  Add alias after table name in FROM clause  FROM tablename alias 43
  • 44. ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Recursive Joins  Recursive query: Table is joined to itself using alias  Use aliases to differentiate the table from itself 44