You are on page 1of 8

Helpful tips on SQL-Plus: the Good, Bad and downright UGLY

First thing you should keep in mind is that SQL is NOT case-sensitive. When you log into SQL, you can change your password. SQL> ALTER USER <your_username> IDENTIFIED BY <new_password>; SQL statements can be entered at the SQL> prompt. For example: SQL> select * 2 from students, takes 3 where students.id= takes.sid; ID NAME SID CID EXPG ---------- ------------------------------ ---------- ---------- ---1 Tom White 1 5500103 A 1 Tom White 1 7001003 A 3 Prentice Hall 3 5010103 C 3 Prentice Hall 3 7001003 A 4 Peter McGraw 4 5010103 C 3 Prentice Hall 3 5500103 B The ";" indicates that the statement should be executed. The same can be accomplished by using "/" on a line by itself. SQL> 2 3 4 select * from students s, takes t where s.id=t.sid /

ID NAME SID CID EXPG ---------- ------------------------------ ---------- ---------- ---1 Tom White 1 5500103 A 1 Tom White 1 7001003 A 3 Prentice Hall 3 5010103 C 3 Prentice Hall 3 7001003 A 4 Peter McGraw 4 5010103 C 3 Prentice Hall 3 5500103 B 6 rows selected. If you leave a blank line after the SQL statement, it is stored in a buer and can be executed later. 1

SQL> select * 2 from students s, takes t 3 where s.id=t.sid 4 SQL> If you later say "run", the statement in the buer will be executed, e.g. SQL> 1 2 3* run select * from students s, takes t where s.id=t.sid

ID NAME SID CID EXPG ---------- ------------------------------ ---------- ---------- ---1 Tom White 1 5500103 A 1 Tom White 1 7001003 A 3 Prentice Hall 3 5010103 C 3 Prentice Hall 3 7001003 A 4 Peter McGraw 4 5010103 C 3 Prentice Hall 3 5500103 B 6 rows selected. You can re-execute a previously executed statement by typing in / and hitting enter. SQL> / ID NAME SID CID EXPG ---------- ------------------------------ ---------- ---------- ---1 Tom White 1 5500103 A 1 Tom White 1 7001003 A 3 Prentice Hall 3 5010103 C 3 Prentice Hall 3 7001003 A 4 Peter McGraw 4 5010103 C 3 Prentice Hall 3 5500103 B 6 rows selected. Editing and SQL*Plus. It is easy to make mistakes typing in SQL statements at the command line so you have a few options: 1. Copy and paste: write the statement in your favorite editing environment, then copy and paste into SQL*Plus. For example, suppose you created the following query:

select * from takes where expgrade=A; and then pasted it into SQL*Plus: SQL> select * from takes where expgrade=A;

SID CID EXPG ---------- ---------- ---1 5500103 A 1 7001003 A 3 7001003 A the 2 3 indicate line numbers (after the fact), and the query is then executed. 2. Use an editor within SQL*Plus. You can specify an editor by adding an enviroment variable EDIT OR to the UNIX shell from which you start sqlplus. For example, you can add the line export EDITOR=emacs to your .bashrc le, if your shell of choice is bash, and your editor of choice is emacs. Alternatively, you can specify the editor, for example emacs, saying "dene _editor= emacs" and the use "edit" to edit the contents of the buer. SQL> define _editor= emacs; SQL> edit Wrote file afiedt.buf 1 select * 2 from takes 3* where expgrade=A SQL> 3. Use some grody SQL*Plus buer manipulation commands: They are too numerous to mention here, but the book "Oracle Programming: A Primer" by Sunderram lists most of them. For your homework, the following SQL*Plus statements will be helpful. host: This executes a host operating system command, e.g. in case you forget the name of your .sql les you can say SQL> host ls *.sql 3

spool: Stores the query results in an operating system le, e.g.

SQL> spool hwAns; To disable spooling, say SQL> spool off start: executes commands stored in an operating system le. For example, if you have a le called hw2.sql you would say:

SQL> start hw2 (the .sql extension is not required) Alternatively, you can execute commands stored in an operating system le like so (again, .sql extension is not required).

SQL> @hw2 set: Sets SQL*Plus system variables, e.g. SQL> set autoCommit on; which commits changes to the database immediately after the change is executed, SQL> set echo on; which lists each command in a le when the le is run with the start command. SQL> set pages 9999 lines 120 which sets the line size to be 120 characters, and the page size to be 9999 rows. The latter setting controls the number of lines per page, i.e., between headings. SQL> set pages 3 SQL> select id, name from Students; ID NAME ---------- -----------------------------1 Tom White ID NAME 4

