|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
平台即服务PaaS、基础设施即服务IaaS、软件即服务SaaS都是我们比较熟悉的MySQL学习教程,现在又出现了数据库即服务DBaaS,以基于云的方式存储结构化数据。oracle|条记
#########创立数据库----look$ORACLE_HOME/rdbms/admin/buildall.sql#############
createdatabasedb01
maxlogfiles10
maxdatafiles1024
maxinstances2
logfile
GROUP1(/u01/oradata/db01/log_01_db01.rdo)SIZE15M,
GROUP2(/u01/oradata/db01/log_02_db01.rdo)SIZE15M,
GROUP3(/u01/oradata/db01/log_03_db01.rdo)SIZE15M,
datafileu01/oradata/db01/system_01_db01.dbf)SIZE100M,
undotablespaceUNDO
datafile/u01/oradata/db01/undo_01_db01.dbfSIZE40M
defaulttemporarytablespaceTEMP
tempfile/u01/oradata/db01/temp_01_db01.dbfSIZE20M
extentmanagementlocaluniformsize128k
charactersetAL32UTE8
nationalcharactersetAL16UTF16
settime_zone=America/New_York;
###############数据字典##########
setwrapoff
select*fromv$dba_users;
grantselectontable_nametouser/rule;
select*fromuser_tables;
select*fromall_tables;
select*fromdba_tables;
revokedbafromuser_name;
shutdownimmediate
startupnomount
select*fromv$instance;
select*fromv$sga;
select*fromv$tablespace;
altersessionsetnls_language=american;
alterdatabasemount;
select*fromv$database;
alterdatabaseopen;
descdictionary
select*fromdict;
descv$fixed_table;
select*fromv$fixed_table;
setoracle_sid=foxconn
select*fromdba_objects;
setserveroutputon
executedbms_output.put_line(sfasd);
#############把持文件###########
select*fromv$database;
select*fromv$tablespace;
select*fromv$logfile;
select*fromv$log;
select*fromv$backup;
/*备份用户表空间*/
altertablespaceusersbeginbackup;
select*fromv$archived_log;
select*fromv$controlfile;
altersystemsetcontrol_files=$ORACLE_HOME/oradata/u01/ctrl01.ctl,
$ORACLE_HOME/oradata/u01/ctrl02.ctlscope=spfile;
cp$ORACLE_HOME/oradata/u01/ctrl01.ctl$ORACLE_HOME/oradata/u01/ctrl02.ctl
startuppfile=../initSID.ora
select*fromv$parameterwherenamelikecontrol%;
showparametercontrol;
select*fromv$controlfile_record_section;
select*fromv$tempfile;
/*备份把持文件*/
alterdatabasebackupcontrolfileto../filepath/control.bak;
/*备份把持文件,并将二进制把持文件变成了asc的文本文件*/
alterdatabasebackupcontrolfiletotrace;
###############redolog##############
archiveloglist;
altersystemarchivelogstart;--启动主动存档
altersystemswitchlogfile;--强行举行一第二天志switch
altersystemcheckpoint;--强迫举行一次checkpoint
altertablspaceusersbeginbackup;
altertablespaceoffline;
/*checkpoint同步频次参数FAST_START_MTTR_TARGET,同步频次越高,体系恢复所需工夫越短*/
showparameterfast;
showparameterlog_checkpoint;
/*到场一个日记组*/
alterdatabaseaddlogfilegroup3(/$ORACLE_HOME/oracle/ora_log_file6.rdosize10M);
/*到场日记组的一个成员*/
alterdatabaseaddlogfilemember/$ORACLE_HOME/oracle/ora_log_file6.rdotogroup3;
/*删除日记组:以后日记组不克不及删;举动的日记组不克不及删;非回档的日记组不克不及删*/
alterdatabasedroplogfilegroup3;
/*删除日记组中的某个成员,但每一个组的最初一个成员不克不及被删除*/
alterdatabsedroplogfilemember$ORACLE_HOME/oracle/ora_log_file6.rdo;
/*扫除在线日记*/
alterdatabaseclearlogfile$ORACLE_HOME/oracle/ora_log_file6.rdo;
alterdatabaseclearlogfilegroup3;
/*扫除非回档日记*/
alterdatabaseclearunarchivedlogfilegroup3;
/*重定名日记文件*/
alterdatabaserenamefile$ORACLE_HOME/oracle/ora_log_file6.rdoto$ORACLE_HOME/oracle/ora_log_file6a.rdo;
showparameterdb_create;
altersystemsetdb_create_online_log_dest_1=path_name;
select*fromv$log;
select*fromv$logfile;
/*数据库回档形式到非回档形式的交换,要启动到mount形态下才干改动;startupmount;然后再翻开数据库.*/
alterdatabasenoarchivelog/archivelog;
achivelogstart;---启动主动回档
altersystemarchiveall;--手工回档一切日记文件
select*fromv$archived_log;
showparameterlog_archive;
######剖析日记文件logmnr##############
1)在init.ora中setutl_file_dir参数
2)从头启动oracle
3)create目次文件
descdbms_logmnr_d;
dbms_logmnr_d.build;
4)到场日记文件add/removelogfile
dhms_logmnr.add_logfile
dbms_logmnr.removefile
5)startlogmnr
dbms_logmnr.start_logmnr
6)剖析出来的内容查询v$logmnr_content--sqlredo/sqlundo
理论:
descdbms_logmnr_d;
/*对数据表做一些操纵,为恢复操纵做筹办*/
update表setqty=10wherestor_id=6380;
delete表wherestor_id=7066;
/***********************************/
utl_file_dir的路径
executedbms_logmnr_d.build(foxdict.ora,$ORACLE_HOME/oracle/admin/fox/cdump);
executedbms_logmnr.add_logfile($ORACLE_HOME/oracle/ora_log_file6.log,dbms_logmnr.newfile);
executedbms_logmnr.start_logmnr(dictfilename=>$ORACLE_HOME/oracle/admin/fox/cdump/foxdict.ora);
#########tablespace##############
select*formv$tablespace;
select*fromv$datafile;
/*表空间和数据文件的对应干系*/
selectt1.name,t2.namefromv$tablespacet1,v$datafilet2wheret1.ts#=t2.ts#;
altertablespaceusersadddatafilepathsize10M;
select*fromdba_rollback_segs;
/*限定用户在某表空间的利用限额*/
alteruseruser_namequota10montablespace_name;
createtablespacexxx[datafilepath_name/datafile_name][sizexxx][extentmanagementlocal/dictionary][defaultstorage(xxx)];
exmple:createtablespaceuserdatadatafile$ORACLE_HOME/oradata/userdata01.dbfsize100MAUTOEXTENDONNEXT5MMAXSIZE200M;
createtablespaceuserdatadatafile$ORACLE_HOME/oradata/userdata01.dbfsize100Mextentmanagementdictionarydefaultstorage(initial100knext100kpctincrease10)offline;
/*9i今后,oracle倡议利用local办理,而不利用dictionary办理,由于local接纳bitmap办理表空间,不会发生体系表空间的志愿争用;*/
createtablespaceuserdatadatafile$ORACLE_HOME/oradata/userdata01.dbfsize100Mextentmanagementlocaluniformsize1m;
createtablespaceuserdatadatafile$ORACLE_HOME/oradata/userdata01.dbfsize100Mextentmanagementlocalautoallocate;
/*在创立表空间时,设置表空间内的段空间办理形式,这里用的是主动办理*/
createtablespaceuserdatadatafile$ORACLE_HOME/oradata/userdata01.dbfsize100Mextentmanagementlocaluniformsize1msegmentspacemanagementauto;
altertablespaceuserdatamininumextent10;
altertablespaceuserdatadefaultstorage(initial1mnext1mpctincrease20);
/*undotablespace(不克不及被用在字典办理模下)*/
createundotablespaceundo1datafile$ORACLE_HOME/oradata/undo101.dbfsize40Mextentmanagementlocal;
showparameterundo;
/*temporarytablespace*/
createtemporarytablespaceuserdatatempfile$ORACLE_HOME/oradata/undo101.dbfsize10mextentmanagementlocal;
/*设置数据库缺省的一时表空间*/
alterdatabasedefaulttemporarytablespacetablespace_name;
/*体系/一时/在线的undo表空间不克不及被offline*/
altertablespacetablespace_nameoffline/online;
altertablespacetablespace_namereadonly;
/*重定名用户表空间*/
altertablespacetablespace_namerenamedatafile$ORACLE_HOME/oradata/undo101.dbfto$ORACLE_HOME/oradata/undo102.dbf;
/*重定名体系表空间,但在重定名前必需将数据库shutdown,偏重启到mount形态*/
alterdatabaserenamefile$ORACLE_HOME/oradata/system01.dbfto$ORACLE_HOME/oradata/system02.dbf;
droptablespaceuserdataincludingcontentsanddatafiles;---droptablespce
/*resizetablespace,autoextenddatafilespace*/
alterdatabasedatafile$ORACLE_HOME/oradata/undo102.dbfautoextendonnext10mmaxsize500M;
/*resizedatafile*/
alterdatabasedatafile$ORACLE_HOME/oradata/undo102.dbfresize50m;
/*给表空间扩大空间*/
altertablespaceuserdataadddatafile$ORACLE_HOME/oradata/undo102.dbfsize10m;
/*将表空间设置成OMF形态*/
altersystemsetdb_create_file_dest=$ORACLE_HOME/oradata;
createtablespaceuserdata;---useOMFstatustocreatetablespace;
droptablespaceuserdata;---userOMFstatustodroptablespace;
select*fromdba_tablespace/v$tablespace/dba_data_files;
/*将表的某分区挪动到另外一个表空间*/
altertabletable_namemovepartitionpartition_nametablespacetablespace_name;
######ORACLEstoragestructureandrelationships#########
/*手工分派表空间段的分区(extend)巨细*/
altertablekong.test12allocateextent(size1mdatafile$ORACLE_HOME/oradata/undo102.dbf);
altertablekong.test12deallocateunused;---开释表中没有效到的分区
showparameterdb;
altersystemsetdb_8k_cache_size=10m;---设置8k块的内存空间块参数
select*fromdba_extents/dba_segments/data_tablespace;
select*fromdba_free_space/dba_data_file/data_tablespace;
/*数据工具所占用的字节数*/
selectsum(bytes)fromdba_extentswhereonwer=kongandsegment_name=table_name;
############UNDOData################
showparameterundo;
altertablespaceusersofflinenormal;
altertablespaceusersofflineimmediate;
recoverdatafile$ORACLE_HOME/oradata/undo102.dbf;
altertablespaceusersonline;
select*fromdba_rollback_segs;
altersystemsetundo_tablespace=undotbs1;
/*疏忽回滚段的毛病提醒*/
altersystemsetundo_suppress_errors=true;
/*在主动办理形式下,不会真正创建rbs1;在手工办理形式则能够创建,且是公有回滚段*/
createrollbacksegmentrbs1tablespaceundotbs;
descdbms_Flashback;
/*在提交了修正的数据后,9i供应了旧数据的回闪操纵,将修正前的数据只读给用户看,但这部分数据不会又恢复在表中,而是旧数据的一个映照*/
executedbms_flashback.enable_at_time(26-JAN-04:12:17:00pm);
executedbms_flashback.disable;
/*回滚段的统计信息*/
selectend_time,begin_time,undoblksfromv$undostat;
/*undo表空间的巨细盘算公式:UndoSpace=[UR*(UPS*DBS)]+(DBS*24)
UR:UNDO_RETENTION保存的工夫(秒)
UPS:每秒的回滚数据块
DBS:体系EXTENT和FILESIZE(也就是db_block_size)*/
select*fromdba_rollback_segs/v$rollname/v$rollstat/v$undostat/v$session/v$transaction;
showparametertransactions;
showparameterrollback;
/*在手工办理形式下,创建大众的回滚段*/
createpublicrollbacksegmentprbs1tablespaceundotbs;
alterrollbacksegmentrbs1online;----在手工办理形式
/*在手工办理形式中,initSID.ora中指定undo_management=manual、rollback_segment=(rbs1,rbs2,...)、
transactions=100、transactions_per_rollback_segment=10
然后shutdownimmediate,startuppfile=....???.ora*/
##########ManagingTables###########
/*chartypemaxlen=2000;varchar2typemaxlen=4000bytes
rowid是18位的64进制字符串(10个bytes80bits)
rowid构成:object#(工具号)--32bits,6位
rfile#(绝对文件号)--10bits,3位
block#(块号)--22bits,6位
row#(行号)--16bits,3位
64进制:A-Z,a-z,0-9,/,+共64个标记
dbms_rowid包中的函数能够供应对rowid的注释*/
selectrowid,dbms_rowid.rowid_block_number(rowid),dbms_rowid.rowid_row_number(rowid)fromtable_name;
createtabletest2
(
idint,
lnamevarchar2(20)notnull,
fnamevarchar2(20)constraintck_1check(fnamelikek%),
empdatedatedefaultsysdate)
)tablespacetablespace_name;
createglobaltemporarytabletest2oncommitdelete/preserverowsasselect*fromkong.authors;
createtableuser.table(...)tablespacetablespace_namestorage(...)pctfree10pctused40;
altertableuser.tablenamepctfree20pctused50storage(...);---changingtablestorage
/*手工分派分区,分派的数据文件必需是表地点表空间内的数据文件*/
altertableuser.table_nameallocateextent(size500kdatafile...);
/*开释表中没有效到的空间*/
altertabletable_namedeallocateunused;
altertabletable_namedeallocateunusedkeep8k;
/*将非分区表的表空间搬到新的表空间,在挪动表空间后,原表中的索引工具将会不成用,必需重修*/
altertableuser.table_namemovetablespacenew_tablespace_name;
createindexindex_nameonuser.table_name(column_name)tablespaceusers;
alterindexindex_namerebuild;
droptabletable_name[CASCADECONSTRAINTS];
altertableuser.table_namedropcolumncol_name[CASCADECONSTRAINTSCHECKPOINT1000];---dropcolumn
/*给表中不必的列做标志*/
altertableuser.table_namesetunusedcolumncommentsCASCADECONSTRAINTS;
/*drop表中不必的做了标志列*/
altertableuser.table_namedropunusedcolumnscheckpoint1000;
/*当在dropcol是呈现非常,利用CONTINUE,避免重删后面的column*/
ALTERTABLEUSER.TABLE_NAMEDROPCOLUMNSCONTINUECHECKPOINT1000;
select*fromdba_tables/dba_objects;
########managingindexes##########
/*createindex*/
example:
/*创立一样平常索引*/
createindexindex_nameontable_name(column_name)tablespacetablespace_name;
/*创立位图索引*/
createbitmapindexindex_nameontable_name(column_name1,column_name2)tablespacetablespace_name;
/*索引中不克不及用pctused*/
create[bitmap]indexindex_nameontable_name(column_name)tablespacetablespace_namepctfree20storage(inital100knext100k);
/*年夜数据量的索引最好不要做日记*/
create[bitmap]indexindex_nametable_name(column_name1,column_name2)tablespace_namepctfree20storage(inital100knext100k)nologging;
/*创立反转索引*/
createindexindex_nameontable_name(column_name)reverse;
/*创立函数索引*/
createindexindex_nameontable_name(function_name(column_name))tablespacetablespace_name;
/*建表时创立束缚前提*/
createtableuser.table_name(column_namenumber(7)constraintconstraint_nameprimarykeydeferrableusingindexstorage(initial100knext100k)tablespacetablespace_name,column_name2varchar2(25)constraintconstraint_namenotnull,column_name3number(7))tablespacetablespace_name;
/*给创立bitmapindex分派的内存空间参数,以减速建索引*/
showparametercreate_bit;
/*改动索引的存储参数*/
alterindexindex_namepctfree30storage(initial200knext200k);
/*给索引手工分派一个分区*/
alterindexindex_nameallocateextent(size200kdatafile$ORACLE/oradata/..);
/*开释索引中没用的空间*/
alterindexindex_namedeallocateunused;
/*索引重修*/
alterindexindex_namerebuildtablespacetablespace_name;
/*一般索引和反转索引的交换*/
alterindexindex_namerebuildtablespacetablespace_namereverse;
/*重修索引时,不锁表*/
alterindexindex_namerebuildonline;
/*给索引收拾碎片*/
alterindexindex_nameCOALESCE;
/*剖析索引,现实上是更新统计的历程*/
analyzeindexindex_namevalidatestructure;
descindex_state;
dropindexindex_name;
alterindexindex_namemonitoringusage;-----监督索引是不是被用到
alterindexindex_namenomonitoringusage;----作废监督
/*有关索引信息的视图*/
select*fromdba_indexes/dba_ind_columns/dbs_ind_expressions/v$object_usage;
##########数据完全性的办理(Maintainingdataintegrity)##########
altertabletable_namedropconstraintconstraint_name;----drop束缚
altertabletable_nameaddconstraintconstraint_nameprimarykey(column_name1,column_name2);-----创立主键
altertabletable_nameaddconstraintconstraint_nameunique(column_name1,column_name2);---创立独一束缚
/*创立外键束缚*/
altertabletable_nameaddconstraintconstraint_nameforeignkey(column_name1)referencestable_name(column_name1);
/*不效验老数据,只束缚新的数据[enable/disable:束缚/不束缚新数据;novalidate/validate:不合错误/对老数据举行考证]*/
altertabletable_nameaddconstraintconstraint_namecheck(column_namelikeB%)enable/disablenovalidate/validate;
/*修正束缚前提,延时考证,commit时考证*/
altertabletable_namemodifyconstraintconstraint_nameinitiallydeferred;
/*修正束缚前提,当即考证*/
altertabletable_namemodifyconstraintconstraint_nameinitiallyimmediate;
altersessionsetconstraints=deferred/immediate;
/*drop一个有外键的主键表,带cascadeconstraints参数级联删除*/
droptabletable_namecascadeconstraints;
/*当truncate外键表时,先将外键设为有效,再truncate;*/
truncatetabletable_name;
/*设束缚前提有效*/
altertabletable_namedisableconstraintconstraint_name;
altertabletable_nameenablenovalidateconstraintconstraint_name;
/*将有效束缚的数据行放进exception的表中,此表纪录了违背数据束缚的行的行号;在此之前,要先建exceptions表*/
altertabletable_nameaddconstraintconstraint_namecheck(column_name>15)enablevalidateexceptionsintoexceptions;
/*运转创立exceptions表的剧本*/
start$ORACLE_HOME/rdbms/admin/utlexcpt.sql;
/*猎取束缚前提信息的表或视图*/
select*fromuser_constraints/dba_constraints/dba_cons_columns;
##################managingpasswordsecurityandresources####################
alteruseruser_nameaccountunlock/open;----锁定/翻开用户;
alteruseruser_namepasswordexpire;---设定口令到期
/*创建口令设置文件,failed_login_attempts口令输几次后锁,password_lock_times指几天后口令被主动解锁*/
createprofileprofile_namelimitfailed_login_attempts3password_lock_times1/1440;
/*创立口令设置文件*/
createprofileprofile_namelimitfailed_login_attempts3password_lock_timeunlimitedpassword_life_time30password_reuse_time30password_verify_functionverify_functionpassword_grace_time5;
/*创建资本设置文件*/
createprofileprfile_namelimitsession_per_user2cpu_per_session10000idle_time60connect_time480;
alteruseruser_nameprofileprofile_name;
/*设置口令解锁工夫*/
alterprofileprofile_namelimitpassword_lock_time1/24;
/*password_life_time指口令文件几工夫到期,password_grace_time指在第一次乐成登录后到口令到期有几地利间可改动口令*/
alterprofileprofile_namelimitpassword_lift_time2password_grace_time3;
/*password_reuse_time指口令在几天内可被重用,password_reuse_max口令可被重用的最年夜次数*/
alterprofileprofile_namelimitpassword_reuse_time10[password_reuse_max3];
alteruseruser_nameidentifiedbyinput_password;-----修正用户口令
dropprofileprofile_name;
/*创建了profile后,且指定给某个用户,则必需用CASCADE才干删除*/
dropprofileprofile_nameCASCADE;
altersystemsetresource_limit=true;---启用志愿限定,缺省是false
/*设置资本参数*/
alterprofileprofile_namelimitcpu_per_session10000connect_time60idle_time5;
/*资本参数(session级)
cpu_per_session每一个session占用cpu的工夫单元1/100秒
sessions_per_user同意每一个用户的并行session数
connect_time同意毗连的工夫单元分钟
idle_time毗连被余暇几工夫后,被主动断开单元分钟
logical_reads_per_session读块数
private_sga用户可以在SGA中利用的公有的空间数单元bytes
(call级)
cpu_per_call每次(1/100秒)挪用cpu的工夫
logical_reads_per_call每次挪用可以读的块数
*/
alterprofileprofile_namelimitcpu_per_call1000logical_reads_per_call10;
descdbms_resouce_manager;---资本办理器包
/*猎取资本信息的表或视图*/
select*fromdba_users/dba_profiles;
######Managingusers############
showparameteros;
createusertestuser1identifiedbykxf_001;
grantconnect,createtabletotestuser1;
alterusertestuser1quota10montablespace_name;
/*创立用户*/
createuseruser_nameidentifiedbypassworddefaulttablespacetablespace_nametemporarytablespacetablespace_namequota15montablespace_namepasswordexpire;
/*数据库级设定缺省一时表空间*/
alterdatabasedefaulttemporarytablespacetablespace_name;
/*制订数据库级的缺省表空间*/
alterdatabasedefaulttablespacetablespace_name;
/*创立os级考核的用户,需晓得os_authent_prefix,暗示oracle和os口令对应的前缀,OPS$为此参数的值,此值能够恣意设置*/
createuseruser_nameidentifiedbyexternallydefaultOPS$tablespace_nametablespace_nametemporarytablespacetablespace_namequota15montablespace_namepasswordexpire;
/*修正用户利用表空间的限额,回滚表空间和一时表空间不同意授与限额*/
alteruseruser_namequota5montablespace_name;
/*删除用户或删除级联用户(用户工具下有工具的要用CASCADE,将其下一些工具一同删除)*/
dropuseruser_name[CASCADE];
/*每一个用户在哪些表空间下有些甚么限额*/
descdba_ts_quotas;select*fromdba_ts_quotaswhereusername=...;
/*改动用户的缺省表空间*/
alteruseruser_namedefaulttablespacetablespace_name;
#########ManagingPrivileges#############
grantcreatetable,createsessiontouser_name;
grantcreateanytabletouser_name;revokecreateanytablefromuser_name;
/*授与权限语法,public标识一切用户,withadminoption同意能将权限授与圈外人的权限*/
grantsystem_privs,[......]to[user/role/public],[....][withadminoption];
select*fromv$pwfile_users;
/*当O7_dictionary_accessiblity参数为True时,标识selectanytable时,包含体系表也能select,不然,不包括体系表;缺省为false*/
showparameterO7;
/*因为O7_dictionary_accessiblity为静态参数,不克不及静态改动,故加scope=spfile,下次启动时才失效*/
altersystemsetO7_dictionary_accessiblity=truescope=spfile;
/*授与工具中的某些字段的权限,如select某表中的某些字段的权限*/
grant[object_privs(column,....)],[...]onobject_nametouser/role/public,...withgrantoption;
/*oracle不同意授与select某列的权限,但能够授insert,update某列的权限*/
grantinsert(column_name1,column_name2,...)ontable_nametouser_namewithgrantoption;
select*fromdba_sys_privs/session_privs/dba_tab_privs/user_tab_privs/dba_col_privs/user_col_privs;
/*db/os/none审计被纪录在数据库/操纵体系/不审计缺省是none*/
showparameteraudit_trail;
/*启动对表的select举措*/
auditselectonuser.table_namebysession;
/*bysession在每一个session中收回command只纪录一次,byaccess则每一个command都纪录*/
audit[createtable][select/update/insertonobjectbysession/access][wheneversuccessful/notsuccessful];
descdbms_fga;---进一步计划,则可以使用dbms_fgs包
/*作废审计*/
noauditselectonuser.table_name;
/*查被审计信息*/
select*fromall_def_audit_opts/dba_stmt_audit_opts/dba_priv_audit_opts/dba_obj_audit_opts;
/*猎取审计纪录*/
select*fromdba_audit_trail/dba_audit_exists/dba_audit_object/dba_audit_session/dba_audit_statement;
###########ManagingRole#################
createrolerole_name;grantselectontable_nametorole_name;grantrole_nametouser_name;setrolerole_name;
createrolerole_name;
createrolerole_nameidentifiedbypassword;
createrolerole_nameidentifiedexternally;
setrolerole_name;----激活role
setrolerole_nameidentifiedbypassword;
alterrolerole_namenotidentified;
alterrolerole_nameidentifiedbypassword;
alterrolerole_nameidentifiedexternally;
grantpriv_nametorole_name[WITHADMINOPTION];
grantupdate(column_name1,col_name2,...)ontable_nametorole_name;
grantrole_name1torole_name2;
/*创建defaultrole,用户登录时,缺省激活defaultrole*/
alteruseruser_namedefaultrolerole_name1,role_name2,...;
alteruseruser_namedefaultroleall;
alteruseruser_namedefaultroleallexceptrole_name1,...;
alteruseruser_namedefaultrolenone;
setrolerole1[identifiedbypassword],role2,....;
setroleall;
setroleexceptrole1,role2,...;
setrolenone;
revokerole_namefromuser_name;
revokerole_namefrompublic;
droprolerole_name;
select*fromdba_roles/dba_role_privs/role_role_privs/dba_sys_privs/role_sys_privs/role_tab_privs/session_roles;
###########BasicSQLSELECT################
selectcol_nameascol_aliasfromtable_name;
selectcol_namefromtable_namewherecol1like_o%;----_婚配单个字符
/*利用字符函数(右侧截取,字段中包括某个字符,右边添补某字符到流动位数,右侧添补某字符到流动位数)*/
selectsubstr(col1,-3,5),instr(col2,g),LPAD(col3,10,$),RPAD(col4,10,%)fromtable_name;
/*利用数字函数(往右/左几位四舍五进,取整,取余)*/
selectround(col1,-2),trunc(col2),mod(col3)fromtable_name;
/*利用日期函数(盘算两个日时代相差几个礼拜,两个日时代相隔几个月,在某个月份上加几个月,某个日期的下一个日期,
某日期地点月的最初的日期,对某个日期的月分四舍五进,对某个日期的月份举行取整)*/
select(sysdate-col1)/7week,months_between(sysdate,col1),add_months(col1,2),next_day(sysdate,FRIDAY),last_day(sysdate),
round(sysdate,MONTH),trunc(sysdate,MONTH)fromtable_name;
/*利用NULL函数(当expr1为空取expr2/当expr1为空取expr2,不然取expr3/当expr1=expr2前往空)*/
selectnvl(expr1,expr2),nvl2(expr1,expr2,expr3),nullif(expr1,expr2)fromtable_name;
selectcolumn1,column2,column3,casecolumn2when50thencolumn2*1.1
when30thencolumn2*2.1
when10thencolumn3/20
elsecolumn3
endasttt
fromtable_name;------利用case函数
selecttable1.col1,table2.col2fromtable1
[CROSSJOINtable2]|-----笛卡儿毗连
[NATURALJOINtable2]|-----用两个表中的同名列毗连
[JOINtable2USING(column_name)]|-----用两个表中的同名列中的某一列或几列毗连
[JOINtable2
ON(table1.col1=table2.col2)]|
[LEFT|RIGHT|FULLOUTERJOINtable2------相称于(+)=,=(+)毗连,全外毗连
ON(table1.col1=table2.col2)];------SQL1999中的JOIN语法;
example:
selectcol1,col2fromtable1t1
jointable2t2
ont1.col1=t2.col2andt1.col3=t2.col1
jointable3t3
ont2.col1=t3.col3;
select*fromtable_namewherecol1<any(selectcol2fromtable_name2wherecontinuegroupbycol3);
select*fromtable_namewherecol1<all(selectcol2fromtable_name2wherecontinuegroupbycol3);
insertinto(selectcol1,col2,col3formtable_namewherecol1>50withcheckoption)values(value1,value2,value3);
MERGEINTOtable_nametable1
USINGtable_name2table2
ON(table1.col1=table2.col2)
WHENMATCHEDTHEN
UPDATESET
table1.col1=table2.col2,
table1.col2=table2.col3,
...
WHENNOTMATCHEDTHEN
INSERTVALUES(table2.col1,table2.col2,table2.col3,...);-----兼并语句
#####################CREATE/ALTERTABLE#######################
altertabletable_namedropcolumncolumn_name;---dropcolumn
altertabletable_namesetunused(col1,col2,...);----设置列有效,这个对照快。
altertabletable_namedropunusedcolumns;---删除被设为有效的列
renametable_name1totable_name2;---重定名表
commentontabletable_nameiscommentmessage;----给表放进正文信息
createtabletable_name
(col1intnotnull,col2varchar2(20),col3varchar2(20),
constraintuk_test2_1unique(col2,col3)));-----界说表中的束缚前提
altertabletable_nameaddconstraintpk_test2primarykey(col1,col2,...);----创立主键
/*创建外键*/
createtabletable_name(ridint,namevarchar2(20),constraintfk_test3foreignkey(rid)referencesother_table_name(id));
altertabletable_nameaddconstraintck_test3check(namelikeK%);
altertabletable_namedropconstraintconstraint_name;
altertabletable_namedropprimarykeycascade;----级联删除主键
altertabletable_namedisable/enableconstraintconstraint_name;----使束缚临时有效
/*删除列,并级联删除此列下的束缚前提*/
altertabletable_namedropcolumncolumn_namecascadeconstraint;
select*fromuser_constraints/user_cons_columns;---束缚前提相干视图
##############CreateViews#####################
CREATE[ORREPLACE][FORCE|NOFORCE]VIEWview_name[(alias[,alias]...)]
ASsubquery
[WITHCHECKOPTION[CONSTRAINTconstraint_name]]
[WITHREADONLY[CONSTRAINTconstraint_name]];------创立视图的语法
example:Createorreplaceviewtestviewasselectcol1,col2,col3fromtable_name;------创立视图
/*利用别号*/
Createorreplaceviewtestviewasselectcol1,sum(col2)col2_aliasfromtable_name;
/*创立庞大视图*/
Createviewview_name(alias1,alias2,alias3,alias4)asselectd.col1,min(e.col1),max(e.col1),avg(e.col1)fromtable_name1e,table_name2dwheree.col2=d.col2groupbyd.col1;
/*当用update修正数据时,必需满意视图的col1>10的前提,不满意则不克不及被改动.*/
Createorreplaceviewview_nameasselect*fromtable_namewherecol1>10withcheckoption;
/*改动视图的值.关于复杂视图能够用update语法修正表数据,但庞大视图则纷歧定能改。如利用了函数,groupby,distinct等的列*/
updateview_namesetcol1=value1;
/*TOP-N剖析*/
select[column_list],rownumfrom(select[column_list]fromtable_nameorderbyTop-N_column)whererownum<=N;
/*找出某列三条最年夜值的纪录*/
example:selectrownumasrank,col1,col2from(selectcol1,col2fromtable_nameorderbycol2desc)whererownum<=3;
#############OtherdatabaseObject###############
CREATESEQUENCEsequence_name[INCREMENTBYn]
[STARTWITHn]
[{MAXVALUEn|NOMAXVALUE}]
[{MINVALUEn|NOMINVALUE}]
[{CYCEL|NOCYCLE}]
[{CACHEn|NOCACHE}];-----创立SEQUENCE
example:
CREATESEQUENCEsequence_nameINCREMENTBY10
STARTWITH120
MAXVALUE9999
NOCACHE
NOCYCLE;
select*fromuser_sequences;---以后用户下纪录sequence的视图
selectsequence_name.nextval,sequence_name.currvalfromdual;-----sequence的援用
altersequencesequence_nameINCREMENTBY20
MAXVALUE999999
NOCACHE
NOCYCLE;-----修正sequence,不克不及改动肇端序号
dropsequencesequence_name;----删除sequence
CREATE[PUBLIC]SYNONYMsynonym_nameFORobject;------创立同义词
DROP[PUBLIC]SYNONYMsynonym_name;----删除同义词
CREATEPUBLICDATABASELINKlink_nameUSEINGOBJECT;----创立DBLINK
select*fromobject_name@link_name;----会见远程数据库中的工具
/*union操纵,它将两个汇合的交集部分紧缩,并对数据排序*/
selectcol1,col2,col3fromtable1_nameunionselectcol1,col2,col3fromtable2_name;
/*unionall操纵,两个汇合的交集部分不紧缩,且不合错误数据排序*/
selectcol1,col2,col3fromtable1_nameunionallselectcol1,col2,col3fromtable2_name;
/*intersect操纵,求两个汇合的交集,它将对反复数据举行紧缩,且排序*/
selectcol1,col2,col3fromtable1_nameintersectselectcol1,col2,col3fromtable2_name;
/*minus操纵,汇合减,它将紧缩两个汇合减后的反复纪录,且对数据排序*/
selectcol1,col2,col3fromtable1_nameminusselectcol1,col2,col3fromtable2_name;
/*EXTRACT抽取工夫函数.此例是抽取以后日期中的年*/
selectEXTRACT(YEARFROMSYSDATE)fromdual;
/*EXTRACT抽取工夫函数.此例是抽取以后日期中的月*/
selectEXTRACT(MONTHFROMSYSDATE)fromdual;
##########################加强的groupby子句#########################
select[column,]group_function(column)...
fromtable
[WHEREcondition]
[GROUPBY[ROLLUP]group_by_expression]
[HAVINGhaving_expression];
[ORDERBYcolumn];-------ROLLUP操纵字,对groupby子句的各字段从右到左举行再聚合
example:
/*其了局看起来象对col1做小计*/
selectcol1,col2,sum(col3)fromtablegroupbyrollup(col1,col2);
/*复合rollup表达式*/
selectcol1,col2,sum(col3)fromtablegroupbyrollup((col1,col2));
select[column,]group_function(column)...
fromtable
[WHEREcondition]
[GROUPBY[CUBE]group_by_expression]
[HAVINGhaving_expression];
[ORDERBYcolumn];-------CUBE操纵字,除完成ROLLUP的功效外,再对ROLLUP后的了局集从右到左再聚合
example:
/*其了局看起来象对col1做小计后,再对col2做小计,最初算总计*/
selectcol1,col2,sum(col3)fromtablegroupbycube(col1,col2);
/*复合rollup表达式*/
selectcol1,col2,sum(col3)fromtablegroupbycube((col1,col2));
/*夹杂rollup,cube表达式*/
selectcol1,col2,col3,sum(col4)fromtablegroupbycol1,rollup(col2),cube(col3);
/*GROUPING(expr)函数,检察select语句种以何字段聚合,其取值为0或1*/
select[column,]group_function(column)...,GROUPING(expr)
fromtable
[WHEREcondition]
[GROUPBY[ROLLUP]group_by_expression]
[HAVINGhaving_expression];
[ORDERBYcolumn];
example:
selectcol1,col2,sum(col3),grouping(col1),grouping(col2)fromtablegroupbycube(col1,col2);
/*groupingsets操纵,对groupby了局集先对col1乞降,再对col2乞降,最初将其了局集并在一同*/
selectcol1,col2,sum(col3)fromtablegroupbygroupingsets((col1),(col2));
越来越多的开发者将继续选择MySQL。Evans的总裁JohnAndrews表示,MySQL学习教程用户对MySQL和其他开源数据库的评价正在赶上甚至超过很多专有商业数据库软件。 |
|