Oracle Workshop - Tutorial: Database Objects
Oracle Workshop - Tutorial: Database Objects
Database Objects
Tables
The create table is used to hold data. This command creates a new table. It is made of
columns that have an associated data type.
Views
When creating a table, using the qualifier "not null" with a column forces the database to
require that the specified column, for any row of data being added, contains data. Null
data is not the same as numeric value "0".
These are fields defined as number. These fields contain only numeric data. All
common arithmetic operations can be performed using numeric data, using infix notation.
There also exists a set of functions available for use on numeric data.
Function Result
ceil(n) Smallest integer greater than or equal to N
abs(n) The absolute value of n
floor(n) Largest integer less than or equal to N
M modulo N – returns the integer remainder of
mod(m,n) or m%n
m divided by n
power(m,n) M to the power of "n"
round(m,n) "m" rounded to "n" decimel places
sign(n) 0 if "n"=0, -1 if "n"<0, +1 if "n">0
sqrt(n) Square root of "n"
+ Addition
- Subtraction
* Multiplication
/ Division
The operators and functions can be used on numeric column data in queries.
The operators and functions can be used on literal data, but requires a table to be named
for correct syntax. The system provides a table named "dual" for this purpose.
These are fields defined as char, varchar, and varchar2. These fields contain values
made from alphanumeric and special characters. There exists a set of functions for use on
character data.
Function Result
Change first character of each char-
initcap(char)
string to upper case
lower(char) Change entire string to lower case
Replace every occurrence of "str1" in
replace(char,str1,str2)
"char" with "str2"
soundex(char) Phonetic representation of char
"n" length substring of "char"
substr(char,m,n)
beginning at position "m"
length(char) Length of character string
The concatenation operator '||' allows two character fields to be joined together.
These are fields defined as date. This type contains both the date and time. There is a set
of functions that can be used on date data.
Function Result
sysdate Current date and time
last_day(d) Last day of the month of date "d"
add_months(d,n) Adds n months to date "d"
months_between(f,s) Difference in months between date "f"
and date "s"
Date of day of the week "dow" after date
next_day(d,dow)
"d"
SQL
Statements
SQL statements fall in two categories, DDL (Data Definition Language) and DML (Data
Manipulation Language). DDL statements allow objects and privileges to be modified.
DML statements are used for querying and updating the database.
The most frequently used SQL DDL commands are alter, drop, create, and grant.
The most frequently used SQL DML commands are insert, describe, update, delete,
and select.
The describe command provides brief summary and description information on a table
and its columns.
describe account;
The insert command is used to add data to a table (i.e. new rows to tables or views).
The select command is used to retrieve data from the database. There are four parts to
the command:
To run a query using the select statement, the words select and from are required. The
statements where and ordered by are optional when executing a query.
ALL and DISTINCT are keywords used to select either “ALL” (default) or the
“DISTINCT” or unique records in your query results. If you wish to retrieve only
distinct records you can use the DISTINCT keyword for the columns you specified after
the “SELECT” statement.
ASC (ascending) and DESC (descending) are key words used to either order the query
results in ascending or descending order.
NOTE: Using the asterisk (*) can be used to display all the column names in the tables
selected.
View all columns where login_name in the list 'larry', 'curly', 'moe'
The update command is used to update date stored in a table. The command has three
parts:
The delete command is used to remove one or more rows of data from a table. The
command has two parts:
The alter command is used to add a column to and existing table, or to modify the data
type of an existing column.