[Principle of Database System] database-class lab

Disclaimer: This article is a blogger original article. Reproduced please contact bloggers authorization. Bloggers public micro-channel number] [Department of Knowing and Doing the campus. https://blog.csdn.net/cxh_1231/article/details/89715640

 1. Experiment:

project name

Content Experiments

1 database management system (DBMS) experiment

(1) the familiar interface and operation of the DBMS.

(2) create the database and view the database properties.

(3) creating a table, the code table and determine the main constraints.

(4) to view and modify the structure of the table.

(5) the input data to the database, to observe what happens when column level constraint violation.

(6) to modify the data.

(7) delete the data, observe what happens when a violation table-level constraints.

(8) back up the database to other disk (such as U disk), to restore the database from the other disk.

Create and modify database of 2

(1) In the DBMS interactive environment, building a database with SQL statements, build tables and insert records.

(2) modify the table structure, the modified data type property of a column, adding new attribute column, delete the existing attribute columns.

(3) the use of a single tuple and the tuple inserted.

Modify, and delete (4) the use of sub-queries.

3 database query

(1) In the DBMS interactive environment, building a database with SQL statements, build tables and insert records.

(2) a simple query operations, including a projection, selecting, sorting data, fuzzy matching query. If the result is not correct to make modifications until correct.

(3) join query operations, including equi NATURAL connected, generally connected, itself connected to the outer connector.

(4) In, comparison operators, and operators Exists nested queries.

(5) the query packet, a packet including the condition expression, select the expression set of conditions.

(6) a set of queries.

(7) was built using a view to create a statement view, query data through views

4 integrated database application

(1) Examples of database application design (such as book sales database).

(2) create a new user, set up and manage data manipulation authority, to achieve security of the database.

(3) define the database structure, the constraint definition data, for integrity of the database.

(4) Depending on the application, using a view for data operations, including data entry, data maintenance, and so data query.

5 data import / export, backup, and restore the database

(1) using DBMS tool to export to a file of another format of the data in the table.

(2)将其它格式的文件数据导入到数据库中。

(3)使用DBMS工具创建一个数据库的备份(海量备份、增量备份)。

(4)使用DBMS工具及所创建的数据库备份恢复这个数据库。

2、各个实验调试过程:

2.1 数据库管理系统(DBMS)实验

调试过程(包括调试方法描述、实验数据记录,实验现象记录,实验过程发现的问题等)

  • 创建SPJDB数据库
  1. 单击桌面的“SQL Server Management Studio”快捷方式,启动SSMS。
  2. 在“服务器名称(S)”中输入“名称”,点击“连接”,连接至数据库
  3. 在“对象资源管理器”中,展开 SQL Server 20008 R2数据库引擎实例CXH。
  4. 右击“数据库”,然后单击“新建数据库”。
  5. 在“新建数据库”对话框中,输入数据库名称:SPJ_DB,其他选项使用默认值,单击“确定”, 完成数据库SPJ_DB的创建。
  • 创建表、确定表的主码和约束条件
  1. 通过分析四个表得知:S表的主码为SNO,P表的主码为PNO,J表的主码为JNO,SPJ表的主码为(SNO,PNO,JNO),所以定义这四个表的过程、SQL语句如下:
    点击“新建查询”按钮,在sql文件中输入下面SQL代码,输入一条执行一条;
    create table S(SNO char(10) primary key,SNAME char(20),STATUS char(10),CITY char(20));
    create table P(PNO char(10) primary key,PNAME char(20),COLOR char(10),WEIGHT char(10));
    create table J(JNO char(10) primary key,JNAME char(20),CITY char(20));
    create table SPJ(SNO char(10),PNO char(10),JNO char(10),QTY char(20),primary key(SNO,PNO,JNO));

     

  2. 创建完成表后,右击“SPJ_DB/表”文件,点击刷新按钮,即可看到新建的四个空表;
  3. 当前表中没有数据,对“SPJ表”中SNO、PNO、JNO三个主码(或者说是外码)的约束条件进行设置,被参照表依次是S、P、J表,代码如下:
    alter table SPJ add constraint SNO foreign key(SNO) references S(SNO);
    alter table SPJ add constraint PNO foreign key(PNO) references P(PNO);
    alter table SPJ add constraint JNO foreign key(JNO) references J(JNO);

     

  • 查看和修改表的结构:
    右击“SPJ_DB\数据库关系图”文件,选择“新建数据库关系图”选项,在新弹出的“添加表”对话框中将“J、P、S、SPJ”四个表依次“选中”→“添加”,在界面中将显示数据库的四个表之间的关系图结构。
  • 向数据库输入数据,观察违反列级约束时出现的情况:
  1. 首先向S表中添加数据,右击“SPJ_DB\表\dbo.S”表,点击“编辑前200行”,即可进入编辑界面,参照课本P71的表格,依次输入数据。
  2. 如果前面输入了SNO为S1的数据,后边再重复输入时,弹出错误信息对话框,提示“未提交行2中的数据……违反了PRIMARY KEY约束……请更正错误并重试……”,这是因为主码唯一的原因。
  3. 对P表、J表同上。
  4. 对SPJ表,同样根据课本表格依次输入数据。
  5. 当SPJ表中的SNO、PNO、JNO三列中的任何一列输入一个在对应的S表、P表、J表中没有的数据时,例如在SNO列中输入“P1”,显示错误信息“未提交行2中的数据……INSERT语句与FOREIGN KEY约束“SNO”冲突……”。
  • 修改数据:
    直接修改即可。
  • 删除数据,观察违反表级约束时出现的情况
    对SPJ表删除数据无影响,但是对S表、P表、J表删除数据时,如删除J表中的第一行,弹出错误信息框,显示“试图删除行1时发生问题……DELETE语句与REFERENCE约束“JNO”冲突……”。
  • 备份数据库到其它磁盘(如U盘),从其它磁盘恢复数据库。
  1. 右击数据库“SPJ_DB”,选择“任务”→“备份”选项,备份数据库界面选择备份目标(路径),然后点击“确定”,即可完成备份,在对应目录下可见文件名为“SPJ_DB.bak”的备份文件。
  2. 恢复数据库即备份数据库的逆过程,右击“CHENXIUHAO\数据库”,选择“还原数据库”,还原的源选择“源设备”,点击对应后边三个点按钮,弹出“指定备份”对话框,点击“添加”按钮,选择要还原的*.bak扩展名文件,“确定”,选择用于还原的备份集列表中勾选刚才添加的文件,点击“确定”即可。

2.2 数据库的创建与修改

调试过程(包括调试方法描述、实验数据记录,实验现象记录,实验过程发现的问题等)

  • 在DBMS的交互式环境里,用SQL语句建库、建表并插入记录。
  1. 点击选项菜单的“新建查询”按钮激活SQL语言输入框,输入以下SQL语句创建数据库SPJ_DB:create  database SPJ_DB;
  2. 从题意知S表的主码为SNO,使用以下SQL语句创建S表:
  • create table S(SNO char(10) primary key,SNAME char(20),STATUS char(10),CITY char(20));
  • P表、J表和S表类似,但SPJ表的主码为(SNO,PNO,JNO),所以创建SPJ表的SQL语句如下:
  • create table SPJ(SNO char(10),PNO char(10),JNO char(10),QTY char(20),primary key(SNO,PNO,JNO));
  1. 首先向S表中插入数据,其中部分语句如下:
  • insert into S values('S1','精益','20','天津');

insert into S values('S2','盛锡','10','北京');

其他表格类似。

  • 使用子查询的修改和删除。
  1. 修改:将城市为北京的供应商S供应给工程J1的零件数修改为666;

UPDATE SPJ set QTY='666' where JNO='J1' and SNO in (SELECT SNO from S where CITY='北京');

  1. 删除:将城市为上海的供应商S供应给工程J4零件的供应情况删除:

delete from  SPJ where JNO='J4' and SNO in (SELECT SNO from S where CITY='上海');

  1. 修改表结构,包括修改属性列的数据类型,增加新的属性列,删除已有的属性列。
  2. 使用单个元组和多元组插入。
  3. 使用子查询的修改和删除。

2.3 数据库查询

调试过程(包括调试方法描述、实验数据记录,实验现象记录,实验过程发现的问题等)

  • 包括投影、选择、数据排序、模糊匹配查询等。
  1. 简单查询操作:
  1. 找出所有零件的名称、颜色和重量

select PNAME,COLOR,WEIGHT from P;

  1. 找出工程项目J2使用的各种零件的名称及其数量

select PNAME,QTY from P,SPJ where P.PNO=SPJ.PNO and JNO='J2';

  1. 找出使用上海产的零件的工程号码

select JNAME from J,SPJ,S where J.JNO=SPJ.JNO and SPJ.SNO=S.SNO and S.CITY='上海';

  1. 找出没有使用天津产的零件的工程号码

select JNO from J where not exists(select * from SPJ,S where SPJ.JNO=J.JNO and SPJ.SNO=S.SNO and S.CITY='天津');

  1. 找出工程项目名带“厂”字的工程号码

select JNO from J where JNAME like'%';

  1. 将P表重量大于15的所以数据,并按重量递增进行排序查询输出

select * from P where WEIGHT>'15' order by WEIGHT asc;

  1. 使用视图创建语句建视图,通过视图查询数据
  1. 对SPJ表构建视图,将代码换成具体的名字,供应数量前添加零件颜色和重量

create view SPJ_B as

select SNAME,JNAME,PNAME,COLOR,WEIGHT,QTY

from S,P,J,SPJ

where S.SNO=SPJ.SNO and P.PNO=SPJ.PNO and J.JNO=SPJ.JNO

  1. 通过上面创建的视图,查询使用蓝色零件的工程名字

select DISTINCT JNAME from SPJ_B where COLOR='';

2.4 数据库综合应用

  1. 设计应用实例的数据库(如图书销售数据库)。
  1. 新建某公司信息数据库,有两个关系模式:

职工(职工号,姓名,,性别,年龄,职务,工资,部门号,联系方式)

部门(部门号,名称,经理姓名,地址,电话)

  1. 创建表过程可采用实验一的SSMS向导的或者实验二的SQL语句,这里不赘述。
  1. 创建新用户,设置和管理数据操作权限,实现数据库的安全性。
  1. 展开“CHENXIUHAO\安全性\登录名”文件,右击“登录名”弹出“登录名-新建”页面;
  2. “常规”选择页输入新的登录名;
  3. “服务器角色”选择页勾选该用户可拥有的权限;
  4. "User Mapping" Select the page the user can check the database management, select the check after the operation can be carried out under the "Database role membership" box, such as reading data (db_datareader), write data (db_datawriter) and so on;
  5. After setting, click "OK" to successfully set
  6. Exit the current user, the new user login name test;

2.5 data import / export, backup and restore

  1. Use DBMS tool data in the table to export to other file formats.
    1. The second experiment created SPJ table view Export steps:
    2. First, right-click the second experiment created SPJ_DB database, find the "mission" → "Export Data" pop-up "SQL Server Import and Export Wizard."
    3. Home Click on "Next", select a data source page, select the database "SPJ_DB", click Next,
    4. Select the target interface, select the target column "flat target file", click "Browse" to select the save path, enter the file name, file format, select "CSV" file, OK, check "in the first line of the display data Column Name" click Next;
    5. Specify Table Copy or Query page, select the first item, click Next,
    6. Configure Flat File Destination Interface "the source table or view" column to select the view you want to export "SPJ_V", click Finish.
    7. Then went to see the path to save the exported Excel file, open view data.
  1. The import file data format to the other database.
  1. Create a backup of a database (massive backup, incremental backup) using the DBMS tools.
  1. Right-click the database "SPJ_DB", select "Tasks" → "Backup" option, back up the database interface to find the bottom of the "target" box, check the "disk", click "Add" button, pop-up "Select Backup Destination" prompt box,
  2. Tip box to select the path, enter the file name, click "OK";
  3. Then click "OK" to complete the backup, the backup files are visible file named "SPJ_DB.bak" in the corresponding backup directory.
  1. Using DBMS tools and database backups created to restore the database.

 

Guess you like

Origin blog.csdn.net/cxh_1231/article/details/89715640