20
SQL Server 2008 Tutorial: Database Creation and Modification J.G. Zheng Spring 2012 IT 4153 Advanced Database

SQL Server 2008 Tutorial: Database Creation and Modification

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SQL Server 2008 Tutorial: Database Creation and Modification

SQL Server 2008 Tutorial: Database Creation and Modification

J.G. Zheng

Spring 2012

IT 4153 Advanced Database

Page 2: SQL Server 2008 Tutorial: Database Creation and Modification

Overview

Creating and modifying structures using the Management Studio GUI

Table designer

Relationship diagram designer

View designer

Executing SQL scripts

Page 3: SQL Server 2008 Tutorial: Database Creation and Modification

Example Logical model in Visio Other requirements Email is unique Budget < 500000

Employee

PK EmpNumber smallint

FirstName nvarchar(50)

LastName nvarchar(50)

Phone char(10)

Email nvarchar(100)

Salary numeric(7,2)

BirthDate date

Project

PK ProjectId int identity

Name nvarchar(100)

Description nvarchar(4000)

StartDate smalldatetime

Budget decimal(8,2)

Assignment

PK,FK1 EmployeeNumber smallint

PK,FK2 ProjectId int

HoursWorkd numeric(6,2)

u:C

d:C

u:C

d:C

Page 4: SQL Server 2008 Tutorial: Database Creation and Modification

Create a Database

4

Right-click and select “New Database”

Name the new database

Customize and remember the file location.

Page 5: SQL Server 2008 Tutorial: Database Creation and Modification

Create Tables

5

1. Right-click and select “New Table”

Column definitions.

More Column definitions.

Default value.

Page 6: SQL Server 2008 Tutorial: Database Creation and Modification

Primary Key

6

Choose a column and set as PK.

Save table as “Employee”

Enter more columns

Page 7: SQL Server 2008 Tutorial: Database Creation and Modification

Surrogate Key (Identity Column)

7

Create the second “Project” table.

ProjectId is the PK. To make it a surrogate key (auto number), first change the data type to int.

Then set the (Is Identity) property to Yes with increment and seed (starting value)

Page 8: SQL Server 2008 Tutorial: Database Creation and Modification

Composite Key

8

Create the intersection table.

Highlight two columns (hold Ctrl key) and set PK.

Page 9: SQL Server 2008 Tutorial: Database Creation and Modification

Relationship Diagram

9

Right click here to create a new relationship diagram

First, add all tables and arrange them nicely in the work area.

Page 10: SQL Server 2008 Tutorial: Database Creation and Modification

View More Metadata

10

Right click a table to change its table view. Use “standard” to display data type and null constraint.

Use this button to add tables

Use these buttons to arrange tables, such as auto-sizing a table. You can also move table around and zoom the view manually.

Page 11: SQL Server 2008 Tutorial: Database Creation and Modification

Modify Columns in Relationship Diagram

11

The FK and PK pair have to have the same data type. Change it to “smallint” and save the diagram. Note: you may encounter a error message to prevent you to do it. If so, see next slide.

Page 12: SQL Server 2008 Tutorial: Database Creation and Modification

!!Set Option!!

12

Go to Menu, Tools, Options and uncheck this option.

Page 13: SQL Server 2008 Tutorial: Database Creation and Modification

Create Relationship

13

Drag the FK column to the primary key column. When releasing the mouse, a dialog will pop up to finish the rest steps. Once the relationship is created, you will see a symbol like this.

Right click on the relationship line and you can delete it.

* means not saved.

Page 14: SQL Server 2008 Tutorial: Database Creation and Modification

More Commands

14

Highlight a table and you can see these command buttons enabled.

View and configure foreign keys

Remove a table from diagram

Show relationship labels

Manage keys and indexes

Page 15: SQL Server 2008 Tutorial: Database Creation and Modification

Referential Actions

15

First, select a table and view its foreign keys

Change referential actions

Click close to finish.

Page 16: SQL Server 2008 Tutorial: Database Creation and Modification

Type SQL in the Query Window

16

4. When finished, click the debug button to see if there are errors. If no errors, execute the script to create tables and other structures.

1. You can also type a new script by clicking on the New Query button.

3. Make sure you are in the right database

2. Type the script here.

Page 17: SQL Server 2008 Tutorial: Database Creation and Modification

Use Script Files

17

You can open an existing script file (.sql).

Make sure you are in the right database

After it is opened, click the execute button to run the script.

A sample script file “DDL-Project.sql” can be downloaded from the course website.

Page 18: SQL Server 2008 Tutorial: Database Creation and Modification

Enter and Update Data

18

Right-click the table and select “Design” to modify the metadata.

Right-click the table and select “Edit Top 200 Rows” to enter and modify data.

The red mark indicates the data is not saved to table yet. Select another row to save the data.

New record

Page 19: SQL Server 2008 Tutorial: Database Creation and Modification

Error Message

19

If you move away from the current record without entering a value for the “required” column, you will get this error message.

Page 20: SQL Server 2008 Tutorial: Database Creation and Modification

Create Views

20

A view is like a virtual table. You can “SELECT FROM” a view just like a table.