0% found this document useful (0 votes)
97 views13 pages

CH - 5 Fundamentals of A Database System

This document discusses Structured Query Language (SQL), including its introduction, data definition language, data manipulation language, schema definition, and creating/modifying tables and indexes. SQL is a standardized query language used with most commercial relational database management systems to define, manipulate, and retrieve data from those databases.

Uploaded by

blackhat0917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views13 pages

CH - 5 Fundamentals of A Database System

This document discusses Structured Query Language (SQL), including its introduction, data definition language, data manipulation language, schema definition, and creating/modifying tables and indexes. SQL is a standardized query language used with most commercial relational database management systems to define, manipulate, and retrieve data from those databases.

Uploaded by

blackhat0917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

5 .

Str uctured Q uer y Language ( SQ L)

 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)

SQL Data Definition Language (DDL)


The Data Definition Language (DDL) part of SQL permits database tables to be created or
deleted. It can also define indexes (keys), specify links between tables, and impose constraints
between database tables.
The most important DDL statements in SQL are:
- CREATE TABLE - creates a new database table.
- ALTER TABLE - alters (changes) a database table.
- DROP TABLE - deletes a database table.
- CREATE INDEX - creates an index (search key).
- DROP INDEX - deletes an index.
The DDL statements are used for a schema definition of a relational database.

SQL Data Manipulation Language (DML)


The Data Manipulation Language (DML) is part of the SQL syntax for executing queries to
insert, retrieve, update, and delete records. The statements are;
- INSERT INTO - inserts new data into a database table.
- SELECT - extracts data from a database table.

Department of CSIT | WSU


EENG 477- Database Systems 2
5. Structured Query Language (SQL)

- UPDATE - updates data in a database table.


- DELETE - deletes data from a database table.
The four most common commands are also known as SQL CRUD statements after the words
Create, Read, Update and Delete data.

 Schema D efinition in SQL


SQL uses the following terms for the corresponding terms in relational model
- Table – Relation
- Column – Attribute
- Row – Tuple

 Schema Creation and Modification


The CREATE SCHEMA command in the SQL statement is used to group database objects such
as tables, views and permissions.
The syntax for the command is:
CREATE SCHEMA <schema_name> AUTHORIZATION <owner>
<schema_name> is the name of the schema and <owner> identifies the user who is the owner of
the schema.
Example:
- CREATE SCHEMA swprjct AUTHORIZATION dbo
SQL statements that can be included as part of the CREATE SCHEMA statement are:
 CREATE TABLE statement
 CREATE VIEW statement
 GRANT statement
 CREATE INDEX statement (not supported in Microsoft SQL Server 2000)
While CREATE SCHEMA command groups database objects the CREATE DATABASE
command in the SQL statement is used to create a new database and the corresponding files for
storing the database.
The syntax for the command is:
CREATE DATABASE <database_name>
Example:
- CREATE DATABASE SWPRJCT
<database_name> is the name of the new database.

Department of CSIT | WSU


EENG 477- Database Systems 3
5. Structured Query Language (SQL)

The command also has different optional parameters in different RDBMS that helps in specifying
owner, file, growth, …

 Table Creation and Modification


The CREATE TABLE command in the SQL statement is used to specify a new relation in a
database by giving it a name and listing its attributes.
The syntax for the command is:
CREATE TABLE <table_name> (
<column_name> <data_type> {column_constraint},
:
<column_name> <data_type> {column_constraint}
)
- <column_name> is the name of the column.
- <data_type> is the SQL supported data types: CHAR(n), VARCHAR(n), INT,
SMALLINT, DECIMAL(i,j), DATE, TIME (DATETIME), …
- {column_constraint} is optional constraints on the column such as NULL, NOT NULL,
PRIMARY KEY, FOREIGN KEY, UNIQUE, DEFAULT, …
Example:
For the PROJECTS and TEAMS relations the corresponding tables can be defined as:
- Projects (PrjId:integer, Name:string, SDate:date, DDate:date, CDate:date)
- Teams (PrjId:integer, Name:string, Descr:string)

