Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Microsoft Access wikipedia , lookup
Relational algebra wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Database model wikipedia , lookup
Relational Model Advanced Topics in Microsoft SQL Server You will begin by reading the introduction from page 119 in the Pratt and Adamski textbook. Then continue below with the section on “Views” in this tutorial. VIEWS Most DBMS's support the creation of views. A view is an application program’s or an individual user’s picture of the database. An individual can use a view to create reports, charts, and other objects using database data. In many cases, an individual can use a view to examine table data as well. Because a view is usually less involved than the full database, its use can represent a great simplification. Views also provide a measure of security because omitting sensitive tables or fields from a view will render them unavailable to anyone who is accessing the database via that view. To illustrate the idea of a view, suppose Juan is interested in the part number, part description, units on hand, and unit price for Premiere Products parts that are in class HW. He is not interested in any of the other fields in the Part table, nor is he interested in any of the rows that correspond to parts in other item classes. Viewing this data would be simpler for Juan if the other rows and fields were not even present. Although you cannot change the structure of the Part table and omit some of its rows just for Juan, you can do the next best thing. You can provide him with a view that consists of precisely the rows and fields he needs to access. Using SQL, the following CREATE VIEW command creates the view that Juan can use to see the data he needs. In SQL Server the “Query Designer” window is used for writing one-time queries which can include CREATE VIEW. These queries are designed as SQL statements that will create or update some object in the SQL Server database and generally will not be needed another time. For example you might want to create a new table or view object, or update a table by creating a primary key for it or changing its data type. Therefore you will execute these statements once and afterwards there will be no need to save the SQL query. You write SQL queries using the “Query Designer” window (similar to the “View Designer” window but not a savable object). Start Visual Studio and open the Web Site that contains the database. Afterwards click the “expand” [ ] arrow for the database to view its objects. Right-click on the “Views” group as in the image below and then from the drop-down menu click on the “New Query” command (see image below). 1 The “Query Designer” window opens ready for you to enter any SQL statement as in the image below. You want to create a new SQL Server view object that lists the part number, description, on hand amount and price for record WHERE the class is housewares. This object will be given the name “Housewares” and will be stored along with other “View” objects in the database. The SQL CREATE VIEW query is: CREATE VIEW SELECT FROM WHERE Housewares AS PartNum, Description, OnHand, Price Part Class= 'HW' The SELECT command that creates the view, which is called the defining query, indicates what to include in the view. Conceptually, given the current data in the Premiere Products database, this view will contain the data shown in the table below. The data does not really exist in this form, however, nor will it ever exist in this form. It is tempting to think that when this view is used, the query 2 is executed and will produce some sort of temporary table named Housewares, which Juan then could access, but that is not what happens. Housewares PartNum AT94 DL71 FD21 Description Iron Cordless Drill Stand Mixer OnHand 50 21 22 Price $24.95 $129.95 $159.95 Instead, the query acts as a sort of window into the database. As far as Juan is concerned, the entire database is just a portion of the Part table. Juan can see any change that affects this smaller portion of the Part table, but he is totally unaware of any other changes that are made in the database. The image below shows how to enter the CREATE VIEW query into the “Query Designer” window to create this new “View” object. Click the <Execute> button on the “Query Designer” toolbar to run the query. After a few moments you should see a report “Command(s) completed successfully” in the “TSQL” window at the bottom half of the SQL Server IDE similar to that in the image below. What has happened is that there now is a new view object named Housewares in the “Views“ group of the “Server Explorer” window (see the object in the image below). This is exactly the same result as when you selected the command “Add New View” for a SELECT statement, wrote the SQL CREATE VIEW command with a SELECT statement and clicked the <Update> button to save it as a “View“ object. 3 When you are done working on the query, click the close [X] button on the tab of the “Query Designer” to close the window. As mentioned before there is no need to save this query; in fact it cannot be saved. It is intended to be executed once, since once the “View” object is created there is no need to create it again. Now you might be wondering why we created the new view in this way. Would it not have been just as easy to create it the way we did it in the previous chapter? That probably is true. However just about every relational database operation can be completed using a SQL query statement (not just SELECT). The “Query Designer” window is the location in SQL Server where these statements can be keyed and executed. When you create a query that involves a view, the DBMS changes the query to one that selects data from the table(s) in the database that created the view. Suppose, for example, that in the “Query Designer” Juan creates the following query: SELECT FROM WHERE * Housewares OnHand < 25 The DBMS does not execute the query in this form. Instead, it merges the query Juan entered with the query that defines the view to form the query that is actually executed. When the DBMS merges the query that creates the view with the query to select rows where the OnHand value is less than 25, the query that the DBMS actually executes is as follows: SELECT FROM WHERE PartNum, Description, OnHand, Price Part Class= 'HW' AND OnHand< 25 In the query that the DBMS executes, the FROM clause lists the Part table rather than the Housewares view, the SELECT clause lists fields from the Part table instead of * to select all fields from the Housewares view, and the WHERE clause contains a compound condition to select only those parts 4 in the HW class (as Juan sees in the Housewares view) and only those parts withOnHand values of less than 25. Juan, however, is unaware that this kind of activity is taking place. To Juan, it seems as though he is using a table named Housewares. One advantage of this approach is that, because the Housewares view never exists in its own right, any update to the Part table is immediately available in Juan’s Housewares view. If the Housewares view really were a table, that would not be the case. To create a view in SQL Server, you simply create and save a “View” (select the command “Add New View” from the “Views” short-cut menus) as a CREATE VIEW/SQL SELECT command. You would save the query using a name like “HousewaresOnOrderLessThan_25”, as shown in the image below. The query results in the image below are displayed in the output window when the “Show Results” command for the “HousewaresOnOrderLessThan_25” view is selected. After creating the view, you can use it right away. You can create a form for the view, base a report on the view, and treat the view as though it were a table. What if Juan wanted different names for the fields? You can use SQL to change the field names in a view by including the new field names in the CREATE VIEW command. For example, if Juan wanted the names of the PartNum, Description, OnHand, and Price fields to be PNum, PDesc, OnHd, and Price, respectively, the CREATE VIEW command would be as follows: 5 CREATE VIEW Housewares2 (PNum, PDesc, OnHd, Price) AS SELECT PartNum, Description, OnHand, Price FROM Part WHERE Class= 'HW' The image below shows how to enter the CREATE VIEW query into the “Query Designer” window to create this new “View” object. Clicking the <Execute> button on the “Query Designer” toolbar runs the query. The new object “Housewares2” is created in the “Views” list. Now when Juan accesses the “Housewares2” view, he uses the field names PNum, PDesc, OnHd, and Price rather than PartNum, Description, OnHand, and Price, respectively. To create a new view to use the “View” the SQL SELECT query is: SELECT FROM WHERE PNum, PDesc, OnHd, Price Housewares2 OnHd < 25 Creating the “View” in SQL Server and saving it using the name “Housewares2OnOrderLessThan _25” is shown in the image below. 6 The query results in the image below are displayed in the output window when the “Show Results” command for the “Housewares2OnOrderLessThan_25” view is selected. The Housewares and Housewares2 views are examples of row-and-column subset views because they consist of a subset of the rows and columns in some individual table, which, in this case, is the Part table. Because the query can be any SQL query, a view can also join two or more tables. Suppose, for example, Francesca needs to know the number and name of each sales rep, along with the number and name of the customers represented by each sales rep. It would be much simpler for her if this information were stored in a single table instead of in two tables that she has to join together. She would like a single table that contains the sales rep number, sales rep name, customer number, and customer name. Suppose she would also like these fields to be named SNum, SLast, SFirst, CNum, and CName, respectively. She could use a join in the CREATE VIEW command as follows: CREATE VIEW SELECT FROM WHERE SalesCust (SNum, SLast, SFirst, CNum, CName) AS Rep.RepNum, LastName, FirstName, CustomerNum, CustomerName Rep, Customer Rep.RepNum = Customer.RepNum Given the current data in the Premiere Products database, conceptually this view is the table as shown below. SalesCust SNum 20 20 20 35 35 35 35 65 65 65 SLast Kaiser Kaiser Kaiser Hull Hull Hull Hull Perez Perez Perez SFirst Valerie Valerie Valerie Richard Richard Richard Richard Juan Juan Juan CNum 148 524 842 282 408 687 725 356 462 608 7 CName Al’s Appliance and Sport Kline’s All Season Brookings Direct The Everything Shop Lee’s Sport and Appliance Deerfield’s Four Seasons Ferguson’s Bargains Galore Johnson’s Department Store The image below shows how to enter the CREATE VIEW query into the “Query Designer” window to create this new view object. Clicking the <Execute> button on the “Query Designer” toolbar runs the query. The new view object “SalesCust” is created in the “Views“ list. To Francesca, the SalesCust view is a real table; she does not need to know what goes on behind the scenes in order to use it. She could find the number and name of the sales rep who represents customer 282, for example, by using the following query: SELECT FROM WHERE SNum, SLast, SFirst SalesCust CNum= '282' Francesca is completely unaware that, behind the scenes, the DBMS converts her query as follows: SELECT FROM WHERE Rep. RepNum AS SNum, LastName AS SLast, FirstName AS SFirst Rep, Customer Rep. RepNum= Customer. RepNum AND CustomerNum= '282' Creating the “View” in SQL Server and saving it using the name “SalesRepForCustomer_282” is shown in the image below. 8 The query results in the image below are displayed in the output window when the “Show Results” command for the “SalesRepForCustomer_282” view is selected. The use of views provides several advantages: Views provide data independence. If the database structure changes (because of fields being added or relationships changing between tables, for example) in such a way that the view can still be derived from existing data, the user can still access and use the same view. If adding extra fields to tables in the database is the only change and these fields are not required by the view’s user, the defining query may not even need to be changed for the user to continue using the view. If relationships are changed, the defining query may be different, but because users need not be aware of the defining query, this difference is unknown to them. They continue accessing the database through the same view as though nothing has changed. Because each user has his own or her own view, different users can view the same data in different ways. A view should contain only those fields required by a given user. This practice has two advantages. First, because the view will, in all probability, contain fewer fields than the overall database and the view is conceptually a single table, rather than a collection of tables, it greatly simplifies the user’s perception of the database. Second, views provide a measure of security. Fields that are not included in the view are not accessible to the view’s user. For example, omitting the Balance field from a view will ensure that a user of the view cannot access any customer’s balance. Likewise, rows that are not included in the view are not accessible. A user of the Housewares view, for example, cannot obtain any information about parts in the AP or SG item classes. INDEXES If you want to find a discussion of a given topic in a book, you could scan the entire book from start to finish, looking for references to the topic you had in mind. More likely, however, you would not have to resort to this technique. If the book had a good index, you could use it to quickly identify the pages on which your topic is discussed. Within relational model systems on both mainframes and personal computers, the main mechanism for increasing the efficiency with which data is retrieved from the database is the index. Conceptually, these indexes are very much like the index in a book. Consider the table below, for 9 example, which shows the Customer table for Premiere Products together with one extra field, RecordNum. This extra field gives the location of the record in the file. (Customer 148 is the first record in the table and is on record 1, customer 282 is on record 2, and so on.) These record numbers are automatically assigned and used by the DBMS, not by its users, which is why you do not normally see them. For illustrative purposes, the table below includes a RecordNum column to show how an index works. Customer RecordNum CustomerNum CustomerName 1 148 Al’s Appliance and Sport 2 282 Brookings Direct 3 356 Ferguson’s 4 408 The Everything Store 5 462 Bargains Galore 6 524 Kline’s 7 608 Johnson’s Department Store 8 687 Lee’s Sport and Appliance 9 725 Deerfield’s Four Seasons 10 842 All Season … … … … … … … … … … … Balance CreditLimit RepNum $6,550.00 $7,500.00 20 $431.50 $10,000.00 35 $5.785.00 $7,500.00 65 $5,285.25 $5,000.00 35 $3,412.00 $10,000.00 65 $12,762.00 $15,000.00 20 $2,106.00 $10.000.00 65 $2,851.00 $5,000.00 35 $248.00 $7,500.00 35 $8,221.00 $7,500.00 20 To rapidly access a customer’s record on the basis of his or her record number, you might choose to create and use an index, as shown in the table below. CustomerNum Index CustomerNum 148 282 356 408 462 524 608 687 725 842 RecordNum 1 2 3 4 5 6 7 8 9 10 The index has two fields. The first field contains a customer number, and the second field contains the number of the record on which the customer number is found. Because customer numbers are unique, there is only a single corresponding record number in this index. That is not always the case, however. Suppose, for example, you wanted to quickly access all customers with a specific credit limit or all customers that are represented by a specific sales rep. You might choose to create and use an index on credit limit as well as an index on sales rep number. These two indexes are shown in the tables below. 10 CreditLimit Index Credit Limit $5000 $7500 $10,000 $15,000 RepNum Index RecordNum 4, 8 1, 3, 9, 10 2, 5, 7 6 RepNum 20 35 65 RecordNum 1, 6, 10 2, 4, 8, 9 3, 5, 7 By examining the CreditLimit index in the table above, you can see that each credit limit occurs in the index along with the numbers of the records on which that credit limit occurs. Credit limit $ 7,500, for example, occurs on records 1, 3, 9, and 10. Further, the credit limits appear in the index in numerical order. If the DBMS uses this index to find those records on which the credit limit is $ 10,000, for example, it could scan the credit limits in the index to find $ 10,000. After doing that, it would determine the corresponding record numbers (2, 5, and 7) and then immediately go to those records in the Customer table, finding these customers more quickly than if it had to scan the entire Customer table one record at a time. Thus, indexes can make the process of retrieving records fast and efficient. NOTE: With relatively small tables, the increased efficiency associated with indexes will not be readily apparent. In practice, it is common to encounter tables with thousands, tens of thousands, or even hundreds of thousands of records. In such cases, the increase in efficiency is dramatic. In fact, without indexes, many operations in such databases would simply not be practical— they would take too long to complete. The field or combination of fields on which the index is built is called the index key. For the Customer table in the examples shown above, there are indexes on the CustomerNum field, the CreditLimit and the RepNum. The index key for an index can be any field or combination of fields in any table. After creating an index, you can use it to facilitate data retrieval. In powerful mainframe relational systems, the decision concerning which index(es) to use (if any) during a particular type of retrieval is a function of the DBMS. As you would expect, the use of any index is not purely advantageous or disadvantageous. An advantage was already mentioned: An index makes certain types of retrieval more efficient. There are two disadvantages. First, the index occupies space on disk. Using this space for an index, however, is technically unnecessary because any retrieval that can be made using the index can also be made without the index, although less efficiently. The other disadvantage is that the DBMS must update the index whenever corresponding data in the database is updated. Without the index, the DBMS would not need to make these updates. The main question you must ask when considering whether to create a given index is this: Do the benefits derived during retrieval outweigh the additional storage required and the extra processing involved in update operations? The following guidelines should help you make this determination. You should create an index on a field (or combination of fields) when one or more of the following conditions exist: The field is the primary key of the table. (In some systems like SQL Server, the DBMS might create this index automatically.) 11 The field is the foreign key in a relationship you have created. You will frequently use the field as a sort field. You will frequently need to locate a record based on a value in this field. You can add and delete indexes as necessary. You can create an index after the database is built— the index does not need to be created at the same time as the database. Likewise, when it appears that an existing index is unnecessary, you can delete it. The exact process for creating an index varies from one DBMS to another. The SQL command to create an index is CREATE INDEX. A SQL CREATE INDEX query can be executed from the “Query Designer” window. Right-click on the “Views” group and then from the drop-down menu click on the “New Query” command. Click the buttons on the “Query Design” window to close the “Show Diagram” pane (used to show the field list for each table in QBE queries), the “Show Criteria” pane (contains the design grid for QBE queries) and the “Results” panes. Then enter the following query as in the image below. (The prefix characters “IX” in the index name “IX_CustomerName” is a SQL Server convention—a common practice—which indicates that this new object is an index.) The SQL CREATE INDEX query is: CREATE INDEX IX_CustomerName ON Customer (CustomerName) The image below shows how to enter the CREATE INDEX query into the “Query Designer” window to create this new index object. Clicking the <Execute> ( ) button on the “Query Designer” toolbar runs the query. This CREATE INDEX command creates an index named IX_CustomerName. The index is for the Customer table and the index key is the CustomerName field. In this example, the index name is the same as the index key including the “IX” prefix. This format is not a requirement, but it is a good general practice. Just like creating new “Views” in the “Query Designer” window, there is no output displayed as a result of the successful execution of this CREATE INDEX query. What has happened is that there is a new index for the “Customer” table which we will be able to see in the “Indexes/Keys” dialog window when we open the “Table Definition” for the table. In SQL Server we usually use the GUI (graphical user interface) approach to work with indexes. This is achieved by clicking the “expand” [ ] arrow to drill down and view the list of tables in the database. Right-click on the table name and select the command “Open Table Definition” as in the image below. 12 13 The “Table Definition” window opens as in the image below. Indexes for the table are created, viewed and updated in the “Indexes/Keys” dialog window. To reach this window click the <Manage Indexes and Keys>button on the “Table Definition” toolbar as in the image below. The “Indexes/Keys” dialog window opens. Notice in the image below that two indexes already exist in this window: PK_Customer—the prefix characters “PK” in the index name “PK_Customer” is a SQL Server convention—a common practice—which indicates that this new object is the primary key IX_CustomerNum—this is the index on the CustomerNum field that we created previously by executing the SQL CREATE INDEX query 14 First of all let us examine the primary key PK_Customer, an index created automatically by SQL Server the table was first created and assigned the CustomerNum field as the primary key. The properties for this index include: Columns—CustomerNum which is the primary key field; it is configured to be in ascending (ASC) order Is Unique—Yes (value set automatically by SQL Server because is it the primary key) which indicates that no two records can have matching values for the CustomerNum field Type—Primary Key (only one per table) (Name)—PK_Customer specified as the prefix “PK” (meaning “primary key“) and the name of the table Now consider the index IX_CustomerName created previously on the CustomerName field by our SQL CREATE INDEX query. The query selected the CustomerName field for the index, gave it the name IX_CustomerName and by default configured it to be in ascending (ASC) order. 15 Indexes other than the primary key also may be unique. We refer to these as alternate or candidate keys and like the primary key no two records can have matching values for these fields. The “Is Unique” and “Type” properties work in combination for these fields. In this case when “Type” is set to “Unique Key” the “Is Unique” property automatically is set to “Yes” as in the two images below. 16 When you create an index whose key is a single field, you have created a single-field index (also called a single-column index). A multiple-field index (also called a multiple-column index) is an index with more than one key field. When creating a multiple-field index, you list the more important key first. In addition, if data for either key appears in descending order, you must follow the field name with the word DESC. To create an index named IX_RepBal with the keys RepNum and Balance and with the balances listed in descending order, you could use the following SQL command: CREATE INDEX IX_RepBal ON Customer (RepNum, Balance DESC) Multiple key indexes may be created for SQL Server tables in the “Index Columns” dialog window for the Columns property by selecting a second “Column Name” below the first (major) sort field and set its “Sort Order” (see image below). 17 Name this index (e.g. IX_RepBal as in the SQL CREATE INDEX query above). Then close the “Index/Keys” dialog window and click the <Save> button on the “Standard” tool bar to save all new and/or updated indexes before closing the table definition. 18 The SQL command used to drop (delete) an index that is no longer necessary is DROP INDEX, which consists of the words “DROP INDEX” followed by the name of the index to delete. To drop the IX_RepBal index (do not actually do this for now), for example, the command is as follows: DROP INDEX RepBal To delete indexes in SQL Server: 1. Open the “Table Definition” mode for the table 2. Click the “Manage Indexes and Keys” button on the “Table Definition” toolbar 3. Select the index name and click the <Delete> button in the “Indexes/Keys” dialog window SECURITY You will read the section on “Security” from pages 130-131 in the Pratt and Adamski textbook, but there are no related exercises in SQL Server. INTEGRITY RULES Read the section on “Integrity Rules” from pages 131-135 in the Pratt and Adamski textbook. We previously have created primary keys and implemented foreign keys relationships when we designed SQL Server databases in Chapter 1. You can review these concepts if necessary by looking back and reading the “Creating Tables in Microsoft SQL Server” tutorial. 19 LEGAL VALUES INTEGRITY In addition to the two integrity rules defined by Codd, there is a third type of integrity, called legal-values integrity. Often there is a particular set of values, called the legal-values, that are allowable in a field. Legal-values integrity is the property that states that no record can exist in the database with a value in the field other than one of the legal values. For example at Premiere Products the legal values for the CreditLimit field are $5,000, $7,500, $10,000, and $15,000. The DBMS must reject an attempt to enter a record with a credit limit of $12,500. In SQL you use the CHECK clause to enforce legal-values integrity. For example, to ensure that the only legal values for credit limits are $5,000, $7,500, $10,000, or $15,000, include the following CHECK clause in a CREATE TABLE or ALTER TABLE command, e.g.: ALTER TABLE Customer ADD CHECK (CreditLimit IN (5000, 7500, 10000, 15000)) The general form of the CHECK clause is the word CHECK followed by a condition. In the previous CHECK clause, the credit limit must be in the set consisting of 5000, 7500, 10000, or 15000. The DBMS will automatically reject any update to the database that violates the condition in the CHECK clause. Legal-values integrity may be enforced in SQL Server while in the “Table Definition” mode for the table from the “Check Constraints” dialog window. Click the “expand” [ ] arrow to drill down and view the list of tables in the database. Right-click on the table name and select the command “Open Table Definition” as in the image below. 20 Click the <Manage Check Constraints>button on the “Table Design” toolbar as in the image below to open the “Check Constraints” dialog window. Then click the <Add> button in the “Check Constraints” dialog window as in the image below which creates the template for a new constraint object. Clicking on the “Expression” property’s Build<…> button opens the “Check Constraint Expression” dialog window. 21 In the “Check Constraint Expression” dialog window you type a Boolean expression similar to that you would enter in a SQL WHERE clause. For example to ensure that the only legal values for credit limits are $5,000, $7,500, $10,000, or $15,000, type the following as per the image below. CreditLimit IN (5000, 7500, 10000, 15000) When you click the <OK> button, you are returned to the “Check Constraints” dialog window, where this new constraint should be given a name. (The default name uses the table name along with the prefix “CK” which is the SQL Server convention that indicates this object is a check constraint.) The example “CK_CreditLimit” in the image below uses the column name (fieldname) along with the “CK” prefix. 22 Then close the “Check Constraints” dialog window and click the <Save> button on the “Standard” tool bar to save all new constraints before closing the table definition. You may want to open the Customer table and try to modify the CreditLimit field value of one of the records so that it violates the constraint. SQL Server automatically will reject any update that violates the condition. 23 STRUCTURE CHANGES An important feature of relational DBMS’s is the ease with which you can change the database structure by adding and removing tables and fields, by changing the characteristics of existing fields, or by creating and dropping indexes. Although the exact manner in which you accomplish these changes varies from one system to another, most systems allow you to make all of these changes quickly and easily. Changes to a table’s structure are made using the SQL ALTER TABLE command. Virtually every implementation of SQL allows the creation of new fields in existing tables. For example, suppose you need to maintain the total quantity currently on order for each part in the Premiere Products database. To implement this change, you might add a new OnOrder field to the Part table. You can do this directly in the table “Design“ view or you can write a SQL ALTER TABLE query as follows: ALTER TABLE Part ADD OnOrder INT To write the ALTER TABLE query use the “Query Designer” window and enter the query as per the following image. Click the <Run> ( ) button on the “Query Designer” toolbar to run the query. When you see the “Query Definitions Differ“ dialog window (remember from our earlier discussion of queries this is not really an error), click the <Continue> button. Click the <OK> button to complete the query execution. At this point, the Part table contains an extra field, OnOrder. In SQL Server you also can add a field in the table “Design“ view at any time. The image below shows the Part table after adding the OnOrder field. 24 For order line records added from this point on, the value of OnOrder is entered just like any other field. For existing rows, OnOrder is typically assigned a null value by the DBMS automatically. The user then can change these values if desired. Since we inserted this new column after records exist with entries for the NumOrdered field in the OrderLine table, values that represent the current state for the OnOrder field should be entered manually for now as in the image below. Some systems allow changes to the properties of existing fields such as increasing the length of a character field. For example, to increase the field size of the CustomerName field in the Customer table from 35 to 40 characters, use the following SQL ALTER TABLE command: ALTER TABLE Customer MODIFY CustomerName VARCHAR( 40) In SQL Server, you can change field properties in the table “Design“ view. The image below shows the CustomerName field after increasing its field size from 35 to 40 characters. 25 Some DBMS systems including SQL Server allow existing fields to be deleted. Oracle is one system that does not allow existing fields to be deleted. For example, the following SQL command deletes the Warehouse field from the Part table (do not actually do this for now): ALTER TABLE Part DROP COLUMN Warehouse In SQL Server, you can delete a field in table “Design“ view by selecting the field as in the image below and pressing the <Delete> key on the keyboard. SQL Server will not give you an option to confirm the deletion of the field, so clicking <Delete> will immediately and permanently delete the field and all data it stores. You can use the SQL DROP TABLE command to delete a table that is no longer needed. For example, to delete the SmallCust table (created in Chapter 3) from the Premiere Products database, you would use the following command: DROP TABLE SmallCust The table and all indexes and views defined on the table would be deleted. The DROP TABLE command deletes the table structure as well as its data. In SQL Server, you can drop (delete) a table, such as the SmallCust table, by right-clicking the Table name in Server Explorer and then clicking “Delete“ on the shortcut menu (see image below). 26 A “Question“ dialog window will open and ask you to confirm the deletion by clicking the <Yes> button. When you do this, SQL Server will delete the table from the database. In some cases, you might need to change a table’s structure in ways that are beyond the capabilities of your DBMS. Perhaps you need to eliminate a field, change the field order, or combine data from two tables into one, but your system does not allow these types of changes. For example, some systems, including Oracle, do not allow you to reduce the size of a field or change its data type. In these situations, you can use the CREATE TABLE command to describe the new table, and then insert values into it using the INSERT command combined with an appropriate SELECT clause, as you learned in Chapter 3. If you are using a version of SQL that supports the SELECT INTO command, as SQL Server does, you can use it to create the new table in a single operation. 27 SYSTEM CATALOG You will read the section on “System Catalog” from pages 140-142 in the Pratt and Adamski textbook, but there are no related exercises in SQL Server. STORED PROCEDURES In a client/server system, the database resides on a computer called the server and users access the database through clients. A client is a computer that is connected to a network and has access through the server to the database. Every time a user executes a query, the DBMS must determine the best way to process the query and provide the results. For example, the DBMS must determine which indexes are available and whether it can use those indexes to make the processing of the query more efficient. If you anticipate running a particular query often, for example, you can improve overall performance by saving the query in a special file called a stored procedure. The stored procedure is placed on the server. The DBMS compiles the stored procedure (translating it into machine code) and creates an execution plan, which is the most efficient way of obtaining the results. From that point on, users execute the compiled, optimized code in the stored procedure. Another reason for saving a query as a stored procedure, even when you are not working in a client/server system, is convenience. Rather than retyping the entire query each time you need it, you can use the stored procedure. For example, suppose you frequently execute a query to change a customer’s credit limit. You can use the same query to select the record using the customer’s number and to change the credit limit. Instead of running the query each time and changing the customer number and the credit limit, it would be simpler to store the query in a stored procedure. When you run the stored procedure, you need to enter only the appropriate customer number and the new credit limit. Stored procedures can be used for SQL SELECT, INSERT, UPDATE and DELETE queries. The basic format is: CREATE PROCEDURE storedProcedureName [@parameter1 dataType @parameter2 dataType, … ] AS sqlStatement storedProcedureName—this is the name by which the object will be saved and stored. parameters—these are optional names (i.e. variables) that the stored procedure can use to communicate dynamically with a user while the procedure is executing or with a running program written in a high-level language (Java, C#, etc.); the use of parameters will be discussed a little bit later sqlStatement—this is any SQL statement that will be saved and stored in the procedure for future reference and execution 28 Consider the following example: CREATE PROCEDURE Rep35 AS SELECT CustomerNum, CustomerName FROM Customer WHERE RepNum = 35 The CREATE PROCEDURE line in the stored procedure causes SQL to create a procedure named “Rep35”and save it as a database object. In this query there are no parameters. After the keyword AS follows the SQL command to be stored. You write SQL queries using the “Stored Procedure” window. Click the “expand” [ ] arrow for the database to view its objects. Right-click on the “Stored Procedures” group as in the image below and then from the drop-down menu click on the “Add New Stored Procedure” command. Modify the initial default procedure template by entering and creating the SELECT stored procedure as per the image below. Clicking the <Save> button on the “Standard” toolbar saves the new object using the name Rep35 that was specified in the CREATE PROCEDURE clause. Notice that two things are performed automatically by SQL Server: The CREATE PROCEDURE command is converted to an ALTER PROCEDURE command; once the procedure is created, it only can be altered (not created again) 29 The prefix “dbo” (for database object) is appended to the front of the stored procedure name, e.g. “dbo.Rep35” To execute the stored procedure in SQL Server you can do any of the following: 1. From the “Stored Procedure” window, right-click anywhere in the procedure and select the command “Execute” from the drop-down menu 2. From the “Server Explorer” window, right-click on any store procedure and select the command “Execute” from the drop-down menu 30 3. From the “Query Designer” window call the command EXEC: – Format: EXEC storedProcedureName [parameter1, parameter2, … ] – Examples: EXEC Rep35 EXEC CustomersWithOrders '148' (this is an example that implements a parameter) In the first two examples above when the command “Execute” is selected from the short-cut menu, the results for the query are displayed in the Visual Studio “Output Window” as in the images above. When you call the EXEC command for a SQL Server query, the query results for SELECT queries are displayed in the “Query Designer” “Results” pane (for all other queries that have no visible output, you will need to find the object that was updated by the query to validate that the operation took place). The “Output” window appears at the bottom of the Visual Studio IDE. Output from the most recently executed stored procedure is displayed in the displays for a few seconds after the procedure is executed. If you wish to view the output again, you can hover the mouse pointer over the <Output> tab to view the “Output” window again. 31 Parameters are declared variables used to provide automated input to a stored procedure from a user. Specifically they are an interactive mechanism that provides the flexibility for users to enter query values directly into the stored procedure while it is executing. One or more parameters are “declared” before the AS clause in the procedure. When the procedure executes, a dialog window opens and the user is prompted to enter value(s) that are assigned to the parameter variables. These values then are substituted into the “parameterized” SQL statement The basic format is: CREATE PROCEDURE storedProcedureName @parameter1 dataType @parameter2 dataType [, … ] AS sqlStatement 32 Consider the following SELECT statement example: CREATE PROCEDURE CustomersWithOrders @CustomerNum INT AS SELECT Customer.CustomerNum, CustomerName, OrderNum, OrderDate FROM Customer, Orders WHERE Customer.CustomerNum = Orders.CustomerNum AND Customer.CustomerNum = @CustomerNum In the second line there is a single parameter (also called an argument), CustomerNum, with the appropriate data types which in this case is INT. These parameters are used in the SQL statement that will be stored. When users run this stored procedure, they will furnish values for the @CustomerNum parameter. The stored procedure then retrieves only those records for which the CustomerNumfield matches the value stored in the @CustomerNum parameter. We will try this in SQL Server. Add a new stored procedure object and enter the procedure as shown in the image below. Clicking the <Save> button on the “Standard” toolbar creates the new stored procedure. Now execute the stored procedure using either method that selects the “Execute” method from a drop-down menu. The “Run Stored Procedure” dialog window displays and you are prompted to enter values for the parameters as in the image below. The query runs based upon the variable parameter values that were entered by the user and the results appear in the “Output Window.” Try executing the procedure more than once with different values for the RepNum field. 33 Parameters are especially useful with update queries (INSERT, UPDATE and DELETE) since they provide the user a mechanism to enter different values each time the query executes. 34 When creating an INPUT query in a stored procedure, parameters provide the values for the new record. The basic format is: CREATE PROCEDURE storedProcedureName @parameter1 dataType @parameter2 dataType [, …] AS INSERT INTO tableName (fieldName1, fieldName2 [, …] ) VALUES (@parameter1, @parameter2 [, …] ) Consider the following example: CREATE PROCEDURE NewPart @PartNum VARCHAR(4), @Description VARCHAR(20), @OnHand INT, @Class VARCHAR(2), @Warehouse VARCHAR(1), @Price MONEY AS INSERT INTO Part (PartNum, Description, OnHand,Class, Warehouse, Price) VALUES (@PartNum, @Description, @OnHand,@Class, @Warehouse, @Price) Try this in SQL Server. Add a new stored procedure object and enter the procedure as shown in the image below. Clicking the <Save> button on the “Standard” toolbar creates the new stored procedure. 35 Now execute the stored procedure using either method that selects the “Execute” method from a drop-down menu. The “Run Stored Procedure” dialog window displays and you are prompted to enter values for the parameters as in the image below. 36 The query runs based upon the variable parameter values that were entered by the user and confirmation that the record was inserted appears in the “Output” window as in the image below, e.g. “(1 row(s) affected)”. After executing the procedure the new record in the Part table can be found when you select the command “Show Table Data” in “Server Explorer” to view the table data. 37 When creating an UPDATE query in a stored procedure, parameters provide values for the updated fields and the WHERE clause that specifies the record to update. The basic format is: CREATE PROCEDURE storedProcedureName @parameter1 dataType @parameter2 dataType [, …] AS UPDATE tableName SET fieldName1 = @parameter1, fieldName2 = @parameter2[, …] WHERE fieldName3 = @parameter2 Consider the following example: CREATE PROCEDURE UpdateOnHand @PartNum VARCHAR(4), @OnHand INT AS UPDATE Part SET OnHand = @OnHand WHERE PartNum = @PartNum Try this in SQL Server. Add a new stored procedure object and enter the procedure as shown in the image below. Clicking the <Save> button on the “Standard” toolbar creates the new stored procedure. 38 Now execute the stored procedure using either method that selects the “Execute” method from a drop-down menu. The “Run Stored Procedure” dialog window displays and you are prompted to enter values for the parameters as in the image below. 39 The query runs based upon the variable parameter values that were entered by the user and confirmation that the record was updated appears in the “Output” window as in the image below, e.g. “(1 row(s) affected)”. After executing the procedure the updated record with the new OnHand value can be found in the Part table when you select the command “Show Table Data” in “Server Explorer” to view the data. 40 When creating a DELETE query in a stored procedure, a single parameter provides the value for the WHERE clause that specifies the record to be deleted. The basic format is: CREATE PROCEDURE storedProcedureName @parameter1 dataType AS DELETE FROM tableName WHERE fieldName1 = @parameter1 Consider the following example: CREATE PROCEDURE DeletePart @PartNum VARCHAR(4) AS DELETE FROM Part WHERE PartNum = @PartNum Try this in SQL Server. Add a new stored procedure object and enter the procedure as shown in the image below. Clicking the <Save> button on the “Standard” toolbar creates the new stored procedure. 41 Now execute the stored procedure using either method that selects the “Execute” method from a drop-down menu. The “Run Stored Procedure” dialog window displays and you are prompted to enter values for the parameters as in the image below. 42 The query runs based upon the variable parameter values that were entered by the user and confirmation that the record was updated appears in the “Output” window as in the image below, e.g. “(1 row(s) affected)”. After executing the procedure the deleted record can no longer be found in the Part table when you select the command “Show Table Data” in “Server Explorer” to view the data. 43 TRIGGERS A trigger is an action that occurs automatically in response to an associated database operation such as an INSERT, UPDATE, or DELETE command. Like a stored procedure, a trigger is stored and compiled on the server. Unlike a stored procedure, which is executed in response to a user request, a trigger is executed in response to a command that causes the associated database operation to occur. The examples in this section assume there is a column named OnOrder in the Part table (which we added during this unit in the "Structure Changes" section; if you did not add this field to the table, you should go back and do it now). This column represents the number of units of a part currently on order from all OrderLine records. For example, if there are two separate order lines for a part and the number ordered on one order line is one (1) and the number ordered on the other order line also is one (1), the OnOrder value for that part is two (2). Adding, changing, or deleting order lines affects the value in the OnOrder column for the part. To ensure that the value is updated appropriately, you can use a trigger in SQL Server. These features are explained in the following sections. Suppose the when a user adds an order line, a SQL Server trigger named AddOrderLine is executed. The trigger must update the OnOrder value for the corresponding part to reflect the new order line. For example, if the value in the OnOrder column for part DR93 is two (2) and the user adds an order line on which the part number is DR93 and the number of units ordered is five (5), seven (7) units of part DR93 now are on order. When a record is added to the OrderLine table, the AddOrderLine trigger updates the Part table by adding the number of units ordered on the new order line to the previous value in the OnOrder column. The trigger itself is added as a new object to the table that is being updated directly. There is syntax within the trigger that links to the related table, the one which must be updated automatically. To create a new trigger, first click the “expand” [ ] arrow for the database to view its objects and then click the subsequent “expand” [ ] arrow to drill down into the "Tables" list. Right-click on the name of the table that is to be updated directly and select the command "Add New Trigger" (see the following image). 44 The following is the general format of a trigger: CREATE TRIGGER triggerName ON dbo.tableName FOR INSERT/UPDATE/DELETE AS DECLARE @parameterName1 DECLARE @parameterName2 DECLARE ... dataType dataType [, …] SELECT @parameter1 = columnName1 FROM inserted/deleted SELECT @parameter2 = columnName2 FROM inserted/deleted [, …] ... sqlStatement The first three lines make up the trigger declaration: 1. The first line (CREATE TRIGGER) indicates that the command is creating a trigger and specifies its name (just like a stored procedure, this becomes an ALTER TRIGGER statement once the trigger is saved) 2. The second line (ON) indicates the name of the table for which an operation causes the trigger to execute (the "dbo" prefix is affixed to the table name automatically when the trigger object is saved) 3. The third line (FOR) indicates whether this trigger will "fire" for either an INSERT or an UPDATE or a DELETE operation 45 Like stored procedures, the trigger itself follows the keyword AS. There are three sections to each trigger: 1. Parameter declaration(s) which is/are similar to those used in stored procedures with the difference that each @parameterName is preceded by the keyword DECLARE and the list is not comma-separated 2. Assignment of values to the parameters in SELECT statement(s) (these are not the same as typical SQL SELECT statements); in these SELECT's values from the inserted or updated or deleted record are assigned as the initial values for the parameter; in the FROM clause, if the qualifier is inserted, the value is taken from the field after the record is updated (or inserted); however if the FROM qualifier is deleted, the value is taken from the field before the record is updated (or deleted) 3. A SQL statement that uses the parameter values assigned from the new or updated or deleted record The following is an example of an INSERT trigger. CREATE TRIGGER AddOrderLine ON dbo.OrderLine FOR INSERT AS DECLARE @PartNum VARCHAR(4) DECLARE @NumOrdered INT SELECT @PartNum = PartNum FROM inserted SELECT @NumOrdered = NumOrdered FROM inserted UPDATE PART SET OnOrder = OnOrder + @NumOrdered WHERE PartNum = @PartNum The three lines of the trigger declaration specify: 1. The trigger is named AddOrderLine 2. An operation on the OrderLine table automatically executes the trigger 3. The specific operation on the table that will cause this trigger to "fire" is when a user attempts to insert an order line The command uses the inserted qualifier which means that it gets the values for both the PartNum and NumOrdered fields after they are inserted into the OrderLine table. If an order line is inserted for which the part number is DR93 and the number ordered is five (5), the parameter @PartNum is assigned "DR93" in the first SELECT statement and @NumOrdered is assigned 2 in the second SELECT. The SQL UPDATE command which updates the Part table executes using those parameter values thus adding five (5) to the OnOrder field for part DR93. 46 Create and save the new INSERT trigger as per the following image: After a trigger is created and its editing window has been closed, the trigger object can be found within the table in which it was created. To view or edit any trigger, drill down to the list of tables in the database and then click the “expand” [ ] arrow for the table to view its components (column names and triggers). Right-click the trigger name and select the "Open" command. 47 To test the trigger, insert a new record into the OrderLine table as in the image below. The OnOrder field for the related record within the Part table should update automatically and look like the image below. 48 The following is an example of an UPDATE trigger. CREATE TRIGGER UpdateOrderLine ON dbo.OrderLine FOR UPDATE AS DECLARE @PartNum DECLARE @OldNumOrdered DECLARE @NewNumOrdered VARCHAR(4) INT INT SELECT @PartNum = PartNum FROM inserted SELECT @OldNumOrdered = NumOrdered FROM deleted SELECT @NewNumOrdered = NumOrdered FROM inserted UPDATE PART SET OnOrder = OnOrder + @NewNumOrdered - @OldNumOrdered WHERE PartNum = @PartNum The three lines of the trigger declaration specify: 1. The trigger is named UpdateOrderLine 2. An operation on the OrderLine table automatically executes the trigger 3. The specific operation on this table that will cause this trigger to "fire" will be when a user attempts to update an order line (rather than after inserting a new record as in the AddOrderLine trigger) The computation to update the OnOrder column includes both @NewNumOrdered and @OldNumOrdered parameters. As with the AddOrderLine trigger, @NewNumOrdered refers to the new value after the update. In an UPDATE trigger, however, there is also an old value, which is the value before the update takes place stored in the @OldNumOrdered field. The command uses the deleted qualifier for the old value of NumOrdered which means that it gets the value before it is updated in the OrderLine table. It uses the inserted qualifier for the new value of NumOrdered which means that it gets that value after it is updated in the OrderLine table. In addition it gets the value for the PartNum field after the update (it uses the inserted qualifier) although in this case it really does not make a difference (it could have used deleted) since the PartNum field value is unchange. If an order line is updated for which the part number is DR93 and the number ordered was five (5) before the update but was changed to three (3), the parameter @PartNum is assigned "DR93" in the first SELECT statement, @OldNumOrdered is assigned 5 in the second SELECT and @NewNumOrdered is assigned 3 in the third SELECT. The SQL UPDATE command which updates the Part table executes using those parameter values. Adding @NewNumOrdered and subtracting @OldNumOrdered results in a net decrease of two (2) to the OnOrder field for part DR93. (The net change also could be positive, in which case the OnOrder value increases.) 49 Create and save the new UPDATE trigger as per the following image: To test the trigger, update the value of the NumOrdered field in the previously inserted record within the OrderLine table as in the image below. 50 The OnOrder field for the related record within the Part table should update automatically and look like the image below. The following is an example of a DELETE trigger. CREATE TRIGGER DeleteOrderLine ON dbo.OrderLine FOR DELETE AS DECLARE @PartNum VARCHAR(4) DECLARE @NumOrdered INT SELECT @PartNum =PartNum FROM deleted SELECT @NumOrdered = NumOrdered FROM deleted UPDATE PART SET OnOrder = OnOrder - @NumOrdered WHERE PartNum = @PartNum The three lines of the trigger declaration specify: 1. The trigger is named DeleteOrderLine 2. An operation on the OrderLine table automatically executes the trigger 3. The specific operation on the table that will cause this trigger will be "fire" when a user attempts to delete an order line The DeleteOrderLine trigger performs a function similar to the other two triggers. When an order line is deleted, the OnOrder value for the corresponding part is updated by subtracting the @NumOrdered parameter from the current OnOrder value. 51 The command uses the deleted qualifier which means that it gets the values for both the PartNum and NumOrdered fields before the record is deleted from the OrderLine table. If an order line is deleted for which the part number was DR93 and the number ordered was three (3), the parameter @PartNum is assigned "DR93" in the first SELECT statement and @NumOrdered is assigned 3 in the second SELECT. The SQL UPDATE command which updates the Part table executes using those parameter values thus subtracting three (3) from the OnOrder field for part DR93. Create and save the new DELETE trigger as per the following image: To test the trigger, select and delete the previously inserted (and updated) record in the OrderLine table as in the image below. 52 The OnOrder field for the related record within the Part table should update automatically and look like the image below. 53