You are on page 1of 21

L Q S

Oracle SQL Scripting

QL S

An Overview of SQL

SQL stands for Structured Query Language. It is the most commonly used relational database language today. SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic.

L SQ

SQL is used for:

Data Manipulation Data Definition Data Administration All are expressed as an SQL statement or command.

SQL

SQL Requirements

SQL Must be embedded in a programming language, or used with a 4GL like VB SQL is a free form language so there is no limit to the the number of words per line or fixed line break.
Not all versions are case sensitive!

Syntax statements, words or phrases are always in lower case; keywords are in uppercase.

SQL is a Relational Database


A Fully Relational Database Management System must:

Represent all info in database as tables Keep logical representation of data independent from its physical storage characteristics Use one high-level language for structuring, querying, and changing info in the database Support the main relational operations Support alternate ways of looking at data in tables Provide a method for differentiating between unknown values and nulls (zero or blank) Support Mechanisms for integrity, authorization, transactions, and recovery

SQL

Design

SQL represents all information in the form of tables Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use SQL is used for data manipulation, definition and administration

TABLES

TABLE
A single store of related information. A table consists of records, and each record is made up of a number of fields.

SQ L

Table Design
Columns describe one characteristic of the entity Rows describe the Occurrence of an Entity

Name Jane Doe John Smith Mary Poe

Address 123 Main Street 456 Second Street 789 Third Ave

CREATE TABLE
CREATE TABLE table_name (column_name data_type, column_name, data_type, ...);;

DATA-Types
* Char(n) * varchar2(n) * integer * date * tinyint(1) * etc.

Data Input

Putting data into a table is INSERT accomplished using the keyword


Variable pub_name New Age Books Pragmatics Binnet & Hardley New Age Books Algodata Hardley Binnet & Infosys Algodata Infosys address 1 1st Street 4 4th Ln 2 2ntd Street 1 1s Street 3 3rd Street 2 2nd Street 3 3rd Street state MA IL DC MA CA DC CA

INSERT INTO publishers VALUES (0010, pragmatics, 4 4th Ln, chicago, il) Keyword

pub_id 0736 0010 0987 0736 1120 0987 1120

Table is updated with new information

Using SQL

To insert data in the current table, use the keyword INSERT INTO
INSERT INTO authors values(000000001, John Smith)

Then issue the statement


SELECT * FROM authors
auth_id 000000001 auth_name John Smith

Data Retrieval (Queries)

Queries search the database, fetch info, and display it. This is done using the keyword SELECT
pub_name New Age Books Binnet & Hardley Algodata Infosys address 1 1st Street 2 2nd Street 3 3rd Street state MA DC CA

SELECT * FROM publishers


pub_id 0736 0987 1120

The * Operator asks for every column in the table.

Data Retrieval (Queries)

SELECT * from publishers where state = CA


pub_id 0736 0987 1120 pub_name New Age Books Binnet & Hardley Algodata Infosys

Queries can be more specific with a few more lines

address 1 1st Street 2 2nd Street 3 3rd Street

state MA DC CA

Only publishers in CA are displayed

Using SQL
If you only want to display the authors name and city from the following table:
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI 123456789 Jane Doe 000000001 John Smith

SELECT auth_name, auth_city FROM publishers


auth_name Jane Doe John Smith auth_city Dearborn Taylor

Using SQL
To delete data from a table, use the DELETE statement: DELETE from authors WHERE auth_name=John Smith
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI 123456789 Jane Doe 000000001 John Smith

Using SQL
To Update information in a database use the UPDATE keyword UPDATE authors SET auth_name=hello
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI 123456789 Jane Doe Hello 000000001 John Smith Hello

Sets all auth_name fields to hello

Using SQL
To change a table in a database use ALTER TABLE. ADD adds a characteristic.
Initializer ALTER TABLE authors Type ADD birth_date datetime null

auth_id

auth_name

auth_city Dearborn Taylor

auth_state MI MI

birth_date . .

123456789 Jane Doe 000000001 John Smith

ADD puts a new column in the table called birth_date

Using SQL
To delete a column or row, use the keyword DROP ALTER TABLE authors DROP birth_date
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI auth_state . . 123456789 Jane Doe 000000001 John Smith

DROP removed the birth_date characteristic from the table

Conclusion

SQL is a versatile language that can integrate with numerous 4GL languages and applications SQL simplifies data manipulation by reducing the amount of code required. More reliable than creating a database using files with linked-list implementation