Topic 5 Introduction To DDL
Topic 5 Introduction To DDL
2
Introduction to SQL
• SQL functions fit into two broad categories:
• Data definition language
• SQL includes commands to:
• Create database objects, such as tables,
indexes, and views
• Define access rights to those database objects
• Data manipulation language
• Includes commands to insert, update, delete, and
retrieve data within database tables
3
Introduction to SQL
(continued)
SQL is relatively easy to learn
Nonprocedural language
4
Introduction to SQL (continued)
5
Introduction to SQL (continued)
6
Introduction to SQL (continued)
7
Creating the Database
Create database structure RDBMS creates physical files that will hold database
Create tables that will hold end-user data Tends to differ substantially from one RDBMS to
another
8
The Database Schema
• Authentication
• Process through which DBMS verifies that only
registered users are able to access database
• Log on to RDBMS using user ID and password created
by database administrator
• Schema
• Group of database objects—such as tables and indexes
—that are related to each other
9
ISO Data types
10
Data Definition
11
Data Definition
12
Creating Table Structures
UNIQUE specification
13
Creating Table Structures (continued)
14
Data Definition Commands
15
Tables Explained
16
SQL Indexes
Index based on
two or more
attributes
Often used to
prevent data
duplication
17
Table name Attribute names
Tables in SQL
Product
Tuples or rows 18
CREATE DataBase
19
CREATE TABLE
20
CREATE TABLE Product (
PName varchar(25),
Price double,
Category varchar(25),
Manufacturer varchar(30)
PName Price ); Category Manufacturer
21
Insert DATA
22
Insert Into Product (PName, Price, Category, Manufacturer)
Values (‘Gizmo’, 19.99, ‘Gadgets’, ‘GizmoWorks’),
(‘Powergizmo’, 29.99,’Gadgets’, ‘GizmoWorks’),
(‘SingleTouch’, 149.99, ‘Photography, ‘Canon’),
(‘MultiTouch’, 203.99, ‘Household’, ‘Hitachi’);
23
PName Price Category Manufacturer
25
Integrity Enhancement Features
Entity integrity
Primary Key
• Referential integrity
Secondary key
• Domain constraints
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
PName Price Category Manufacturer
Foreign
Gizmo £19.99 Gadgets GizmoWorks
key
Powergizmo £29.99 Gadgets GizmoWorks
SingleTouch £149.99 Photography Canon
MultiTouch £203.99 Household Hitachi
27
Primary Key Constraint
UNIQUE
Not NULL
28
Foreign Key Constraint
CREATE TABLE Product (
PName varchar(25) NOT NULL UNIQUE PRIMARY KEY
Price double,
Category varchar(25) NOT NUL,
Manufacturer varchar(30),
FOREIGN KEY (Manufacturer) REFERENCES Company(CName)
);
Current Table
Manufacture Parent Table
PName Price Category
r StockPri
CName Country
Gizmo £19.99 Gadgets GizmoWorks ce
Powergizmo £29.99 Gadgets GizmoWorks Gizmo
25 USA
SingleTouc Works
£149.99 Photography Canon
h Canon 65 Japan
29
MultiTouch £203.99 Household Hitachi Hitachi 15 Japan
SQL Constraints
30
Check Constraint
31
Default Constraint
32
Indexing
• The CREATE INDEX statement is used to create indexes in
tables.
• Indexes are used to retrieve data from the database quickly.
The users cannot see the indexes, they are just used to speed
up searches/queries
33
SELECT
SELECT <attributes>
<attributes>
Basic form: (plus FROM
FROM <one <one or
or more
more
many many more relations>
relations>
bells and whistles) WHERE
WHERE <conditions>
<conditions>
SQL Query
34
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo £19.99 Gadgets GizmoWorks
Powergizmo £29.99 Gadgets GizmoWorks
SingleTouch £149.99 Photography Canon
MultiTouch £203.99 Household Hitachi
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE category=‘Gadgets’
category=‘Gadgets’
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
36
Selective view
Input Schema
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
Output Schema 37
Oracle Apex SQL environment
https://apex.oracle.com/pls/apex/f?p=4550:1:10356567
2582544
:::::
Oracle Apex SQL environment
https://apex.oracle.com/pls/apex/f?p=4550:1:10356567
2582544
:::::
Oracle Apex SQL environment
41
4
2
Questions ?