Lab 1
Lab 1
1. INTRODUCTION
Oracle is a relational database management system (DBMS) that stores, manages, and
manipulates data. It consists of the database manager, the Database Administrator
utility and other user utilities. The query language which will be used is called SQL
(an acronym for Structured Query Language), and is implemented as part of a
package called SQL*Plus distributed by ORACLE corporation.
2. USING SQL*PLUS
2.1 Logging into the gateway server
Launch the Terminal. Enter SSH [email protected] for Host Name
and your EID for User Name. Answer Yes to the Host Identification question. Then
enter your password for EID as your login password. You can then see the server
shell command prompt.
SQL*Plus will display the version of the Oracle Server and JServer already built in:
SQL*Plus: Release 9.0.1.0.0 - Production on Wed Jan 26 14:22:52 2011
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit
Production With the Partitioning, OLAP, Data Mining and Real Application
Testing options
1
2.4 Getting Help
You may get information on the commands and conventions of SQL*Plus, SQL, and
PL/SQL using help command:
SQL > help
The following command displays a list of SQL*Plus, SQL and PL/SQL commands:
SQL > help index
Get help on the SQL*Plus clause LIST:
SQL > help list
3. LAB OBJECTIVES
In this lab section, you will learn to:
Define tables
- Create your own tables
- Drop your own tables
Manipulate tables
- Insert tuples into your tables
- Altering your own tables
Query information from tables
- Select all the columns from a table
- Select specific columns from a table
Creating a new table from existing data
Updating data in tables
Deleting tuples rows in tables
DEPT
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SALGRADE
GRADE LOSAL HISAL
1 700 1200
2 1201 1400
2
3 1401 2000
4 2001 3000
5 3001 9999
EMP
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7655 JONES MANAGER 7839 2-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 1-MAY-91 2850 30
7782 CLARK MANAGER 7839 9-JUN-81 2450 10
7788 SCOTT ANALYST 7655 21-MAR-87 3000 20
7839 KING PRESIDENT 12-NOV-81 5000 0 10
7844 TURNER SALESMAN 7698 18-SEP-81 1500 30
7876 ADAMS CLERK 7788 24-APR-87 1100 20
7900 JAMES CLERK 7698 3-DEC-81 950 30
7902 FORD ANALYST 7655 3-DEC-81 3000 20
7934 MILLER CLERK 7782 3-JAN-81 1300 10
NUMBER (n,d)
n = maximum number of digits
d = maximum number of digits right of decimal.
DATE
The relevant data plus the actual time of day
LONG
Up to 65,536 characters (64K) may be stored per field
RAW
Raw binary data
3
creates the table definition of DEPT into your local database.
The SALGRADE and EMP tables are created as follows:
SQL > CREATE TABLE SALGRADE
2 (GRADE NUMBER,
3 LOSAL NUMBER,
4 HISAL NUMBER);
User variables & (or called substitution variable) can be used to help reduce the
tedium of data insertion. For example, to insert rows into the DEPT table, enter the
following command:
SQL > INSERT INTO DEPT
2 VALUES (&DEPTNO, ‘&DNAME’, ‘&LOC’);
and just repeatedly invoke this command by typing / or the RUN command, and
enter new values accordingly. For those entries that are “empty”, e.g. some COMM
fields, use NULL as the input.
To run the script file we prepared for inserting all tuples, enter the following :
SQL > START /public/cs3402/Lab1Data.sql
4
After each insert, update and delete operations that would modify the data
in the database, you are suggested to do a “COMMIT” operation to
ensure the changes take effect immediately:
SQL> COMMIT;
Commit complete.
5
7.1.2 Selecting Specific Columns from a Table
If only some of the columns are to be selected then list only the names of the columns
that you want to see.
Select just the DNAME and DEPTNO columns from the DEPT table.
SQL > SELECT DNAME, DEPTNO FROM DEPT;
The order in which the columns are listed in the SELECT clause controls the left-to-
right sequence in which the columns are displayed. When an asterisk is used, the
left-to-right sequence in which the columns are displayed is the order in which they
were defined when the table was created.
Select all columns from the EMP table.
SQL > SELECT * FROM EMP;
Look at the results of the queries in this section. Notice that the result of any query is
itself a table called the result table – made up of columns and rows.
List all the jobs in the EMP table without eliminating duplicates.
SQL > SELECT JOB FROM EMP;
You can eliminate duplicate rows in the result by specifying DISTINCT in the
SELECT clause.
List all the DISTINCT jobs in the EMP table.
SQL > SELECT DISTINCT JOB FROM EMP;
6
WHERE DEPTNO = 30
WHERE Column-name Comparison-operator Constant-value
A WHERE clause can contain a numeric constant-value (30 in the example above)
or a character constant-value shown as in the example below.
List the names, numbers, and departments of all clerks
SQL > SELECT ENAME, EMPNO, DEPTNO
2 FROM EMP
3 WHERE JOB = ‘CLERK’;
If the constant value that you specify is character data then it has to be enclosed in
single quotes (‘CLERK’). The case also matters: try the previous query, but replace
CLERK by clerk. Numeric data does not need to be enclosed in quotes.
A WHERE clause search condition can use any of the following comparison
operators:
= Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Find the names and department numbers of all departments with a department
number greater than 20.
SQL > SELECT DNAME,DEPTNO
2 FROM DEPT
3 WHERE DEPTNO > 20;
Find the names and department numbers of all departments with a department
number greater than or equal to 20.
SQL > SELECT DNAME, DEPTNO
2 FROM DEPT
3 WHERE DEPTNO >=20;
7
2 FROM EMP
3 WHERE DEPTNO = 20
4 AND SAL > 2000;
Here, the two search conditions are connected by the word AND. The AND means
that both search-conditions must be true for a given row to be retrieved. Any number
of search-conditions may be ANDed to a WHERE clause.
Find all the salesmen in department 30 who have a salary greater then or equal to
$1,500.
SQL > SELECT ENAME, SAL, DEPTNO
2 FROM EMP
3 WHERE JOB = ‘SALESMAN’
4 AND DEPTNO = 30
5 AND SAL >= 1500;
Here the search-conditions are connected by the word OR. OR means that if either
search-condition is true then the row is to be selected.
The WHERE clause in the above query can be written suing parentheses without
changing the meaning of query.
Find all the manager (in any department), and all the clerks in department 10.
SQL > SELECT *
2 FROM EMP
3 WHERE JOB = ‘MANAGER’
8
4 OR (JOB = ‘CLERK’ AND DEPTNO = 10);
Notice how in the last query both managers and clerks have to be in department 10,
not just the clerks. When a WHERE clause contains more than two search conditions
you should use parentheses to “classify the meaning” of the WHERE clause.
8. DATA MANIPULATION
8.1 Creating a New Table From Existing Data
You can create a new table containing data from existing tables.
Create a new table MY_EMP from existing table EMP.
SQL > CREATE TABLE MY_EMP AS
2 SELECT * FROM EMP;