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

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

studentdb 数据库架构与操作指南

一、数据库架构创建

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

  • 创建 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 表的 snopnojno 列上建立普通索引

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

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

    你可能感兴趣的文章
    pprint 排序字典但不是集合?
    查看>>
    pptp拨号上网
    查看>>
    ppt上的倒计时小工具_PPT中有哪些「看似很 LOW,实则惊艳」的小工具
    查看>>
    PPT添加视频的路径问题
    查看>>
    PPT美化插件 islide 安装过程问题“加载com加载项时运行出现错误”
    查看>>
    Prefix Tuning:详细解读Optimizing Continuous Prompts for Generation
    查看>>
    PreparedStatement 与Statement 的区别,以及为什么推荐使用 PreparedStatement ?
    查看>>
    PreparedStatement 查询 In 语句 setArray 等介绍。
    查看>>
    presentModalViewController显示半透明的一个view
    查看>>
    PresentViewController切换界面
    查看>>
    PyTorch之torch.utils.data.DataLoader解读
    查看>>
    PyTorch之DataLoader杂谈
    查看>>
    presto、druid、sparkSQL、kylin的对比分析
    查看>>
    Presto分布式大数据查询引擎
    查看>>
    Presto架构及原理
    查看>>
    Presto(一)集群部署
    查看>>
    Presto(二)开启安全认证
    查看>>
    Pricing procedure Steps and Details in SAP MM (from SCN)
    查看>>
    Prim 算法在不同权重范围内的性能分析及其实现
    查看>>
    Primace 5.0软件与KEIL单片机软件联合在线仿真步骤
    查看>>