SlideShare a Scribd company logo
SQL302




            Intermediate SQL Programming
   Based on SQL Clearly Explained by Jan Harrington and Microsoft SQL Server T-
   SQL Fundamentals by Itzki Ben-gan

         Workshop 3 – Modifying Data, Managing the
                        Database
SQL Server/Employees               SQL302 Module 3                                1
Note on SQL302 Slides
    • These slides were originally designed to support the
      single SQL302 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
      MySQL, and MS Access slides out into their own
      slide sets. These SQL302 slides will focus on
      Microsoft SQL Server.


SQL Server/Employees      SQL302 Module 3                    2
Warning!
• Below are some table name changes to be
  aware of in doing queries. We have created
  synonyms so either name should work.

        New Name               Old Name
        Orders                 Order_filled
        Order_Lines            Orderlines


SQL Server/Employees    SQL302 Module 3        3
SQL302 Contact Information



              P.O. Box 6142
              Laguna Niguel, CA 92607
              949-489-1472
              http://www.d2associates.com
              slides.1@dhdursoassociates.com
              Copyright 2001-2011. All rights reserved.


SQL Server/Employees                    SQL302 Module 3   4
SQL302 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?
      sql.support@dhdursoassociates.com

SQL Server/Employees    SQL302 Module 3       5
SQL302 Module 3
• Part 1 – Modifying Data
• Part 2 – Managing Database Structures




SQL Server/Employees    SQL302 Module 3   6
SQL302

                       SQL Programming

                       Part 1 – Modifying Data


SQL Server/Employees          SQL302 Module 3    7
Relational Database with constraints (from text)




SQL Server/Employees     SQL302 Module 3                  8
Sample Employees Database




Bookstore2 &          SQL204 Module 1      9
Employees
Transactions
  • Statements can be grouped
    into transactions
  • All must succeed
  • If so they are all
    committed
  • If not they are all rolled
    back
  • Helps guarantee data is
    consistent

SQL Server/Employees     POS/410 Workshop 3   10
SQL Server Transactions Syntax
                   begin transaction
                       Statements
                       …
                       …
                   if @@error = 0
                       Commit transaction
                   else
                       Rollback error

SQL Server/Employees         POS/410 Workshop 3   11
Data Modification Statements

           • End-user rarely sees these
             statements

           • Application developer prepares
             these statements “behind the
             scenes” based on forms or web
             pages filled out by user



SQL Server/Employees     SQL302 Module 3      12
Data Modification Statements
           • This course (SQL302)
                –      Select Into
                –      Table Valued Constructors (TVCs)
                –      Update
                –      Delete
           • Basic DML already covered in
             SQL202

SQL Server/Employees            SQL302 Module 3           13
Select into
• Select….into will copy a table
• Uses a regular SQL statement with into
  clause before the from not after
• Syntax:
    Select <select list>
    Into <tablename>
    From <tablename>

SQL Server/Employees     SQL302 Module 3   14
Select into
• Example:
      – Copy a subset of the employees table to a
        temporary working table
• Code:




SQL Server/Employees     SQL302 Module 3            15
Insert with TVC
• Newer form of insert statement long
  available in MySQL
• Can shorten your code
• Each element in the insert list can actually
  be an expression but we will use literals in
  this class


SQL Server/Employees       SQL302 Module 3       16
Insert with TVC




SQL Server/Employees       SQL302 Module 3   17
Update and Delete
           • Updates and deletes can use
             subqueries

           • SQL Server allows update and
             delete statements to be based on
             joins, too

           • We will review this in the next few
             slides.
SQL Server/Employees        SQL302 Module 3        18
Update
• You can use subqueries in an update.
      – In where predicate
      – To set the field value

     UPDATE table
     SET columnname = value from subquery
     [WHERE condition with subquery]


SQL Server/Employees     SQL302 Module 3    19
UPDATE w/ Subquery in where
• Example: Increase range minimums for unused
  job codes




SQL Server/Employees   SQL302 Module 3          20
UPDATE Results




