Sei sulla pagina 1di 31

Database Development, Management & Administration

Course Lecturer: Denis Ssebuggwawo, PhD.

Course Tutor: Ms L. Tamale


1

If your application is to store data in a DB, you must choose a

Structured Query Language (SQL) Introduction

SQL is widely used as a relational database language, designated by ANSI and ISO as a standard Created by IBM SQL is a complete database language. It is used for Defining relational database Creating views and specifying queries In addition, it allows for rows to be inserted, updated and deleted

Structured Query Language Introduction


In DB terminology, SQL is both a Data Definition Language (DDL) and a Data Manipulation Language (DML) SQL is not a complete programming language like: Pascal, Delphi, C, C++, Java, COBOL, Python, Perl, etc.
3

Structured Query Language Introduction


SQL statements can be embedded in general purpose programming languages (3GLs or 4GLs) The embedded SQL statements handle the database processing The statements in the general purpose language perform the necessary tasks to complete the application.

Structured Query Language (SQL) Base table, Views and Keys


The DDL part of SQL encompasses statements to operate on tables, views and indexes A base table is an autonomous named table. It is autonomous because it exists in its own right; it is physically stored within the database. It has a name by which it can be referencedspecified in the CREATE Statement
5

Structured Query Language (SQL) Base table, Views and Keys


Short-lived temporary tables such as those formed as a result of a query are not named. A view is not autonomous because it is derived from one or more base tables and does not exist independently. A key is one or more columns identified as such in the description of the table, index, or a referential constraint.

Structured Query Language (SQL)


Primary, Foreign and Composite Keys
The same column can be part of more than one key e.g. primary key or foreign key A composite key is an ordered set of columns of the same table e.g., primary key of LINEITEM is always the composite of (SALENO, LINENO) in that order
7

Instances of Branch and Staff (part) Relations

Structured Query Language (SQL)


Primary, Foreign and Composite Keys
The application of the referential integrity rule: the value of the foreign key must be equal to the value of the primary key,
means each component of the foreign key must equal each component of a composite primary key.
8

Structured Query Language (SQL) Unique Keys and Indexes


A unique key is another type of key. Its purpose is to ensure that no two values of a key are equal This constraint is enforced by the DBMS during the execution of INSERT and UPDATE statements A unique key is part of the index mechanism
9

An index is an ordered set of pointers to rows of the base table.

Structured Query Language (SQL) Unique Keys and Indexes

Indexes are used to accelerate data access and ensure uniqueness. An index may be thought of as a table that contains two columns
10

Structured Query Language (SQL) Unique Keys and Indexes


An index may be thought of as a table that contains two columns: the first column contains values of the index key, and

the second column contains a list of pointers or addresses of rows in the table
11

Structured Query Language (SQL)


Index Example
ITEMTYPE INDEX ITEMTYPE ITEM ITEMNO 1 2 3 4 5 6 ITEMNAME Pocket knife-Nile Pocket knifeThames Compass Geo Positioning Sys Map Measure Hat-polar explorer
ITEMTYPE
ITEMCOLOR

C
C

E E N N N C

Brown Brown Red

C
C

E
E

F
N

7
8 9 10

Hat polar explorer


Boots-snakeproof Boots-snakeproof Safari hat

C
C C F

White
green Black khaki
12

N
N

Structured Query Language (SQL)


Data Definition and Manipulation
Data definition involves: Definition of tables, views and indexes Data manipulation includes: Specifying queries Maintaining a database INSERT UPDATE DELETE

13

Structured Query Language Data Definition

SQL Data Types

14

Structured Query Language (SQL)


Data Types

14

Structured Query Language (SQL)


Data Types
BOOLEAN (true or false, Yes or No) INTEGER (full word- +ve and ve whole numbers) 31 binary digits SMALLINT (half-word) 15 binary digits FLOAT (single or double precision floating point numbers) Scientific work DECIMAL (decimal number system) Commercial applications CHAR and VARCHAR (CHAR-fixed length, VARCHAR variable length) Character strings DATE (format: yyyymmdd; CURRENT DATE etc.), TIME (format: hhmmss, ;CURRENT TIME etc., ), TIMESTAMP, and INTERVAL BLOB (spreadsheet, graph, fax, satellite image, voice pattern or digitalized data) CLOB (reports, correspondence, chapters of a manual, and contracts) 13 GRAPHIC and VARGRAPHIC (storing e.g., Japanese or Chinese symbols)

Structured Query Language (SQL)


Creating Database: CREATE DATABASE CREATE is used to create a new database, table, view or an index either interactively or by embedding the statement in a host language Format: Creating Database
CREATE DATABASE database-name
TYPE = database-type;
Example: CREATE DATABASE CUUDBGROUP ;
18

