Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Practical Database Programming With Java

.pdf
Скачиваний:
778
Добавлен:
10.06.2015
Размер:
31.58 Mб
Скачать

2.10 Create Microsoft SQL Server 2008 Sample Database 47

Click the OK button to finish this saving. A new table named dbo.LogIn is added into the new database under the Tables folder in the Object Explorer window.

To add data into this LogIn table, right click on this table and select Edit Top 200 Rows item from the pop-up menu. Enter all login data that is shown in Table 2.18 into this table. Your finished LogIn table should match one that is shown in Figure 2.25.

Table 2.18. The data in the LogIn table

user_name

pass_word

faculty_id

student_id

abrown

america

B66750

NULL

ajade

tryagain

NULL

A97850

awoods

smart

NULL

A78835

banderson

birthday

A52990

NULL

bvalley

see

NULL

B92996

dangles

tomorrow

A77587

NULL

hsmith

try

NULL

H10210

jerica

excellent

NULL

J77896

jhenry

test

H99118

NULL

jking

goodman

K69880

NULL

sbhalla

india

B86590

NULL

sjohnson

jermany

J33486

NULL

ybai

reback

B78880

NULL

Figure 2.25. The finished LogIn table.

48 Chapter 2 Introduction to Databases

Figure 2.26. The design view of the Faculty table.

One point to be noted is that you must place a NULL for any field that has no value in this LogIn table, since it is different for the blank field between the Microsoft Access and the SQL Server database. Go to the File|Save All item to save this table. Now let’s continue to create the second table Faculty.

2.10.2 Create the Faculty Table

Right click on the Tables folder under the CSE_DEPT database folder and select the New Table item to open the design view of a new table, which is shown in Figure 2.26.

For this table, we have seven columns: faculty_id, faculty_name, office, phone, college, title, and email. The data types for the columns faculty_id and faculty_name are nvarchar(50), and all other data types can be either text or nvarchar(50), since all of them are string variables. The reason we selected the nvarchar(50) as the data type for the faculty_id is that a primary key can work for this data type, but it does not work for the text. The finished design view of the Faculty table should match one that is shown in Figure 2.26.

Since we selected the faculty_id column as the primary key, click on that row and then go to the Toolbar and select the Primary Key tool. In this way, the faculty_id is chosen as the primary key for this table, which is shown in Figure 2.26.

Now go to the File menu item and select the Save Table_1, and enter Faculty into the box for the Choose Name dialog as the name for this table; click OK to save this table.

Next you need to enter the data into this Faculty table. To do that, first open the table by right clicking on the dbo.Faculty folder under the CSE_DEPT database folder in the Object Explorer window, and then select Open Table item to open this table. Enter the data that is shown in Table 2.19 into this Faculty table.

2.10 Create Microsoft SQL Server 2008 Sample Database 49

Table 2.19. The data in the Faculty table

faculty_id

faculty_name

office

phone

college

title

email

A52990

Black Anderson

MTC-218

750-378-9987

Virginia Tech

Professor

banderson@college.edu

A77587

Debby Angles

MTC-320

750-330-2276

University of Chicago

Associate Professor

dangles@college.edu

B66750

Alice Brown

MTC-257

750-330-6650

University of Florida

Assistant Professor

abrown@college.edu

B78880

Ying Bai

MTC-211

750-378-1148

Florida Atlantic University

Associate Professor

ybai@college.edu

B86590

Satish Bhalla

MTC-214

750-378-1061

University of Notre Dame

Associate Professor

sbhalla@college.edu

H99118

Jeff Henry

MTC-336

750-330-8650

Ohio State University

Associate Professor

jhenry@college.edu

J33486

Steve Johnson

MTC-118

750-330-1116

Harvard University

Distinguished Professor

sjohnson@college.edu

K69880

Jenney King

MTC-324

750-378-1230

East Florida University

Professor

jking@college.edu

Figure 2.27. The completed Faculty table.

Your finished Faculty table should match one that is shown in Figure 2.27.

Now go to the File menu item and select Save All to save this completed Faculty data table. Your finished Faculty data table will be displayed as a table named dbo. Faculty that has been added into the new database CSE_DEPT under the folder Tables in the Object Explorer window.

2.10.3 Create Other Tables

In the similar way, you need to create the rest of three tables: Course, Student, and

StudentCourse. Select course_id, student_id, and s_course_id as the primary keys for these three tables (refer to Tables 2.20–2.22). For the data type selections, follow the directions below:

The data type selections for the Course table:

course_idnvarchar(50) (Primary key)

credit—smallint

50 Chapter 2 Introduction to Databases

Table 2.20. The data in the Course table

course_id

course

credit

 

classroom

schedule

 

enrollment

 

faculty_id

 

CSC-131A

Computers in Society

3

 

TC-109

M-W-F: 9:00-9:55 AM

 

28

 

A52990

 

CSC-131B

Computers in Society

3

 

TC-114

M-W-F: 9:00-9:55 AM

 

20

 

B66750

 

CSC-131C

Computers in Society

3

 

TC-109

T-H: 11:00-12:25 PM

 

25

 

A52990

 

CSC-131D

Computers in Society

3

 

TC-109

M-W-F: 9:00-9:55 AM

 

30

 

B86590

 

CSC-131E

Computers in Society

3

 

TC-301

M-W-F: 1:00-1:55 PM

 

25

 

B66750

 

CSC-131I

Computers in Society

3

 

TC-109

T-H: 1:00-2:25 PM

 

32

 

A52990

 

CSC-132A

Introduction to Programming

3

 

TC-303

M-W-F: 9:00-9:55 AM

 

21

 

J33486

 

 

 

 

 

CSC-132B

Introduction to Programming

3

 

TC-302

T-H: 1:00-2:25 PM

 

21

 

B78880

 

CSC-230

Algorithms & Structures

3

 

TC-301

M-W-F: 1:00-1:55 PM

 

20

 

A77587

 

CSC-232A

Programming I

3

 

TC-305

T-H: 11:00-12:25 PM

 

28

 

B66750

 

CSC-232B

Programming I

3

 

TC-303

T-H: 11:00-12:25 PM

 

17

 

A77587

 

CSC-233A

Introduction to Algorithms

3

 

TC-302

M-W-F: 9:00-9:55 AM

 

18

 

H99118

 

CSC-233B

Introduction to Algorithms

3

 

TC-302

M-W-F: 11:00-11:55 AM

 

19

 

K69880

 

CSC-234A

Data Structure & Algorithms

3

 

TC-302

M-W-F: 9:00-9:55 AM

 

25

 

B78880

 

CSC-234B

Data Structure & Algorithms

3

 

TC-114

T-H: 11:00-12:25 PM

 

15

 

J33486

 

CSC-242

Programming II

3

 

TC-303

T-H: 1:00-2:25 PM

 

18

 

A52990

 

CSC-320

Object Oriented Programming

3

 

TC-301

T-H: 1:00-2:25 PM

 

22

 

B66750

 

CSC-331

Applications Programming

3

 

TC-109

T-H: 11:00-12:25 PM

 

28

 

H99118

 

CSC-333A

Computer Arch & Algorithms

3

 

TC-301

M-W-F: 10:00-10:55 AM

 

22

 

A77587

 

CSC-333B

Computer Arch & Algorithms

3

 

TC-302

T-H: 11:00-12:25 PM

 

15

 

A77587

 

CSC-335

Internet Programming

3

 

TC-303

M-W-F: 1:00-1:55 PM

 

25

 

B66750

 

CSC-432

Discrete Algorithms

3

 

TC-206

T-H: 11:00-12:25 PM

 

20

 

B86590

 

CSC-439

Database Systems

3

 

TC-206

M-W-F: 1:00-1:55 PM

 

18

 

B86590

 

CSE-138A

Introduction to CSE

3

 

TC-301

T-H: 1:00-2:25 PM

 

15

 

A52990

 

 

 

 

 

CSE-138B

Introduction to CSE

3

 

TC-109

T-H: 1:00-2:25 PM

 

35

 

J33486

 

CSE-330

Digital Logic Circuits

3

 

TC-305

M-W-F: 9:00-9:55 AM

 

26

 

K69880

 

CSE-332

Foundations of Semiconductors

3

 

TC-305

T-H: 1:00-2:25 PM

 

24

 

K69880

 

CSE-334

Elec. Measurement & Design

3

 

TC-212

T-H: 11:00-12:25 PM

 

25

 

H99118

 

CSE-430

Bioinformatics in Computer

3

 

TC-206

Thu: 9:30-11:00 AM

 

16

 

B86590

 

CSE-432

Analog Circuits Design

3

 

TC-309

M-W-F: 2:00-2:55 PM

 

18

 

K69880

 