SQL Server/Employees       SQL302 Module 3   21
UPDATE FROM Statement
   SQL Server allows updates from a table, including joined
   tables. As an example one could update quantity on hand
   in an inventory table from a transaction table.


                   UPDATE table
                   SET columnname = value…
                   [FROM tablelist]
                   [WHERE condition]

SQL Server/Employees      SQL302 Module 3                     22
Update from
• Example: increase managerial salaries




SQL Server/Employees      SQL302 Module 3   23
UPDATE From Results




SQL Server/Employees   SQL302 Module 3   24
Update w/ subquery in set clause
• Update employee temp table – set salaries to the
  average for that code




SQL Server/Employees   SQL302 Module 3               25
DELETE FROM Statement
   SQL Server allows deletes from a table based on a
   subquery or a join condition.

   Basic syntax:

                       DELETE [from] table
                       FROM tablelist
                       [WHERE condition]


SQL Server/Employees         SQL302 Module 3           26
Delete from
• Example: Delete unused job titles




SQL Server/Employees     SQL302 Module 3   27
DELETE FROM Results




SQL Server/Employees   SQL302 Module 3   28
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

SQL Server/Employees        SQL302 Module 3             29
SQL302

                       SQL Programming

              Part 2– Managing Database Structures


SQL Server/Employees        SQL302 Module 3          30
Managing Database Structures
•   Create
•   Alter
•   Synonyms
•   Views




SQL Server/Employees   SQL302 Module 3   31
Managing Database Structures
• We will first create a table and then use the
  alter command to
      – Add columns
      – Modify columns
      – Add constraints




SQL Server/Employees   SQL302 Module 3        32
Creating Tables
• Use create statement
• Specify:
      – Columns with data types and column
        constraints
      – Table constraints
           • Foreign key references
           • Primary key designation


SQL Server/Employees       SQL302 Module 3   33
Create Table
     Basic syntax:
     Create table <table-name>
     (
     <column1> <datatype> <constraints>
     ,.. <column1> <datatype> <constraints>
     …
     <table constraints>
     );
SQL Server/Employees       SQL302 Module 3    34
Create Table
      Example 1: SQL202 Create summary table
      (we will do this step by step using the alter
      statement in succeeding slides)
      Create table summary(
      isbn varchar(20) primary key,
      How_many int check (how_many >= 0),
      Constraint isbn_fk
      Foreign key (isbn) references books(isbn)
      )

SQL Server/Employees      SQL302 Module 3             35
Create Table
      Example 2: Create summary table
      Create table #summary(
      isbn varchar(20) primary key);




SQL Server/Employees      SQL302 Module 3   36
Modifying a Table Design
• Applies to tables
• Use ALTER statement
      –   Add columns
      –   Delete columns
      –   Rename columns
      –   Add column constraints
      –   Add table constraints

SQL Server/Employees     SQL302 Module 3   37
Modifying a Table Design
      Basic syntax:
      Alter <table-name>
      Add <field-name>,
      Add <table-constraint>,
      Alter <field-name>
      Etc.

SQL Server/Employees   SQL302 Module 3   38
Add a column

         Example: add a column to hold how
         many times a book has been sold
         alter table #summary
         Add how_many
         numeric(5,2);



SQL Server/Employees      SQL302 Module 3    39
Modify a column
• Example
      – Change the how many column to an integer
• Code
  alter table #summary
  alter column how_many int;



SQL Server/Employees        SQL302 Module 3        40
New column results




SQL Server/Employees    SQL302 Module 3   41
Column Constraints
•   Primary key
•   Not NULL
•   CHECK clause
•   Default
•   Unique



SQL Server/Employees    SQL302 Module 3   42
Add a column constraint

        Example: modify the how many column
        so there is a check constraint on
        how_many


        alter table #summary
        add constraint check
        (how_many >=0);
SQL Server/Employees   SQL302 Module 3        43
Table Constraints
• Primary Key
• Foreign Key
• Compare fields against each other. I.e.
  ship_date >= order_date




