Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Topic 5

Database Introduction to SQL

Systems •(The content presented in this session has adopted from


the module recommended reading –
Arish Siddiqui •Connolly T, M. and Begg C. E. Database Systems: A
Practical Approach to Design, Implementation and
Management, 6/E)
Objectives
• Explore basic commands and functions of SQL
• How to use SQL for data administration (to create tables,
indexes, and views)
• How to use SQL for data manipulation (to add, modify,
delete, and retrieve data)
• How to use SQL to query a database to extract useful
information

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

Basic command set has vocabulary of less than 100 words

Nonprocedural language

American National Standards Institute (ANSI) prescribes a


standard SQL
Several SQL dialects exist

4
Introduction to SQL (continued)

5
Introduction to SQL (continued)

6
Introduction to SQL (continued)

7
Creating the Database

Following two tasks must be completed: First task:

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

• SQL DDL allows database objects such as schemas, domains, tables,


views, and indexes to be created and destroyed.
• Main SQL DDL statements are:

• CREATE SCHEMA DROP SCHEMA


• CREATE/ALTER DOMAIN DROP DOMAIN
• CREATE/ALTER TABLE DROP TABLE
• CREATE VIEW DROP VIEW

• Many DBMSs also provide:


• CREATE INDEX DROP INDEX

11
Data Definition

• Relations and other database objects exist in an


environment.
• Each environment contains one or more catalogs, and each
catalog consists of set of schemas.
• Schema is named collection of related database objects.
• Objects in a schema can be tables, views, domains,
assertions, collations, translations, etc (having SAME
owner)

12
Creating Table Structures

Use one line per column (attribute) definition

Use spaces to line up attribute characteristics and constraints

Table and attribute names are capitalized

NOT NULL specification

UNIQUE specification
13
Creating Table Structures (continued)

Primary key attributes contain both a NOT NULL and a


UNIQUE specification

RDBMS will automatically enforce referential integrity for


foreign keys

Command sequence ends with semicolon

14
Data Definition Commands

Examine simple database model and database Understand data environment


tables that will form basis for many SQL
examples

15
Tables Explained

• The schema of a table is the table name and its attributes:


Product(PName, Price, Category, Manfacturer)

• A key is an attribute whose values are unique;


we underline a key

Product(PName, Price, Category, Manfacturer)

16
SQL Indexes

When primary key is Using CREATE INDEX


declared, DBMS Often need additional command, SQL indexes
Composite index
automatically creates indexes can be created on basis
unique index of any selected attribute

Index based on
two or more
attributes

Often used to
prevent data
duplication

17
Table name Attribute names
Tables in SQL
Product

PName Price Category Manufacturer

Gizmo £19.99 Gadgets GizmoWorks

Powergizmo £29.99 Gadgets GizmoWorks


We will be using SQL to create this Table
SingleTouch £149.99 Photography Canon
from scratch
MultiTouch £203.99 Household Hitachi

Tuples or rows 18
CREATE DataBase

CREATE DATABASE databasename;

CREATE DATABASE ProductDB;

19
CREATE TABLE

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

20
CREATE TABLE Product (
PName varchar(25),
Price double,
Category varchar(25),
Manufacturer varchar(30)
PName Price ); Category Manufacturer

21
Insert DATA

INSERT INTO table_name (column1, column2, column3


, ...)

VALUES (value1, value2, value3, ...);

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’);

PName Price Category Manufacturer


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

Gizmo £19.99 Gadgets GizmoWorks

Powergizmo Insert into Product


£29.99 Gadgets GizmoWorks
Values ( ‘New’,
SingleTouch £149.99 Photography Canon
99.99,
MultiTouch £203.99 ‘Household’,
Household Hitachi
New 99.99£ ‘Canon’
Household Canon
);
24
SQL Constraints

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

25
Integrity Enhancement Features

Entity integrity

Primary Key
• Referential integrity

Secondary key
• Domain constraints

Set of all unique values permitted for that attribute


• General constraints

e.g. not null


26
Keys and Foreign Keys
Company
CName StockPrice Country

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

CREATE TABLE Product (


PName varchar(25) NOT NULL
UNIQUE PRIMARY KEY,
Price double,
Category varchar(25) NOT NUL,
Manufacturer varchar(30)
);

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

• NOT NULL - Ensures that a column cannot have a NULL value


• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row
in a table
• FOREIGN KEY - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column when no value is specified
• INDEX - Used to create and retrieve data from the database very quickly

30
Check Constraint

CREATE TABLE Product (


PName varchar(25) NOT NULL
UNIQAUE PRIMARY KEY,
Price double CHECK (Price>1),
Category varchar(25) NOT NUL,
Manufacturer varchar(30)
);

31
Default Constraint

CREATE TABLE Product (


PName varchar(25) NOT NULL
UNIQAUE PRIMARY KEY,
Price double CHECK (Price>1),
Category varchar(25) NOT NUL,
Manufacturer varchar(30) DEFAULT ‘canon’
);

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

CREATE Index myIndex on Product (


PName varchar(25),
Price double,
Category varchar(25),
Manufacturer varchar(30)
);

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’

PName Price Category Manufacturer


Gizmo £19.99 Gadgets GizmoWorks
Powergizmo £29.99 Gadgets GizmoWorks
“selection”
35
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 PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100

PName Price Manufacturer


“selection” and SingleTouch £149.99 Canon
“projection” MultiTouch £203.99 Hitachi

36
Selective view
Input Schema

Product(PName, Price, Category, Manfacturer)

SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100

Answer(PName, Price, Manfacturer)

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

CREATE TABLE EMP


(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2));
Conclusion

Introduction to Data Definition Data SQL DDL


SQL Language. Manupilation Commands

41
4
2

Questions ?

You might also like