数据库作业习题答案 更正版

发布 2022-09-02 22:31:28 阅读 1776

一、基础知识。

1、数据库系统、数据库管理系统的组成、功能和特点。

2、数据模型—关系的基本概念及特点。

3、关系数据库的基本概念及特点。

4、关系的完整性即码的定义和作用。

二、sql命令。

1、基本表定义create table命令(要求:数据类型number、char、date,完整性约束条件:not null、primary key)

2、修改基本表alter table命令(要求:增加字段、删除字段、修改字段类型和宽度)

3、删除表drop table命令。

4、数据查询select命令(要求:能指定查询字段列表、能设置简单查询条件、能进行简单多表查询)

5、插入记录insert into命令。

6、修改数据update命令。

7、删除数据delect命令。

一、填空题(一空1分,共10分)

执行如下sql命令序列后。

create table customer (

customer_id char(18) constraint cust_pk primary key ,

cust_name char(20),cust_city char(30) default 'cd',cust_phone char(13),cust_gender char(1) default 'm' constraint cust_gender check(cust_gender='m' or cust_gender='f'),cust_job char(30));

create table account (

customer_id char(18) ,

account_no char(8) constraint acct_pk primary key ,

account_pwd char(6),branch_id char(4) default '001',balance number(14, 2) default 0,

build_date date,

acct_type number(2,0) default 0 ,status char(1) default 0 constraint acct_status check (status in ('0','1','2'))constraint acct_fk_cus foreign key(customer_id) references customer(customer_id) on delete cascade);

create table deposit (

account_no char(8),

amount number(14, 2) default 0,

oper_date date,

oper_type char(1) default 'c',constraint deposit_fk_acc foreign key(account_no) references account);

create table staff (

staff_id char(18) constraint staff_pk primary key ,staff_name char(15),staff_pwd char(15));

insert into staff(staff_id,staff_name,staff_pwd) values

'staff01','staff01','111111');

1.一共有___4___个表。

表中有___0___记录。

关系中有___3___个属性。

4.一共执行了___5___条sql命令。

表中的amount字段是___数值___数据类型。

表中的___account_no __字段被设置为了主键(主码)。

7.在customer关系中,不把cust_name属性设置为主键的原因是,客户的___姓名___可能重复。

表和___account___表有公共属性。

命令中的(staff_id,staff_name,staff_pwd)子句可以被省略。

10.__deposit __表中没有设置主键。

二、以第一题创建的表为基础,根据要求写出合适sql命令(一小题2分,共20分)

1.查询customer表中所有记录的所有字段的内容。

select * from customer;

2.查询账户余额大于100000元的账号和客户身份证号码。

select account_no,customer_id from account where balance>100000;

3.查询账户余额在26000元(包括26000元)到200000元(不包括200000元)的账号和客户姓名。

select account_no,cust_name from account,customer where

and balance>=26000 and balance<200000;

4.向staff表中增加一条记录,记录数据自定。

insert into staff(staff_id,staff_name,staff_pwd) values('staff02','staff02','222222');

5.向deposit表中增加一个属性,属性的名字和参数自定。

alter table deposit add oper_name char(8);

6.向账号为“12345678”的账户中存款1000元。

insert into deposit values(‘12345678’,1000,sysdate, 'c');

update account set balance=balance+1000

where account_no=’12345678’;

7.从staff表中删除staff_id的值是“123456789012345678”的记录。

delete from staff where staff_id='123456789012345678';

8.将staff表中的staff_name的数据宽度修改为20个字节。

alter table staff modify staff_name char(20);

9.删除staff表。

drop table staff;

10.删除staff表中的所有记录。

delete from staff;

1. 将自己作为客户加入客户信息表。

insert into customer values(‘150125x’,’luozijing’,’cd’,’189m’,’stu’);

2. 在编码为“0101”和“0102”的分行为自己各开立一个存款账户(账号为学号后跟01或02),账户初始余额为0,开户日期为当前日期,其他信息自行确定。

insert into account(account_no,branch_id,build_date,balance) values(‘01328016’,’0101’,sysdate,0);

insert into account(account_no,branch_id,build_date,balance) values(‘01328017’,’0102’,sysdate,0);

3. 向刚刚开立的两个存款账户中各存入10000元和20000元(注意:既要保存交易明细,也要修改余额)

insert into deposit(account_no,amount) values(‘01328016’,10000);

update account set balance= balance+10000 where account_no=’ 01328016’;

insert into deposit(account_no,amount) values(‘01328017’,20000);

update account set balance= balance+20000 where account_no=’ 01328017’;

4. 为在“0101”银行开立的上述账户转增利息,利率为5%

update account set balance=balance+balance*5% where account_no=’ 01328016’;

5. 从在“0101”银行开立的上述账户中支取2000元;

insert into deposit(account_no,amount, oper_type)values(’ 01328016’,2000,’w’);

update account set balance=balance-2000 where account_no=’ 01328016’;

6. 为表account增加一个列:close_date 存放销户日期。

alter table account add close_date date;

7.列出余额大于10000的各个账户的账号和余额,结果按余额降序排列。

select account_no,balance from account where balance>1000 order by balance desc;

8. 列出“成都”市的客户的存款余额总和。

select sum(balance) from customer,account

where and cust_city=’cd’;

SQL数据库习题答案

1 模型中,同一个关系中的不同属性,其属性名 b a.可以相同 b.不能相同 c.可以相同,但数据类型不同 d.必须相同。2 数据库系统由数据库 a 组成。a dbms 应用程序 支持数据库运行的软硬件环境和dba 3 计算机数据管理技术的发展可以划分为三个阶段,在某个阶段数据是以文件形式长期存储在...

数据库习题1附答案

创建emp info表完成下列练习,表的结构说明如下。empno 员工号。empname 员工姓名。job 工作。mgr 上级编号。hiredate 受雇日期。sal 薪金。comm 佣金。deptno 部门编号。创建emp info表完成下列练习,表的结构说明如下。empno 员工号。empnam...

数据库作业

select from 学生基本信息表。select from 选课表。select from 课程表。1 按性别统计查询出贵州籍学生男和女的人数。select 性别as 性别,count 学号 as 人数from 学生基本信息表。where 籍贯like 贵州 group by 性别。2 统计至少...