SQL Server/Employees        SQL302 Module 3   44
Add a table constraint

       Example: add a foreign key constraint
       with cascade options




SQL Server/Employees   SQL302 Module 3         45
Constraints on Summary Table




SQL Server/Employees   SQL302 Module 3   46
Synonyms
• Can create another name for an object
      – Used to provide a shorthand way to refer a
        long, fully qualified table name
      – Used where we want scripts to use an old
        name as well as the newer name
• Syntax:
      – Create <synonym> for <objectname>


SQL Server/Employees    SQL302 Module 3              47
Synonyms
• Example:
      – create a synonym for the orderlines table
• Code
  create synonym orderlines for
  order_lines;



SQL Server/Employees    SQL302 Module 3             48
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




SQL Server/Employees   SQL302 Module 3       49
Views

   Basic syntax:
   Create view <view-name> (<column-list>)
   As
   <select statement>
   Column list can be used to name the output columns
   instead of using the names in the select list

SQL Server/Employees     SQL302 Module 3                50
Creating a View




SQL Server/Employees       SQL302 Module 3   51
Using Views
• Can be used like a table subject to various
  limitations
      – Cannot insert into grouped queries, etc.
      – Etc.
• Sample select from syntax:
         select column-list
         from employee_view

SQL Server/Employees      SQL302 Module 3          52
Inserting into a view
• Must insert into all required columns in
  underlying table
• Sample code:




SQL Server/Employees    SQL302 Module 3      53
Using a View




SQL Server/Employees      SQL302 Module 3   54
SQL302




                       [end of course]
SQL Server/Employees     SQL302 Module 3   55
Notes




SQL Server/Employees   SQL302 Module 3   56

More Related Content

PPT
SQL302 Intermediate SQL Workshop 2
PPT
SQL302 Intermediate SQL Workshop 1
PDF
Mcts self paced training kit exam 432 sql server 2008 - implementation and ...
PPT
SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3
PDF
Copy Of Mysql Datadictionary
PDF
Module 3 design and implementing tables
DOC
Oracle sql material
SQL302 Intermediate SQL Workshop 2
SQL302 Intermediate SQL Workshop 1
Mcts self paced training kit exam 432 sql server 2008 - implementation and ...
SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3
Copy Of Mysql Datadictionary
Module 3 design and implementing tables
Oracle sql material

What's hot (13)

PPTX
Oracle Data Redaction
PPTX
Oracle Data Redaction
PPTX
MS SQL - Database Programming Concepts by RSolutions
PPTX
Oracle Data Redaction - UKOUG - TECH14
PPT
MYSQL.ppt
DOC
Sql queries
DOCX
SQL Tutorial for BCA-2
DOC
Using MySQL Meta Data Effectively
PPT
Sql views
PPT
SQL Inteoduction to SQL manipulating of data
PPTX
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
PPT
SQL212.1 Introduction to SQL using Oracle Module 1
PPTX
Data Redaction - OTN TOUR LA 2015
Oracle Data Redaction
Oracle Data Redaction
MS SQL - Database Programming Concepts by RSolutions
Oracle Data Redaction - UKOUG - TECH14
MYSQL.ppt
Sql queries
SQL Tutorial for BCA-2
Using MySQL Meta Data Effectively
Sql views
SQL Inteoduction to SQL manipulating of data
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
SQL212.1 Introduction to SQL using Oracle Module 1
Data Redaction - OTN TOUR LA 2015
Ad

Viewers also liked (20)

PPT
Pharma Powerpoint 2
PDF
我國數位產業學習發展與推動概況
PDF
Course Catalog
PPTX
Show Your Work: Cheap & Easy Tools for Presenting Data
PPT
Comic Book
PDF
Tagger.fm Muziek Digitaal 091028
PDF
SQL212 Oracle SQL Manual
PDF
Presentatie REC 250309
PPT
eParticipation in The Netherlands
 