CREATE TABLE Projects (


PrjId INT NOT NULL PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
SDate DATE NOT NULL,
DDate DATE NULL,
CDate DATE NULL

CREATE TABLE Teams (


PrjId INT NOT NULL FOREIGN KEY REFERENCES Projects(PrjId),
Name VARCHAR(30) NOT NULL,
Description VARCHAR(100) NULL,
PRIMARY KEY (PrjId, Name)

)
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:

ALTER TABLE Projects ALTER COLUMN PrjId SMALLINT

ALTER TABLE Teams ALTER COLUMN PrjId SMALLINT

Example 2:

CREATE TABLE Projects (


PrjId SMALLINT NOT NULL,
Name VARCHAR(30) NOT NULL,
SDate DATE NOT NULL,

Department of CSIT | WSU


EENG 477- Database Systems 5
5. Structured Query Language (SQL)

DDate DATE NULL,


CDate DATE NULL

CREATE TABLE Teams (


TeamId SMALLINT NOT NULL,
PrjId SMALLINT NOT NULL,
Name VARCHAR(30) NOT NULL,
Description VARCHAR(100) NULL,

ALTER TABLE Projects ADD


CONSTRAINT [PK_Projects] PRIMARY KEY CLUSTERED (PrjId)

ALTER TABLE Projects ADD Description VARCAHR(200)

ALTER TABLE Teams ADD


CONSTRAINT [PK_Teams] PRIMARY KEY CLUSTERED (TeamId)

ALTER TABLE Teams ADD


CONSTRAINT [FK_Teams_Projects] FOREIGN KEY (PrjId)
REFERENCES Projects (PrjId)

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

 Index Creation and Modification


Indexes are the heart of fast data access. In fact, as the database grows, indexes are the guarantee
to fast data access. Data access can be fast without indexes, but only if the table is small. If the
table contains thousands or millions of rows, data access has to be done through indexes. Indices
in a book, helps to find information about a specific subject without having to read the entire
book. The same applies to a database index; it helps to find information about a specific row or
rows without having to search through the entire table.
An index for a table is managed by an external table which consists of the search key (index
attribute) and a pointer to the location of the data as columns.

Department of CSIT | WSU


EENG 477- Database Systems 6
5. Structured Query Language (SQL)

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)

Clustered versus Nonclustered Index


For a clustered index the data is both stored and sorted on the index key; whereas, for a
nonclustered index the actual data is not stored in the index.
The default index is always nonclustered. One can create a clustered index by specifying it, as in
the following example:
Example:
CREATE CLUSTERED INDEX PrjId ON Projects(PrjId)

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

 Simple Query Construc ts and S yntax


The simplest DML query in the SQL statement is the SELECT-FROM-WHERE statement
used for retrieving information from a database. The SQL DML also supports data insertion,
modification and deletion through the INSERT INTO, UPDATE and DELETE statements.

 The SELECT-FROM-WHERE Statement


The syntax for the SELECT-FROM-WHERE statement which consists of three clauses
SELECT, FROM and WHERE as shown below:
SELECT <column_list>
FROM <table_list>
WHERE <condition>

Department of CSIT | WSU


EENG 477- Database Systems 7
5. Structured Query Language (SQL)

- <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

To retrieve all the columns from the team table:


SELECT Projects.Name, Teams.*
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

Department of CSIT | WSU


EENG 477- Database Systems 8
5. Structured Query Language (SQL)

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%’

Department of CSIT | WSU


EENG 477- Database Systems 9
5. Structured Query Language (SQL)

 INSERT, UPDATE and DELETE



INSERT
The INSERT statement adds one or more new rows to a table. In a simplified treatment,
INSERT has this form:
INSERT INTO <table_name>| <view_name> [(column_list)] data_values
- data_values are one or more rows to be inserted into the named table or view.
- column_list is a list of column names, separated by commas, that can be used to specify the
columns for which data is supplied.
If column_list is not specified, all the columns in the table or view receive data. When a
column_list does not name all the columns in a table or view, a value of NULL (or the default
value if a default is defined for the column) is inserted into any column not named in the list. All
columns not specified in the column list must either allow null values or have a default assigned.
The data values supplied must match the column list. The number of data values must be the
same as the number of columns, and the data type, precision, and scale of each data value must
match those of the corresponding column.
There are two ways to specify the data values:
 VALUES (<value_or_expression> [,..n])
 SELECT <subquery>

The VALUES statement inserts a single row with the column values <value_or_expression> in
the columns listed in the INSERT INTO column list. The SELECT subquery is a standard
query that results a temporary table and the resulting rows in the table are inserted to the table
in the INSERT INTO clause. The columns in the subquery need to much the columns in the
columns list.
Example
INSERT INTO Projects(PrjId, Name, SDate)
VALUES (1, 'Test Project', '05-25-2006')

INSERT INTO Teams


VALUES (1, 1, 'Programmers Team 1', Programmers team for project 2.')

INSERT INTO Teams(TeamId, PrjId, Name)


SELECT TeamId+10, 2, Name FROM Teams WHERE PrjId=1

Department of CSIT | WSU


EENG 477- Database Systems 10
5. Structured Query Language (SQL)

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

 Nested Su bqu eries and Complex Queries


The SELECT-FROM-WHERE statement discussed so far is the simplest SQL statement for
querying a database. SQL SELECT statements can be combined together to form Subqueries.
Subqueries in a SQL statement are complete form of SELECT-FROM-WHERE statements
that are contained in one query.

Department of CSIT | WSU


EENG 477- Database Systems 11
5. Structured Query Language (SQL)

They can be used in different ways:


 Subqueries in the WHERE clause to form nested queries,
 Subqueries in set operations such as UNION, EXCEPT, …, and
 Subqueries in the FROM clause as constant tables

 Nested Queries
SQL SELECT statements can be contained in the WHERE clause of another SQL statement to
form Nested queries. The SELECT statement that contains the nested query is said to be the
outer query. Subqueries in a nested SQL statement can produce scalar value (constant) or table.
Subqueries resulting scalar value can be used in comparison expression of the WHERE clause
similar to constant or column value comparisons. For subqueries that result table special
operators are used in the test expression such as the operator IN that is used to test the existence
of a scalar value in the resulting table.
Example:
Considering the following relations,
- Employees(EmpId, Name, BDate, SubCity, Kebele, Phone, Salary)
- Teams(TeamId, PrjId, Name, Descr)
- EmpTeams(EmpId, TeamId)
- Projects(PrjId, Name, SDate, DDate, CDate, CustId)
- Customers(CustId, Name, Address)
Write a query to retrieve all the projects that are owned by the customer ‘XYZ’. (Assume
name of a customer is unique)
SELECT Name, SDate, DDate, CDate
FROM Projects
WHERE CustId = (SELECT CustId
FROM Customers
WHERE Name=’XYZ’)

Alternative for the above query is;


SELECT p.Name, SDate, DDate, CDate
FROM Projects AS p, Customers AS c
WHERE p.CustId = c.CustId AND c.Name=’XYZ’

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

Department of CSIT | WSU


EENG 477- Database Systems 12
5. Structured Query Language (SQL)

FROM EmpTeams AS et, Teams AS t


WHERE et.TeamId= t.TeamId AND
PrjId IN (SELECT PrjId
FROM Projects AS p, Customers AS c
WHERE p.CustId=c.CustId
AND c.Name=’XYZ’))

 Embedd ed and Dynamic SQL


The idea of the embedded SQL arose long before the SQL procedural extensions were developed.
It was introduced by IBM in the beginning of the 1980s and then implemented by many other
SQL vendors. The dynamic SQL was the logical continuation of the embedded SQL principles
that alleviated some limitations and inconveniences of the latter.
Embedded SQL is placed inside a host program, not built on the fly. The actual SQL code is
embedded into the application code and converted to the code by a preprocessor and then
compiled normally. The preprocessor converts these statements into API calls appropriate for
the host language.
Static SQL is simply native SQL code that is handled normally. It doesn't change at runtime and
it is a constant string literal. Since SQL has to exist in a host program to talk to the outside
world, it will be embedded somewhere. Or it can be part of a trigger, stored procedure, etc.
Dynamic SQL is made up on the fly by a procedure or end user as a string containing SQL
statements that may change at runtime. For instance the where clause on a SQL statement may
be dependent on factors not known at compile time. The ANSI Standards have PREPARE and
EXECUTE statements for this. Microsoft and other vendors will do it differently, but it is the
same idea. Like having Query Analyzer in your program -- and just as dangerous.
The difference between static and dynamic SQL has to do with when the plan for database access
is determined. With static SQL the plan is determined before your program ever runs (or at least
could be). This means that the database doesn't have to figure out how to find the data you are
interested in at runtime. It also means that if the database statistics change radically the plan
used by your query may become out of date.
The plan used to execute dynamic SQL statements is determined at runtime. This means that
knowledge only available at runtime may be used to form the SQL statement. It also means that
the plan will be up to date with the current database statistics. Unfortunately the database will
have to do extra work at runtime to determine what the plan should be.
Department of CSIT | WSU

You might also like