---------- -----------------------------2 Mike Maxwell ID NAME ---------- -----------------------------3 Prentice Hall ID NAME ---------- -----------------------------4 Mike ID NAME ---------- -----------------------------5 Mark OReilly SQL> set pages 20 SQL> select id, name from Students; ID NAME ---------- -----------------------------1 Tom White 2 Mike Maxwell 3 Prentice Hall 4 Mike 5 Mark OReilly If you do not know what relations are in your name space, you can see this in the table_name eld of the tabs relation. SQL> select table_name from tabs; TABLE_NAME -----------------------------COURSES PROFESSORS STUDENTS TAKES TEACHES If you cannot remember the attributes in one of these relations, use "describe": SQL> describe students; Name Null? Type ----------------------------------------5

ID NAME

NOT NULL NUMBER(38) VARCHAR2(30)

If you need help on a command, use "help" (unfortunately, this doesnt seem to work with all commands), e.g. SQL> help describe; DESCRIBE -------Lists the column definitions for a table, view, or synonym, or the specifications for a function or procedure. DESC[RIBE] {[schema.]object[@connect_identifier]} You can create a table with primary key(s) through SQL command. SQL> CREATE TABLE <tablename> ( 2 a <type> PRIMARY KEY, 3 b <type>, 4 c <type> 5 ); One example would be: SQL> CREATE TABLE Players ( 2 playerID INTEGER PRIMARY KEY, 3 salary INTEGER, 4 team VARCHAR2(128) 5 ); Also, you can add constraints to table denition by specifying constraints on columns. Constraints can be named and can be disabled or re-enabled. If you do not assign a name, Oracle will assign one for you, so it is better if you named it. There are four constraints : NOT NULL, CHECK VALUE, UNIQUE, FOREIGN KEY NOT NULL : the column can not have a null value CHECK VALUE : the column must have values that are allowed by the check condition (e.g. CHECK (salary>0)) UNIQUE : the column must have unique values FOREIGN KEY : Values in the column must exists in a PRIMARY KEY column of another table. Lets try to add constraints to the Players table:

SQL> CREATE TABLE Players ( 2 playerID INTEGER PRIMARY KEY, 3 salary INTEGER CONSTRAINT chk_salary CHECK (salary > 0 ), 4 team VARCHAR(128) CONSTRAINT unq_team_name UNIQUE; 5 ); Usually, people use the prex nn for NOT NULL condition, chk for CHECK constraints, unq for UNIQUE constraints, and fk for FOREIGN KEY constraints. To disable or enable constraints: SQL> ALTER TABLE Players DISABLE CONSTRAINT chk_salary; SQL> ALTER TABLE Players ENABLE CONSTRAINT chk_salary; After creating a table, you can inser tuples into the table SQL> INSERT INTO <tablename> VALUES (a,b,c,..); (a,b,c,..) is the list of values in order of the attributes. Make sure all constraints of columns are satised (e.g. no NULL values should be assiged to NOT NULL column). One example would be: SQL> INSERT INTO Players VALUES (1,200000,Braves); You can also update some values in tables under given conditions. SQL> UPDATE table_name 2 SET column1=value, column2=value2,... 3 WHERE some_column=some_value An example would be: SQL> UPDATE Players 2 SET team = Phillies, salary = 300000 3 WHERE playerID = 1; You can alter a table to change table denition. SQL> ALTER TABLE Players ADD (birthYear int); SQL> ALTER TABLE Players MODIFY (team char(46)); Note that in the second example, the length of the team changed. You can delete all rows of a table by truncate command. The result is automatically committed, so after you execute this command, all rows will be PERMANENTLY deleted. SQL> TRUNCATE TABLE <tablename> When you want to delete a table, try SQL> DROP TABLE <tablename> 7

Make sure the table you want to drop is not referenced by any other table. If it is referenced by other tables, you should drop those tables rst. In other words, only the tables that are not referenced by other tables can be dropped. SQL commands that insert, update, or delete tuples in tables need to be committed to save the modication permanently in Oracle. For example, if you want to commit a tuple after inserting it, this will do the trick: SQL> INSERT INTO Players VALUES (1,Braves); SQL> commit; One exception for this rule is that all commands executed immediately BEFORE an DDL are automatically committed. In your Oracle account, there is a table called dual. You can query this table to get information about your account, system date/time, and execute mathematical functions. Here are some examples: SQL> select user from dual; SQL> select sysdate from dual; SQL> select power(4,3) from dual; To leave, simply type SQL> quit;

You might also like