Professional Documents
Culture Documents
In order to interact with Oracle you should use the SQL*Plus Application. This application is available on both
the windows and UNIX platforms.
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:
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
The easiest way to get information about a table is with the DESCRIBE command:
DESCRIBE tablename
Deleting Tables
To Drop all your tables you can use the following in a script:
Sample Session
Table created.
SQL> alter table students add( sign_shape number);
Table altered.
Table altered.
Table altered.
Created by; Sajith Gunasekara
(DCSD 10.1 NIBM_ku)
SQL> DESCRIBE sign_shape
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:
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:
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:
Deleting Data
You can delete data with the delete expression. The optional where clause limits the rows effected.
Sample Session
1 row created.
SQL>
Created by; Sajith Gunasekara
(DCSD 10.1 NIBM_ku)
SQL> select * from signs;
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:
Foreign keys should refer to unique tuples. To create a new table with foreign keys in addition use:
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:
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
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:
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:
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:
Deleting Constraints
Constraints can be deleted with the drop constraint command. where tablename is the table name
and someconstraint is the constraint name:
Sample Session
Table created.
CONSTRAINT_NAME C
------------------------------ -
SYS_C001400 P
SYS_C001401 R
SYS_C001401 R
SQL>
SQL> alter table students drop constraint SYS_C001400;
Table altered.
Table altered.
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.