Download Benefits of a Standardized Relational Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

Microsoft Access wikipedia , lookup

Functional Database Model wikipedia , lookup

Btrieve wikipedia , lookup

Tandem Computers wikipedia , lookup

Oracle Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Null (SQL) wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Delta & Pine Land Company
Benefits of a Standardized
Relational Language
SQL?
SQL
?
SQL (pronounced "ess
"ess--queque-el") stands for Structured Query Language.
A RDB language is used to communicate with a database.
ANSI - SQL is the standard language for RDBMS such as Oracle, Sybase,
Microsoft SQL Server, Ingres.
RDBMS contains schemas – Collection of related objects (tables, views,
triggers, indexes etc).
SQL a nonnon-procedural language: Can be executed without being called in
procedures. (dynamic SQL can be called in .NET, Java, PHP etc)
Reduced training costs
Productivity
Application portability and longevity.
Reduced dependence on a single vendor
Cross--system communication
Cross
Executed in two distinct waysways- Interactive and programmatic
Interactive -> Command Line – InterpreterInterpreter- SQL Engine
Programmatic--> Host LanguageProgrammatic
Language-Embedded SQLSQL-Compiler
Disadvantages of a standardized
relational language
SQL Categories
stifle creativity and innovation
never enough to meet all needs
offspring of compromises
using special features may result in loss of
portability
Common SQL Commands
Data Definition Language (DDL):
Select
Insert
Update
Delete
Data Control Language (DCL):
11/25/97
Drop
Rename
Commands that define a database, including creating,
altering, and dropping tables and establishing
constraints.
Data Manipulation Language (DML)
Data Control Language (DCL)
Commands that maintain and query a database.
Commands that control a database, including
administering privileges and committing data.
Querying a Database with SQL
SQL statement
is entered
SQL> SELECT loc
2 FROM dept;
Data Manipulation Language (DML):
Create
Alter
Data Definition Language (DDL):
Grant
Revoke
Statement is sent to
database
Database
Data is displayed
LOC
-----------NEW YORK
DALLAS
CHICAGO
BOSTON
1
Delta & Pine Land Company
Writing SQL Statements
SQL statements are not case sensitive
(but criteria within quotation marks are)
SQL statements can be on one or more lines
Clauses are usually placed on separate lines
Keywords cannot be split across lines
Tabs and spaces are allowed to enhance
readability
Each SQL statement (not line) ends with a
semicolon (;)
Data Definition Language (DDL)
to Create Tables
Data Manipulation Language
(DML)
DDL example in SQL
CREATE TABLE ORDER
(Order_Id
char
not null,
Order_Date date
default sysdate,
sysdate,
Customer_Id char
not null,
Constraint Order_pk primary key ((order_Id
order_Id)) ,
Constraint Order_fk foreign key ((Customer_Id
Customer_Id))
references Customer(Customer_Id
Customer(Customer_Id));
));
Identify appropriate datatypes
Identify columns that should accept null values
Identify columns that need to be unique
Identify all PK/FK
Determine any default values to be inserted
Identify columns which need a domain
specification
Create the table
This is the major focus of our coverage of
SQL
Most useful for querying database based
on specific criteria
Includes: insert, delete, update, and select
Base table :A table in the relational data model containing inserted raw data that is likely
to correspond to one physical file in secondary storage.
View. A view is a virtual table and is often part of an external database. In contrast to a
Cust_ID
Order_ID
Customer
Order
Order_Date
defined base table (relation), a view is not permanently represented in storage. A view
definition is stored, and the contents of the view are calculated each time the view is
referenced in a query. A view may join multiple tables or views together and may contain
derived (or virtual) columns, while base tables cannot. In comparison to a temporary real
table, a view consumes very little storage space.
DML - Inserting
Useful for populating a database
Syntax is:
INSERT INTO Product(Product_ID, Product
_Name, Unit_Price, On_Hand)
VALUES (1, ‘End Table’, 175, 8);
Or
INSERT INTO Ca_Customer
11/25/97
SELECT * FROM Customer
WHERE State=‘CA’;
2
Delta & Pine Land Company
DML - Deleting
DELETE FROM Customer
WHERE State=‘HI’;
DML - Updating
UPDATE Product
SET Unit_Price = 775
WHERE Product_ID = 7;
Or
DELETE FROM Customer;
DML - SELECT
Statement Syntax:
SELECT [DISTINCT] column_list
FROM table_list
[WHERE conditional expression]
[GROUP BY column_list]
[HAVING conditional expression]
[ORDER BY column_list] ;
SELECT
statement
processing order
The Basic SELECT Statement
SELECT column1, column2, column3,...
FROM table;
11/25/97
SELECT identifies what columns
FROM identifies which table
3
Delta & Pine Land Company
Example SELECT Statement
?
SQL> SELECT deptno, loc
2 FROM dept;
SQL> SELECT *
2 FROM dept;
DEPTNO
--------10
20
30
40
DEPTNO
--------10
20
30
40
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
Specifying Output Headings
DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
––––––––––––NEW YORK
DALLAS
CHICAGO
BOSTON
Specifying Output Headings
Double quotes
SQL> SELECT loc AS location
2 FROM dept;
SQL> SELECT loc AS “Location”
2 FROM dept;
LOCATION
------------NEW YORK
DALLAS
CHICAGO
BOSTON
Location
------------NEW YORK
DALLAS
CHICAGO
BOSTON
Note Upper-Case Heading
Duplicate Output
11/25/97
Note Mixed-Case Heading
Suppressing Duplicate Output
SQL> SELECT job
2 FROM emp;
SQL> SELECT DISTINCT job
2 FROM emp;
JOB
--------PRESIDENT
MANAGER
MANAGER
MANAGER
SALESMAN
SALESMAN
...
JOB
--------ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
Total records displayed
Each unique job is listed only once
4
Delta & Pine Land Company
Limiting Rows with WHERE
WHERE Clause Criteria
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE job = ‘CLERK’;
ENAME
---------JAMES
SMITH
ADAMS
MILLER
Case sensitive;
single quotes
JOB
SAL
--------- --------CLERK
950
CLERK
800
CLERK
1100
CLERK
1300
SQL Comparison Operators
OPERATOR
=
>
>=
<
<=
<> or !=
MEANING
Equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Not equal to
BETWEEN Operator Example
11/25/97
Text and dates (sometimes) are enclosed in
single quotes
Numbers are not enclosed in quotes
Text values are case sensitive
Date values are format sensitive
Oracle’s default date format is DDDD-MM
MM--YY,
YY,
SQL> SELECT ename, hiredate
2 FROM emp
3 WHERE hiredate >= ’01-Jan-2007';
SQL Comparison Operators
OPERATOR
MEANING
BETWEEN . . . AND . . . Is between the values
specified (inclusive)
IN(list)
Is in a list of values
specified
LIKE
Matches a pattern
(may include wildcards)
IS NULL
Is a null value
IN Operator Example
SQL> SELECT ename, sal
2 FROM emp
3 WHERE sal BETWEEN 1000 AND 1500;
SQL> SELECT ename, job
2 FROM emp
3 WHERE job IN(‘PRESIDENT’,’MANAGER’);
ENAME
SAL
---------- --------MARTIN
1250
TURNER
1500
WARD
1250
ADAMS
1100
MILLER
1300
ENAME
---------KING
BLAKE
CLARK
JONES
JOB
--------PRESIDENT
MANAGER
MANAGER
MANAGER
5
Delta & Pine Land Company
MySQL Screen Shots
11/25/97
6
Delta & Pine Land Company
LIKE Operator Example
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE ‘J%’;
ENAME
---------JONES
JAMES
The % is a wildcard character
that stands for zero to many
characters. The underscore
character (_) can be used to
stand for exactly one character.
[ LIKE is not the same as = ]
IS NULL Operator Example
SQL> SELECT ename, mgr
2 FROM emp
3 WHERE mgr IS NULL;
ENAME
MGR
---------- --------KING
You cannot use = NULL or != NULL
11/25/97
7
Delta & Pine Land Company
Update studentlist set exam_center = NULL where length(
length(ltrim
ltrim((rtrim(
rtrim(exam_center)))
exam_center))) = ‘
‘;
11/25/97
8