Lab for LU02 the Relational Database Model

Total Page:16

File Type:pdf, Size:1020Kb

Lab for LU02 the Relational Database Model

LAB FOR LU02 – THE RELATIONAL DATABASE MODEL

OVERVIEW

In this lab we will learn the about the relational database model and how it is put into practice using the Microsoft SQL server instance on the Ist-s-students server.

LEARNING OBJECTIVES

By the time the lab is complete you should be able to:

. Connect to SQL Server and log-on to your database and use its basic capabilities. . Explain the basic concepts of the relational model . Describe the relational database terminology. . Explain the importance of Entity integrity and Referential integrity . Describe how and when to use logical domain to constrain and clean data input. . Understand primary, foreign and surrogate keys . Demonstrate how to create database objects and enter data on SQL server. . Describe the characteristics of good naming conventions and explain why they are important

WHAT YOU WILL NEED

To complete this lab, you will need:

1. Your SQL server login and password on the Iststudents server. To get this, please follow this link to the Iststudents tutorials and watch “Lesson 2” http://itservices.syr.edu/HelpandSupport/IststudentsHelp/tabid/189/Default.aspx

2. SQL Server Management Studio so that you can connect to your SQL database. SQL Server Management Studio is installed in the iSchool’s Labs. If you need a personal copy, visit: http://itservices.syr.edu/MyITServices/MyDownloads/tabid/154/Default.aspx

3. An active connection to the SQL Server instance on Iststudents. To watch a tutorial which shows you how to connect to the SQL server watch “Lesson 4” on this page: http://itservices.syr.edu/HelpandSupport/IststudentsHelp/tabid/189/Default.aspx

LAB GOALS

This lab consists of 5 parts:

1. An overview of SQL Server Management Studio

2. Get familiar with the Fudgemart Schema

3. Create basic tables

1 / 14 4. Set basic constraints, primary and foreign keys on the Fudgemart tables

5. Enter data into the tables – to understand how the constraints work.

PART 1: CONNECTING TO YOUR DATABASE ON ISTSTUDENTS

In just about each lab from this point forward, you will need to connect to your SQL Server database on the Iststudents server (ist-s-students.syr.edu) using SQL Server Management Studio. It’s a really good idea to get this process down pat so that you don’t waste precious lab time on trivial issues. 

1A: CONNECTING TO SQL SERVER

1) Open the Microsoft SQL Server Management Studio in the Start menu (in the SQL Server 2005 group).

2) From the menu, select File  Connect Object Explorer.

3) The following dialog will appear:

This is always the same- Server host name and TCP port.

Enter your logon, as retrieved in step 1 of the “What you will need” section of this document

Never check this in Enter your password the public labs!!

4) And when you’re ready, click Connect.

IMPORTANT: IF YOU’RE HAVING TROUBLE, PLEASE REVIEW THE STEPS IN THE “WHAT YOU WILL NEED” SECTION OF THIS DOCUMENT. REMEMBER THAT IF YOU CHANGE YOUR PASSWORD, IT CAN TAKE UP TO 5 MINUTES FOR THAT CHANGE TO TAKE EFFECT.

2 / 14 5) After the SQL Server Management Studio program connects to the server, browse the object explorer on the left side of the window to access your database.

Browse the object explorer until you find your database.

This side of the screen allows you to manipulate database object, add data, and write SQL scripts.

NOTE: YOUR DATABASE NAME IS THE SAME AS YOUR LOGON NAME!

Next, please review the various steps in the SQL server Quick how-to. This will be useful for the subsequent portions of the lab.

3 / 14 1B: SQL SERVER MANAGEMENT STUDIO QUICK HOW-TO:

How do I … Follow these steps

Create a table From the object explorer, right click on your database Tables and select New Table. This will open the table designer window in a new tab.

… Add a column From the table designer window, go to a blank row and enter the column name, select a data type, and whether or not to allow null.

… Remove a column From the table designer window, right click on the column you wish to delete and select delete column from the menu.

… Set a primary key constraint From the table designer window, right click on the column you wish to set as primary key, (and establish entity integrity) and select set primary key from the menu. NOTE: If you need to select more than one column for primary key (composite primary key) then hold down the CTRL key while left- clicking on each column, before right clicking.

… Set an identity column From the table designer window, set a column to data type int. While that column is (surrogate key) selected, change the identity specification in the column properties section so that (Is Identity) reads Yes.

