It is recommended that you create SQL scripts that will create your tables for you in Oracle. Here's how to do it:
1. Create a file with the extension .sql to denote that it contains SQL statements to be executed. Example: tables.sql
2. In this file, put the SQL statements needed to create this table. You may also choose to populate this table.
Example:
DROP TABLE XCUSTOMERS;
CREATE TABLE XCUSTOMERS (
CUSTNO NUMBER(9) NOT
NULL PRIMARY KEY,
CUSTNAME CHAR(20),
CUSTCITY CHAR(20));
INSERT INTO XCUSTOMERS VALUES(1000,'Guy, Buddy','Chicago');
INSERT INTO XCUSTOMERS VALUES(1001,'Rush, Otis','Chicago');
INSERT INTO XCUSTOMERS VALUES(1002,'Clapton, Eric','London');
INSERT INTO XCUSTOMERS VALUES(1003,'Green, Peter','London');
INSERT INTO XCUSTOMERS VALUES(1004,'Bloomfield, Mike','Chicago');
INSERT INTO XCUSTOMERS VALUES(1005,'King, Freddie','Dallas');
INSERT INTO XCUSTOMERS VALUES(1006,'King, B.B.','Memphis');
3. After saving the file, run SQLPlus by typing the following at the prompt:
sqlplus /
4. At the SQL prompt, run the script by typing the name of the script preceeded by the `@` sign:
SQL> @smp.sql
5. You should see your table being created/populated. To check, do a select on the table:
SQL> select * from xcustomers;
CUSTNO CUSTNAME
CUSTCITY
---------- -------------------- --------------------
1000 Guy, Buddy
Chicago
1001 Rush, Otis
Chicago
1002 Clapton, Eric
London
1003 Green, Peter
London
1004 Bloomfield, Mike
Chicago
1005 King, Freddie
Dallas
1006 King, B.B.
Memphis
7 rows selected.
SQL>
6. There have been questions on how to reference foreign keys. The following examples illustrate how.
CREATE TABLE emp
(empno NUMBER
PRIMARY KEY,
ename
VARCHAR2(10) NOT NULL,
job
VARCHAR2(9),
mgr
NUMBER REFERENCES emp(empno),
hiredate
DATE,
deptno
NUMBER(2) NOT NULL,
FOREIGN KEY
deptno REFERENCES dept(deptno));
mgr is a foriegn key to the same table
deptno is a foriegn key to the dept table
You can reference foreign keys in two ways: 1) after defining the attribute
(as in the case of manager's employee number), or 2) after all attributes
have been defined (as in the department number).