第11章 数据库应用实践指导 - 图文(7)
第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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [高等教育]公司协助某村精准扶贫工作总结.doc
- [高等教育]高二生物知识点总结(全)
- [高等教育]苏教版数学三年级下册《解决问题的策略
- [高等教育]仪器分析课程学习心得
- [高等教育]2017年五邑大学数学与计算科学学院333
- [高等教育]人教版七年级下册语文第四单元测试题(
- [高等教育]2018年秋七年级英语上册Unit7Howmuchar
- [高等教育]2017年八年级下数学教学工作小结
- [高等教育]湖南省怀化市2019届高三统一模拟考试(
- [高等教育]四年级下册科学_基础训练及答案教材
- [高等教育]城郊煤矿西风井管路伸缩器更换施工安全
- [高等教育]昆八中20182019学年度上学期期末考试
- [高等教育]项目部各类人员任命书
- [高等教育]上市公司经营水务产业的模式
- [高等教育]人教版高二化学第一学期第三章水溶液中
- [高等教育]【中考物理第一轮复习资料】四.压强与
- [高等教育]金坑水电站报废改建工程机电设备更新改
- [高等教育]高中生物教学工作计划简易版
- [高等教育]2017年西华大学攀枝花学院(联合办学)44
- [高等教育]最新整理超短爆笑英文小笑话大全
- 优秀教师继续教育学习心得体会
- 阳历到阴历的转换
- 留守儿童教育案例分析
- 华师17春秋学期《玩教具制作与环境布置
- 测速传感器新型安装装置的现场应用
- 人教版小学数学三年级下册第四单元
- 创业个人意向书
- 山东省潍坊市2012年高考仿真试题(三)
- [恒心][好卷速递]四川省成都外国语学校
- 多少人错把好转反应当成了病情加重处理
- 中外广播电视史复习资料整理
- 江苏省扬州市江都区宜陵镇中学2014-201
- 工程造价专业毕业实习报告
- 广西师范学院心理与教育统计
- aympkrq基于 - asp的博客网站设计与开
- 建筑业外出经营相关流程操作(营改增后
- 人治 德治 法治
- [精华篇]常识判断专项训练题库
- 中国共产党为什么要实行民主集中
- 小学数学第三册第一单元试卷(A、B、C




