Oracle SQL Commands
Frequent Used Commands.
create table ...
drop table ...
alter table ...
desc tablename
insert
delete
update
column columnname format a10; /*Format column*/
select table_name from user_tables;
select * from user_objects where object_type = 'INDEX';
DROP TABLE SP;
DROP TABLE S;
DROP TABLE P;
CREATE TABLE S
(S# CHAR(2) NOT NULL,
SNAME CHAR(20) NOT NULL,
CITY VARCHAR(20),
STATUS INTEGER,
pk_s primary key (S#));
/* the above is standard SQL, below is Oracle 7 SQL create table
statement.*/
/* both should work. I prefer the latter, because ... */
CREATE TABLE S
(S# CHAR(2) NOT NULL,
SNAME CHAR(20) NOT NULL,
CITY VARCHAR(20),
STATUS INTEGER,
constraint pk_s primary key (S#));
CREATE TABLE P
(P# CHAR(2) NOT NULL,
PNAME VARCHAR(20) NOT NULL,
COLOR VARCHAR(10),
WEIGHT NUMBER(7,2),
CITY VARCHAR(20),
constraint pk_p primary key (P#));
CREATE TABLE SP
(S# CHAR(2) NOT NULL,
P# CHAR(2) NOT NULL,
QTY NUMBER(3) NOT NULL,
constraint pk_sp primary key (S#, P#),
constraint fk_sp
foreign key (S#) references S,
foreign key (P#) references P);
COMMIT;
alter table sp add (constraint check_qty check QTY < 1000);
alter table sp disable constraint pk_sp;
alter table sp disable constraint fk_sp;
alter table sp disable constraint fk_sp;
alter table sp enable constraint pk_sp;
alter table sp enable constraint fk_sp;
alter table sp enable constraint fk_sp;
insert into s values ('S1','Liang',null,null);
insert into s values ('S2','Liang',null,null);
insert into s values ('S3','Liang',null,null);
insert into P values ('P1','Screw',null,null, null);
insert into P values ('P2','Nut',null,null, null);
insert into P values ('S3','Dry Wall',null,null, null);
delete from SP where S# = 'S1';
insert into SP values ('S1','P1',100);
update SP
set P# = 'P2', QTY = QTY*2
where S# = 'S1';
Created by Y. Daniel Liang, June 15, 1996.
Back to Oracle at IPFW
Oracle basic commands and queries
Some basic commands which may be handy for technical support people without
a lot of Oracle skills. All commands should be executed from SQLPlus unless
otherwise indicated:
LIST ALL USERS:
select username from dba_users;
TO LIST ALL ROLES:
select role from dba_roles;
TO LIST ALL ROLES FOR ALL USERS:
select * from user_role_privs;
TO LIST DEFAULT TABLESPACES FOR ALL USERS:
select username,created,default_tablespace from dba_users;
TO LIST CURRENTLY CONNECTED USERS:
select username,sid,serial#,schemaname from v$session;
TO CLOSE A USER'S CONNECTION:
NOTE: USERNAME is case sensitive.
First determine user sid and serial number:
select osuser, username, sid, serial# from v$session where username =
'USERNAME';
Then kill session:
alter system kill session '<sid>,<serial#>';
TO CREATE A NEW USER:
create user USERNAME
identified by PASSWORD
default tablespace DEFAULT_TABLESPACE
temporary tablespace temp;
TO ALLOW ACCESS TO THE DATABASE:
grant connect to USERNAME;
TO DELETE A USER AND ALL OWNED OBJECTS (A SCHEMA):
drop user USERNAME cascade;
TO LIST ALL TABLESPACES:
select tablespace_name from dba_data_files;
TO SHOW FREE SPACE IN A GIVEN TABLESPACE:
select max(bytes)/1024/1024 from dba_free_space where
tablespace_name='TABLESPACE_NAME';
TO CREATE A NEW TABLESPACE:
- Data tablespace names will typically be 'SCHEMA_data##.dbf'
- Index tablespace names will typically be 'SCHEMA_index##.dbf'
create tablespace TABLESPACE_NAME
datafile 'DATA_PATH'
size INITIAL_SIZE --- initial size e.g.200M
extent management local --- this may vary on your system
autoallocate --- this may vary on your system
online;
TO ADD ADDITIONAL CAPACITY TO A TABLESPACE (REQUIRES SYSTEM
PRIVILEGES):
alter tablespace TABLESPACE_NAME
add datafile 'DATA_PATH'
size INITIAL_SIZE; --- e.g. 200M
TO RESTART ORACLE INSTANCE:
- From a command prompt run:
set ORACLE_SID=SID_NAME
svrmgrl
connect internal
startup