More Related Content
Similar to A Tour To SQL (20)
A Tour To SQL
- 1. SQL
Originally, SQL was called SEQUEL (Structured English QUEry Language) and was designed and implemented at
IBM Research as the interface for an experimental relational database system called SYSTEM R.
A joint effort by ANSI and ISO has led to a standard version of SQL (ANSI 1986), called SQL86
or SQLl.
A revised and much expanded standard called SQL2 (also referred to as SQL92) was subsequently developed.
The next version of the standard was originally called SQL3, but is now called SQL99.
We will try to cover the latest version of SQL as much as possible.
SQL Schema:
An SQL schema is identified by a schema name, and includes an authorization identifier to indicate the
user or account who owns the schema, as well as descriptors for each element in the schema. Schema
elements include tables, constraints, views, domains, and other constructs (such as authorization
grants) that describe the schema.
A schema is created via the CREATE SCHEMA statement, which can include all the schema elements'
definitions. Alternatively, the schema can be assigned a name and authorization identifier, and the
elements can be defined later.
For example, the following statement creates a schema called COMPANY, owned by the user with
authorization identifier JSMITH:
CREATE SCHEMA COMPANY AUTHORIZATION JSMITH;
SQL Catalog
A named collection of schemas in an SQL environment.
An SQL environment is basically an installation of an SQLcompliant RDBMS on a computer sysrem.
A catalog always contains a special schema called INFORMATION_SCHEMA, which provides
information on all the schemas in the catalog and all the element descriptors in these schemas.
Integrity constraints such as referential integrity can be defined between relations only if they exist in
schemas within the same catalog. Schemas within the same catalog can also share certain elements,
such as domain definitions.
Persistent data
Persistent data is data whose lifetime typically exceeds that of individual application program
executions. In other words, it is data that (a) is stored in the database and (b) persists
from the moment it is created until the moment it is explicitly destroyed.
A relationship is an association among entities.
Security means the protection of the data in the database against unauthorized access.
a. Security means making sure users are allowed to do the
things they're trying to do.
b. Integrity means making sure the things they're trying to do
are correct.
A host language is a language in which a data sublanguage is
embedded. The host language is responsible for providing
various nondatabase facilities, such as I/O operations, local
- 2. variables, computational operations, ifthenelse logic, and
so on.
A foreign key is a column or combination of columns in one
relation whose values are required to match those of the primary
key in some other relation (or possibly in the same relation).
Privileges are the actions that a user is permitted to carry out for a given database object
Four types of database constraints (uniqueness constraints, primary and foreign key
constraints, and check constraints) are closely associated with a single database table.
They are specified as part of the CREATE TABLE statement and can be modified or
dropped using the ALTER TABLE statement. The other two types of database integrity
constraints, assertions and domains, are created as independent "objects" within a
database, independent of any individual table definition.
INTEGRITY CONSTRAINTS
An integrity constraint is a condition specified on a database schema which prevents the entry of incorrect
information and restricts the data that can be stored in an instance of the database to satisfy this constraint.
Domain Constraints and Distinct Types
CREATE TYPE ratingtype AS INTEGER
This statement defines a new distinct type called ratingtype, with INTEGER
as its source type
A user can define a new domain using the CREATE DOMAIN statement, which
uses CHECK constraints
CREATE DOMAIN ratingval INTEGER DEFAULT 1
CHECK ( VALUE >= 1 AND VALUE <= 10 )
Key Constraints
1. Primary key constraint
CREATE TABLE Students ( sid CHAR(20) ,
name CHAR (30) ,
login CHAR(20) ,
age INTEGER,
gpa REAL,
UNIQUE (name, age),
CONSTRAINT StudentsKey PRIMARY KEY (sid)
);
While violating the constraint, constraint name eg. StudentsKey is returned as error.
2. Foreign key constraint
CREATE TABLE Enrolled ( studid CHAR(20) ,
cid CHAR(20),
grade CHAR(10),
PRIMARY KEY (studid, cid),
FOREIGN KEY (studid) REFERENCES Students
);
- 3. General Constraints
Constraints over a Single Table
SQL supports the creation of assertions, which are constraints not associated with anyone table.
As an example, suppose that we wish to enforce the constraint that the number of boats plus the number of sailors
should
be less than 100.
CREATE ASSERTION smallClub
CHECK (( SELECT COUNT (S.sid) FROM Sailors S ) ( SELECT COUNT (B. bid) FROM Boats B) < 100 )
We can specify complex constraints over a single table using table constraints, which have the form CHECK
conditionalexpression. For example, to ensure that rating must be an integer in the range 1 to 10, we could use:
CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid),
CHECK (rating >= 1 AND rating <= 10 ))
NULL
It I s used to represent a missing value, but that it usually has one of three different interpretationsvalue unknown (exists but is
not known), value not available (exists but is purposely withheld), or attribute not applicable (undefined for this tuple). Consider
the following examples to illustrate each of the three meanings of NULL.
1. Unknown value: A particular person has a date of birth but it is not known, so it is represented by NULL in the database.
2. Unavailable or withheld value: A person has a home phone but does not want it to be listed, so it is withheld and
represented as NULL in the database.
3. Not applicable attribute: An attribute LastCollegeDegree would be NULL for a person who has no college degrees,
because it does not apply to that person.
SQL Command Types:
SQL is a tool for organizing, managing, and retrieving data stored by a computer
Database. In fact, SQL works with one specific type of database, called a relational database.
SQL is used to control all of the functions that a DBMS provides for its users, including:
• Data definition
• Data retrieval.
• Data manipulation.
• Access control.
• Data sharing.
• Data integrity.
SQL is not really a complete computer language like COBOL, C, C++, or Java.
SQL contains no IF statement for testing conditions, and no GOTO, DO, or FOR
statements for program flow control. Instead, SQL is a database sublanguage, consisting
of about forty statements specialized for database management tasks
The main body of the SQL language consists of about 40 statements, which are
Summarized below: Each statement requests a specific action from the DBMS,
such as creating a new table, retrieving data, or inserting new data into the database
Data Manipulation(DML4)
SELECT
Retrieves data from the database
INSERT
A dds new rows of data to the database
DELETE
Removes rows of data from the database
- 5. Prepares a SQL statement for dynamic execution
EXECUTE
Executes a SQL statement dynamically
DESCRIBE
Describes a prepared query
.
SQL KEY WORDS (207+) and POTENTIAL ANSI/ISO KEY WORDS(75+)
Most DBMS brands allow different users to create tables with the same name
With the proper permission, you can also refer to tables owned by other users, by using a qualified table name.
A qualified table name specifies both the name of the table's owner(ie.schema name) and the name of the table, separated
by a period (.).
A qualified column name specifies both the name of the table containing the column and the name of the column,
separated by a period (.).
SQL Data Types
The ANSI/ISO SQL standard specifies the various types of data that can be stored in a
SQLbased database and manipulated by the SQL language. The original SQL1 standard
specified only a minimal set of data types. The SQL2 standard expanded this list to
include variablelength character strings, date and time data, bit strings, and other types.
Today's commercial DBMS products can process a rich variety of different kinds of data,
and there is considerable diversity in the particular data types supported across different
DBMS brands. Typical data types include:
•Integers. Columns holding this type of data typically store counts, quantities, ages, and
so on. Integer columns are also frequently used to contain I.D. numbers, such as
customer, employee, and order numbers.
•Decimal numbers. Columns with this data type store numbers that have fractional
parts and must be calculated exactly, such as rates and percentages. They are also
frequently used to store money amounts.
•Floating point numbers. Columns with this data type are used to store scientific
numbers that can be calculated approximately, such as weights and distances.
Floating point numbers can represent a larger range of values than decimal numbers
but can produce roundoff errors in computations.
• Fixedlength character strings. Columns holding this type of data typically store names
of people and companies, addresses, descriptions, and so on.
•Variablelength character strings. This data type allows a column to store character
strings that vary in length from row to row, up to some maximum length. (The SQL1
standard permitted only fixedlength character strings, which are easier for the DBMS
to process but can waste considerable space.)
•Money amounts. Many SQL products support a MONEY or CURRENCY type, which is
usually stored as a decimal or floating point number. Having a distinct money type
allows the DBMS to properly format money amounts when they are displayed.
•Dates and times. Support for date/time values is also common in SQL products,
although the details vary dramatically from one product to another. Various
combinations of dates, times, timestamps, time intervals, and date/time arithmetic are
generally supported. The SQL2 standard includes an elaborate specification for DATE,
TIME, TIMESTAMP, and INTERVAL data types, including support for time zones and
time precision (for example, tenths or hundredths of seconds).
- 6. •Boolean data. Some SQL products, such as Informix Dynamic Server, support logical
(TRUE or FALSE) values as an explicit type, and some permit logical operations
(comparison, AND/OR, and so on) on the stored data within SQL statements.
•Long text. Several SQLbased databases support columns that store long text strings
(typically up to 32,000 or 65,000 characters, and in some cases even larger). This
allows the database to store entire documents, product descriptions, technical papers,
resumes, and similar unstructured text data. The DBMS usually restricts the use of
these columns in interactive queries and searches.
•Unstructured byte streams. Several DBMS products allow unstructured, variablelength
sequences of bytes to be stored and retrieved. Columns containing this data
are used to store compressed video images, executable code, and other types of
unstructured data. SQL Server's IMAGE data type, for example, can store a stream of
up to 2 billion bytes of data.
•Asian characters. As databases grow to support global applications, DBMS vendors
have added support for fixedlength and variablelength strings of 16bit characters
used to represent Kanji and other Asian characters. Searching and sorting on these
GRAPHIC and VARGRAPHIC types is usually not permitted, however
The following list lists the data types specified in the ANSI/ISO SQL standard.
Data Type Description
CHAR(len)
CHARACTER(len)
Fixedlength character strings
VARCHAR(len)
CHAR VARYING(len)
CHARACTER VARYING(len)
Variablelength character strings*
NCHAR(len)
NATIONAL CHAR(len)
NATIONAL CHARACTER(len)
Fixedlength national character strings*
NCHAR VARYING(len)
NATIONAL CHAR VARYING(len)
NATIONAL CHARACTER
VARYING(len)
Variablelength national character strings*
INTEGER
INT
Integer numbers
SMALLINT
Small integer numbers
BIT(len)
Fixedlength bit string*
BIT VARYING(len) Variablelength bit string*
NUMERIC(precision,scale)
DECIMAL(precision,scale)
DEC(precision,scale)
Decimal numbers
FLOAT(precision)
Floating point numbers
REAL
Lowprecision floating point numbers
DOUBLE PRECISION
Highprecision floating point numbers
DATE
- 8. String converted to all uppercase letters
SQL Operators
Comparison Test (=, <>, <, <=, >, >=)
Range Test (BETWEEN)
Set Membership Test (IN)
Pattern Matching Test (LIKE)
Wildcard Characters (%,_)
Escape Characters *
Null Value Test (IS NULL)
Compound Search Conditions (AND, OR, and NOT)
Sorting Query Results (ORDER BY Clause)
Combining Query Results (UNION ALL and UNION DISTINCT) *
SQL2 UNION, INTERSECT, and DIFFERENCE(EXCEPT) Operator
Embedded SQL
Commercial SQL database vendors offer two basic techniques for using SQL within an
application program:
•Embedded SQL. In this approach, SQL statements are embedded directly into the
program's source code, intermixed with the other programming language statements.
Special embedded SQL statements are used to retrieve data into the program. A
special SQL precompiler accepts the combined source code and, along with other
programming tools, converts it into an executable program.
•Application program interface. In this approach, the program communicates with the
DBMS through a set of function calls called an application program interface, or API.
The program passes SQL statements to the DBMS through the API calls and uses API
calls to retrieve query results. This approach does not require a special precompiler.
Dynamic SQL Concepts
The central concept of dynamic SQL is simple: don't hardcode an embedded SQL
statement into the program's source code. Instead, let the program build the text of a
SQL statement in one of its data areas at runtime. The program then passes the
statement text to the DBMS for execution "on the fly." Although the details get quite
complex, all of dynamic SQL is built on this simple concept, and it's a good idea to keep it
in mind.