资源描述
SQL学习要点:
一、基础知识
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命令
SQL模拟题参考答案
一、填空题(一空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______个表。
2.customer表中有______0____记录。
3.staff关系中有______3____个属性。
4.一共执行了_____5_____条SQL命令。
5.deposit表中的amount字段是_____数值_____数据类型。
6.account表中的____ACCOUNT_NO ______字段被设置为了主键(主码)。
7.在customer关系中,不把cust_name属性设置为主键的原因是,客户的____姓名______可能重复。
8.customer表和______account____表有公共属性。
9.insert命令中的(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
account.customer_id=customer.customer_id 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;
SQL课后习题参考答案
1. 将自己作为客户加入客户信息表
Insert into customer values(‘150125199410270022x’,’luozijing’,’CD’,’18980056982’,’M’,’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 customer.customer_id=account.customer_id and cust_city=’CD’;
9. 查询所有活期存款账户的账号、余额
Select account_no,balance from account where acct_type=0;
10. 列出位于“重庆”市“解放碑”的客户的身份证号、姓名、性别
Select customer_id,cust_name,cust_gender from customer
where cust_city like ‘%重庆%’ and cust_city like ‘%解放碑%’;
11. 按分行统计存款总额
Select branch_id,sum(balance) from account group by branch_id;
12. 列出存款总额大于100000元的客户的身份证号、姓名(该题目有二义性,遇考试按下面的作法一来做)
作法一:将存款总额理解为帐户表中的余额
Select account.customer_id,customer.cust_name
From account,customer
Where account.customer_id=customer.customer_id and balance>100000;
作法二:将存款总额理解为帐户表中的同一客户的所有金额
Select customer_id From account
Group by customer_id
Having sum(balance)>100000;
Select customer_id, cust_name from customer
Where customer_id=’前述查询到的身份证号’ or customer=’ ’;
13. 列出没有填写街道的客户的姓名和联系电话。
Select cust_name,cust_phone from customer where cust_city is null;
展开阅读全文