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

PPT
SQL200.3 Module 3
PPT
SQL212.3 Introduction to SQL using Oracle Module 3
PDF
Access tips access and sql part 1 setting the sql scene
PPT
Sql presentation 1 by chandan
PPT
SQL212.1 Introduction to SQL using Oracle Module 1
DOC
Dbms lab Manual
PPT
MySql slides (ppt)
PDF
Sql ch 13 - sql-views
SQL200.3 Module 3
SQL212.3 Introduction to SQL using Oracle Module 3
Access tips access and sql part 1 setting the sql scene
Sql presentation 1 by chandan
SQL212.1 Introduction to SQL using Oracle Module 1
Dbms lab Manual
MySql slides (ppt)
Sql ch 13 - sql-views

What's hot (20)

PDF
PPTX
Creating database using sql commands
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPTX
Getting Started with MySQL II
DOCX
Dbms practical list
PPTX
Xml and databases
DOCX
PPT
SQL Tutorial - How To Create, Drop, and Truncate Table
PDF
View & index in SQL
PPTX
SQL Server Learning Drive
PPTX
DDL(Data defination Language ) Using Oracle
PPT
MySQL lecture
PPS
06 qmds2005 session08
PPTX
Sql basics
PPT
SQL Inteoduction to SQL manipulating of data
PDF
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
PPT
Introduction to SQL
PPT
SQL Tutorial - Basic Commands
Creating database using sql commands
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Getting Started with MySQL II
Dbms practical list
Xml and databases
SQL Tutorial - How To Create, Drop, and Truncate Table
View & index in SQL
SQL Server Learning Drive
DDL(Data defination Language ) Using Oracle
MySQL lecture
06 qmds2005 session08
Sql basics
SQL Inteoduction to SQL manipulating of data
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
Introduction to SQL
SQL Tutorial - Basic Commands
Ad

Viewers also liked (20)

PPT
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
PPT
SQL200.2 Module 2
PPTX
MS SQL SERVER: Creating Views
PDF
Sql db optimization
PDF
Using grouping sets in sql server 2008 tech republic
PPTX
Consultas en MS SQL Server 2012
PPS
Sql xp 04
PPT
Sql tuning guideline
PPTX
Statistics
PPTX
Sub-queries,Groupby and having in SQL
PPT
e computer notes - Subqueries
PPSX
Locking in SQL Server
PPT
Review of SQL
RTF
Triggers-Sequences-SQL
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
PPT
Lecture 4. MS SQL. DML Triggers
PPTX
Sql query analyzer & maintenance
PPT
Locking And Concurrency
PDF
SQL Server - Using Tools to Analyze Query Performance
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
SQL200.2 Module 2
MS SQL SERVER: Creating Views
Sql db optimization
Using grouping sets in sql server 2008 tech republic
Consultas en MS SQL Server 2012
Sql xp 04
Sql tuning guideline
Statistics
Sub-queries,Groupby and having in SQL
e computer notes - Subqueries
Locking in SQL Server
Review of SQL
Triggers-Sequences-SQL
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Lecture 4. MS SQL. DML Triggers
Sql query analyzer & maintenance
Locking And Concurrency
SQL Server - Using Tools to Analyze Query Performance
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
PPTX
SQL commands in database managemant systems
PPTX
introduction to SQL query language beginner.ppt
PPTX
SQL_SERVER_BASIC_1_Training.pptx
PPT
SQL302 Intermediate SQL Workshop 3
PPTX
SQL_SERVER_BASIC_1_Training.pptx
PDF
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
PDF
Dms 22319 micro project
PPTX
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
PPT
My sql with querys
PPTX
PostgreSQL Database Slides
PPTX
DBMSLab_SQL_4thsem_CI_17163544545446962.pptx
PPTX
Introduction to Oracle Database.pptx
DOC
Module 3
PDF
SQL200A Microsoft Access SQL Design
PPT
mysqlHiep.ppt
PPT
MySQL Database System Hiep Dinh
PDF
Sql views, stored procedure, functions
PPT
Introduction to Threading in .Net
DOCX
Android sq lite-chapter 22
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL commands in database managemant systems
introduction to SQL query language beginner.ppt
SQL_SERVER_BASIC_1_Training.pptx
SQL302 Intermediate SQL Workshop 3
SQL_SERVER_BASIC_1_Training.pptx
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
Dms 22319 micro project
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
My sql with querys
PostgreSQL Database Slides
DBMSLab_SQL_4thsem_CI_17163544545446962.pptx
Introduction to Oracle Database.pptx
Module 3
SQL200A Microsoft Access SQL Design
mysqlHiep.ppt
MySQL Database System Hiep Dinh
Sql views, stored procedure, functions
Introduction to Threading in .Net
Android sq lite-chapter 22

More from Dan D'Urso (20)

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

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Machine Learning_overview_presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
A Presentation on Artificial Intelligence
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Network Security Unit 5.pdf for BCA BBA.
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Per capita expenditure prediction using model stacking based on satellite ima...
Machine Learning_overview_presentation.pptx
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
The AUB Centre for AI in Media Proposal.docx
A comparative analysis of optical character recognition models for extracting...
Network Security Unit 5.pdf for BCA BBA.

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]