Structured Query Language (SQL)


Creating Tables: CREATE TABLE Format: Creating Tables
CREATE TABLE base-table-name
column-definitionblock [primary-key-block]

[referential-constraint-block] [unique-block]

18

Structured Query Language (SQL) Column Definition


The column definition block in the table defines the columns in the table. Each column definition consists of a column name, data type, and optionally the specification that the column cannot contain null values.
19

Structured Query Language (SQL) Column Definition


(Column definition [,.]) where column definition is of the form

Column-name data-type [NOT NULL]


Example: stkcode CHAR(3) NOT NULL

The not null clause specifies that the particular column must have a value whenever a row is inserted.

NOT NULL should be defined for a column that is a primary key or a component of a primary key.

19

Structured Query Language (SQL) Primary Key Definition


The column definition block specifies a set of column values comprising the primary key. Once the primary key is defined the system enforces its uniqueness by checking that the primary key of a new row does not already exist in the table
21

Structured Query Language (SQL) Primary Key Definition


Format:
PRIMARY KEY (Column-name [ASC/DESC][,])

Example: PRIMARY KEY (stkcode) ASC

A table can have only one primary key, which may be a single key or a composite key
22

Structured Query Language (SQL) Primary Key Definition


The optional ASC or DESC clause specifies whether the values from this key are arranged in ascending or descending order NB:

While it is not mandatory to define a primary key, it is good practice to always define a primary key for a table,
23

Structured Query Language (SQL) Referential Constraint Definition-1 The referential constraint block defines a foreign key.
It consists of one or more columns in the table that together must match a primary key (or else be null) of the specified table. A foreign key value is null when any one of the columns in the row comprising the foreign key is null
24

Structured Query Language (SQL) Referential Constraint Definition-1 Format:


FOREIGN KEY constraint-name (Column-name[,]) REFERENCES table-name ON DELETE [RESTRICT/CASCADE/SET NULL]

Example: FOREIGN KEY fknation (natcode) REFERENCES Nation


ON DELETE RESTRICT NB To enforce this in MySQL, you may need to change the type of the table from ISAM to INNODB by issuing a command similar to the following:

ALTER TABLE Nation TYPE = INNODB;

Once the foreign key is defined, the DBMS will check every insert and update to ensure that the constraint is observed
25

Structured Query Language (SQL)


Referential Constraint Definition-2
The constraint-name definition supports naming a referential constraint. You cannot use a constraint-name more than once in the same table.

Column-name identifies the column or columns that comprise the foreign key. The data type and length of the foreign key must match exactly the data type and length of the primary key columns The clause REFERENCES table-name specifies the name of the existing table that contains the primary key, which cannot be the name of the table being created.
26

Structured Query Language (SQL)


Referential Constraint Definition-2
The ON DELETE clause defines the action taken when a row is deleted from the other table containing the primary key. There are three options: 1. RESTRICT- prevents the deletion of the primary key row untill all corresponding rows in the related table, the one containing the foreign key, have been deleted. RESTRICT is the default for this clause and also the cautious approach for preserving data integrity 2. CASCADE- causes all corresponding rows in the related table also to be deleted. 3. SET NULL sets the foreign key to null for all corresponding 19 rows in the related table.

Structured Query Language (SQL)

Unique Definition
The unique definition block creates a unique index for the specified column or columns A unique key is constrained so that no two of its values are equal Columns appearing in the unique constraint must be defined as NOT NULL Format:
UNIQUE constraint-name (Column-name [ASC/DESC][,])
Example: UNIQUE unqnatcode (natcode) DESC or UNIQUE (natcode)
28

Structured Query Language (SQL)


Unique Definition

The constraint is enforced by the DBMS during execution of the INSERT and UPDATE statements. The column-name clause identifies the column or columns that comprise the unique key. These columns should not be the same as those of the tables primary key definition.
The ASC/DESC option specify whether the columns of the unique key are arranged in ascending or descending order, respectively.

Structured Query Language (SQL)


Creating Tables - Example Format: Creating tables
Example: CREATE TABLE Stock
(stkcode CHAR(3) NOT NULL, descript VARCHAR( 30),

uc INTEGER (15),
stkfirm CHAR(20), stkprice DECIMAL (6,2), stkqty DECIMAL (8), stkdiv DECIMAL (5,2), stkpe DECIMAL(5), natcode CHAR(3), arrdateE DATE,

PRIMARY KEY (stkcode),


FOREIGN KEY fknation (natcode) REFERENCES Nation, ON DELETE RESTRICT) UNIQUE (natcode); 30

Potrebbero piacerti anche