教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

第11章 数据库应用实践指导 - 图文(7)

来源:网络收集 时间:2026-09-19
导读: 第11章 数据库应用实验指导 ) go create table course ( course_id char(6) not null, dept_id char(6) not null, course_name char(20) null, constraint PK_COURSE primary key nonclustered (course_id) ) Go cr

第11章 数据库应用实验指导

)

go

create table course (

course_id char(6) not null, dept_id char(6) not null, course_name char(20) null,

constraint PK_COURSE primary key nonclustered (course_id) ) Go

create table student (

stu_id char(6) not null, dept_id char(6) not null, name char(8) null, sex char(2) null, birthday datetime null, address char(40) null, totalscore int null, nationality char(8) null, grade char(2) null, school char(20) null, class char(16) null, major char(30) null,

constraint PK_STUDENT primary key nonclustered (stu_id) ) go

create table teacher (

teacher_id char(6) not null, dept_id char(6) not null, teacher_name char(8) null, rank char(6) null,

constraint PK_TEACHER primary key nonclustered (teacher_id) ) Go

create table teacher_course (

teacher_id char(6) not null, course_id char(6) not null, term_id char(2) null,

constraint PK_TEACHER_COURSE primary key (teacher_id, course_id) ) Go

create table student_teacher_course (

course_id char(6) not null, stu_id char(6) not null, teacher_id char(6) not null, term char(2) null, score int null,

constraint PK_STUDENT_TEACHER_COURSE primary key (course_id, stu_id, teacher_id) ) Go

alter table course /*修改表结构,添加一个外键*/

add constraint FK_COURSE_DEPARTMENT foreign key (dept_id) references department (dept_id) go

alter table course

add constraint FK_COURSE_DEPARTMEN_DEPARTME foreign key (dept_id) references department (dept_id) go

alter table student

add constraint FK_STUDENT_DEPARTMEN_DEPARTME foreign key (dept_id) references department (dept_id) go

alter table student_teacher_course

add constraint FK_STUDENT__STUDENT_T_COURSE foreign key (course_id) references course (course_id) go

alter table student_teacher_course

数据库原理及应用学习与实践指导 SQL Server 2012

add constraint FK_STUDENT__STUDENT_T_STUDENT foreign key (stu_id) references student (stu_id) go

alter table student_teacher_course

add constraint FK_STUDENT__STUDENT_T_TEACHER foreign key (teacher_id) references teacher (teacher_id) go

alter table teacher

add constraint FK_TEACHER_DEPARTMEN_DEPARTME foreign key (dept_id) references department (dept_id) go

alter table teacher_course

add constraint FK_TEACHER__TEACHER_C_TEACHER foreign key (teacher_id) references teacher (teacher_id) go

alter table teacher_course

add constraint FK_TEACHER__TEACHER_C_COURSE foreign key (course_id) references course (course_id) go

(2)修改表:把表student_teacher_course 中term列删除,并将score的数据类型改为float。实验操作步骤;在“新建查询”窗口中输入下列SQL语句:

USE teachingSystem GO

ALTER TABLE student_teacher_course DROP COLUMN term GO

ALTER TABLE student_teacher_course ALTER COLUMN score float GO

(3)删除表:使用SSMS删除表。在“对象资源管理器”窗口中,展开“数据库”节点,再展开所选择的具体数据库节点,展开“表”节点,右键要删除的表,选择“删除”命令或按下“DELETE”键。

使用T-SQL语句删除表

在数据库teachingSystem中建一个表Test1,然后删除。

USE teachingSystem GO

DROP TABLE Test1

说明:在删除表的时候可能会出现?删除对象?的对话框,如删除department表。这是因为所删除的表中拥有被其他表设置了外键约束的字段,如果删除了该表,必然对其他表的外键约束造成影响,数据库系统禁止删除被设置了外键的表。如图11-65所示。

图11-65 删除对象对话框

3.插入数据库记录和修改数据库记录

1)使用SSMS和T-SQL添加记录 给系部表(department),课程表(course),学生表(student)和教师表(teacher),教

第11章 数据库应用实验指导

师开课表(teacher_course),学生选课表(student_teacher_course)添加适当的记录。注意先后次序。先给无外键约束的表进行添加记录,然后再给有外键的表添加,否则无法添加。

使用SSMS添加记录:在“对象资源管理器”窗口中,展开“数据库”节点,再展开所选择的具体数据库节点,展开“表”节点,右键要插入纪录的表,选择“编辑前200行”命令,即可输入纪录值和修改记录。

使用T-SQL添加记录:如给教务管理系统数据库的表添加适量记录:

USE teachingSystem GO

/*添加课程表记录*/

INSERT course (course_id,dept_id,course_name) VALUES ('100001', '1001 ', '数据库原理')

INSERT course (course_id,dept_id,course_name VALUES (N'100002', N'1001', N'面向对象程序设计') /*添加系部表记录*/

INSERT department(dept_id, dept_name, dept_head, dept_phone, dept_addr) VALUES ('1001', '电子信息学院', '王老师', '1391001011', '图书馆7楼')

INSERT department(dept_id, dept_name, dept_head, dept_phone, dept_addr)VALUES ('1002', '机械学院', '刘老师', '1891020202', '文理大楼2楼')

INSERT department(dept_id, dept_name, dept_head, dept_phone, dept_addr)VALUES ('1003', '电气学院', '张老师', '1893774737', '华宁路2332号') /*添加学生表记录*/

INSERT student(stu_id, dept_id, name,sex, birthday, address, totalscore, nationality, grade, school, class, major) VALUES ('1201', N'1001', '高燕', '女', CAST(0x0000806800000000 AS DateTime),'上海', NULL, ' …… 此处隐藏:3634字,全部文档内容请下载后查看。喜欢就下载吧 ……

第11章 数据库应用实践指导 - 图文(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/604288.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)