PDF
Resume Portfolio Linkedin 01 2009
PDF
青年創業及圓夢網 創業稅務大小事【創業懶人包 】
PPT
AVB201.1 MS Access VBA Module 1
PDF
AIN100
PPTX
Operació t4 i josef mengele
PPT
AVB202 Intermediate Microsoft Access VBA
PDF
IntelliStick
PPTX
PPT
Displays
PPT
George Washington Teacher’s Institute
Pharma Powerpoint 2
我國數位產業學習發展與推動概況
Course Catalog
Show Your Work: Cheap & Easy Tools for Presenting Data
Comic Book
Tagger.fm Muziek Digitaal 091028
SQL212 Oracle SQL Manual
Presentatie REC 250309
eParticipation in The Netherlands
 
Resume Portfolio Linkedin 01 2009
青年創業及圓夢網 創業稅務大小事【創業懶人包 】
AVB201.1 MS Access VBA Module 1
AIN100
Operació t4 i josef mengele
AVB202 Intermediate Microsoft Access VBA
IntelliStick
Displays
George Washington Teacher’s Institute
Ad

Similar to SQL302 Intermediate SQL Workshop 3 (20)

PDF
SQL302 Intermediate SQL
PDF
DP080_Lecture_1 SQL lecture document .pdf
PPT
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
DOC
Module 3
PPS
06 qmds2005 session08
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
PPSX
MS SQL Server
PPTX
Database COMPLETE
PDF
Sql overview-1232931296681161-1
PDF
SQL Overview
PPT
SQL200.1 Module 1
PPTX
THE DATABASE LANGUAGE SQL (Cơ sở dữ liệu).pptx
ODP
Ms sql-server
PDF
sql notes Provideby AGN HUB Tech & It Solutions
DOCX
Teradata imp
PDF
Introduction to the Structured Query Language SQL
PDF
Sql a practical introduction
PDF
Sql a practical introduction
DOC
6232 implementing a microsoft sql server 2008 database
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
SQL302 Intermediate SQL
DP080_Lecture_1 SQL lecture document .pdf
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
Module 3
06 qmds2005 session08
ms-sql-server-150223140402-conversion-gate02.pptx
MS SQL Server
Database COMPLETE
Sql overview-1232931296681161-1
SQL Overview
SQL200.1 Module 1
THE DATABASE LANGUAGE SQL (Cơ sở dữ liệu).pptx
Ms sql-server
sql notes Provideby AGN HUB Tech & It Solutions
Teradata imp
Introduction to the Structured Query Language SQL
Sql a practical introduction
Sql a practical introduction
6232 implementing a microsoft sql server 2008 database
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS

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
PDF
SQL200A Microsoft Access SQL Design
PPTX
Microsoft access self joins
PDF
AIN106 Access Reporting and Analysis
PDF
Course Catalog
PDF
SQL201W MySQL SQL Manual
PPT
SQL206 SQL Median
PPT
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
PPT
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
PDF
SQL202 SQL Server SQL Manual
PDF
AIN102 Microsoft Access Queries
PPT
AIN102S Access string function sample queries
PPT
AIN102.2 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
SQL200A Microsoft Access SQL Design
Microsoft access self joins
AIN106 Access Reporting and Analysis
Course Catalog
SQL201W MySQL SQL Manual
SQL206 SQL Median
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL202.1 Accelerated Introduction to SQL Using SQL Server Module 1
SQL202 SQL Server SQL Manual
AIN102 Microsoft Access Queries
AIN102S Access string function sample queries
AIN102.2 Microsoft Access Queries