CSE-433

Digital Signal Processing

3

 

TC-206

T-H: 2:00-3:25 PM

 

18

 

H99118

 

CSE-434

Advanced Electronics Systems

3

 

TC-213

M-W-F: 1:00-1:55 PM

 

26

 

B78880

 

CSE-436

Automatic Control and Design

3

 

TC-305

M-W-F: 10:00-10:55 AM

 

29

 

J33486

 

CSE-437

Operating Systems

3

 

TC-303

T-H: 1:00-2:25 PM

 

17

 

A77587

 

CSE-438

Advd Logic & Microprocessor

3

 

TC-213

M-W-F: 11:00-11:55 AM

 

35

 

B78880

 

CSE-439

Special Topics in CSE

3

 

TC-206

M-W-F: 10:00-10:55 AM

 

22

 

J33486

 

 

 

 

 

 

 

 

 

 

 

 

Table 2.21. The data in the student table

student_id

student_name

gpa

credits

major

schoolYear

email

A78835

Andrew Woods

3.26

108

Computer Science

Senior

awoods@college.edu

A97850

Ashly Jade

3.57

116

Information System Engineering

Junior

ajade@college.edu

B92996

Blue Valley

3.52

102

Computer Science

Senior

bvalley@college.edu

H10210

Holes Smith

3.87

78

Computer Engineering

Sophomore

hsmith@college.edu

J77896

Erica Johnson

3.95

127

Computer Science

Senior

ejohnson@college.edu

2.10 Create Microsoft SQL Server 2008 Sample Database 51

Table 2.22. The data in the StudentCourse table

s_course_id

student_id

course_id

credit

major

1000

H10210

CSC-131D

3

CE

1001

B92996

CSC-132A

3

CS/IS

1002

J77896

CSC-335

3

CS/IS

1003

A78835

CSC-331

3

CE

1004

H10210

CSC-234B

3

CE

1005

J77896

CSC-234A

3

CS/IS

1006

B92996

CSC-233A

3

CS/IS

1007

A78835

CSC-132A

3

CE

1008

A78835

CSE-432

3

CE

1009

A78835

CSE-434

3

CE

1010

J77896

CSC-439

3

CS/IS

1011

H10210

CSC-132A

3

CE

1012

H10210

CSC-331

2

CE

1013

A78835

CSC-335

3

CE

1014

A78835

CSE-438

3

CE

1015

J77896

CSC-432

3

CS/IS

1016

A97850

CSC-132B

3

ISE

1017

A97850

CSC-234A

3

ISE

1018

A97850

CSC-331

3

ISE

1019

A97850

CSC-335

3

ISE

1020

J77896

CSE-439

3

CS/IS

1021

B92996

CSC-230

3

CS/IS

1022

A78835

CSE-332

3

CE

1023

B92996

CSE-430

3

CE

1024

J77896

CSC-333A

3

CS/IS

1025

H10210

CSE-433

3

CE

1026

H10210

CSE-334

3

CE

1027

B92996

CSC-131C

3

CS/IS

1028

B92996

CSC-439

3

CS/IS

enrolment—int

faculty_idnvarchar(50)

All other columns—either nvarchar(50) or text

The data type selections for the Student table:

student_idnvarchar(50) (Primary key)

student_namenvarchar(50)

gpa—float

52Chapter 2 Introduction to Databases

credits—int

All other columns—either nvarchar(50) or text

The data type selections for the StudentCourse table:

s_course_idint (Primary key)

student_idnvarchar(50)

course_idnvarchar(50)

credit—int

major—either nvarchar(50) or text

Enter the data that are shown in Tables 2.20–2.22 into each associated table, and save each table as Course, Student, and StudentCourse, respectively.

The finished Course table should match one that is shown in Figure 2.28.

The finished Student table should match one that is shown in Figure 2.29. The finished StudentCourse table should match one that is shown in Figure 2.30.

One point you need to note is that you can copy the content of the whole table from the Microsoft Access database file to the associated data table opened in the Microsoft SQL Server environment if the Microsoft Access database has been developed.

To make these copies and pastes, first you must select a whole blank row from your destination table—table in the Microsoft SQL Server database, and then select all data rows from your source table—Microsoft Access database file by highlighting them, and

Figure 2.28. The completed Course table.

2.10 Create Microsoft SQL Server 2008 Sample Database 53

Figure 2.29. The completed Student table.

Figure 2.30. The completed StudentCourse table.

