CH - 5 Fundamentals of A Database System
CH - 5 Fundamentals of A Database System
Introduc ti on
Structured Query Language (SQL) is a query language that is standardized by the American
National Standards Institute (ANSI) for most commercial relational database management
systems (RDBMS). To retrieve or update information users execute 'queries' (SQL Statements)
to pull or modify the requested information from the database using criteria that is defined by
the user.
Unfortunately, there are many different versions of the SQL language, but to be in compliance
with the ANSI standard, they must support the same major keywords in a similar manner (such
as SELECT, UPDATE, DELETE, INSERT, WHERE, and others). Most of the SQL database
programs also have their own proprietary extensions in addition to the SQL standard such as
TSQL of Microsoft SQL Server and PLSQL of Oracle!
SQL supports data definition, query and update in Data Definition Language and Data
Manipulation Language (DML)
The command also has different optional parameters in different RDBMS that helps in specifying
owner, file, growth, …
)
Department of CSIT | WSU
EENG 477- Database Systems 4
5. Structured Query Language (SQL)
The primary key constraint in a relation is enforced by using the key word PRIMARY KEY
following the key attribute or incase of multiple attributes it can be specified on a separate line as
shown in the Teams table above.
The referential integrity constraint in a relational database is implemented by the use of a
foreign key. If the referential integrity enforced using a FOREIGN KEY is violated the default
SQL statement forces the rejection of the violating tuple. However, by the use of the optional
referential trigged actions the designer can attach clauses to the foreign key constraint such as:
- ON DELETE {CASCADE | NO ACTION | SET DEFAULT | SET NULL}
- ON UPDATE {CASCADE | NO ACTION | SET DEFAULT | SET NULL }
The default case is NO ACTION, on which the violating action is rejected. CASCADE option
ON DELETE deletes all the referencing rows on deletion of a row. SET DEFAULT and SET
NULL allow replacing for all the referencing rows the column value by the default value or null
value. (Microsoft SQL Server 2000 doesn’t support SET DEFALUT and SET NULL)
The ALTER TABLE command allows modification (adding, changing, or dropping) of a column
or constraint in a table.
The syntax for the command is:
ALTER TABLE <table_name>
[ALTER COLUMN <column_name> <new_data_type>] |
[ADD <column_definition> | <constraint>] |
[DROP <column_name> | < constraint>]
- <table_name> is the name of the table to be altered.
- The ALTER TABLE command takes either of the three optional actions ALTER
COLUMN, ADD or DROP. The ALTER COLUMN option modifies an existing column
definition, the ADD option adds a new column or constraint and the DROP option drops
existing column or constraint.
Example 1:
Example 2:
The DROP command is used to drop an exiting table, database or schema. The syntax for the
command is:
DROP TABLE <table_name>
DROP DATABASE <database_name>
DROP SCHEMA <schema_name>
Example:
DROP TABLE Projects
Creating indexes is a straightforward process when done with the CREATE INDEX statements.
The basic CREATE INDEX statement is:
CREATE [CLUSTERED | NONCLUSTERED] INDEX <index_name>
ON {<table> | <view> } ( <column> [ ASC | DESC ] [ ,...n ] )
Example:
The following statement creates the DueDate nonclustered index on the Projects table:
CREATE INDEX DueDate ON Projects(DDate)
NOTE: A table can have only one clustered index. If a primary key constraint is created on a
table, a clustered index may be created to support the constraint.
The DROP command is also used with indexes to drop an existing database in a table. The
syntax for the command is:
DROP INDEX <index_name> [,...n ]
Example:
DROP INDEX DueDate
- <column_list> is the list of column names whose values are retrieved by the query.
- <table_list> is the list of table names required in the process.
- <condition> is Boolean expression (conditional expression) that determines the rows to be
selected in the query. The expression is build from the logical comparison operators (=, >,
<, >=, <= and <>)
The column list in the SELECT clause can be replaced by an asterisk (*) to retrieve all the
columns in the participating tables.
The WHERE clause is an optional clause needed when a condition is to be set for retrieval of
rows, if the clause is not used in the statement, all the rows for the selected columns in the
specified tables will be retrieved.
Example:
A query to retrieve all the columns for all projects:
SELECT *
FROM Projects
A query to retrieve the name and due date of projects that are not yet completed:
SELECT Name, DDate
FROM Projects
WHERE CDate=NULL
A query to retrieve the projects name and corresponding team names for projects that are
not yet completed:
SELECT Projects.Name, Teams.Name
FROM Projects, Teams
WHERE Projects.PrjId=Teams.PrjId AND CDate=NULL
In SQL queries it may happen that two participating tables have columns with identical names,
to avoid the ambiguity of the columns the name of the table is used together with the column
name as shown above. Ambiguity may also arise if a single table is to participate more than once
in a query, in such situations an alias may be used for the tables as shown in the following query.
Example:
SELECT p.Name, t.Name
FROM Projects AS p, Teams AS t
WHERE p.PrjId=t.PrjId AND CDate=NULL
The SELECT statement by default results a bag of rows rather than a set of rows (i.e. duplicate
rows may exist in the resulting rows). To remove duplicates and have a set of rows as a result
one can the DISTINCT key word on the SELECT clause as follows:
SELECT DISTINCT <column_list>
FROM <table_list>
WHERE <condition>
Example:
A query to retrieve employees name, the projects they are participating and due date of
the project.
SELECT DISTINCT e.Name, p.Name, p.DDate
FROM Employees AS e, EmpTeams AS et, Teams AS t, Projects AS p
WHERE e.EmpId=et.EmpId AND et.TeamId=t.TeamId AND p.PrjId=t.PrjId
If the SELECT is not DISTINCT the resulting table (view) will include identical set of
rows for an employee participating in different teams for same project.
Strings in the WHERE clause can be compared with the use of the comparison operators (=, <,
>, <=, >= and <>) and also the LIKE operator that provides the capability to compare strings
on the basis of pattern match. The expression is of the form:
S LIKE P
Where S is the string or the column name to be compared and p is the pattern constructed from
two special characters:
- _ : refers to a match to any one character in S, and
- % : refers to zero or more character sequences match in S.
String constants in SQL are enclosed by a single apostrophe. If the string consists of an
apostrophe a escape sequence with an apostrophe is used (i.e. two single apostrophes are used to
refer to a single apostrophe in a string constant).
The LIKE expression can also be used with the NOT operation as follows
S NOT LIKE P
Example:
A query to retrieve employees with a name starting by the letter ‘A’.
SELECT *
FROM Employees
WHERE Name=’A%’
UPDATE
The UPDATE statement changes the existing data in a table. The syntax for the UPDATE
command is:
UPDATE <table_name>| <view_name>
SET <column_name> = <value> [,..n]
WHERE <condtion>
- <value> is new value to be assigned to the column <column_name>
- The WHERE clause specifies the <condition> for selecting the rows to be modified. If the
WHERE clause is not included the update will be done for all existing rows in the table
Example
UPDATE Teams
SET Description = 'Programmers team for project2’
WHERE TeamId = 11
DELETE
The DELETE statement removes row(s) from a table. The syntax for the DELETE command
is:
DELETE FROM <table_name>| <view_name>
WHERE <condtion>
- The WHERE clause specifies the <condition> for selecting the rows to be deleted. If the
WHERE clause is not included the all existing rows in the table will be deleted unless
there is a constraint that protects the deletion of the rows.
Example
DELETE Teams
WHERE TeamId=2
Write a query to retrieve employees name and phone that are participating on projects
that are owned by the customer ‘XYZ’.
SELECT Name, Phone
FROM Employees
WHERE EmpId IN (SELECT EmpId