… Set a default value From the table designer window, select the column for which you wish to set the default. While that column is selected, change the default value or binding in the column properties section to the desired value or expression.

… Manage a check constraint From the table designer window, right-click on any column and select check constraints from the menu. The check constraints window will appear. Click Add to add a constraint and at minimum, enter the expression and name. When you’re done, click close.

… Create a unique constraint (secondary From the table designer window, right-click on any column and select indexes / keys from key) the menu. The indexes / keys window will appear. Click Add to add a unique constraint and at minimum, select the columns to use, set the type to unique key and enter the constraint name. When you’re done, click close.

… Add a foreign key constraint From the table designer window, right-click on any column and select relationships from (and establish referential integrity) the menu. The foreign key relationships window will appear. Click Add to add a foreign key and at minimum, select the tables and columns to use under the Tables and columns specification and enter a foreign key name. When you’re done, click close.

Modify a table From the object explorer, right click on the database table you wish to modify, and select Modify from the menu. Once again the table designer window will appear.

Delete (Drop a table) From the object explorer, right click on the database table you wish to delete, and select Delete from the menu. This will drop the table and all related constraints (primary / foreign key, unique, and check constraints associated with the table).

4 / 14 Create a database diagram From the object explorer, right click on database diagrams and from the menu, select new database diagram. This will open the database diagram designer window in a new tab.

… Add a table to the diagram From the database diagram designer window, right click in the whitespace of your diagram and select add table from the menu. Select the table(s) to add from the tables window, and click add. When you’re done, click close.

… Add some text to the diagram From the database diagram designer window, right click in the whitespace of your diagram and select new text annotation from the menu. Enter your text in the text box.

… Remove a table from the diagram From the database diagram designer window, right click on the title of the table window and select remove from diagram from the menu tables window, and click add. When you’re done, click close.

… Add a foreign key constraint From the database diagram designer window, simply drag the primary key and drop on the foreign key in the foreign key table. This will make a new relationship link.

… Remove a foreign key constraint From the database diagram designer window, right click on the relationship link and select delete relationships from database from the menu.

To enter data into a table From the object explorer, right click on the table and select open table from the menu.

PART 2: FYI: THE FUDGEMART DATABASE SCHEMA

Throughout the semester we will use several different case studies to help enforce the concepts we learn in class. One of the recurring case-studies we will use in class and the labs is the Fudgemart database. This database supports the business operations of a fictitious mega-store retailer and e-tailer called Fudgemart. The Fudgemart database supports all aspects of the business from human resources, to payroll, to sales transactions, and e- commerce. In each lab we will add new database objects and data to the Fudgemart schema.

PRODUCTS AND VENDORS

Here’s the conceptual model for products and vendors in the Fudgemart database:

5 / 14 A Product is supplied by 1 and Products only 1 Vendor A Vendor supplies 0 or More Products

Supplies / Supplied by Vendors

PART 3: CREATE BASIC TABLES

In this part you will create the tables which are part of the fudgemart product and vendors schema. Use the How- To in part 1b for the details of how to create the following 3 tables on SQL Server:

3A: FUDGEMART_PRODUCTS

6 / 14 3B: FUDGEMART_VENDORS

3C: FUDGEMART_DEPARTMENTS_LOOKUP

PART 4: SET CONSTRAINTS: CHECKS, DEFAULTS, UNIQUES, PRIMARY AND FOREIGN KEYS

In this part, you will set the physical and logical domain on the tables using constraints. You will also learn how to create a database diagram which represents the physical data model for the Fudgemart schema.

4A: CONSTRAINTS FOR FUDGEMART_PRODUCTS TABLE

Add the following constraints to the table, in order:

Column / Constraint name Constraint Type Value

Product_id Identity (surrogate key) Yes

Product_retail_price Default Value 0

Product_wholesale_price Default Value 0

Product_add_date Default Value (Getdate())

Product_is_active Default Value 1

U_Product_name Unique Key Product_name (ASC)

CK_fudgemart_products_prices Check ([product_wholesale_price]>=(0) AND [product_retail_price]>=(0))

7 / 14 4B: CONSTRAINTS FOR FUDGEMART_VENDORS TABLE Add the following constraints to the table, in order:

Column / Constraint name Constraint Type Value

