博客
关于我
数据库原理实验报告(一)
阅读量:65 次
发布时间:2019-02-25

本文共 1836 字,大约阅读时间需要 6 分钟。

studentdb 数据库架构与操作指南

一、数据库架构创建

在 studentdb 中,我们需要创建两个主要架构:Production 和 Person。以下是创建过程的详细步骤:

  • 创建 Production 架构

    • 使用以下命令在数据库中创建新的架构:
    create schema Production;
    • 注意:架构命名不能以数字开头,必须遵守数据库规范。
  • 创建 Person 架构

    • 在创建 Person 架构前,需要先为用户 st 添加必要的权限:
    create login st with password='suntao123';create user st for login st;grant create table to st;
    • 使用 SQL Server 身份验证登录服务器,并执行以下命令创建 Person 架构:
    create schema Person AUTHORIZATION st;
  • 测试架构权限

    • 使用以下命令验证用户 st 是否有表创建权限:
    create table Person.t1(id int, name char(10));-- 成功
    • 创建 Production 架构的表时会失败,因为架构权限未正确设置。
  • 二、表结构修改

    按照要求对各表进行结构修改:

  • 修改 course 表

    • 将 cname 列的数据类型改为 varchar(40):
    alter table course alter column cname varchar(40);
  • 在 student 表中添加新列

    • 添加 birthday 列,类型为 datetime,默认值为空:
    alter table student add birthday datetime default null;
  • 修改 sc 表约束

    • 删除原有约束:
    alter table sc drop constraint CK__sc__grade__2A4B4B5E;
    • 添加新约束,确保 grade 不超过 150:
    alter table sc add constraint grade_new check (grade <= 150);
  • 为 student 表添加缺省约束

    • 为 ssex 列设置缺省值为 '男':
    alter table student add constraint ssex__new default '男' for ssex;
  • 为 student 表添加检查约束

    • 确保 sdept 只能是 'CS'、'MA' 或 'IS':
    alter table student add constraint ck_student check (sdept in ('CS','MA','IS'));
  • 为 student 表添加唯一性约束

    • 为 sname 列添加唯一性约束:
    alter table student add unique (sname);
  • 为 SC 表添加外键约束

    • 确保 sc 表中的 sno 外键依赖于 student 表中的 fk_S_c 约束:
    alter table sc add constraint fk_S_c foreign key (sno) references student(sno);
  • 禁用 Student 表的 sdept 检查约束

    • 使用 nocheck 关键字禁用约束:
    alter table student nocheck constraint ck_student;
  • 三、索引创建

    为提高查询效率,创建以下索引:

  • 在 student 表的 sname 列上建立普通降序索引

    create unique index sname on student(sname desc);
  • 在 course 表的 cname 列上建立唯一索引

    create unique index cname on course(cname);
  • 在 sc 表的 sno 列上建立聚集索引

    create clustered index index_sno on sc(sno);
  • 在 SPJ 表的 sno、pno 和 jno 列上建立普通索引

    use spjdb;create unique index sno on SPJ(sno asc, pno asc, jno desc);
  • 以上步骤完成后,数据库架构和数据表结构将得到有效优化,各项约束和索引也将正常生效。

    转载地址:http://nxh.baihongyu.com/

    你可能感兴趣的文章
    Python3 tutorial day3
    查看>>
    python3 week4
    查看>>
    Python3 创建数组
    查看>>
    python3 套接字异常(a byte-like object is required not str)
    查看>>
    python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
    查看>>
    python3 文本变图片
    查看>>
    Python3 查看 com 组件的属性
    查看>>
    python3 案例分享--发邮件
    查看>>
    python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
    查看>>
    Python3 求最大/小值及索引值 Numpy
    查看>>
    python3+django使用celery执行某些任务失败的解决方案
    查看>>
    Python3+Requests+Unittest实战系列【一】
    查看>>
    python3+requests+unittest实战系列【一】
    查看>>
    python3+requests+unittest实战系列【二】
    查看>>
    Python3+Requests+Unittest实战系列【二】
    查看>>
    python3----ASCII
    查看>>
    Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
    查看>>
    Python3-如何自我更新作为服务运行的应用程序
    查看>>
    Python3.1-网格模拟概念性问题
    查看>>
    Python3.12 解决conda环境libstdc++版本过低问题
    查看>>