Anda di halaman 1dari 6

DBMS with Oracle 11g

In order to interact with Oracle you should use the SQL*Plus Application. This application is available on both
the windows and UNIX platforms.

 On Windows go to the Start Menu, then select


 Start >Programs > Oracle - DBBOOK_HOME > Application Development > SQL * Plus. Use your
installation name in place of DBBOOK_HOME.
 On UNIX you can type sqlplus from a Unix Shell. Your instructor should have provided information on
how to configure your account and login to Unix.

You will be prompted for your username, connect string, and password. If you are using Oracle on windows
with the CD accompanying the text, then you may leave the connect string blank. On Unix the connect string
will follow your username and the @ symbol in the format username@connectstring.

Note: If you have installed Oracle on Windows, you may use it to connect to your Unix based server. In order to do this
copy the entries from the tnsnames.ora in the $ORACLE_HOME/network/admin directory on the Unix system to the
tnsnames.ora file on the Windows machine. Then, use the connect string as you would in Unix. There is also a version of
SQL*Plus on windows that works in command line mode, which is identical to the Unix version.

TABLES

Creating Tables

Oracle data is stored in tables which represent your fundamental schema design. You create them with the
create table command:

create table tablename (columnname type, columnname type ...);

where tablename is the name of your table, columnname is the name of your column and type is the type as specified
in the table below. There are additional types available; however these are the most common. Give some thought as to
the order of columns as it can affect the default order they are displayed and data is entered.

Type Description
Variable length string up to n with maximum 2000 characters. Specified as
varchar2(n) 'characters' where characters
are the characters in the string.
Holds a date. By default they are specified as day-month-year as in '12-DEC-
date
1990'
number integer or real value up to 40 decimal digits
number(n) up to n digits
Up to n digits with d after the decimal point. The user can help provide
number(n,d)
formatting information to applications

Observing Table Information

The easiest way to get information about a table is with the DESCRIBE command:

DESCRIBE tablename

Created by; Sajith Gunasekara


(DCSD 10.1 NIBM_ku)
Altering Tables

To alter a table use the alter table command:

alter table tablename add (columnname datatype, ...);


alter table tablename modify (column newdatatype);
alter table drop column columnname;

Deleting Tables

To delete a table use the following:

drop table tablename;

To Drop all your tables you can use the following in a script:

>> SET NEWPAGE 0


>> SET SPACE 0
>> SET LINESIZE 80
>> SET PAGESIZE 0
>> SET ECHO OFF
>> SET FEEDBACK OFF
>> SET HEADING OFF
>> SET MARKUP HTML OFF
>> SET ESCAPE \
>> SPOOL DELETEME.SQL
>> select 'drop table ', table_name, 'cascade constraints \;' from user_tables;
>> SPOOL OFF
>> @DELETEME

Sample Session

SQL> create table sign_sales(Color varchar2(30),date_sold date,


2 price_each number);

Table created.
SQL> alter table students add( sign_shape number);

Table altered.

SQL> alter table students modify (sign_shape varchar2(10));

Table altered.

SQL> alter table students drop column sign_shape ;

Table altered.
Created by; Sajith Gunasekara
(DCSD 10.1 NIBM_ku)
SQL> DESCRIBE sign_shape

Name Null? Type


----------------------------------------- -------- ----------------------------
COLOR VARCHAR2(30)
DATE_SOLD DATE
PRICE_EACH NUMBER
SQL> drop table sign_sale;

Table dropped.

DATA

Inserting Data

Working with data is fundamental to database systems. It is possible to load data with SQL. It can also be
done using the Oracle bulk loader. To load values in SQL*Plus use the following syntax:

Insert into tablename values (somevalue, somevalue, ...);

where somevalue is a value to insert and tablename is the name of the table. Values are inserted in the order of the
columns of the table. The first value is inserted into the first column, etc.

Querying Data

Querying data in SQL is one of a database systems major tasks, and cannot be fully described here. In its
most basic form a query consists of the SELECT statement:

select columnname, columnname... from tablename;

Where columnname is the name of a column or * for all columns and tablename is the name of the table.

Altering Data

You can alter data with the update expression. The optional where clause limits the rows effected. The set
clause describes the change as follows:

update tablename set columnname=expression where whereclause;

Deleting Data

You can delete data with the delete expression. The optional where clause limits the rows effected.

delete from tablename where whereclause;

Sample Session

SQL> insert into signs values(19.95,'White','Rectangle','Park Somewhere Else');

1 row created.

SQL>
Created by; Sajith Gunasekara
(DCSD 10.1 NIBM_ku)
SQL> select * from signs;

PRICE_EACH COLOR SHAPE DESCRIPTION


---------- --------- ------------ -------------------
19.95 White Rectangle Park Somewhere Else

SQL>
SQL> update signs set price_each = 4.00 where price_each < 20;

