Sunteți pe pagina 1din 28

Chapter 7:

Introduction to SQL
Dr. Vandana Janeja
Information System Dept

2005 by Prentice Hall

Objectives

Create a database using SQL data


definition language

Tables, constraints, views

Write single table queries using SQL


Learn how to use Oracle SQL*Plus

Chapter 7

Roadmap

An overview of SQL
How to login in to Oracle using
SQL*Plus
How to create tables and defining
column data types
How to insert and delete rows
How to write a query without
conditions

Chapter 7

The Physical Design Stage of SDLC


(Figures 2-4, 2-5 revisited)
Project Identification
and Selection
Project Initiation
and Planning
Analysis

Purpose programming, testing,


training, installation, documenting
Deliverable operational
programs, documentation, training
materials, program/data structures
Logical Design
Physical
Physical Design
Design

Database activity
physical database design and
database implementation

Chapter 7

Implementation
Implementation
Maintenance

SQL Overview

Structured Query Language

The standard for using relational database


management systems (RDBMS)
Declarative and high level: user only
specifies what he wants (to create, to query,
to change); DBMS executes it
Results also in table format
SQL is case insensitive

Chapter 7

Where to Use SQL

Connect directly to DBMS (e.g., oracle)

SQL*Plus: a client tool to connect to oracle


Oracle supports PL*SQL, a superset of SQL
(SQL + some low level programming)
We provide a web interface in class

Embedded in other programming languages

C: Open DataBase Connectivity (ODBC)

Each DBMS provides an ODBC driver


Your program can be written just once

JAVA: JDBC
Internet: use VBScript and Microsoft Active
Server Pages

Chapter 7

SQL-92 and SQL-99


Standards

Specify syntax/semantics for data


definition and manipulation
Benefits:

Reduced training costs


Productivity
Application portability
Application longevity
Reduced dependence on a single vendor
Cross-system communication

Chapter 7

Oracle SQL*Plus

Open browser, go to

http://oracle92.is.umbc.edu:7778/isqlplus

Type in your login (lastname) and password


(same as login)
Connection Identifier: oracle11g.is.umbc.edu
Click login
Change your password:
click preference->change password
Remember to logout or close browser window

Chapter 7

SQL Environment

Schema

The structure that contains descriptions of


objects created by a user (base tables,
views, constraints)
In Oracle, each user has his own schema
After login, by default you only see your
schema
Usually you can not see others schema
unless other user gives you permission

Chapter 7

SQL Environment

Catalog

A set of schemas that constitute the


description of a database
Catalog is stored as tables
Example:

User_tables:

where the tables user


created are stored
Can use SQL to find out schema
information (will discuss later)
Chapter 7

10

General Steps of Database


Implementation

Create tables (including columns,


primary key constraints, foreign key
constraints)
Insert rows into those tables
Implement required business
function using SQL statements
(select, insert, delete, update)

Chapter 7

11

General Steps of Database


Implementation

Given a business function

A search function (e.g., search for


employees), write select (search)
statement
An update function (e.g., raise the salary of
an employee), write update statement
A delete function (e.g., delete an
employee), write delete statement
An insert function (e.g., insert a new
employee), write an insert statement

Chapter 7

12

Create Table
CREATE TABLE table-name
(column1-name data-type,

last-column-name data-type)
E.g.,
create table dept (
did int,
dname varchar(30),
primary key (did)
);
Chapter 7

13

Create Table
SQL is not case sensitive
Last column has no comma
Semi colon represents end of each
statement
(can run several statements in one
execution)
Comments:
-- comments on the same line
/* comments that span multiple lines */

Chapter 7

14

Drop A Table
Drop table dept
A good habit to drop a table
before create a table

Otherwise if an old table with


same name exists, can not
create the new table

Chapter 7

15

Column Data Types

String types

CHAR(n) fixed-length character data,


n characters long Maximum length = 2000
bytes

Usually only used for short strings, otherwise waste


space
Return an error if try to insert a string with size > n

VARCHAR(n) variable length character data,


maximum 4000 bytes

Chapter 7

When strings are relatively long and have different


lengths

16

Column Data Types

Use '' to represent the value of a string,


E.g., add IT department to dept table:
insert into dept
values (1,'IT');
Use notepad or wordpad when writing the
single quote
Single quote in PowerPoint or word does
not work (wrong format)
It is a good habit to save all your SQL statements
(especially create table and insert) in a file

Chapter 7

17

Column Data Types

Numeric types

NUMBER general purpose numeric data type


INTEGER integer
FLOAT floating point in scientific notation

You may specify precision (number of


digits) if necessary (usually not necessary)
E.g., number(10) means a number with up
to 10 digits

Chapter 7

18

Column Data Types


Date type
create table emp (
eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references dept(did));

Chapter 7

19

Column Data Types

When representing a value of date


type
Oracle use 'DD-MON-YY' format, where
DD is day of month, MON is 3 letter
abbreviation of month, YY is 2 digits
year (1950-2050)
insert into emp
values(1,'jeff',1, '01-JAN-05',70000);

Chapter 7

20

Undo All Insert

Delete from table-name;


E.g. delete from emp;
All rows in emp table will be dropped

Chapter 7

21

Column Data Types

A better format (more natural)


Date 'YYYY-MM-DD', where Date is a
prefix,
E.g.,
insert into emp
values(1,'jeff',1,date '2005-1-1',70000);

Chapter 7

22

Insert:

Insert

Template:
insert into table-name values(values for all
columns, separated by comma)
Values in the column order
Dont forget values keyword
Dont forget how to represent strings and
date E.g.,
insert into dept values (1,'IT');
insert into dept values (2, 'HR');

Chapter 7

23

Insert

create table emp (


eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references dept(did));
insert into emp
values(1,'jeff',1,date '2005-1-1',70000);
insert into emp
values(2,'susan',2,date '2005-6-1',50000);
Chapter 7

24

SQL Environment

Delete:

Delete from emp where eid = 1


Remove rows in emp table with eid equals
1
Delete from emp
remove all rows in employee
Grammar:
Delete from table-name [where condition]
[] represents optional
Do not forget from keyword

Chapter 7

25

select

Select * from emp


List all rows and all columns from
employee
Select ename, did from emp
List name and department columns
from employee (for all rows)
Select ename from emp
where did = 1
List names of employees working in
department 1

Chapter 7

26

select

Grammar:
select * or column-name, , columnname
from table-name
[where condition]

Chapter 7

27

select

Find out what tables have been


created:
Select table_name from user_tables;
Useful Oracle specific command:

Describe table-name
E.g., describe emp;
It returns column names, column data
types

Chapter 7

28

S-ar putea să vă placă și