Oracle_day01
oracle第一天:
(1)oracle安装
a>oracle下常用命令
sqlplus scott/tiger@192.168.56.101:1521/orcl
conn sys/raobing@192.168.56.101:1521/orcl as sysdba
show user;
set linesize 150;
show linesize;
set pagesize 20;
show pagesize;
col ename for a8;
ed 回车
spool d:1.text
spool off
host cls
/ /* */ --
SQLPlus命令帮助: SQL> help index
(2)创建表空间+用户+系统管理员授权
a>创建表空间tablespace
create tablespace waterboss
datafile 'c:waterboss.dbf' -- 指定数据文件
size 100m -- 指定数据文件大小
autoextend on -- 启用自动扩展
next 10m; -- 指定每次扩展空间大小
创建表空间,指定多个数据文件:
create tablespace testtb
datafile '/u01/app/oracle/oradata/orcl/testtb01.dbf' size 100M,
'/u01/app/oracle/oradata/orcl/testtb02.dbf' size 100M
autoextend on next 10M;
b>创建用户user
create user shilin identified by 123 default tablespace waterboss
c>给创建的新用户授权
grant ‘ORA-01045: user lacks CREATE SESSION privilege; logon denied’,
解决办法:grant create session,resource to
系统管理员给新用户授权:
grant dba to shilin;
(3)创建表+维护表结构
a>oracle常用数据类型 char,varchar2,long,number,date,clob,blob
b>创建表+约束
1.基本创建表语句
create table myemp3( id number(10), name varchar2(10) constraint myemp3_name_nn not null,--非空约束 gender varchar2(4) default '男',--默认值 deptno NUMBER(2), email varchar2(20), constraint myemp3_id_pk primary key(id), --主键约束 constraint myemp3_gender_ck check(gender in('男','女')), --检查约束 constraint myemp3_email_uk unique(email), --唯一约束 constraint myemp3_deptno_fk foreign key(deptno) references dept(deptno) on delete set null --外键约束 -- 没有这种写法constraint myemp3_name_nn not null ; );
2.使用子查询创建表(CTAS)
create table myemp2 as select * from myemp where 1=2;
由于1=2为假,该语句只拷贝了结构,没有拷贝数据;
c>删除表:
1.彻底删除表 drop table myemp3 purge;
不加purge是暂时放到回收站中了
2.drop之后的数据放到了recyclebin回收站中
查看回收站:show recyclebin;
彻底清除回收站:purge recyclebin;
彻底删除表:drop table myemp3 purge;
查看回收站表数据: select * from "BIN$YTQc8f5pTZ6kuFyu4GSeug==$0";
3.闪回 flashback table myemp3 to before drop;
e>修改表(add/modify/drop/rename)
1.修改表中列
oracle: alter table myemp3 modify ename vachar2(20); 不能带column
mysql : alter table myemp3 modify (column) ename varchar(20);
2.增加表中列
oracle: alter table myemp3 add gender varchar2(4);
mysql : alter table myemp3 add (column) gender varchar2(4);
3.删除表中列
oracle: alter table myemp3 drop column gender; 必须带column
mysql : alter table myemp3 drop (column) gender;
4.重新命名表中列名
oracle: alter table myemp3 rename column ename to myname; 必须带column
mysql : alter table myemp3 change (column) ename myname varchar(20);
5.重新命名表名
oracle: rename myemp3 to myemp4;
mysql : rename table mysqltname3 to mysqltname4; 必须带table
(3)对表数据操作(insert,update,delete)以及对表操作(alter)
a> 向表中插入数据(insert into values)
insert into 表名(id,name) values(xx,yy),(xx1,yy1) (mysql中可以这样写,oracle不能这样写)
inser all into 表名(id,name) values(xx1,yy1) into 表名(id,name) values(xx2,yy2) select 1 from dual;
b>修改表中数据(update set) update 表名 set dateddd=dateadd-3,col1=col1+1;
c>删除表中数据 delete from 表名 where xx; truncate table 表名;
d>oracle中的事务
1.oracle和mysql事务区别:
oracle中事务: 在insert/update/delete 中默认操作之后需要commit才能改变真正改变数据;
mysql中事务: 直接执行insert/update/delete 就已经改变了数据
2.设置事务: set transaction read only;
3.oracle中支持3种事务; read commited, read only ,serializable
(4)导入和导出 imp scott/tiger exp scott/tiger 11g以上,可以使用命令expdp/impdp进行导出导入