SQL302 Intermediate SQL Workshop 3

  • 1. SQL302 Intermediate SQL Programming Based on SQL Clearly Explained by Jan Harrington and Microsoft SQL Server T- SQL Fundamentals by Itzki Ben-gan Workshop 3 – Modifying Data, Managing the Database SQL Server/Employees SQL302 Module 3 1
  • 2. Note on SQL302 Slides • These slides were originally designed to support the single SQL302 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 MySQL, and MS Access slides out into their own slide sets. These SQL302 slides will focus on Microsoft SQL Server. SQL Server/Employees SQL302 Module 3 2
  • 3. Warning! • Below are some table name changes to be aware of in doing queries. We have created synonyms so either name should work. New Name Old Name Orders Order_filled Order_Lines Orderlines SQL Server/Employees SQL302 Module 3 3
  • 4. SQL302 Contact Information P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com slides.1@dhdursoassociates.com Copyright 2001-2011. All rights reserved. SQL Server/Employees SQL302 Module 3 4
  • 5. SQL302 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? sql.support@dhdursoassociates.com SQL Server/Employees SQL302 Module 3 5
  • 6. SQL302 Module 3 • Part 1 – Modifying Data • Part 2 – Managing Database Structures SQL Server/Employees SQL302 Module 3 6
  • 7. SQL302 SQL Programming Part 1 – Modifying Data SQL Server/Employees SQL302 Module 3 7
  • 8. Relational Database with constraints (from text) SQL Server/Employees SQL302 Module 3 8
  • 9. Sample Employees Database Bookstore2 & SQL204 Module 1 9 Employees
  • 10. Transactions • Statements can be grouped into transactions • All must succeed • If so they are all committed • If not they are all rolled back • Helps guarantee data is consistent SQL Server/Employees POS/410 Workshop 3 10
  • 11. SQL Server Transactions Syntax begin transaction Statements … … if @@error = 0 Commit transaction else Rollback error SQL Server/Employees POS/410 Workshop 3 11
  • 12. Data Modification Statements • End-user rarely sees these statements • Application developer prepares these statements “behind the scenes” based on forms or web pages filled out by user SQL Server/Employees SQL302 Module 3 12
  • 13. Data Modification Statements • This course (SQL302) – Select Into – Table Valued Constructors (TVCs) – Update – Delete • Basic DML already covered in SQL202 SQL Server/Employees SQL302 Module 3 13
  • 14. Select into • Select….into will copy a table • Uses a regular SQL statement with into clause before the from not after • Syntax: Select <select list> Into <tablename> From <tablename> SQL Server/Employees SQL302 Module 3 14
  • 15. Select into • Example: – Copy a subset of the employees table to a temporary working table • Code: SQL Server/Employees SQL302 Module 3 15
  • 16. Insert with TVC • Newer form of insert statement long available in MySQL • Can shorten your code • Each element in the insert list can actually be an expression but we will use literals in this class SQL Server/Employees SQL302 Module 3 16
  • 17. Insert with TVC SQL Server/Employees SQL302 Module 3 17
  • 18. Update and Delete • Updates and deletes can use subqueries • SQL Server allows update and delete statements to be based on joins, too • We will review this in the next few slides. SQL Server/Employees SQL302 Module 3 18
  • 19. Update • You can use subqueries in an update. – In where predicate – To set the field value UPDATE table SET columnname = value from subquery [WHERE condition with subquery] SQL Server/Employees SQL302 Module 3 19
  • 20. UPDATE w/ Subquery in where • Example: Increase range minimums for unused job codes SQL Server/Employees SQL302 Module 3 20
  • 22. UPDATE FROM Statement SQL Server allows updates from a table, including joined tables. As an example one could update quantity on hand in an inventory table from a transaction table. UPDATE table SET columnname = value… [FROM tablelist] [WHERE condition] SQL Server/Employees SQL302 Module 3 22
  • 23. Update from • Example: increase managerial salaries SQL Server/Employees SQL302 Module 3 23
  • 24. UPDATE From Results SQL Server/Employees SQL302 Module 3 24
  • 25. Update w/ subquery in set clause • Update employee temp table – set salaries to the average for that code SQL Server/Employees SQL302 Module 3 25
  • 26. DELETE FROM Statement SQL Server allows deletes from a table based on a subquery or a join condition. Basic syntax: DELETE [from] table FROM tablelist [WHERE condition] SQL Server/Employees SQL302 Module 3 26
  • 27. Delete from • Example: Delete unused job titles SQL Server/Employees SQL302 Module 3 27
  • 28. DELETE FROM Results SQL Server/Employees SQL302 Module 3 28
  • 29. 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 SQL Server/Employees SQL302 Module 3 29
  • 30. SQL302 SQL Programming Part 2– Managing Database Structures SQL Server/Employees SQL302 Module 3 30
  • 31. Managing Database Structures • Create • Alter • Synonyms • Views SQL Server/Employees SQL302 Module 3 31
  • 32. Managing Database Structures • We will first create a table and then use the alter command to – Add columns – Modify columns – Add constraints SQL Server/Employees SQL302 Module 3 32
  • 33. Creating Tables • Use create statement • Specify: – Columns with data types and column constraints – Table constraints • Foreign key references • Primary key designation SQL Server/Employees SQL302 Module 3 33
  • 34. Create Table Basic syntax: Create table <table-name> ( <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> ); SQL Server/Employees SQL302 Module 3 34
  • 35. Create Table Example 1: SQL202 Create summary table (we will do this step by step using the alter statement in succeeding slides) Create table summary( isbn varchar(20) primary key, How_many int check (how_many >= 0), Constraint isbn_fk Foreign key (isbn) references books(isbn) ) SQL Server/Employees SQL302 Module 3 35
  • 36. Create Table Example 2: Create summary table Create table #summary( isbn varchar(20) primary key); SQL Server/Employees SQL302 Module 3 36
  • 37. Modifying a Table Design • Applies to tables • Use ALTER statement – Add columns – Delete columns – Rename columns – Add column constraints – Add table constraints SQL Server/Employees SQL302 Module 3 37
  • 38. Modifying a Table Design Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Alter <field-name> Etc. SQL Server/Employees SQL302 Module 3 38
  • 39. Add a column Example: add a column to hold how many times a book has been sold alter table #summary Add how_many numeric(5,2); SQL Server/Employees SQL302 Module 3 39
  • 40. Modify a column • Example – Change the how many column to an integer • Code alter table #summary alter column how_many int; SQL Server/Employees SQL302 Module 3 40
  • 41. New column results SQL Server/Employees SQL302 Module 3 41
  • 42. Column Constraints • Primary key • Not NULL • CHECK clause • Default • Unique SQL Server/Employees SQL302 Module 3 42
  • 43. Add a column constraint Example: modify the how many column so there is a check constraint on how_many alter table #summary add constraint check (how_many >=0); SQL Server/Employees SQL302 Module 3 43
  • 44. Table Constraints • Primary Key • Foreign Key • Compare fields against each other. I.e. ship_date >= order_date SQL Server/Employees SQL302 Module 3 44
  • 45. Add a table constraint Example: add a foreign key constraint with cascade options SQL Server/Employees SQL302 Module 3 45
  • 46. Constraints on Summary Table SQL Server/Employees SQL302 Module 3 46
  • 47. Synonyms • Can create another name for an object – Used to provide a shorthand way to refer a long, fully qualified table name – Used where we want scripts to use an old name as well as the newer name • Syntax: – Create <synonym> for <objectname> SQL Server/Employees SQL302 Module 3 47
  • 48. Synonyms • Example: – create a synonym for the orderlines table • Code create synonym orderlines for order_lines; SQL Server/Employees SQL302 Module 3 48
  • 49. 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 SQL Server/Employees SQL302 Module 3 49
  • 50. Views Basic syntax: Create view <view-name> (<column-list>) As <select statement> Column list can be used to name the output columns instead of using the names in the select list SQL Server/Employees SQL302 Module 3 50
  • 51. Creating a View SQL Server/Employees SQL302 Module 3 51
  • 52. Using Views • Can be used like a table subject to various limitations – Cannot insert into grouped queries, etc. – Etc. • Sample select from syntax: select column-list from employee_view SQL Server/Employees SQL302 Module 3 52
  • 53. Inserting into a view • Must insert into all required columns in underlying table • Sample code: SQL Server/Employees SQL302 Module 3 53
  • 54. Using a View SQL Server/Employees SQL302 Module 3 54
  • 55. SQL302 [end of course] SQL Server/Employees SQL302 Module 3 55
  • 56. Notes SQL Server/Employees SQL302 Module 3 56