choose the Copy menu item. Next, you need to paste those rows by clicking that blank row in the Microsoft SQL Server database and then click the Paste item from the Edit menu item. An error message may be displayed as shown in Figure 2.31.

Just click the OK button and your data will be pasted to your destination table without problem. The reason for that error message is because of the primary key, which cannot be an NULL value. Before you can finish this paste operation, the table cannot identify whether you will have a non-null value in your source row that will be pasted in this column or not.

54 Chapter 2 Introduction to Databases

Figure 2.31. An error message when performing a paste job.

2.10.4 Create Relationships among Tables

Next, we need to set up relationships among these five tables using the Primary and Foreign Keys. In the Microsoft SQL Server 2008 Express database environment, the relationship between tables can be set by using the Keys folder under each data table from the Object Explorer window. Now let’s begin to set up the relationship between the LogIn and the Faculty tables.

2.10.4.1 Create Relationship between the LogIn and the Faculty Tables

The relationship between the Faculty and the LogIn table is one-to-many, which means that the faculty_id is a primary key in the Faculty table, and it can be mapped to many faculty_id that are foreign keys in the LogIn table. To set up this relationship, expand the LogIn table and the Keys folder that is under the LogIn table. Currently, only one primary key, PK_LogIn, is existed under the Keys folder.

To add a new foreign key, right click on the Keys folder and select the New Foreign Key item from the pop-up menu to open the Foreign Key Relationships dialog, which is shown in Figure 2.32.

The default foreign relationship is FK_LogIn_LogIn*, which is displayed in the Selected Relationship box. Right now, we want to create the foreign relationship between the LogIn and the Faculty tables, so change the name of this foreign relationship to FK_LogIn_Faculty by modifying its name in the (Name) box that is under the Identity pane, and then press the Enter key from your keyboard. Then select two tables by clicking on the Tables And Columns Specification item that is under the General pane. Click the expansion button that is located on the right of the Tables And Columns Specification item to open the Tables and Columns dialog, which is shown in Figure 2.33.

Click the drop-down arrow from the Primary key table combo box and select the Faculty table, since we need the primary key faculty_id from this table, then click the blank row that is just below the Primary key table combo box and select the faculty_id column. You can see that the LogIn table has been automatically selected and displayed in the Foreign key table combo box. Click the drop-down arrow from the box that is just under the Foreign key table combo box and select the faculty_id

2.10 Create Microsoft SQL Server 2008 Sample Database 55

Figure 2.32. The opened Foreign Key Relationships dialog box.

Figure 2.33. The opened Tables and Columns dialog box.

as the foreign key for the LogIn table.Your finished Tables and Columns dialog should match one that is shown in Figure 2.34.

Click the OK button to close this dialog.

Before we can close this dialog, we need to do one more thing, which is to set up a cascaded relationship between the Primary key (faculty_id) in the parent table Faculty and the Foreign keys (faculty_id) in the child table LogIn. The reason we need to do this

56 Chapter 2 Introduction to Databases

Figure 2.34. The finished Tables and Columns dialog box.

is because we want to simplify the data updating and deleting operations between these tables in a relational database, such as CSE_DEPT. You will have a better understanding about this cascading later on when you learn how to update and delete data against a relational database in Chapter 7.

To do this cascading, scroll down along this Foreign Key Relationships dialog and expand the item Table Designer. You find the INSERT And UPDATE Specifications item. Expand this item by clicking the small plus icon; two subitems are displayed, which are:

Delete Rule

Update Rule

The default value for both subitems is No Action. Click the No Action box for the Delete Rule item, and then click the drop-down arrow and select the Cascade item from the list. Perform the same operation for the Update Rule item. Your finished Foreign Key Relationships dialog should match one that is shown in Figure 2.35.

In this way, we established the cascaded relationship between the Primary key in the parent table and the Foreign keys in the child table. Later on, when you update or delete any Primary key from a parent table, the related foreign keys in the child tables will also be updated or deleted without other additional operations. It is convenient! Click the Close button to close this dialog.

Go to the File|Save LogIn menu item to open the Save dialog and click the Yes button to save this relationship. You can select Yes or No to the Save Change Script dialog box if it appears.

Now right click on the Keys folder under the LogIn table from the Object Explorer window, and select the Refresh item from the popup menu to refresh this Keys folder. Immediately, you can find a new foreign key named FK_LogIn_Faculty, which appears

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]