+ All Categories
Home > Documents > Microsoft SQL Server 2008

Microsoft SQL Server 2008

Date post: 16-Feb-2016
Category:
Upload: wells
View: 45 times
Download: 0 times
Share this document with a friend
Description:
Microsoft SQL Server 2008. From the Program menu choose: Microsoft SQL Server 2008 R2  SQL Server Management Studio . You may see a window indicating the MS SQL Server Management Studio is configuring for first time use. Be patient; it sometimes takes a while. . - PowerPoint PPT Presentation
Popular Tags:
27
Microsoft SQL Server 2008 From the Program menu choose: Microsoft SQL Server 2008 R2 SQL Server Management Studio. You may see a window indicating the MS SQL Server Management Studio is configuring for first time use. Be patient; it sometimes takes a while.
Transcript
Page 1: Microsoft SQL Server 2008

Microsoft SQL Server 2008

From the Program menu choose: Microsoft SQL Server 2008 R2 SQL Server Management Studio. You may see a window indicating the MS SQL Server Management Studio is configuring for first time use. Be patient; it sometimes takes a while.

Page 2: Microsoft SQL Server 2008

In the Connect to Server window, select or enter:

Database Engine for the Server typeICSD for the server nameWindows Authentication for Authentication.Then press the connect button.

In the Object Explorer pane, expand Databases. If you don’t see an Object Explorer pane, select it from the View menu.

Page 3: Microsoft SQL Server 2008

For this course there are four databases (identified by folders):

CS451 FamilyCS451 PackersCS451 SupplierCS451 University

You should have read-only access to each one. I will use these during the semester.

Page 4: Microsoft SQL Server 2008

To see the tables in one of them, expand the database folder and then the Tables folder.Right click on one of the table names and select Select Top 1000 RowsTest this and let me know of any access problems.You should not be able to change the data in any of these tables.

Page 5: Microsoft SQL Server 2008

If you want tables you can edit, you can create tables in your group database or copy (import) them from one of the existing databases.Subsequent slides show how to do both.

Page 6: Microsoft SQL Server 2008

Creating Tables in SQL ServerYour group databases are named CS451 G1 (or G2, G3, G4, etc.)Expand the folder for the database assigned to you and right click on the Tables folderselect New Table.

Page 7: Microsoft SQL Server 2008

In the resulting window you can specify column names, types, length, and whether null values are allowed.A column that has no value may be assigned null.Any comparison with data involving that column will ALWAYS evaluate to false.Create some columns using whatever names and types you want.

Page 8: Microsoft SQL Server 2008

To create a primary key (unique identifier):

highlight the appropriate column(s) by clicking in the area to the left of the column name. You can select more than one column (composite key) by using the Shift key while selecting additional columns With the column(s) highlighted, right click and select Set Primary Key to specify the highlighted column(s) as the primary key.

Page 9: Microsoft SQL Server 2008

To save the tableselect FileSave table and specify the table name.

To add dataright click on the appropriate table name (in the Tables folder)select Edit Top 200 Rows and start entering data.

Click on the red exclamation point (!) in the toolbar to save the data or simply close the table

Page 10: Microsoft SQL Server 2008

Modifying Tables/Setting Up Table Constraints

Open a table in design view (Right click on the table name and select Design).Can add, remove, or change columns as needed.When you select a column you can change various column properties that appear in the lower pane. For this course you don’t need to worry about most of those properties.

Page 11: Microsoft SQL Server 2008

In the Table Designer toolbar there are some icons associated with managing check constraints, managing indexes and keys, and relationships. Alternatively, you can right click in the table’s design window and select one of these options.

Page 12: Microsoft SQL Server 2008

To put constraints on specific fieldsSelect a columnRight click and select Check Constraints.Click on the Add button.Each constraint has a default name. You can provide your own in the (Name) textbox.Enter an expression that defines a constraint for a specific attribute. Save the table. The next slide has some examples.

Page 13: Microsoft SQL Server 2008

Table/Column Constraint Meaning

Supplier/S ([status] >= 0 and [status] <= 50) Status lies between 0 and 50, inclusive

Supplier/S ([S#] like 'S[0-8]') Supplier must start with ‘S’ and be followed by a digit that is not 9.

Supplier/SP ([QTY] % 100 = 0) QTY must be a multiple of 100.

University/Student ([credits] >= 0) Credits must not be negative.

University/Student ([gpa] >= 0 and [gpa] <= 4) GPA must lie between 0 and 4, inclusive.

Page 14: Microsoft SQL Server 2008

Anything entered as data must meet the specified constraintTakes the burden off of the programmers’ to verify data integrity.Results in more consistent data.

Page 15: Microsoft SQL Server 2008

Setting up referential integrity constraints (foreign keys)

Assume I have tables A, B, and AB and that AB should contain foreign keys that match primary keys in A and B. Here’s how you might proceed.Open AB in design view.Click on the icon (or right click and select the option) associated with Relationships.

Page 16: Microsoft SQL Server 2008

As before click on the Add button and provide a name for the constraint. Look for an option Tables and Columns Specification and click the ellipsis button (…) associated with it.

Page 17: Microsoft SQL Server 2008

In the menu associated with Primary Key Table choose table ABelow that, choose the appropriate column. Make sure that column is a primary key for A.Under the Foreign key Table specification, choose the foreign key field from table AB

Page 18: Microsoft SQL Server 2008

Make sure you save the changes.You will not be allowed to make changes that violate referential integrity.

Page 19: Microsoft SQL Server 2008

Creating ER Diagrams that reflect your referential integrity rules

Select Database Diagrams under the specified database. Right click and select New Database Diagram.Select the tables that will appear in the diagram and click Add and then Close.

Page 20: Microsoft SQL Server 2008

The diagram appears and reflects the referential integrity rules you defined in the database constraints.Save the diagram and give it a name.

Page 21: Microsoft SQL Server 2008

Importing base tables from one database to another

NOTE: This will move data to a destination database but it does NOT preserve any constraints defined in the source database table.

Page 22: Microsoft SQL Server 2008

Right Click on the destination database (your group database) and select tasks Import Data. An import Wizard will be activated.Click Next.

Page 23: Microsoft SQL Server 2008

Make the following selections to define the source database before clicking Next:

Under Data Source select Microsoft OLE DB Provider for SQL Server. Under Server name select ICSD. If you don’t see it, just type it inSelect the Use Windows Authentication radio button.Under Database select the database you want to copy. For example: CS451 Supplier.Click Next.

Page 24: Microsoft SQL Server 2008

Make/verify the following selections for your destination database:

Under Destination select Microsoft OLE DB Provider for SQL Server. Under Server select (or type) ICSD.Select the Use Windows Authentication radio button.Under Database select the database you want to copy to. For example: CS451_G1.Click Next.

Page 25: Microsoft SQL Server 2008

Select the radio button for Copy data from one or more tables or views. Click Next. Select ONLY the base tables you want to copy. DO NOT select views.Click Next.Check the checkbox for Execute Immediately and click Next.

Page 26: Microsoft SQL Server 2008

Verify that the summary provided is correct and click finish.This just copies the tables but does not preserve the constraints that may exist on the tables.To copy the constraints we’ll need to learn about scripting, which we’ll do later.

Page 27: Microsoft SQL Server 2008

The new tables may not appear in your list of tables (remember, you’re still looking at what the client Studio Management has. It has not yet gotten the updated information from the server). However, there is a refresh icon near the top just under Object Explorer. Click on it and the new tables should appear in your object explorer view.


Recommended