Chapter 7 SQL
SQL Uses
• Database Definition and Querying
– Can be used as an interactive query language
– Can be imbedded in programs
• Relational Calculus combines Select, Project
and Join operations in a single command:
SELECT
IS 257 – Fall 2011
SELECT
• Syntax:
– SELECT [DISTINCT] attr1, attr2,…, attr3 FROM rel1
r1, rel2 r2,… rel3 r3 WHERE condition1 {AND | OR}
condition2 ORDER BY attr1 [DESC], attr3 [DESC]
IS 257 – Fall 2011
SELECT
• Syntax:
– SELECT a.author, b.title FROM authors a, bibfile b,
au_bib c WHERE a.AU_ID = c.AU_ID and c.accno =
b.accno ORDER BY a.author ;
• Examples in Access...
IS 257 – Fall 2011
SELECT Conditions
• = equal to a particular value
• >= greater than or equal to a particular value
• > greater than a particular value
• <= less than or equal to a particular value
• <> not equal to a particular value
• LIKE “*term*” (may be other wild cards in other systems)
• IN (“opt1”, “opt2”,…,”optn”)
• BETWEEN val1 AND val2
• IS NULL
IS 257 – Fall 2011
Relational Algebra Selection using SELECT
• Syntax:
– SELECT * FROM rel1 WHERE condition1 {AND |
OR} condition2;
IS 257 – Fall 2011
Relational Algebra Projection using SELECT
• Syntax:
– SELECT [DISTINCT] attr1, attr2,…, attr3 FROM rel1
r1, rel2 r2,… rel3 r3;
IS 257 – Fall 2011
Relational Algebra Join using SELECT
• Syntax:
– SELECT * FROM rel1 r1, rel2 r2 WHERE r1.linkattr
= r2.linkattr ;
IS 257 – Fall 2011
Sorting
• SELECT BIOLIFE.[Common Name],
BIOLIFE.[Length (cm)]
FROM BIOLIFE
ORDER BY BIOLIFE.[Length (cm)] DESC;
Note: the square brackets are not part of the standard,
But are used in Access for names with embedded blanks
IS 257 – Fall 2011
Subqueries
• SELECT SITES.[Site Name], SITES.[Destination
no]
FROM SITES
WHERE sites.[Destination no] IN (SELECT
[Destination no] from DEST where [avg temp
(f)] >= 78);
• Can be used as a form of JOIN.
IS 257 – Fall 2011
Aggregate Functions
• Count
• Avg
• SUM
• MAX
• MIN
• Others may be available in different systems
IS 257 – Fall 2011
Using Aggregate functions
• SELECT attr1, Sum(attr2) AS name FROM
tab1, tab2 ...
GROUP BY attr1, attr3 HAVING condition;
IS 257 – Fall 2011
Using an Aggregate Function
• SELECT DIVECUST.Name, Sum([Price]*[qty]) AS Total
FROM (DIVECUST INNER JOIN DIVEORDS ON DIVECUST.[Customer
No] = DIVEORDS.[Customer No]) INNER JOIN DIVEITEM ON
DIVEORDS.[Order No] = DIVEITEM.[Order No]
GROUP BY DIVECUST.Name
HAVING (((DIVECUST.Name) Like "*Jazdzewski"));
IS 257 – Fall 2011
GROUP BY
• SELECT DEST.[Destination Name], Count(*) AS
Expr1
FROM DEST INNER JOIN DIVEORDS ON
DEST.[Destination Name] =
DIVEORDS.Destination
GROUP BY DEST.[Destination Name]
HAVING ((Count(*))>1);
• Provides a list of Destinations with the
number of orders going to that destination
IS 257 – Fall 2011
SQL Commands
• Data Definition Statements
– For creation of relations/tables…
IS 257 – Fall 2011
Create Table
• CREATE TABLE table-name (attr1 attr-type
PRIMARY KEY, attr2 attr-type,…,attrN attr-
type);
• Adds a new table with the specified attributes
(and types) to the database.
IS 257 – Fall 2011
Access Data Types (Not MySQL)
• Numeric (1, 2, 4, 8 bytes, fixed or float)
• Text (255 max)
• Memo (64000 max)
• Date/Time (8 bytes)
• Currency (8 bytes, 15 digits + 4 digits decimal)
• Autonumber (4 bytes)
• Yes/No (1 bit)
• OLE (limited only by disk space)
• Hyperlinks (up to 64000 chars)
IS 257 – Fall 2011
Access Numeric types
• Byte
– Stores numbers from 0 to 255 (no fractions). 1 byte
• Integer
– Stores numbers from –32,768 to 32,767 (no fractions) 2 bytes
• Long Integer (Default)
– Stores numbers from –2,147,483,648 to 2,147,483,647 (no
fractions). 4 bytes
• Single
– Stores numbers from -3.402823E38 to –1.401298E–45 for negative
values and from 1.401298E–45 to 3.402823E38 for positive values.
4 bytes
• Double
– Stores numbers from –1.79769313486231E308 to –
4.94065645841247E–324 for negative values and from
1.79769313486231E308 to 4.94065645841247E–324 for positive
values. 15 8 bytes
• Replication ID
– Globally unique identifier (GUID) N/A 16 bytes
IS 257 – Fall 2011
Oracle Data Types
• CHAR (size) -- max 2000
• VARCHAR2(size) -- up to 4000
• DATE
• DECIMAL, FLOAT, INTEGER, INTEGER(s), SMALLINT,
NUMBER, NUMBER(size,d)
– All numbers internally in same format…
• LONG, LONG RAW, LONG VARCHAR
– up to 2 Gb -- only one per table
• BLOB, CLOB, NCLOB -- up to 4 Gb
• BFILE -- file pointer to binary OS file
IS 257 – Fall 2011
Creating a new table from existing tables
• Access and PostgreSQL Syntax:
SELECT [DISTINCT] attr1, attr2,…, attr3 INTO
newtablename FROM rel1 r1, rel2 r2,… rel3 r3
WHERE condition1 {AND | OR} condition2 ORDER
BY attr1 [DESC], attr3 [DESC]
IS 257 – Fall 2011
How to do it in MySQL
mysql> SELECT * FROM foo;
+---+
|n|
+---+
|1|
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM bar;
+------+---+
|m |n|
+------+---+
| NULL | 1 |
+------+---+
IS 257 – Fall 2011
courses.ischool.berkeley.edu/i257/f11/Lecture
s/Lecture01_257.ppt
SQL - History
• Structured Query Language
• SEQUEL from IBM San Jose
• ANSI 1992 Standard is the version used by
most DBMS today (SQL92)
• Basic language is standardized across
relational DBMSs. Each system may have
proprietary extensions to standard.
IS 257 – Fall 2011
MySQL Data Types
• MySQL supports all of the standard SQL numeric data types.
These types include the exact numeric data types (INTEGER,
SMALLINT, DECIMAL, and NUMERIC), as well as the
approximate numeric data types (FLOAT, REAL, and DOUBLE
PRECISION). The keyword INT is a synonym for INTEGER, and
the keyword DEC is a synonym for DECIMAL
• Numeric (can also be declared as UNSIGNED)
– TINYINT (1 byte)
– SMALLINT (2 bytes)
– MEDIUMINT (3 bytes)
– INT (4 bytes)
– BIGINT (8 bytes)
– NUMERIC or DECIMAL
– FLOAT
– DOUBLE (or DOUBLE PRECISION)
IS 257 – Fall 2011
MySQL Data Types
• The date and time types for representing temporal
values are DATETIME, DATE, TIMESTAMP, TIME, and
YEAR. Each temporal type has a range of legal values,
as well as a “zero” value that is used when you
specify an illegal value that MySQL cannot represent
– DATETIME '0000-00-00 00:00:00'
– DATE '0000-00-00'
– TIMESTAMP (4.1 and up) '0000-00-00 00:00:00'
– TIMESTAMP (before 4.1) 00000000000000
– TIME '00:00:00'
– YEAR 0000
IS 257 – Fall 2011
MySQL Data Types
• The string types are CHAR, VARCHAR, BINARY,
VARBINARY, BLOB, TEXT, ENUM, and SET
• Maximum length for CHAR is 255 and for VARCHAR
is 65,535
Value CHAR(4) Storage VARCHAR(4) Storage
"" " " 4 "" 1
"ab" "ab " 4 "ab" 3
"abcd" "abcd" 4 "abcd" 5
• VARCHAR
"abcdefg" uses 1 or 2 bytes
"abcd" for the length
4 "abcd" 5
• For longer things there is BLOB and TEXT
IS 257 – Fall 2011
MySQL Data Types
• A BLOB is a binary large object that can hold a
variable amount of data.
• The four BLOB types are TINYBLOB, BLOB,
MEDIUMBLOB, and LONGBLOB. These differ only in
the maximum length of the values they can hold
• The four TEXT types are TINYTEXT, TEXT,
MEDIUMTEXT, and LONGTEXT. These correspond to
the four BLOB types and have the same maximum
lengths and storage requirements
• TINY=1byte, BLOB and TEXT=2bytes,
MEDIUM=3bytes, LONG=4bytes
IS 257 – Fall 2011
MySQL Data Types
• BINARY and VARBINARY are like CHAR and VARCHAR but are
intended for binary data of 255 bytes or less
• ENUM is a list of values that are stored as their addresses in
the list
– For example, a column specified as ENUM('one', 'two', 'three') can
have any of the values shown here. The index of each value is also
shown:
• Value = Index
• NULL = NULL
• ‘’ = 0
• 'one’ = 1
• ‘two’ = 2
• ‘three’ = 3
– An enumeration can have a maximum of 65,535 elements.
IS 257 – Fall 2011
MySQL Data Types
• The final string type (for this version) is a SET
• A SET is a string object that can have zero or more values,
each of which must be chosen from a list of allowed values
specified when the table is created.
• SET column values that consist of multiple set members are
specified with members separated by commas (‘,’)
• For example, a column specified as SET('one', 'two') NOT NULL
can have any of these values:
– ''
– 'one'
– 'two'
– 'one,two‘
• A set can have up to 64 member values and is stored as an
8byte number
IS 257 – Fall 2011
Controlling Data Integrity
• Default values
• Range control
• Null value control
• Referential integrity (next time)
• Handling missing data
IS 257 – Fall 2011
Designing Physical Records
• A physical record is a group of fields stored in
adjacent memory locations and retrieved
together as a unit
• Fixed Length and variable fields
IS 257 – Fall 2011
Physical File Design
• A Physical file is a portion of secondary storage (disk
space) allocated for the purpose of storing physical
records
• Pointers - a field of data that can be used to locate a
related field or record of data
• Access Methods - An operating system algorithm for
storing and locating data in secondary storage
• Pages - The amount of data read or written in one
disk input or output operation
IS 257 – Fall 2011