Basic Introduction To SQL PLUS: Getting Started
Basic Introduction To SQL PLUS: Getting Started
Basic Introduction To SQL PLUS: Getting Started
The SQL*PLUS (pronounced "sequel plus") program allows you to store and retrieve data in the relational database management system ORACLE. Databases consists of tables which can be manipulated bystructured query language (SQL) commands. A table is made up of columns (vertical) and rows (horizontal). A row is made up of fields which contain a data value at the intersection of a row and a column. Be aware that SQL*PLUS is a program and not a standard query language.
Getting Started
It is a prerequisite that users are registered for ORACLE, an ORACLE account is needed. On Unix platforms you must start the script oraenv to set the ORACLE environment. Enter the command . oraenv and press <Return;>. Don't forget to type a blanc between the dot and oraenv. If you are working with a PC using MS Windows, simply use Netinstall to install the product. You can find the software in the database folder. Enter sqlplus on unix systems or run it on Windows from the start menue. Answer the displayed prompts by entering your ORACLE user-name and password. The SQL*PLUS command prompt SQL > indicates that you are ready to work.
/ ;
If you use substitution variables, like &variable, instead of values or names in your SQL statement, SQL*PLUS will prompt you and substitute the entered value. A substitution variable is a user variable name preceded by an ampersand.
If you run a command file in which a substitution variable like &1 is used, you will be prompted for that value. You can avoid being prompted by passing an argument to the command file.
and returns only one copy of duplicate rows select col1, col2 ... from tabname selects specified columns from table tabname select col1, col2*3 from tabname selects col1,col2 from table tabname and lists col1, col2 multiplied by 3 select 2*3 from dual calculates 2*3 and will display the result
returns all distinct rows selected by either query returns all rows selected by either query, including all duplicates returns all distinct rows selected by both queries returns all distinct rows selected by the first query but not the second
Example select * from table1 union all select * from table2 This will combine all rows, columns of table1 and table2.
Simple Join
select col1,tab1.col2,col3 from tab1,tab2 where tab1.col2=tab2.col2 This is the most common type of join. It returns rows from two tables based on an equality condition, therefore it is also called an equi-join.
Non-Equi Join
select tab1.col1,tab2.col2 from tab1,tab2 where tab1.col1 between lowval and highval Since this join doesn't return rows based on a equality condition, it is called a non-equi join.
Self Join
select alias1.col1,alias2.col1 "Header 2" from tabname alias1,tabname alias2 where alias1.col2=alias2.col3 In this example the table tabname is joined with itself. Using of two different alias names for the same table allows you to refer to it twice. Since the names of the resulting columns in this example are the same, the second column gets a new header.
Outer Join
select col1,col2 from tab1,tab2 where tab1.col1=tab2.col2(+) Suppose you want to retrieve information from two tables where not all rows match but the result should contain all values from one or more columns. A simple join will select only matching rows whereas the outer join extends the result. All matching rows will be selected and when you append the outer join operator (+) to a column name, those rows which do not match will also be selected. In the example the number of rows which are selected is the number of rows in table tab2. If rows match, the outer join works as a simple join, if not, the values from tab2.col2 and a NULL value for the non existing value of tab1.col1 will be selected.
number(p) specifies a fixed point number. number specifies a floating point number. char(size) specifies fixed length (max 255) character data of length size. varchar2(size) specifies variable length (max 2000) character string having a maximum length of size bytes. create table tabname as subquery creates table tabname subquery inserts rows into the table upon its creation. A subquery is a form of the select command which enables you to select columns from an existing table. create view viewname as subquery creates view viewname A view is a logical table based on one or more tables. drop table tabname alter table tabname add (col1 type1,col2 type2,...) alter table tabname modify (col1 type1,col2 type2,...) rename oldname to newname alter user user identified by newpassword; grant privilege on object to user revoke privilege on object from user removes table tabname from the database adds columns to table tabname modifies column definitions renames table oldname enables user to change the password to newpassword grants a privilege to user revokes a privilege from user
Schema
When you select data from a table or you insert data into a table then this object has to be in your own schema. In other words, you must be the owner. If you are not the owner of the object, but the owner granted some privileges to you, you have to specify schema.tabname.
Example
select * from scott.emp