SlideShare a Scribd company logo
SQL/200 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL200 Module 3 Based on  SQL Clearly Explained  by Jan Harrington
Note on SQL200 Slides These slides were originally designed to support the single SQL200 course which was used for any of MS Access, Oracle and SQL Server. As such you may see here slides developed in any one of the above products. We are in the process of migrating the Oracle slides and the MS Access slides out into their own slide sets.  These SQL200 slides (used in SQL202 as well as SQL200) will focus on Microsoft SQL Server. Bookstore SQL200 Module 3
Warning! Below are some table name changes to be aware of in doing queries. We have created synonyms so either name should work. Bookstore2 SQL200  Module 2 New Name Old Name Orders Order_filled Order_Lines Orderlines
SQL200 Contact Information Bookstore SQL200 Module 3 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address]   Copyright 2001-2011. All rights reserved.
SQL200 Resources Bookstore database scripts found on box.net at http://tinyurl.com/SQLScripts   Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases   Follow up questions? [email_address]   Bookstore SQL212  Module 1
SQL200 Module 3 Part 1 – Modifying Data Part 2 – Managing Database Structures Part 3 – Creating Views and Indexes Part 4 -- Security Bookstore SQL200 Module 3
SQL/200 SQL Programming Part 1 – Modifying Data Bookstore SQL200 Module 3
Bookstore SQL200 Module 3 Relational Database with constraints (from text)
Data Modification Statements Insert Update Delete Bookstore SQL200 Module 3
Data Modification Statements End-user rarely sees these statements Application developer prepares these statements “behind the scenes” based on forms filled out by user Bookstore SQL200 Module 3
Insert Adds new rows to an existing table Two forms: Single Row Multi-Row Bookstore SQL200 Module 3
Single Row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Values (<value-list>)
Single Row Insert Bookstore SQL200 Module 3 Basic Example: insert into  sources(source_numb, source_name, source_street) values (22,'Specialty Books', 'Canal Street')
Insert Statement Bookstore SQL200 Module 3
Sources table after Insert Bookstore SQL200 Module 3
Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement> We will do this after creating a new table later in this module
Update Updates fields in an existing row Bookstore SQL200 Module 3 Basic Syntax: Update <table-name> Set <field1> = new value, <field2> = new value,… Where <selection-criteria>
Update Increase Ingram prices by 10% Bookstore SQL200 Module 3 Example: Update books Set retail_price = retail_price *1.10 Where source_numb = 1
Ingram Book Prices before Update Bookstore SQL200 Module 3
Ingram Book Prices after Update Bookstore SQL200 Module 3
Bookstore SQL200 Module 3 After update in MS Access
Delete Deletes one or more rows Bookstore SQL200 Module 3 Basic Syntax: Delete from <table-name> Where <selection-criteria>
Delete Bookstore SQL200 Module 3 Example: Delete from sources Where source_numb = 22
Delete Bookstore SQL200 Module 3
Sources table after Delete Bookstore SQL200 Module 3
Delete and Referential Integrity Can affect referential integrity when deleting a “parent” row Can do following with child… Cascade: delete the child row Set null: set the child’s foreign key null Set default: as above but to default value No action: don’t allow delete of parent row Referential integrity can be established when creating or modifying table structures which we will look at later in the class Bookstore SQL200 Module 3
SQL/200 SQL Programming Part 2– Managing Database Structures Bookstore SQL200 Module 3
DDL Create Alter Drop Bookstore SQL200 Module 3
Schemas Logical view of a database; sort of a “sub-database” – we will not cover these in this module or… Catalogs Clusters Domains (somewhat like a user defined datatype) These topics are highly dependent upon the vendor DBMS and installation practices Bookstore SQL200 Module 3
Tables Base tables Temporary tables  Local (or module scope) Global (session scope) Bookstore SQL200 Module 3
Creating Tables Use create statement Specify: Columns with data types and column constraints Table constraints Foreign key references Primary key designation Bookstore SQL200 Module 3
Data Types Int – integers or whole numbers Ex: how_many int Char – fixed length fields Ex: state char(2) Varchar/Varchar2 – variable length fields Ex: address varchar(35) Money – money field; same as MS Access currency Date/Datetime – date and time  And many others – see documentation or Help Bookstore SQL200 Module 3
Create Table Bookstore SQL200 Module 3 Basic syntax: Create table <table-name> <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> Note: often preceded by a drop
Temporary Tables Bookstore SQL200 Module 3 Basic syntax (SQL standard): Create [global] temporary table <table-name> <rest of statement as for normal create> Note: SQL Server uses a different syntax. Just put a #in front of the table name as in #mytable.
Column Constraints Primary key Not NULL CHECK clause Default Unique Bookstore SQL200 Module 3
Table Constraints Primary Key Foreign Key Compare fields against each other. I.e. ship_date >= order_date Bookstore SQL200 Module 3
But first – the Drop Statement Deletes a database “object” Drop table <table-name> Drop view <view-name> Drop index <index-name> Drop domain <domain-name> Bookstore SQL200 Module 3
Create Table Bookstore SQL200 Module 3 Example 1: Create  a summary table Create  table summary( isbn varchar(20)  primary key , How_many int  check  (how_many >= 0), Constraint  isbn_fk Foreign key  (isbn)  references  books(isbn) )
Create Summary Table Bookstore SQL200 Module 3
Constraints on Summary Table Bookstore SQL200 Module 3
Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> [(<column list>)] Select <select-statement>
Multi-row Insert Bookstore SQL200 Module 3 Basic Example:  (store # times each book ordered) Insert into summary Select isbn, count(*) From orderlines Group by isbn;
Multi-row Insert Bookstore SQL200 Module 3
Bookstore SQL200 Module 3 After multi-row insert in MS Access
SQL/200 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL200 Module 3
Views Think of a view as a named query wherein the definition is stored in the database Can be read like a table Some are updateable Bookstore SQL200 Module 3
Views Bookstore SQL200 Module 3 Basic syntax: Create view <view-name> (<column-list>) As <select statement>
Creating a View Bookstore SQL200 Module 3
Using Views Can be used like a table subject to various limitations Cannot insert into grouped queries, etc. Etc. Sample syntax: select  column-list from employee_view Bookstore SQL200 Module 3
Using a View Bookstore SQL200 Module 3
Indexes Used  to speed searches, joins, etc. Placed on: primary and foreign keys Secondary keys In commercial practice often managed by DBA’s for large databases Bookstore SQL200 Module 3
Indexes Bookstore SQL200 Module 3 Basic syntax: Create [unique] index <index-name>  On <table-name> (field-name> [desc]) Note: can place index on a composite key;  ex: state and city
Indexes Bookstore SQL200 Module 3 Basic example: create index state_inx on customers(customer_state)
Customers table with index Bookstore SQL200 Module 3
Dropping an index Basic Syntax: Drop index <table-name.index-name>; Bookstore SQL200 Module 3
Modifying a Table Design Applies to tables Use ALTER statement Add columns Delete columns Rename columns Add column constraints Add table constraints Bookstore SQL200 Module 3
Modifying a Table Design Bookstore SQL200 Module 3 Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Modify <field-name> Etc.
Modify a Table Design Bookstore SQL200 Module 3 Example: add a phone number field alter table publishers add phone char(12);
Bookstore SQL200 Module 3 After alter publishers table
SQL/200 SQL Programming Part 4 – Security Bookstore SQL200 Module 3
Security Important DBA function Beyond scope of this course Typically controlled through Enterprise Manager or Studio GUI’s In commercial practice application security frequently controlled via own login and a “users” table or similar Bookstore SQL200 Module 3
Security Specifics can vary by product Access: workgroup administrator SQL Server: users, roles Oracle: users, roles Bookstore SQL200 Module 3
SQL Security Statements Grant Revoke Deny Bookstore SQL200 Module 3
Grant Bookstore SQL200 Module 3 Syntax: Grant <access-right> [with grant option] On <object> to <user>   Note: by default only tables owners and admins can access a table. Others must be granted the relevant rights.
Access Rights Select Update Insert Delete References All privileges Bookstore SQL200 Module 3
Grant Bookstore SQL200 Module 3 Example: Grant update On employees to ddurso
Revoke Revokes the rights Syntax similar to grant Bookstore SQL200 Module 3 [end module]
Notes Bookstore SQL200 Module 3

More Related Content

What's hot (20)

PDF
Sql
Atul Pant
 
PPTX
Creating database using sql commands
Belle Wx
 
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPTX
Getting Started with MySQL II
Sankhya_Analytics
 
DOCX
Dbms practical list
RajSingh734307
 
PPTX
Xml and databases
Raghu nath
 
DOCX
Sq lite
Revuru Bharadwaja
 
PPT
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
PDF
View & index in SQL
Swapnali Pawar
 
PPTX
SQL Server Learning Drive
TechandMate
 
PPTX
DDL(Data defination Language ) Using Oracle
Farhan Aslam
 
PPT
MySQL lecture
webhostingguy
 
PPS
06 qmds2005 session08
Niit Care
 
PPTX
Sql basics
Genesis Omo
 
PPT
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
PDF
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp
 
PPT
Introduction to SQL
Tayyab Hussain
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
Creating database using sql commands
Belle Wx
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Getting Started with MySQL II
Sankhya_Analytics
 
Dbms practical list
RajSingh734307
 
Xml and databases
Raghu nath
 
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
View & index in SQL
Swapnali Pawar
 
SQL Server Learning Drive
TechandMate
 
DDL(Data defination Language ) Using Oracle
Farhan Aslam
 
MySQL lecture
webhostingguy
 
06 qmds2005 session08
Niit Care
 
Sql basics
Genesis Omo
 
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp
 
Introduction to SQL
Tayyab Hussain
 
SQL Tutorial - Basic Commands
1keydata
 

Viewers also liked (20)

PPT
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
Dan D'Urso
 
PPT
SQL200.2 Module 2
Dan D'Urso
 
PPTX
MS SQL SERVER: Creating Views
sqlserver content
 
PDF
Sql db optimization
Nikhildas P C
 
PDF
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
PPTX
Consultas en MS SQL Server 2012
CarlosFloresRoman
 
PPS
Sql xp 04
Niit Care
 
PPT
Sql tuning guideline
Sidney Chen
 
PPTX
Statistics
Riteshkiit
 
PPTX
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
PPT
e computer notes - Subqueries
ecomputernotes
 
PPSX
Locking in SQL Server
Prashant Gogoi
 
PPT
Review of SQL
Information Technology
 
RTF
Triggers-Sequences-SQL
Patrick Seery
 
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
PPT
Lecture 4. MS SQL. DML Triggers
Alexey Furmanov
 
PPTX
Sql query analyzer & maintenance
nspyrenet
 
PPT
Locking And Concurrency
sqlserver.co.il
 
PDF
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
Dan D'Urso
 
SQL200.2 Module 2
Dan D'Urso
 
MS SQL SERVER: Creating Views
sqlserver content
 
Sql db optimization
Nikhildas P C
 
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
Consultas en MS SQL Server 2012
CarlosFloresRoman
 
Sql xp 04
Niit Care
 
Sql tuning guideline
Sidney Chen
 
Statistics
Riteshkiit
 
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
e computer notes - Subqueries
ecomputernotes
 
Locking in SQL Server
Prashant Gogoi
 
Review of SQL
Information Technology
 
Triggers-Sequences-SQL
Patrick Seery
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
Lecture 4. MS SQL. DML Triggers
Alexey Furmanov
 
Sql query analyzer & maintenance
nspyrenet
 
Locking And Concurrency
sqlserver.co.il
 
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
Ad

Similar to SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3 (20)

PPT
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
Dan D'Urso
 
PPTX
SQL commands in database managemant systems
pmselvaraj
 
PPTX
introduction to SQL query language beginner.ppt
PatriceRochon1
 
PPTX
SQL_SERVER_BASIC_1_Training.pptx
KashifManzoorMeo
 
PPT
SQL302 Intermediate SQL Workshop 3
Dan D'Urso
 
PPTX
SQL_SERVER_BASIC_1_Training.pptx
QuyVo27
 
PDF
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
AbhishekKumarPandit5
 
PDF
Dms 22319 micro project
ARVIND SARDAR
 
PPTX
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
PPT
My sql with querys
NIRMAL FELIX
 
PPTX
PostgreSQL Database Slides
metsarin
 
PPTX
DBMSLab_SQL_4thsem_CI_17163544545446962.pptx
dgfs55437
 
PPTX
Introduction to Oracle Database.pptx
SiddhantBhardwaj26
 
DOC
Module 3
cs19club
 
PDF
SQL200A Microsoft Access SQL Design
Dan D'Urso
 
PPT
mysqlHiep.ppt
webhostingguy
 
PPT
MySQL Database System Hiep Dinh
webhostingguy
 
PDF
Sql views, stored procedure, functions
Om Vikram Thapa
 
PPT
Introduction to Threading in .Net
webhostingguy
 
DOCX
Android sq lite-chapter 22
Dr. Ramkumar Lakshminarayanan
 
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
Dan D'Urso
 
SQL commands in database managemant systems
pmselvaraj
 
introduction to SQL query language beginner.ppt
PatriceRochon1
 
SQL_SERVER_BASIC_1_Training.pptx
KashifManzoorMeo
 
SQL302 Intermediate SQL Workshop 3
Dan D'Urso
 
SQL_SERVER_BASIC_1_Training.pptx
QuyVo27
 
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
AbhishekKumarPandit5
 
Dms 22319 micro project
ARVIND SARDAR
 
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
My sql with querys
NIRMAL FELIX
 
PostgreSQL Database Slides
metsarin
 
DBMSLab_SQL_4thsem_CI_17163544545446962.pptx
dgfs55437
 
Introduction to Oracle Database.pptx
SiddhantBhardwaj26
 
Module 3
cs19club
 
SQL200A Microsoft Access SQL Design
Dan D'Urso
 
mysqlHiep.ppt
webhostingguy
 
MySQL Database System Hiep Dinh
webhostingguy
 
Sql views, stored procedure, functions
Om Vikram Thapa
 
Introduction to Threading in .Net
webhostingguy
 
Android sq lite-chapter 22
Dr. Ramkumar Lakshminarayanan
 
Ad

More from Dan D'Urso (20)

PPT
SQL201S Accelerated Introduction to MySQL Queries
Dan D'Urso
 
PPT
LCD201d Database Diagramming with Lucidchart
Dan D'Urso
 
PPTX
Database Normalization
Dan D'Urso
 
PPT
VIS201d Visio Database Diagramming
Dan D'Urso
 
PPT
PRJ101a Project 2013 Accelerated
Dan D'Urso
 
PPT
PRJ101xl Project Libre Basic Training
Dan D'Urso
 
PPT
Introduction to coding using Python
Dan D'Urso
 
PPTX
Stem conference
Dan D'Urso
 
PPTX
Microsoft access self joins
Dan D'Urso
 
PDF
SQL302 Intermediate SQL
Dan D'Urso
 
PDF
AIN106 Access Reporting and Analysis
Dan D'Urso
 
PPT
SQL302 Intermediate SQL Workshop 2
Dan D'Urso
 
PDF
Course Catalog
Dan D'Urso
 
PPT
SQL302 Intermediate SQL Workshop 1
Dan D'Urso
 
PDF
SQL212 Oracle SQL Manual
Dan D'Urso
 
PDF
SQL201W MySQL SQL Manual
Dan D'Urso
 
PDF
AIN100
Dan D'Urso
 
PPT
SQL206 SQL Median
Dan D'Urso
 
PDF
SQL202 SQL Server SQL Manual
Dan D'Urso
 
PDF
AIN102 Microsoft Access Queries
Dan D'Urso
 
SQL201S Accelerated Introduction to MySQL Queries
Dan D'Urso
 
LCD201d Database Diagramming with Lucidchart
Dan D'Urso
 
Database Normalization
Dan D'Urso
 
VIS201d Visio Database Diagramming
Dan D'Urso
 
PRJ101a Project 2013 Accelerated
Dan D'Urso
 
PRJ101xl Project Libre Basic Training
Dan D'Urso
 
Introduction to coding using Python
Dan D'Urso
 
Stem conference
Dan D'Urso
 
Microsoft access self joins
Dan D'Urso
 
SQL302 Intermediate SQL
Dan D'Urso
 
AIN106 Access Reporting and Analysis
Dan D'Urso
 
SQL302 Intermediate SQL Workshop 2
Dan D'Urso
 
Course Catalog
Dan D'Urso
 
SQL302 Intermediate SQL Workshop 1
Dan D'Urso
 
SQL212 Oracle SQL Manual
Dan D'Urso
 
SQL201W MySQL SQL Manual
Dan D'Urso
 
AIN100
Dan D'Urso
 
SQL206 SQL Median
Dan D'Urso
 
SQL202 SQL Server SQL Manual
Dan D'Urso
 
AIN102 Microsoft Access Queries
Dan D'Urso
 

Recently uploaded (20)

PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Next level data operations using Power Automate magic
Andries den Haan
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Kubernetes - Architecture & Components.pdf
geethak285
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 

SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3

  • 1. SQL/200 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL200 Module 3 Based on SQL Clearly Explained by Jan Harrington
  • 2. Note on SQL200 Slides These slides were originally designed to support the single SQL200 course which was used for any of MS Access, Oracle and SQL Server. As such you may see here slides developed in any one of the above products. We are in the process of migrating the Oracle slides and the MS Access slides out into their own slide sets. These SQL200 slides (used in SQL202 as well as SQL200) will focus on Microsoft SQL Server. Bookstore SQL200 Module 3
  • 3. Warning! Below are some table name changes to be aware of in doing queries. We have created synonyms so either name should work. Bookstore2 SQL200 Module 2 New Name Old Name Orders Order_filled Order_Lines Orderlines
  • 4. SQL200 Contact Information Bookstore SQL200 Module 3 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2011. All rights reserved.
  • 5. SQL200 Resources Bookstore database scripts found on box.net at http://tinyurl.com/SQLScripts Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases Follow up questions? [email_address] Bookstore SQL212 Module 1
  • 6. SQL200 Module 3 Part 1 – Modifying Data Part 2 – Managing Database Structures Part 3 – Creating Views and Indexes Part 4 -- Security Bookstore SQL200 Module 3
  • 7. SQL/200 SQL Programming Part 1 – Modifying Data Bookstore SQL200 Module 3
  • 8. Bookstore SQL200 Module 3 Relational Database with constraints (from text)
  • 9. Data Modification Statements Insert Update Delete Bookstore SQL200 Module 3
  • 10. Data Modification Statements End-user rarely sees these statements Application developer prepares these statements “behind the scenes” based on forms filled out by user Bookstore SQL200 Module 3
  • 11. Insert Adds new rows to an existing table Two forms: Single Row Multi-Row Bookstore SQL200 Module 3
  • 12. Single Row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Values (<value-list>)
  • 13. Single Row Insert Bookstore SQL200 Module 3 Basic Example: insert into sources(source_numb, source_name, source_street) values (22,'Specialty Books', 'Canal Street')
  • 14. Insert Statement Bookstore SQL200 Module 3
  • 15. Sources table after Insert Bookstore SQL200 Module 3
  • 16. Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement> We will do this after creating a new table later in this module
  • 17. Update Updates fields in an existing row Bookstore SQL200 Module 3 Basic Syntax: Update <table-name> Set <field1> = new value, <field2> = new value,… Where <selection-criteria>
  • 18. Update Increase Ingram prices by 10% Bookstore SQL200 Module 3 Example: Update books Set retail_price = retail_price *1.10 Where source_numb = 1
  • 19. Ingram Book Prices before Update Bookstore SQL200 Module 3
  • 20. Ingram Book Prices after Update Bookstore SQL200 Module 3
  • 21. Bookstore SQL200 Module 3 After update in MS Access
  • 22. Delete Deletes one or more rows Bookstore SQL200 Module 3 Basic Syntax: Delete from <table-name> Where <selection-criteria>
  • 23. Delete Bookstore SQL200 Module 3 Example: Delete from sources Where source_numb = 22
  • 25. Sources table after Delete Bookstore SQL200 Module 3
  • 26. Delete and Referential Integrity Can affect referential integrity when deleting a “parent” row Can do following with child… Cascade: delete the child row Set null: set the child’s foreign key null Set default: as above but to default value No action: don’t allow delete of parent row Referential integrity can be established when creating or modifying table structures which we will look at later in the class Bookstore SQL200 Module 3
  • 27. SQL/200 SQL Programming Part 2– Managing Database Structures Bookstore SQL200 Module 3
  • 28. DDL Create Alter Drop Bookstore SQL200 Module 3
  • 29. Schemas Logical view of a database; sort of a “sub-database” – we will not cover these in this module or… Catalogs Clusters Domains (somewhat like a user defined datatype) These topics are highly dependent upon the vendor DBMS and installation practices Bookstore SQL200 Module 3
  • 30. Tables Base tables Temporary tables Local (or module scope) Global (session scope) Bookstore SQL200 Module 3
  • 31. Creating Tables Use create statement Specify: Columns with data types and column constraints Table constraints Foreign key references Primary key designation Bookstore SQL200 Module 3
  • 32. Data Types Int – integers or whole numbers Ex: how_many int Char – fixed length fields Ex: state char(2) Varchar/Varchar2 – variable length fields Ex: address varchar(35) Money – money field; same as MS Access currency Date/Datetime – date and time And many others – see documentation or Help Bookstore SQL200 Module 3
  • 33. Create Table Bookstore SQL200 Module 3 Basic syntax: Create table <table-name> <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> Note: often preceded by a drop
  • 34. Temporary Tables Bookstore SQL200 Module 3 Basic syntax (SQL standard): Create [global] temporary table <table-name> <rest of statement as for normal create> Note: SQL Server uses a different syntax. Just put a #in front of the table name as in #mytable.
  • 35. Column Constraints Primary key Not NULL CHECK clause Default Unique Bookstore SQL200 Module 3
  • 36. Table Constraints Primary Key Foreign Key Compare fields against each other. I.e. ship_date >= order_date Bookstore SQL200 Module 3
  • 37. But first – the Drop Statement Deletes a database “object” Drop table <table-name> Drop view <view-name> Drop index <index-name> Drop domain <domain-name> Bookstore SQL200 Module 3
  • 38. Create Table Bookstore SQL200 Module 3 Example 1: Create a summary table Create table summary( isbn varchar(20) primary key , How_many int check (how_many >= 0), Constraint isbn_fk Foreign key (isbn) references books(isbn) )
  • 39. Create Summary Table Bookstore SQL200 Module 3
  • 40. Constraints on Summary Table Bookstore SQL200 Module 3
  • 41. Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> [(<column list>)] Select <select-statement>
  • 42. Multi-row Insert Bookstore SQL200 Module 3 Basic Example: (store # times each book ordered) Insert into summary Select isbn, count(*) From orderlines Group by isbn;
  • 43. Multi-row Insert Bookstore SQL200 Module 3
  • 44. Bookstore SQL200 Module 3 After multi-row insert in MS Access
  • 45. SQL/200 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL200 Module 3
  • 46. Views Think of a view as a named query wherein the definition is stored in the database Can be read like a table Some are updateable Bookstore SQL200 Module 3
  • 47. Views Bookstore SQL200 Module 3 Basic syntax: Create view <view-name> (<column-list>) As <select statement>
  • 48. Creating a View Bookstore SQL200 Module 3
  • 49. Using Views Can be used like a table subject to various limitations Cannot insert into grouped queries, etc. Etc. Sample syntax: select column-list from employee_view Bookstore SQL200 Module 3
  • 50. Using a View Bookstore SQL200 Module 3
  • 51. Indexes Used to speed searches, joins, etc. Placed on: primary and foreign keys Secondary keys In commercial practice often managed by DBA’s for large databases Bookstore SQL200 Module 3
  • 52. Indexes Bookstore SQL200 Module 3 Basic syntax: Create [unique] index <index-name> On <table-name> (field-name> [desc]) Note: can place index on a composite key; ex: state and city
  • 53. Indexes Bookstore SQL200 Module 3 Basic example: create index state_inx on customers(customer_state)
  • 54. Customers table with index Bookstore SQL200 Module 3
  • 55. Dropping an index Basic Syntax: Drop index <table-name.index-name>; Bookstore SQL200 Module 3
  • 56. Modifying a Table Design Applies to tables Use ALTER statement Add columns Delete columns Rename columns Add column constraints Add table constraints Bookstore SQL200 Module 3
  • 57. Modifying a Table Design Bookstore SQL200 Module 3 Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Modify <field-name> Etc.
  • 58. Modify a Table Design Bookstore SQL200 Module 3 Example: add a phone number field alter table publishers add phone char(12);
  • 59. Bookstore SQL200 Module 3 After alter publishers table
  • 60. SQL/200 SQL Programming Part 4 – Security Bookstore SQL200 Module 3
  • 61. Security Important DBA function Beyond scope of this course Typically controlled through Enterprise Manager or Studio GUI’s In commercial practice application security frequently controlled via own login and a “users” table or similar Bookstore SQL200 Module 3
  • 62. Security Specifics can vary by product Access: workgroup administrator SQL Server: users, roles Oracle: users, roles Bookstore SQL200 Module 3
  • 63. SQL Security Statements Grant Revoke Deny Bookstore SQL200 Module 3
  • 64. Grant Bookstore SQL200 Module 3 Syntax: Grant <access-right> [with grant option] On <object> to <user> Note: by default only tables owners and admins can access a table. Others must be granted the relevant rights.
  • 65. Access Rights Select Update Insert Delete References All privileges Bookstore SQL200 Module 3
  • 66. Grant Bookstore SQL200 Module 3 Example: Grant update On employees to ddurso
  • 67. Revoke Revokes the rights Syntax similar to grant Bookstore SQL200 Module 3 [end module]