Download lecture7

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

IMDb wikipedia , lookup

DBase wikipedia , lookup

Concurrency control wikipedia , lookup

Oracle Database wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

Functional Database Model wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Clusterpoint wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Lecture 7 – Database and SQL
Database?
A database is a collection of associated or
related data.
Examples – LIS (landuse information
databse); Medical office database; tax
database.
Differences between database and file?
Database Models
A conceptual way of thinking about data in a
database is called a logical model.
Over the past 30 years or so, 3 different
logical database models have evolved:
1. The hierarchical model
2. The network model
3. The relational model
The relational Model
Dr. Edgar F. Codd introduced the relational
model around 1970.
Idea: “If you ignore the way data files are
connected and arrange your data into
simple two-dimensional, unordered tables,
then you can develop an algebra for
queries and focus on the data as data, not
as a physical realization of a logical
model”
What is an ArcSDC Service?
An ArcSDE service conveys spatial data
between geographic information system
applications and a database. The
database may be any one of the
supported RDBMSs:
- Oracle
- SQL Server
- Informix
Applications
The applications that can connect to and
access spatial data from an ArcSDE
service include ArcInfo, ArcView GIS, Map
Objects, ArcIMS, and ArcSDE CAD client,
as well as custom-built applications
created by either you or an ESRI business
partner.
Properties of an ArcSDE service
The home directory – which is defined by
the system variable, SDEHOME.
The ArcSDE server monitor – the giomgr
process
The gsrvr process – dedicated to a singleuser application connection.
Properties of An ArcSDR service
(Cont.)
The TCP/IP servce name and port
number – giomgr listens for application
connection requests on a dedicated
TCP/IP service name and port number
The database
The configuration files – ArcSDE service
can be configured to control the number of
application connections as well as the OS
resources each connection may obtain.
Geographic Features in a SQL
Database
Feature - abstraction of a real world phenomena
A feature attribute - characteristic of a feature“
Thus, a geographic feature is a real world
phenomena with a feature attribute that defines
its dimension and location in space.
In a SQL database, features are managed in
tables, an instance of a feature corresponds to a
row, and an attribute of a feature to a column.
Data Access
Data access is built around the standard cursor
model defined in the Structured Query
Language (SQL).
The data access model builds on the traditional
model by supporting spatial and raster
constraints in a query.
A database connection is a connection to a source
of geographic data. It represents the ArcSDE
server, which can respond to requests for
geographic information.
Standards for Spatial Data in a
SQL database
ArcSDE is based on standards for managing spatial data in
a SQL database that have been defined by both
international standards bodies, and industry
organizations:
OpenGIS Consortium - Simple Features Specification for
SQL
ISO19125-1, Geographic Information - Simple feature
access - Part 1: Common architecture
ISO19125-2, Geographic Information - Simple feature
access - Part 2: SQL option
ISO 19107, Geographic information ; Spatial schema
ISO 13249-3, Information technology — Database
languages — SQL Multimedia and Application
Packages — Part 3: Spatial
SQL and Access
SQL – SEQUEL (Structured English Query
Language) and was originally an IBM
product.
Access – readily available to windows user,
also provides a good platform on which to
learn SQL. Second only to Oracle, Access
is the most widely used SQL product in
business and industry.
Getting into SQL in Access
Path:
Queries -> Create query in Design view ->
New -> New Query -> Design View ->
Close Show Table -> Click “SQL” button
Typing a simple SQL query:
Select *
From Student
Where class=4
Running s SQL Query
Click to run the
script
You can save your query when you try to
close your query window, or through
File/Save menu
Editing a SQL Query
Viewing Table Design
Beginning SQL Commands in
Access
Basic statements in SQL –
The Select Statement
Selecting Fields
Using Order BY
Using ASC or DESC
Ordering Within an Order
Selecting Rows (Tuples)
The Where Clause
Using AND
Using OR
Beginning Statements
SQL statements in Access must begin with
one of the following commands:
Select
Insert
Delete
Update
Procedure
Select is the most commonly used command
The SELECT Statement
Select Syntax:
SELECT fields FROM Tables;
As in most versions of SQL, SQL commands
in Access have to be terminated by
semicolon.
The “*” means “all fields” of the table:
Select *
FROM Student;
Selecting Fields
Select one field:
SELECT field_name FROM table;
SELECT sname FROM Student;
Select More than one field:
Select sname, class FROM Student;
Using ORDER BY:
SELECT sname, class
FROM Student
ORDER BY class;
Using ASC or DESC
SELECT sname, class
FROM Student
ORDER BY class ASC;
Ordering Within an Order:
SELECT sname, class
FROM Student
ORDER BY class DESC, sname;
SELECTING Rows (Tuples)
The WHERE Clause can restrict the output
of rows in the results set by adding a
WHERE clause.
SELECT fields or attributes or columns
FROM Table
WHERE criteria;
SELECT *
FROM Student
WHERE class=4;
Comparisons Operators
All that can be used in a WHERE clause are
listed here:
>
<>
=
>=
<=
Logical Operators
To include multiple conditions in a WHERE
clause, logical operators, AND and OR,
and the BETWEEN operator may be used.
Using AND
SELECT *
FROM Student
WHERE class = 5
AND major = ‘ACCT’;
Using OR and BETWEEN
SELECT *
FROM Student
WHERE class = 4
OR major = ‘ACCT’;
SELECT …
FROM
WHERE
BETWEEN value1 and value2
Question – could you write a SQL scripts to find all students of class 1,
2, and 3?
The COUNT Function
Number of rows what will be obtained in a
result set without actually displaying all of
the result set itself.
SELECT COUNT(*)
FROM Student;
SELECT COUNT(class)
FROM Student;
Question – did you get same output from above examples?
Why?
NULL Values
NULL values are used to designate missing
data in tables.
SELECT sname, class
FROM Student
WHERE class IS NULL;
Using Aliases
We can enhance the output and readability of a
query by using column and table aliases.
SELECT sname AS Student_name, major AS Student_major, class AS
Student_class
FROM Student
Where class = 4;
A Table alias is a temporary name for a table that
allows us to short-hand a table’s name when we
reference that table in a query.
SELECT s.sname
FROM Student s
WHERE s.class = 4
Table Qualifiers
SELECT *
FROM Student
WHERE student.class = 4
A Convention for Writing SQL
Statements
Use Uppercase letters for keywords
Align the keywords on separate lines.
Access does not allow comments in the
SQL window
Single quotes and double quotes have the
same meaning.
Access allows blank lines in the SQL
window, but does not allow any characters
after the semicolon.
Creating and Populating Tables
Data Types in Access
Opening a Database
- Creating a New Database to Create a Table
- Opening an Existing Database to Create a table
Creating a table
Inserting Values into a table
The Update Command
The Alter Table Command
Data Types in Access
Access supports 14 primary data types:
Text, Char, Integer, Real,
Float, Decimal, Smallint, Tinyint,
Money, Datetime, Binary, Bit,
Image, and UniqueIdentifier.
Internal/External names
The synonyms are external names.
Valid synonyms for the Text field are:
LONGTEXT, LONGCHAR, NOTE, NTEXT,
and MEMO.
Similar structures exist for other data types.
Opening a Database
You can create a new database from
scratch, or open an existing one. (Try this
by yourself to see how.)
To introduce new data into your database,
you will need to create tables.
CREATE TABLE Tablename
(attribute_name type, attribute_name, type, ….);
Example:
CREATE TABLE Employee (name
address
employee_number
salary
VARCHAR(20),
VARCHAR(20),
NUMBER,
CURRENCY);
Inserting Values into a Table
Using INSERT INTO … VALUES
INSERT INTO Names
VALUES(‘Joe Smith’);
Note – Names is the name of an existing table
INSERT INTO Employee
VALUES (‘Joe Smith’, ‘102 Hamilton Av.’, 101, 2500);
Note – if an attribute is unknown, you need to use
“NULL” to identify
Values (‘Joe Smitch’, null, 101, null)
Using INSERT INTO .. SELECT
This is useful for inserting multiple rows.
INSERT INTO target_table(field)
“Select clause”;
Example:
INSERT INTO Names(name)
SELECT name
FROM Employee;
Question: Can you add employees with salary > 2600?
The Update Command
For example set all salaries in the table
Emp2 to zero, you may do so with one
update command:
UPDATE Emp2
SET sal = 0;
UPDATE Employee
SET salary = 0
WHERE employee_number = 101
The Alter Table Command
ALTER TABLE Tablename
ADD column-name type;
ALTER TABLE Employee
ADD balance CURRENCY;
Changing a Column’s Type in a table
ALTER TABLE EMPLOYEE
ALTER COLUMN balance NUMBER;
Deleting a Column in a Table
ALTER TABLE Tablename
DROP column-name;
You can also delete rows from a table:
DELETE FROM Table
WHERE (condition);
DELETE *
FROM Employee
WHERE salary>100