21天学通Oracle课后答案第三版Word格式.docx
- 文档编号:7959140
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:52
- 大小:344.87KB
21天学通Oracle课后答案第三版Word格式.docx
《21天学通Oracle课后答案第三版Word格式.docx》由会员分享,可在线阅读,更多相关《21天学通Oracle课后答案第三版Word格式.docx(52页珍藏版)》请在冰点文库上搜索。
sqlplussys/abc123@
10.1.0-Productionon31-7月-201119:
37:
17
Copyright(c)1991,2004,Oracle.Allrightsreserved.
Startingtnslsnr:
pleasewait...
TNSLSNRfor32-bitWindows:
Version10.1.0.-Production
SystemparameterfileisD:
\oracle\NETWORK\ADMIN\
Logmessageswrittentod:
\oracle\network\log\
Listeningon:
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=to(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oftheLISTENER
------------------------
Aliaslistener_1522
VersionTNSLSNRfor32-bitWindows:
StartDate31-7月-201119:
Uptime0days0hr.0min.0sec
TraceLeveloff
SecurityON:
LocalOSAuthentication
SNMPOFF
ListenerParameterFileD:
ListenerLogFiled:
ListeningEndpointsSummary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=Summary...
Service"
ORCL"
has1instance(s).
Instance"
statusUNKNOWN,has1handler(s)forthisservice...
Thecommandcompletedsuccessfully
这样,即使没有其他监听程序的存在,或者1521端口冲突。
Oracle也会自动通过监听程序LISTENER_1522来连接数据库ORCL。
第3章SQLPlus和PL/SQL
1.利用sqlplus登录数据库,并查看数据库版本。
利用SQLPlus登录数据库:
C:
\>
sqlplus/assysdba
SQL*Plus:
Release10.1.0.-Productionon星期日7月1018:
43:
502011
Copyright(c)1982,2004,Oracle.Allrightsreserved.
Connectedto:
OracleDatabase10gEnterpriseEditionRelease10.1.0.-Production
WiththePartitioning,OLAPandDataMiningoptions
SQL>
在控制台上打印出的信息,可以清楚看到Oracle数据库的版本为10.1.0.。
2.在对数据库进行重要操作时,首先应该确认数据库身份,以免在其他数据库上进行操作。
尝试利用SQLPlus显示数据库实例名称。
1)利用SQLPlus登录数据库:
2)键入showparameterinstance_name来查看实例名称
showparameterinstance_name
NAMETYPEVALUE
-----------------------------------------------------------------------------
instance_namestringorcl
3.试着利用SQLPlus来创建一个新表test(idnumber,namevarchar2(20))。
可以通过如下步骤利用SQLPlus来创建一个新表:
(1)利用SQLPlus登录数据库ORCL。
在Windows的【开始】|【运行】的【打开】文本框中输入sqlplusscott/abc123@orcl来登录数据库ORCL。
(2)在SQLPlus命令行下输入如下命令来创建新表test:
createtabletest(idnumber,namevarchar2(20));
(3)在SQLPlus会出现表创建成功的提示,如图所示。
此时,证明表创建成功。
第4章Oracle数据库
1.在数据库中创建一个表lob_source(idnumber,descriptionclob)。
将表lob_test的数据导入另外一个数据表lob_dest(idnumber,descriptionclob)。
1)创建表lob_source
createtablelob_source(idnumber,descriptionclob);
Tablecreated.
2)向表lob_source中插入测试数据
insertintolob_sourcevalues(1,'
aclobtextfromsource'
);
1rowcreated.
3)创建测试表lob_dest
createtablelob_dest(idnumber,descriptionclob);
4)向测试表lob_dest中插入测试数据,但是不包含clob类型的description列
insertintolob_dest(id)values
(1);
5)利用表lob_source中的description信息,更新表lob_dest中的description信息。
updatelob_destsetdescription=(selectdescriptionfromlob_sourcesourcewhere=;
1rowupdated.
select*fromlob_dest;
IDDESCRIPTION
--------------------------------------------------
1aclobtextfromsource
该实例实际说明了针对lob类型的数据的操作方式。
由于lob类型的数据的特殊性,因此在实现数据库迁移时,如果遇到棘手的lob类型处理,可以考虑利用本例所演示的方法。
2.利用exp/imp方式,将数据库orcl中users表的内容,迁移到数据库test中。
1)在数据库orcl中,创建测试表users
createtableusers(user_idnumber,user_namevarchar(20));
insertintousersvalues(1,'
allen'
insertintousersvalues(2,'
mike'
commit;
Commitcomplete.
2)导出表users到d:
\
expsystem/abc123@10.1.0-Productionon星期三7月1300:
06:
222011
OracleDatabase10gEnterpriseEditionRelease10.1.0.-Production
ExportdoneinZHS16GBKcharactersetandAL16UTF16NCHARcharacterset
AbouttoexportspecifiedtablesviaConventionalPath...
..exportingtableUSERS2rowsexported
Exportterminatedsuccessfullywithoutwarnings.
3)将d:
\的内容导入数据库test
impsystem/abc123@10.1.0-Productionon星期三7月1300:
10:
092011
ExportfilecreatedbyEXPORT:
V10.01.00viaconventionalpath
importdoneinZHS16GBKcharactersetandAL16UTF16NCHARcharacterset
.importingSYSTEM'
sobjectsintoSYSTEM
..importingtable"
USERS"
2rowsimported
Importterminatedsuccessfullywithoutwarnings.
3.如果数据库处于归档模式,那么,随着时间的累积,归档日志将会占用很大空间。
一旦达到默认大小20G,那么将导致数据库挂起,在告警日志中一般会有如下提示:
ORA-00257:
archivererror.Connectinternalonly,untilfreed。
利用修改参数db_recovery_file_dest_size的方式,快速解决数据库无法归档的问题。
1)查看默认空间大小
showparameterdb_recovery_file_dest_size
NAMETYPEVALUE
---------------------------------------------------
db_recovery_file_dest_sizebiginteger2G
2)修改其大小
altersystemsetdb_recovery_file_dest_size=3Gscope=both;
Systemaltered.
这一用法,适合于快速处理现场由于归档日志过大导致的数据库挂起。
第5章Oracle数据表对象
1.创建一个表空间testsize,其数据文件大小为2M,并设置自动增长尺寸为1M。
在表空间中建立一个数据表,并向其中插入大量数据,观察表空间文件的变化。
1)创建一个大小为2M,自动增长尺寸为1M的表空间
createtablespacetestsizedatafile'
e:
\database\oracle\'
size2M
2autoextendonnext1M
3/
Tablespacecreated
2)创建一个数据表test_tablespace_size(test_datavarchar2(100))
createtabletest_tablespace_size(test_datavarchar2(100))tablespacetestsize;
Tablecreated
3)利用如下SQL语句向表test_tablespace_size中插入数据
begin
2foriin1..100000loop
3insertintotest_tablespace_sizevalues('
09'
4endloop;
5commit;
6end;
7/
PL/SQLproceduresuccessfullycompleted
4)此时,表空间文件将增长为3M。
2.删除表空间testsize,同时删除其物理文件。
删除表空间应该使用droptablespace命令,同时删除物理文件,应使用includingcontentsanddatafiles。
droptablespacetestsizeincludingcontentsanddatafiles;
Tablespacedropped.
3.在数据库中创建一个表test_bak,并向其中插入10条记录。
利用exp/imp命令来实现该数据表的备份/恢复。
1)在数据库中创建表test_bak(idnumber)。
createtabletest_bak(idnumber);
2)向其中插入10条数据。
2foriin1..10loop
3insertintotest_bakvalues(i);
5end;
6/
Commitcomplete
3)利用exp命令备份该表
expsystem/abc123@10.1.0-Productionon星期六7月1614:
51:
542011
..exportingtableTEST_BAK10rowsexported
Exportterminatedsuccessfullywithoutwarnings..
4)在数据库中删除表test_bak。
droptabletest_bak;
Tabledropped
5)将表test_bak重新导入数据库
impsystem/abc123@10.1.0-Productionon星期六7月1614:
54:
242011
TEST_BAK"
10rowsimported
6)重新查询表test_bak
select*fromtest_bak;
ID
----------
1
2
3
4
5
6
7
8
9
10
10rowsselected
第6章约束
1.查看表customers的主键状况,如果有,则重建其主键,如果没有,选择其中一列创建主键。
1)利用如下SQL语句查看表customers的主键状况:
selecttable_name,constraint_name,constraint_type,statusfromuser_constraints
2wheretable_name='
CUSTOMERS'
andconstraint_type='
P'
;
TABLE_NAMECONSTRAINT_NAMECONSTRAINT_TYPESTATUS
------------------------------------------------------------------------------------------------------
CUSTOMERSSYS_C005015PENABLED
2)此时,在已有主键的情况下,首先删除主键SYS_C005015。
altertablecustomersdropprimarykey;
altertablecustomersdropprimarykey
ORA-02273:
thisunique/primarykeyisreferencedbysomeforeignkeys
3)表customers中的主键与其他表的外键关联,可以利用cascade选项来删除关联约束。
altertablecustomersdropprimarykeycascade;
Tablealtered
4)重新创建基于列customer_id的主键。
altertablecustomersaddprimarykey(customer_id);
2.在数据库中,创建表country(country_id,country_name)、city(city_id,country_id,city_name),并建立到的外键关联。
1)创建表country和city
createtablecountry(country_idnumber,country_namevarchar2(50));
createtablecity(city_idnumber,city_namevarchar2(50),country_idnumber);
2)在表country的country_id列上创建主键约束
altertablecountryaddprimarykey(country_id);
3)在表city上创建country_id到表country(country_id)的外键关联
altertablecityaddforeignkey(country_id)referencescountry(country_id);
3.验证所建外键关联的作用。
1)尝试向表city中添加城市信息。
insertintocity(city_id,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 21 天学通 Oracle 课后 答案 第三