SQL Queries Funda
SQL Queries Funda
SQL Queries Funda
A collection of facts from which conclusions may be drawn; In computer science, data is any information
in a form suitable for use with a computer.
Database
A database is a collection of data that is organized so that its contents can easily be accessed, managed,
and updated. The most prevalent type of database
RDBMS is a Relational Data Base Management System Relational DBMS. This adds the additional
condition that the system supports a tabular structure for the ...
A DBMS also has to provide some uniform methods independent of a specific application for
accessing the information that is stored.
Cardinality
Codd's 12 rules
Codd's 12 rules are a set of 12 rules proposed by Edgar F. Codd, a pioneer of the
relational model for databases, designed to define what is required from a database
management system in order for it to be considered relational, i.e., an RDBMS. [1][2]
Codd produced these rules as part of a personal campaign to prevent his vision of
the relational database being diluted, as database vendors scrambled in the early
1980s to repackage existing products with a relational veneer. Rule 12 was
particularly designed to counter such a positioning. In fact, the rules are so strict that
all popular so-called "relational" DBMSs fail on many of the criteria
The rules
Rule 000: The system must qualify as relational, as a database, and as a
management system.
For a system to qualify as a relational database management system
(RDBMS), that system must use its relational facilities (exclusively) to
manage the database.
Rule 1: The information rule:
All information in the database is to be represented in one and only one way,
namely by values in column positions within rows of tables.
Rule 2: The guaranteed access rule:
All data must be accessible with no ambiguity. This rule is essentially a
restatement of the fundamental requirement for primary keys. It says that
every individual scalar value in the database must be logically addressable by
specifying the name of the containing table, the name of the containing
column and the primary key value of the containing row.
Rule 3: Systematic treatment of null values:
The DBMS must allow each field to remain null (or empty). Specifically, it must
support a representation of "missing information and inapplicable information"
that is systematic, distinct from all regular values (for example, "distinct from
zero or any other number," in the case of numeric values), and independent of
data type. It is also implied that such representations must be manipulated by
the DBMS in a systematic way.
Rule 4: Active online catalog based on the relational model:
The system must support an online, inline, relational catalog that is accessible
to authorized users by means of their regular query language. That is, users
must be able to access the database's structure (catalog) using the same
query language that they use to access the database's data.
Rule 5: The comprehensive data sublanguage rule:
The system must support at least one relational language that
(a) Has a linear syntax
(b) Can be used both interactively and within application programs,
(c) Supports data definition operations (including view definitions), data
manipulation operations (update as well as retrieval), security and integrity
constraints, and transaction management operations (begin, commit, and
rollback).
Rule 6: The view updating rule:
All views that are theoretically updatable must be updatable by the system.
Rule 7: High-level insert, update, and delete:
The system must support set-at-a-time insert, update, and delete operators.
This means that data can be retrieved from a relational database in sets
constructed of data from multiple rows and/or multiple tables. This rule states
that insert, update, and delete operations should be supported for any
retrievable set rather than just for a single row in a single table.
Rule 8: Physical data independence:
Changes to the physical level (how the data is stored, whether in arrays or
linked lists etc.) must not require a change to an application based on the
structure.
Rule 9: Logical data independence:
Changes to the logical level (tables, columns, rows, and so on) must not
require a change to an application based on the structure. Logical data
independence is more difficult to achieve than physical data independence.
Rule 10: Integrity independence:
Integrity constraints must be specified separately from application programs
and stored in the catalog. It must be possible to change such constraints as
and when appropriate without unnecessarily affecting existing applications.
Rule 11: Distribution independence:
The distribution of portions of the database to various locations should be
invisible to users of the database. Existing applications should continue to
operate successfully :
(a) when a distributed version of the DBMS is first introduced; and
(b) when existing distributed data are redistributed around the system.
Rule 12: The nonsubversion rule:
If the system provides a low-level (record-at-a-time) interface, then that
interface cannot be used to subvert the system, for example, bypassing a
relational security or integrity constraint.
Some of Oracle's tools to access the database and create programs are:
SQL*Plus has a command line interface. With it, you can access the database and write stored
procedures, you can run SQL commands to retrieve data and you can run scripts of either SQL, PL/SQL
or built-in SQL*Plus commands, or a mixture of those three things.
Oracle Developer is a 4GL GUI application Builder. With Developer, you can create forms, reports, and
graphics. Oracle*Forms and Oracle*Reports are two components of Oracle Developer. Earlier versions
created client-server applications, but the more recent versions create web applications that run under
the Oracle Application Server (OAS). OAS is a web-based application server sold by Oracle. OAS is
licensed separately and is very expensive (as are its closed source competitors). The current version is
OAS 10g.
HTML DB is a fairly new application builder geared toward web development (added to the DB with
release 9iR2). HTML DB does not need an application server. This tool runs from the database and can
be presented to the web using the Apache web server that comes with the database. Since this is not
an additional license, it provides a cheaper way to develop applications. Developer is a feature-rich
thick client with all of the normal GUI widgets. HTML DB is HTML-based and is very thin and limited to
the HTML provided widgets.
Oracle Enterprise Manager (OEM) is the Enterprise GUI tool to manage the database. From this tool,
you can perform any action in the database that is required. This tool is primarily used for
administration but can also be provided to developers for application tuning and monitoring. In Oracle
10g, OEM also provides Grid control.
It is usually pronounced as "es-que-el" or "sequel". I pronounce it both ways and my ears don't even
notice a difference anymore.
SQL came from Dr. EF Codd in the late 60's and was embraced by scientists at IBM including RF Boyce
(ever heard of Boyce-Codd Normal Form (BCNF)?).
Though IBM (as it has done with most computer technology) was a heavy lifter making SQL a workable
language, it was not the first to really exploit it (as with most of its computer technology).
No, it was good old Oracle that released the first commercial product based on SQL. And, since Oracle's
earliest customer was the U.S. Government, the popularity of SQL and Oracle was secured.
Standard SQL is characterized by its ease of use and great power in basic operations.
Because it does not really excel at advanced analytics, each vendor invariably creates its own
extensions to ANSI SQL. Oracle is no exception. Where, in some technologies, being 100% ANSI or ISO
compliant can be a good thing, it's usually not in the world of RDBMS's.
This is largely because of the architectural differences of the various RDBMS platforms.
Why use SQL?
It is the most powerful and flexible tool available for dealing with databases.
Because they are (lazy?)...(stupid?)... it's hard to find a nice word, but I'll say they are just ignorant.
Many developers think SQL is universal or at least should be. They understand perfectly that you can't
take VB code and get a Java compiler to compile it, but they don't understand that 2 databases can
have even slight variations. As a result, they try to keep their DATABASE application code DATABASE
agnostic and then say a lot of silly things like "Oracle sucks."
We'll discuss re-educating developers (they want to succeed as much as you do) later. In the next few
posts, we'll go over how to use SQL and how Oracle handles it.
There are 3 things Oracle needs to know from your statment in order to run:
What
Where (don't confuse with WHERE-clause)
When (because the WHERE-clause goes here)
like
WHAT
What do you want your statement to do? The basic options are
SELECT ____ -- return some data [SELECT col1 FROM tab1;] If you want more than one column, just list
them and seperate them with a comma.
DELETE --remove some data [DELETE FROM tab1;] *this would delete everything
(from) WHERE
Where to do WHAT from above. If there is more than one place (table) involved, just list them and
seperate them with a comma.
WHEN
This is an optional piece that contains the conditional logic you want to use. It is the WHERE-Clause. In
most cases, you are making a big mistake by not using this optional piece. We will see how important
the WHERE-Clause is soon, especially when we discuss indexes.
In this statement
the where-clause says ONLY delete data older than two years.
Course Outline.
Writing Basic SQL Select Statements Subqueries Creating Views
[ ] [ ] [ ]
List the capabilities of SQL SELECT Describe the types of problems that Describe a view
statements subqueries can solve
[ ]
[ ] [ ] Create, alter the definition, and drop a
Execute a basic SELECT statement Define subqueries view
[ ] [ ] [ ]
Differentiate between SQL statements and List the types of subqueries Retrieve data through a view
iSQL*Plus commands
[ ] [ ]
Write single-row and multiple-row Insert, update and delete data through a
subqueries view
[ ]
Limit the rows retrieved by a query
[ ] [ ]
Describe various types of functions Describe each DML statement
available in SQL
[ ]
[ ] Insert rows into a table
Use character, number, and date functions
in SELECT statements
[ ]
Update rows in a table
[ ]
Use conversion functions
[ ]
Delete rows from a table
[ ]
Merge rows in a table
[ ]
Control transactions
[ ]
Write SELECT statements to access data
from more than one table using equality
and nonequality joins
[ ]
View data that generally does not meet a
join condition by using outer joins
[ ]
Join a table to itself using a self-join
Aggregating Data using Group Creating and Managing Tables
Functions
[ ]
[ ] Describe the main database objects
Identify the available group functions
[ ]
[ ] Create tables
Use group functions
[ ]
[ ] Describe the datatypes that can be used
Group data using the GROUP BY clause when specifying column definition
[ ] [ ]
Include or exclude grouped rows by using Alter table definitions
the HAVING clause
[ ]
Drop, rename and truncate tables
Including Constraints
[ ]
Describe constraints
[ ]
Create and maintain constraints
SELECT
Purpose
Use a SELECT statement or subquery to retrieve data from one or more tables, object tables,
views, object views, or materialized views.
If part or all of the result of a SELECT statement is equivalent to an existing materialized view,
then Oracle Database may use the materialized view in place of one or more tables specified in
the SELECT statement. This substitution is called query rewrite. It takes place only if cost
optimization is enabled and the QUERY_REWRITE_ENABLED parameter is set to TRUE. To determine
whether query write has occurred, use the EXPLAIN PLAN statement.
See Also:
• EXPLAIN PLAN
Additional Topics
• Prerequisites
• Syntax
• Semantics
• Examples
Prerequisites
For you to select data from a table or materialized view, the table or materialized view must be in
your own schema or you must have the SELECT privilege on the table or materialized view.
Syntax
select::=
(for_update_clause ::=)
subquery::=
Description of the illustration subquery.gif
subquery_factoring_clause ::=
select_list::=
Description of the illustration select_list.gif
table_reference::=
flashback_query_clause ::=
query_table_expression::=
Description of the illustration query_table_expression.gif
sample_clause ::=
subquery_restriction_clause::=
table_collection_expression ::=
Description of the illustration table_collection_expression.gif
join_clause ::=
inner_cross_join_clause::=
(table_reference::=, query_partition_clause::=)
outer_join_clause::=
(table_reference::=, query_partition_clause::=)
query_partition_clause::=
outer_join_type::=
where_clause::=
hierarchical_query_clause ::=
group_by_clause ::=
rollup_cube_clause::=
(grouping_expression_list::=)
grouping_sets_clause::=
(rollup_cube_clause::=, grouping_expression_list::=)
grouping_expression_list::=
expression_list::=
model_clause ::=
Description of the illustration model_clause.gif
cell_reference_options::=
return_rows_clause::=
reference_model::=
(model_column_clauses::=, cell_reference_options::=)
main_model::=
model_column_clauses::=
Description of the illustration model_column_clauses.gif
(query_partition_clause::=, model_column::=)
model_column::=
model_rules_clause::=
(single_column_for_loop::=, multi_column_for_loop::=)
single_column_for_loop::=
multi_column_for_loop::=
order_by_clause ::=
Description of the illustration order_by_clause.gif
for_update_clause ::=
Semantics
subquery_factoring_clause
The WITH query_name clause lets you assign a name to a subquery block. You can then
reference the subquery block multiple places in the query by specifying the query name. Oracle
Database optimizes the query by treating the query name as either an inline view or as a
temporary table.
You can specify this clause in any top-level SELECT statement and in most types of subqueries.
The query name is visible to the main query and to all subsequent subqueries except the
subquery that defines the query name itself.
See Also:
ALL
Specify ALL if you want the database to return all rows selected, including all copies of
duplicates. The default is ALL.
* (asterisk)
Specify the asterisk to select all columns from all tables, views, or materialized views
listed in the FROM clause.
If you are selecting from a table rather than from a view or a materialized view, then
columns that have been marked as UNUSED by the ALTER TABLE SET UNUSED
statement are not selected.
http://youngcow.net/doc/oracle10g/server.102/b14200/statements_10002.htm
order_by_clause
Use the order_by_clause to specify how data is ordered within a partition. For all analytic
functions except PERCENTILE_CONT and PERCENTILE_DISC (which take only a single key), you
can order the values in a partition on multiple keys, each defined by a value_expr and each
qualified by an ordering sequence.
Within each function, you can specify multiple ordering expressions. Doing so is especially
useful when using functions that rank values, because the second expression can resolve ties
between identical values for the first expression.
Whenever the order_by_clause results in identical values for multiple rows, the function
returns the same result for each of those rows. Please refer to the analytic example for SUM for
an illustration of this behavior.
When used in an analytic function, the order_by_clause must take an expression (expr). The
SIBLINGS keyword is not valid (it is relevant only in hierarchical queries). Position (position)
and column aliases (c_alias) are also invalid. Otherwise this order_by_clause is the same as
that used to order the overall query or subquery.
ASC | DESC
Specify the ordering sequence (ascending or descending). ASC is the default.
Specify whether returned rows containing nulls should appear first or last in the ordering
sequence.
NULLS LAST is the default for ascending order, and NULLS FIRST is the default for descending
order.
Analytic functions always operate on rows in the order specified in the order_by_clause of the
function. However, the order_by_clause of the function does not guarantee the order of the
result. Use the order_by_clause of the query to guarantee the final result ordering