Você está na página 1de 44

Introduction to SQL

CMPUT 291
File and Database Management
Systems

Goals
Work with SQL statements
DDL and DML statements

Work with Oracle SQL*Plus


Work with Oracle SQL Developer
Execute SQL statements from batch file

SQL overview

SQL*Plus
SQL*Plus is a command line interface to Oracle
databases that can be used to execute SQL statements.
SQL Developer is a graphical user interface to a
database that can be used to browse database objects,
run SQL statements, and build queries.
Oracle is a Database Management System (DBMS).
Oracle supports Structured Query Language (SQL).

Setting Environment Variables


To run SQL*Plus, you must ensure you have the
necessary environment and path variables set.
To accomplish this, execute the following
command
source /oracle/oraenv
You can have this done automatically for you by adding
the above command line to your .bashrc file which lives
in your home directory.

Running SQL*Plus
To run SQL*Plus, issue the following command:
sqlplus
You will be promoted for your Oracle user-name and
password.
First time you login, both your Oracle username and
password are your Unix id.
e.g. your Unix id is Johndoe , then your
Oracle username: Johndoe
Oracle password: Johndoe

You will require to change your password the first time you
login!

Changing Password
When you enter SQL*Plus, the prompt will look like:
Run passw command in SQL*Plus prompt:
You will see:

Password Restrictions
Must start with an alphabetic character
May ONLY include alphabetic, numeric, and
underscore characters. No special characters!
Can not be longer than 30 characters.
Not case sensitive.
For security, had better select a password different from
your Unix password!

Help Facility
SQL>help <keyword>
Example: To see help for append command:
SQL>help append
Help command is for SQL*Plus commands only.

Exit SQL*Plus

To leave SQL*Plus program:


SQL>exit

Oracle SQL Developer


How to run the program:
> sqldeveloper.sh
How to connect to the database:
Username and password: Oracle database username and the
changed password
Connection type: Basic
Role: default
Host name: gwynne.cs.ualberta.ca
Port number: 1521
SID: CRS

More Information about Oracle


SQL Developer
You can find more information about using
Oracle and associated tools in here:
http://gwynne.cs.ualberta
(the address is accessible only from lab machines or via a ssh
tunneling)

And you can find some useful tutorial in here:


http://st-curriculum.oracle.com/tutorial/SQLDeveloper/index.htm

Working with SQL Statements


Data Definition Language (DDL):
DDL statements are used to build and modify
the structure of your tables and other objects
in the database.
Example: create, alter, drop

Data Manipulation Language (DML):


DML statements are used to work with the
data.
Example: insert, update, delete

Create Table Syntax (DDL)

Basic Data Type in Oracle

Basic Data Type in Oracle

Create Table Example

Create Table Example

Dont miss the semicolon at the end of the command!

Verifying Creation of a Table (SQL*Plus)


You can verify that a table is created, or see the
structure of the table using command:

describe <TableName>;
To see all attributes of movie table:

SQL> describe Movie;


You can also use describe command, to recall
the name or type of the attributes of the table.

Alter Table Syntax (DDL)


To add attributes to a defined table:

To remove attributes from table definition:


Where <attribute list> is a comma separated list of attributes.
Be careful!! If drop a column, you will lose its data for all records!!!

Add Attributes Example


To add attributes duration and year to the
movie table

Add Attributes Example


To add attributes duration and year to the
movie table

Drop Attributes Example


To remove attribute duration from movie:

Drop Attributes Example


To remove attribute duration from movie:

Insert Records Syntax (DML)

Insert Records Example


Insert 2 rows into movie table:
(Spiderman, 1, 1901)
(Chicago, 2, 2002)

Insert Records Example


Insert 2 rows into movie table:
(Spiderman, 1, 1901)
(Chicago, 2, 2002)

Update Records Syntax (DML)

Update Records Example


Update year attribute of Spiderman movie
from 1901 to 2001

Update Records Example


Update year attribute of Spiderman movie
from 1901 to 2001:

Select Syntax (DML)

Select Example
Show all records of movie table

Show only movie title and year from movie table

Select Example
Show all records of movie table:
select * from movie;
Show only movie title and year from movie table:
select title, year from movie;

Delete Syntax (DML)

Delete Records Example


Delete all records with movie_number = 1

Delete Records Example


Delete all records with movie_number = 1
delete from movie where movie_number = 1

You can check the result using select:


select * from movie where movie_number = 1

Transactions
Open another session (of SQL*Plus) on your system.
Issue the following command, in the first session:
insert into movie values(T3, 4, 2003);

Check to see if this record is inserted, in the second


session:
select * from movie;

Committing Transactions
This command commits the current transaction.
All changes made by the transaction become
visible to others and are guaranteed to be
durable if a crash occurs.
commit;

Check to see if this record is inserted, in the


second session again:
select * from movie;

Drop Table Syntax (DDL)


To remove a defined table:

Drop Table Example


Remove table movie

Drop Table Example


Remove table movie:
drop table movie;
You can check the result using describe:
describe movie;

Database Catalogue
Built-in tables that keep information about the
database objects.
Examples: User_Tables and All_Tables .
These tables are managed by Oracle.
You can query these tables to get information
about tables stored in the system.

Example of Using Catalogue Tables


Get a list of tables you defined:
select table_name from USER_TABLES;

Get a list of tables owned by a specific


user
select table_name from ALL_TABLES where
owner=C291;

You can see the structure of these tables:


describe USER_TABLES;
describe ALL_TABLES;

Executing SQL Commands Stored


in a File (Batch)
You can store SQL commands in a file and run them in
batch mode in SQL*Plus.
Create a file in an editor and store some SQL commands
in the file.
To run the stored commands:
If file extension is sql (default in SQL*Plus):
SQL>@filename
If file extension is not sql (like .txt):
SQL>@filename.txt

Você também pode gostar