ORACLE数据库操作入门
1、数据库工作环境基础设置:
在LINUX下用ORACLE用户登陆操作系统,然后用SQLPLUS 以数据库的 超户 登陆 数据库:
$ $ORACLE_HOME/bin/sqlplus / as sysdba
建立表空间:
SQL>CREATE TABLESPACE ITS DATAFILE '/u01/oradata/its01.dbf' SIZE 100M
AUTOEXTEND ON NEXT 10M MAXSIZE 32767M;
建用户test:
SQL>CREATE USER test IDENTIFIED BY test DEFAULT TABLESPACE ITS TEMPORARY
TABLESPACE TEMP ACCOUNT UNLOCK;
赋test连接数据库、资源访问权限:
SQL>GRANT CONNECT,RESOURCE TO test;
2、常用SQL操练:
在LINUX下用ORACLE用户登陆操作系统,然后用SQLPLUS 以数据库的test用户登陆本地数据库:
$ $ORACLE_HOME/bin/sqlplus test/test
SQL>
建表:
SQL>create table ccc(a varchar(2),n varchar(2));
插数据入表:
SQL>insert into ccc values('aa','bb');
建表:
create table bb2(a varchar(2),n varchar(2), PRIMARY KEY ( a ));
插数据入表:
insert into bb2 values('aa','bb');
insert into bb2 values('a1','b1');
insert into bb2 values('a2','b2');
insert into bb2 values('a3','b3');
insert into bb2 values('a4','b4');
提交数据:
commit;
查询:
select * from bb2;
select count(*) from bb2;
修改表AAA、BBB添加CC字段,默认值zz:
ALTER TABLE aaa ADD cc varchar2(4) default 'zz';
ALTER TABLE bbb ADD cc varchar2(4) default 'zz';
修改表AAA的字段名CC为DD:
alter table aaa rename column cc to dd;
修改表BBB的字段CC长度3,默认值为x4:
alter table bbb modify cc varchar2(3) default 'x4';
insert into bbb values('aa','bb','xx');
commit;
删除BBB表中字段cc的值为zz的记录:
delete from bbb where cc='zz';
commit;
更新BBB表中字段a的值为az:
update bbb set a='az';
commit;
insert into bbb values('a1','bb','xx');
insert into bbb values('a2','bb','xx');
insert into bbb values('a3','bb','xx');
commit;
删除BBB表所有记录:
truncate table bbb;
insert into bbb values('a1','bb','xx');
insert into bbb values('a2','bb','xx');
commit;
删除bbb表:
drop table bbb;
建表:
create table bbb(a varchar(2),n varchar(2),cc varchar2(3), PRIMARY KEY ( a ));
insert into bbb values('a1','bb','xx');
insert into bbb values('a2','bb','xx');
commit;
建索引:
CREATE INDEX nIndex ON bbb (n,cc desc);
删除索引:
drop index nIndex;
查看索引:
select INDEX_NAME, TABLE_NAME from user_indexes;