1 row updated.

SQL>
SQL> delete from signs ;

1 row deleted.

SQL>

CONSTRAINTS

Adding Constraints

In order to finish realizing a schema in Oracle, it is necessary to add the required constraints. Oracle
supports several types of constraints; to enforce complex rules may require the use of active database
features such as triggers. In this section the examples are limited to key constraints.

Constraints can be added to new tables or after table creation. To add primary key constraints to a new
table specify the primary key after the columns similar to the following:

create table tablename ( columnname type, columnname type ..., primary


key(keycolumn,keycolumn,...);

where keycolumn is the name of a column that is part of the key.

Foreign keys should refer to unique tuples. To create a new table with foreign keys in addition use:

create table tablename ( columnname type, columnname type ..., primary


key(keycolumn,keycolumn,...),
foreign key(foreignkeycolumn,foreignkeycolumn,...) references foreigntable,
foreign key(foreignkeycolumn,foreignkeycolumn,...) references foreigntable,...);

where foreignkeycolumn is the column of the foreign key in table foreigntable. It must match a column in the current
table. You can also add constraints with the alter table command where tablename is the name of the table
and tableconstraint is the constraint definition:

Alter table tablename add tableconstraint;

Observing Constraints

Often you may wish to see what constraints exist on a table. The view USER_CONSTRAINTS is maintained
by Oracle and has information about constraints. Some interesting columns include, the

Created by; Sajith Gunasekara


(DCSD 10.1 NIBM_ku)
CONSTRAINT_NAME, CONSTRAINT_TYPE where P refers to primary key, and R is a foreign key
constraints, the TABLE_NAME on which it is defined, and the R_CONSTRAINT_NAME for foreign key
constraints. Type DESCRIBE USER_CONSTRAINTS for more informaion.

Once you know the CONSTRAINT_NAME, you can use the USER_CONS_COLUMNS view to find the
COLUMN_NAME and POSITION for that CONSTRAINT_NAME. You can query these views as you would any other
table:

select column_name, position, constraint_name from User_cons_columns;

Observing Constraints

Often you may wish to see what constraints exist on a table. The view USER_CONSTRAINTS is maintained
by Oracle and has information about constraints. Some interesting columns include, the
CONSTRAINT_NAME, CONSTRAINT_TYPE where P refers to primary key, and R is a foreign key
constraints, the TABLE_NAME on which it is defined, and the R_CONSTRAINT_NAME for foreign key
constraints. Type DESCRIBE USER_CONSTRAINTS for more informaion.

Once you know the CONSTRAINT_NAME, you can use the USER_CONS_COLUMNS view to find the
COLUMN_NAME and POSITION for that CONSTRAINT_NAME. You can query these views as you would any other
table:

select column_name, position, constraint_name from User_cons_columns;

Using Constraints

At times you may need to create constraints that are interdependent, such as two tables that refer to each
other’s primary key in a foreign key constraint. One way to deal with this problem is to defer constraints.
They constraint must be made deferrable and the constraints mode set to deferred. You can substitute the
names of specific constraints or use the all keyword. They will be checked at the end of the transaction
instead of after each insert. To enable them make the mode immediate:

create table tablename ( columnname type, columnname type ...,


foreign key(foreignkeycolumn,foreignkeycolumn,...) references foreigntable deferrable);
set constraints all deferred;
set constraints all immediate;

Deleting Constraints

Constraints can be deleted with the drop constraint command. where tablename is the table name
and someconstraint is the constraint name:

alter table tablename drop constraint someconstraint;

Sample Session

SQL> create table bids( bid_id varchar2(10), bidder_id varchar2(10), item_id


varchar2(10),
2 bid_amount number, primary key(bid_id), foreign key ( item_id ) references
3 auction_items, foreign key (bidder_id) references members(member_id) deferrable);

Table created.

Created by; Sajith Gunasekara


(DCSD 10.1 NIBM_ku)
SQL>
SQL> select constraint_name, constraint_type from user_constraints where
2 table_name='BIDS';

CONSTRAINT_NAME C
------------------------------ -
SYS_C001400 P
SYS_C001401 R
SYS_C001401 R

SQL>
SQL> alter table students drop constraint SYS_C001400;

Table altered.

SQL> alter table students add primary key( bid_id );

Table altered.

SQL> set constraints all deferred;

Constraint set.

Using SQL*Plus
SQL*Plus provides an interactive command line interface to Oracle. In it you type
various commands to manipulate your data. It has many built in features.

Ending Your Session


To end your session type:
quit
In the windows version you can select Exit from the file menu.

Changing Your Password

Type passw to change your password. Try to avoid certain


characters like @ and / in your password, as these will
confuse oracle.
passw

Created by; Sajith Gunasekara


(DCSD 10.1 NIBM_ku)