Vendor_id Identity (surrogate key) Yes

U_Vendor_name Unique Key Vendor_Name (ASC)

8 / 14 4D: SET PRIMARY KEY CONSTRAINTS FOR EACH TABLE

Set the primary keys on the tables as follows:

Table Name Primary Key Column(s)

Fudgemart_products Product_id

Fudgemart_departments_lookup Department_id

Fudgemart_vendors Vendor_id

4E: MAKE FOREIGN KEY CONSTRAINTS TO CONNECT THE TABLES USING A DATABASE DIAGRAM

In this last step in part 4, make a database diagram, called fudgemart, and add each of the 3 tables to the diagram. Create foreign key constraints by dragging and dropping as follows: (Note the foreign keys are created on the “drop” side of the drag and drop operation.)

Drag From Drop To

Table Column Table Column fudgemart_departments_lookup department_id fudgemart_products product_department fudgemart_vendors vendor_id fudgemart_products product_vendor_id

When you’re finished, your diagram should look something like this:

9 / 14

10 / 14 PART 5: ENTER DATA INTO THE TABLES

In this final step we will test our database design (the metadata we created). This lesson will show you how the constraints work to help keep the data ARTC.

5A: UNDERSTANDING IDENTITIES (SURROGATE KEYS) AND UNIQUE CONSTRAINTS

First, enter the following rows into fudgemart_vendors. NOTE: you cannot enter the vendor_id since it is a surrogate key, it will be chosen for you. Don’t worry if they are not exactly the same as shown. NOTE2: To enter null, don’t enter a value for the attribute.

Next, enter the following row, which should cause an error due to a unique constraint:

Here is the error you should see:

This tells us our unique constraint is working and we cannot enter the same vendor_name more than once.

Finally, enter one more row:

Notice how the surrogate key skipped a record. That is because the DBMS needs to allocate that number before the update is attempted. Once the insert fails, the number is lost and not reused. This is an important feature of most DBMSs allows multiple users to insert data and not “step on each other’s toes” so to speak.

When you’re done, you should have the following data in your fudgemart_vendors table:

11 / 14

5B: UNDERSTANDING FOREIGN KEY CONSTRAINTS, CHECK CONSTRAINTS, AND DEFAULTS

This next part will help us understand how foreign key constraints work. Try and enter the following into the fudgemart_prodcuts table:

And you will see this error, which tells us you cannot insert a row into fudgemart_products without there first being a row in fudgemart_departments_lookup that matches the data we’re using in products.

Why does this happen? Because foreign key constraints implement referential integrity, which states the value we enter into fudgemart_products for department must either be NULL or a value in fudgemart_departments_lookup. But since we’re not allowing NULL, we need a value in fudgemart_departments_lookup. How do we remedy this? First, enter the following into fudgemart_departments_lookup:

Then you can enter the row into fudgemart_products: (Leave the product_is_active, product_add_date, and product_description columns as NULL.)

Some questions:

. Why is the product_id not 1? Because of our failed inserts and the way surrogate keys are implemented.

. Why did the row use “True” for product_is_active, and the current date for product_add_date? Well, you have defaults to thank for that.

12 / 14 This last part I’ll leave as an exercise – try to add another row where the product_retail_price is -100.00 will the row insert? What constraint catches this error?

THIS CONCLUDES OUR LAB.

13 / 14 Lab Questions

1) This lab had you create the metadata objects in a specific order: tables, constraints, primary keys, foreign keys, saving the table design between each step. Why? (Hint: Can you create them in any order you like? What about the primary and foreign key constraints?)

2) Can the database designer control the “identity” used by a surrogate key? What are your options, if any?

3) As a follow-up to question 2), when a new row is added, can you guarantee a specific number will be used? Does this really matter? Explain.

4) We noticed in part 5, it is impossible to enter a row in fudgemart_products without there being at least one row in fudgemart_products_lookup and one row in fudgemart_vendors. What aspect of our metadata (database design) makes this so?

5) As a follow-up to question 4), what could be changed in the database design to relax this requirement, but not break the existing relationships? Would altering the database in this manner change the meaning, and our intensions behind the original database design?

6) Even though the vendor_name column is not a primary key, how come we could not enter the same name more than once? Is this a good way to do database design? Why not use vendor_name as primary key instead?

14 / 14

Recommended publications