实验一 数据定义和数据更新
发布日期:2021-05-10 01:18:21 浏览次数:16 分类:精选文章

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

Experimental Report on Database Operations


Experiment 1: Data Definition and Data Update

1.1 Experiment Name

Data Definition and Data Update


1.2 Experiment Number

1.1.1

1.3 Experiment Unit

Computing Science and Technology Class of 2016


2.1 Purpose of the Experiment

  • Familiarize oneself with database interactive SQL tools.
  • Apply SQL language to define and modify basic tables and indexes.
  • Update data in tables through specific commands.

2.2 Experimental Environment

  • Software: Oracle 11g, SQL Developer
  • Operating System: Windows 7
  • Environment: Local development environment

2.3 Experimental Content and Results

2.3.1 Basic Table Operations

  • Table Creation

    • Student Table: Student(Sno, Sname, Ssex, Sage, Sdept)
    create table Student(
    Sno char(9) primary key,
    Sname char(20),
    Ssex char(2),
    Sage smallint,
    Sdept char(20)
    );
    • Course Table: Course(Cno, Cname, Cpno, Ccredit)
    create table Course(
    Cno char(4) primary key,
    Cname char(40),
    Cpno char(4),
    Ccredit smallint,
    foreign key (Cpno) references Course(Cno)
    );
    • Student Enrollment Table: SC(Sno, Cno, Grade)
    create table SC(
    Sno char(9),
    Cno char(4),
    Grade smallint,
    primary key (Sno, Cno),
    foreign key (Sno) references Student(Sno),
    foreign key (Cno) references Course(Cno)
    );
  • Table Modification

    • Add Blood Type to Student Table
    alter table Student add BloodType char(2);
    • Modify Department Column Type
    alter table Student modify (Sdept varchar2(40));
    • Add Custom Constraint to Age Column
    alter table Student add check (Sage > 15 and Sage < 30);
    • Remove Constraint
    alter table Student drop constraint CK_STUDENT;
    • Remove Blood Type Column
    alter table Student drop BloodType;
  • Table Deletion

    • Delete Student Table with Constraints
    drop table Student cascade CONSTRAINTS;
    • Delete Student Enrollment Table and Its Constraints
    drop table SC cascade CONSTRAINTS;
  • 2.3.2 Index Operations

  • Index Creation

    • Create Unique Index on Student Name in Student Table
    create unique index stusnam on Student(Sname);
    • Create Composite Index on Student Enrollment Table
    create unique index i_sc on SC(Sno asc, Cno desc);
  • Index Deletion

    • Remove Indexes from Student and Course Tables
    drop index stusnam;
    drop index i_sc;
  • 2.3.3 Data Operations

  • Data Insertion

    • Insert Data into Student Table
    insert into Student values('200215121', '李勇', '男', '20', 'CS');
    insert into Student values('200215122', '刘晨', '女', '19', 'CS');
    insert into Student values('200215123', '王敏', '女', '18', 'MA');
    insert into Student values('200215125', '张立', '男', '19', 'IS');
    • Insert Course Data
    insert into Course values('2', '数字', null, '2');
    insert into Course values('6', '数据处理', null, '2');
    insert into Course values('7', 'PASCAL语言', '6', '4');
    insert into Course values('4', '操作系统', '6', '3');
    insert into Course values('5', '数据结构', '7', '4');
    insert into Course values('1', '数据库', '5', '4');
    insert into Course values('3', '信息系统', '1', '4');
    • Insert Student Enrollment Data
    insert into SC values('200215121', '1', '92');
    insert into SC values('200215121', '2', '85');
    insert into SC values('200215121', '3', '88');
    insert into SC values('200215122', '4', '90');
    insert into SC values('200215122', '3', '80');
  • Data Update

    • Update Age of Wang Mei
    update Student set Sage = 20 where Sname = '王敏';
    • Increment Age of All Students
    update Student set Sage = Sage + 1;
    • Reset Grades for CS Department Students
    update SC set Grade = '0' where Sno in(
    select Sno from Student where Sdept = 'CS'
    );
  • Data Deletion

    • Delete Information for Liu Chen and Related Students
    delete from Student where Sdept in(
    select Sdept from Student where Sname = '刘晨'
    );
    • Delete CS Department Enrollment Data
    delete from SC where Sno in(
    select Sno from Student where Sdept = 'CS'
    );

  • 3.1 Considerations

  • When defining foreign keys, always enable cascading operations to avoid orphaned records.
  • Use varchar2 for columns expecting variable lengths to minimize issues.
  • Name constraints descriptively for better readability.
  • Enter SQL commands using English input methods, including spaces and symbols.
  • Ensure schema changes are committed after testing to avoid errors.

  • 4.1 Discussion Questions

  • Implementing Foreign Key Constraints

    alter table table_name add constraint FK foreign key (column_name) references referenced_column;
  • Deleting a Table with Foreign Keys

    • Enable cascading deletion before dropping the table.
  • Modifying Column Data Types

    • Columns must be empty or have NULL values to modify or drop constraints.

  • 5. Advisor's Comments


    #_end#

    上一篇:实验二 数据查询
    下一篇:mysql5.5安装

    发表评论

    最新留言

    第一次来,支持一个
    [***.219.124.196]2025年04月09日 02时48分04秒