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
Concurrency control wikipedia , lookup
Functional Database Model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Ingres (database) wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Clusterpoint wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
L AB – T HE R ELATIONAL D ATABASE M ODEL O VERVIEW 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. L EARNING O BJECTIVES 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 W HAT 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://www.youtube.com/watch?v=2TXV61So-i0 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: https://answers.syr.edu/display/ischool/Microsoft+SQL+Server 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://ischool.syr.edu/facstaff/it/support/videotutorials.aspx?webSiteID=e4Rth5MAgUCVnQB7V_jKiw&a mp;videoID=Jk1hC0lBoUSoxsQYBFd67Q L AB G OALS This lab consists of 5 parts: 1. An overview of SQL Server Management Studio 1 / 11 2. Get familiar with the Fudgemart Schema 3. Create basic tables 4. Set basic constraints, primary and foreign keys on the Fudgemart tables 5. Enter data into the tables – to understand how the constraints work. P ART 1: C ONNECTING TO YOUR DATABASE ON I STSTUDENTS 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. 1 A : C ONNECTING 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 sameServer 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 the public labs!! Enter your password 4) And when you’re ready, click Connect. IMPORTANT: IF YOU’RE HAVING TROUBLE, PLEASE REVIEW THE STEPS IN THE “W HAT YOU W ILL 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 / 11 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. NOTE: YOU MIGHT WANT TO PRINT THE FOLLOWING PAGE FOR REFERENCE. 3 / 11 1 B : SQL S ERVER M ANAGEMENT S TUDIO QUICK H OW -T O : 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. 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. From the table designer window, right click on the column you wish to delete and select delete column from the menu. From the table designer window, right click on the column you wish to set as primary key, 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 leftclicking on each column, before right clicking. From the table designer window, set a column to data type int. While that column is selected, change the identity specification in the column properties section so that (Is Identity) reads Yes. 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. 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. From the table designer window, right-click on any column and select indexes / keys from 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. From the table designer window, right-click on any column and select relationships from 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. 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. 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). 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. 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. 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. 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. 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. From the database diagram designer window, right click on the relationship link and select delete relationships from database from the menu. From the object explorer, right click on the table and select open table from the menu. … Add a column … Remove a column … Set a primary key constraint (and establish entity integrity) … Set an identity column (surrogate key) … Set a default value … Manage a check constraint … Create a unique constraint (secondary key) … Add a foreign key constraint (and establish referential integrity) Modify a table Delete (Drop a table) Create a database diagram … Add a table to the diagram … Add some text to the diagram … Remove a table from the diagram … Add a foreign key constraint … Remove a foreign key constraint To enter data into a table 4 / 11 P ART 2: FYI: T HE F UDGEMART D ATABASE S CHEMA 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 ecommerce. In each lab we will add new database objects and data to the Fudgemart schema. P RODUCTS AND V ENDORS Here’s the conceptual model for products and vendors in the Fudgemart database: Products A Vendor supplies 0 or More Products Vendors Supplies / Supplied by P ART 3: C REATE A Product is supplied by 1 and only 1 Vendor BASIC TABLES In this part you will create the tables which are part of the fudgemart product and vendors schema. Use the HowTo in part 1b for the details of how to create the following 3 tables on SQL Server: Important: Don’t save the tables with the asterisk * in the name! After you create each table, close the table edit window. 5 / 11 3A: FUDGEMART _ PRODUCTS 3B: FUDGEMART _ VENDORS 3C: FUDGEMART _ DEPARTMENTS _ LOOKUP P ART 4: S ET C ONSTRAINTS : 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. For this step you will re-open each table in design mode and add the constraints to the table. 4A: CONSTRAINTS FOR FUDGEMART _ PRODUCTS TABLE Add the following constraints to the table, in order: (Refer to Part 1.B for details on how to complete these.) 6 / 11 Column / Constraint name Constraint Type Value Product_id Product_retail_price Product_wholesale_price Product_add_date Product_is_active U_Product_name CK_fudgemart_products_prices Identity (surrogate key) Default Value Default Value Default Value Default Value Unique Key Check Yes 0 0 (Getdate()) 1 Product_name (ASC) ([product_wholesale_price]>=(0) AND [product_retail_price]>=(0)) Important: If you get an error when saving this table “Saving changes is not permitted” follow the instructions at this site: http://www.mssqltips.com/tip.asp?tip=1740 4B: CONSTRAINTS FOR FUDGEMART _ VENDORS TABLE Add the following constraints to the table, in order: Column / Constraint name Constraint Type Value Vendor_id U_Vendor_name Identity (surrogate key) Unique Key Yes Vendor_Name (ASC) 4 D : S ET PRIMARY KEY CONST RAINTS FOR EACH TABLE Set the primary keys on the tables as follows: Table Name Primary Key Column(s) Fudgemart_products Fudgemart_departments_lookup Fudgemart_vendors Product_id Department_id Vendor_id 4E: MAKE FOREIGN KEY CON STRAINTS TO CONNECT THE TABLES USING A D ATABASE 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 Table fudgemart_departments_lookup fudgemart_vendors Drop To Column department_id vendor_id Table fudgemart_products fudgemart_products When you’re finished, your diagram should look something like this: 7 / 11 Column product_department product_vendor_id P ART 5: E NTER DATA INTO THE T ABLES 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. 5 A : U NDERSTANDING I DENTITIES (S URROGATE K EYS ) AND U NIQUE C ONSTRAINTS 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: 8 / 11 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: 5 B : U NDERSTANDING F OREIGN K EY 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. 9 / 11 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. THIS CONCLUDES OUR LAB. L AB Q UESTIONS 1) In this lab you had to create the metadata objects in a specific order: 1. Table columns, 2. column constraints, 3. primary key constraints, 4. foreign key constraints, 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) Open the fudgemart_products table. Try to add another product where the product_retail_price is -100.00. Will the row insert? What constraint catches this error and why? 3) What is the difference between a primary key constraint and a unique constraint? 4) Can the database designer control the “identity” used by a surrogate key? That is to say, can you dictate the behavior of a surrogate key? What are your options, if any? 5) As a follow-up to the previous question when a new row is added, can you guarantee a specific number will be used as the surrogate key? Does this really matter? Explain. 6) We noticed in part 5, it is impossible to enter a row in the fudgemart_products table 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? 10 / 11 7) As a follow-up to the previous question what could be changed in the database design to relax a bitbut , but not break the existing relationships? 8) Would altering the database in this manner described in the previous question change the meaning, and our intensions behind the original database design? Explain why or why not. 9) Even though the vendor_name column is not a primary key, how come we could not enter the same name more than once? 10) Why not use vendor_name as primary key instead for the vendors table? Why is vendor_name a poor choice for primary key? 11 / 11