博客
关于我
数据库原理实验报告(一)
阅读量: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/

    你可能感兴趣的文章
    php 标准规范
    查看>>
    PHP 浮点型精度运算相关问题
    查看>>
    php 浮点型计算精度问题
    查看>>
    php 特定时间段统计,jpgraph某个时间段的数据统计
    查看>>
    php 生成csv mac下乱码
    查看>>
    php 生成证书 签名及验签
    查看>>
    php 的rsa加密与解密
    查看>>
    PHP 的标准输入与输出
    查看>>
    php 笔记 (早前的,很乱)
    查看>>
    PHP 第一天
    查看>>
    Redis使用量暴增,快速定位有哪些大key在作怪
    查看>>
    php 结课作业答案,北语201803考试批次《PHP》(结课作业)1.pdf
    查看>>
    PHP 统计数据功能 有感
    查看>>
    SpringBoot处理JSON数据
    查看>>
    Redis使用基本套路
    查看>>
    php 解决项目中多个自动加载冲突问题
    查看>>
    PHP 设置调试工具XDebug PHPStorm IDE
    查看>>
    php 身份证号检测
    查看>>
    PHP 输入输出流合集
    查看>>
    PHP 过滤器(Filter)
    查看>>