- Create a table with
CREATE TABLEfollowing Oracle’s naming rules. - Choose suitable Oracle datatypes for columns.
- Display a table definition with
DESCRIBE. - Insert rows (all columns or selected columns) and understand NULL.
- Retrieve rows with
SELECTusing projection, expressions, aliases,WHERE,ORDER BY, special operators andDISTINCT. - Edit the SQL buffer with SQL*Plus commands.
Creating a Table
Before creating a table, decide: what data it stores, its name (which should depict its content), its columns, the name, datatype and maximum length of each column, and the integrity rules to be implemented.
create table COURSES ( ccode varchar2(5), name varchar2(30), duration number(3), fee number(5), prerequisite varchar2(100) );
Table created.
Rules for naming tables and columns
- The name must begin with a letter (A–Z or a–z).
- Letters, digits and the special characters _ $ # are allowed.
- Maximum length is 30 characters.
- It must not be an SQL reserved word, and no other object in your account may have the same name.
Oracle Datatypes
| Datatype | Description (as given in the notes) |
|---|---|
| VARCHAR2(len) | Variable-length string of up to len characters; maximum 4000. |
| VARCHAR(len) | Same as VARCHAR2, but use VARCHAR2 because Oracle may change the usage of VARCHAR in future. |
| CHAR(len) | Fixed-length string, padded on the right with spaces; default width 1, maximum 2000. |
| NUMBER / NUMBER(p,s) | Numbers (up to 40 digits plus decimal point and sign). p = maximum significant digits, s = digits to the right of the decimal point. |
| DATE | Dates from 1-1-4712 B.C. to 31-12-4712. |
| LONG | Variable-length character data up to 2 GB; only one LONG column per table; cannot be used in functions, WHERE clause, indexing or subqueries. |
| RAW / LONG RAW | Like VARCHAR2 / LONG but for binary data (sound, images). |
| CLOB, BLOB, NCLOB | Large character and binary objects, up to 4 GB each (see Chapter 21). |
| BFILE | Pointer to an external file; only the file name is stored in the column. |
| ROWID | Unique number used by Oracle to identify each row. |
| NCHAR / NVARCHAR2 | Same as CHAR / VARCHAR2 but support national language characters. |
DESCRIBE and INSERT
DESC[RIBE] objectname is a SQL*Plus command that shows each column’s name, datatype and whether it can hold NULL. It works for tables, views, synonyms, functions and procedures.
insert into courses
values ('ora','Oracle database',25,4500,'Knowledge of Windows');
insert into courses
values ('c','C Programming',25,3000,null); -- NULL for an unknown value
insert into courses (ccode,name)
values ('odba','Oracle Database Administration'); -- other columns get NULL / default
commit; -- make the changes permanent- Character and date values are enclosed in single quotes.
- One INSERT ... VALUES inserts one row; repeat it for more rows (or use a subquery – Chapter 11).
- DATE values are given as DD-MON-YY or DD-MON-YYYY. With a two-digit year the current century is used, so give four digits when needed.
- NULL means a value that is not available or not known – it is neither 0 nor blank.
- Issue
COMMITafter inserting; uncommitted rows may be lost, e.g. on power failure.
SELECT: Projection, Expressions and Aliases
SELECT [DISTINCT | ALL] {* | expr [alias], ...} FROM [schema.]table [WHERE condition] [ORDER BY {expr | position | alias} [ASC | DESC], ...]select name, fee, fee * 0.15 discount from courses;
NAME FEE DISCOUNT -------------------- ----- --------- Oracle database 4500 675 VB.NET 5500 825 C programming 3500 525 ASP.NET 5000 750 Java Language 4500 675 XML Programming 4000 600
- Projection – selecting only some of the columns.
*selects all columns. - Arithmetic operators + − * / may be used in expressions.
- A column alias replaces the expression as the column heading.
- A schema prefix (owner name) is needed only for tables of another account.
ORDER BY
- Sorts the retrieved rows; the rows stored in the table are not changed.
- Default order is ascending; use DESC for descending. Several columns may be given.
- You can sort by column name, by column position in the select list (not in the table) or by alias.
- ORDER BY must be the last clause of SELECT.
- NULLs come last in ascending order and first in descending order.
select name, duration, fee from courses order by duration, fee desc; select name, fee, fee * 0.15 from courses order by 3;
Selection with WHERE and SQL Operators
The WHERE clause implements the selection operator – only rows satisfying the condition are returned.
| Operator | Meaning |
|---|---|
| = != or <> > >= < <= | Relational (comparison) operators |
| AND, OR, NOT | Combine conditions / negate a condition |
| BETWEEN v1 AND v2 | Value in the range, inclusive of both values (works with dates too) |
| IN (list) | Value matches any value in the comma-separated list |
| LIKE pattern | % = zero or more characters, _ = exactly one character |
| IS NULL / IS NOT NULL | Tests for null values |
select name from courses where duration between 20 and 25; select name from courses where duration in (20,30); select * from courses where ccode like '_b%' and prerequisite like '%programming%'; select * from courses where fee is null; -- correct select * from courses where fee = null; -- never true: use IS NULL
'ora' does not match 'ORA'. Oracle treats two NULLs as different values, so = NULL never matches.SELECT DISTINCT fee FROM courses; returns each fee value only once.
Editing the SQL Buffer
The last SQL command entered is kept in the SQL buffer. SQL*Plus commands (such as DESCRIBE) are not stored in the buffer.
| Command | Purpose |
|---|---|
| A[PPEND] text | Adds text to the end of the current line |
| DEL | Deletes the current line |
| I[NPUT] [text] | Adds line(s) after the current line |
| L[IST] | Displays the buffer |
| R[UN] / / | Runs the buffer (“/” does not display the command) |
| SAVE / GET filename | Saves the buffer to a file / loads a file into the buffer |
| START filename | Executes the commands in a file (start file / command file) |
| EDIT [filename] | Opens the host editor |
| HOST command | Runs an operating-system command |
| EXIT | Quits SQL*Plus |
- Column list = projection; WHERE = selection; ORDER BY = sorting of the result only.
- Use IS NULL, never = NULL; BETWEEN is inclusive; LIKE uses % and _.
- COMMIT makes inserted rows permanent.
Chapter 3 Notes (PDF)
Question / Answer (15 questions)
Moderate-level questions based on the Chapter 3 notes (PDF). Click a question to see the answer.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The chapter notes PDF is the course study material and is credited to its author below. The explanations, questions and MCQs on this page are original study aids written for this course from the topics covered in that PDF. The other works are cited for verification and further reading. Oracle, SQL*Plus and PL/SQL are trademarks of Oracle Corporation; other names belong to their respective owners.
- Srikanth, P. (n.d.). Oracle For Beginners (chapter notes – the PDF shown on each chapter page). Srikanth Technologies. http://www.srikanthtechnologies.com/
- Oracle Corporation. (n.d.). Oracle Database SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/
- Oracle Corporation. (n.d.). SQL*Plus User’s Guide and Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqpug/
- Oracle Corporation. (n.d.). Oracle Database Concepts, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/
- Elmasri, R., & Navathe, S. B. (2016). Fundamentals of Database Systems (7th ed.). Pearson.