CSE 303 Lec 2 SQLIntro
CSE 303 Lec 2 SQLIntro
CSE 215
Lecture 02
2022
Structure Query Language (SQL)
CSE303:Ashikur Rahman 1
Today’s Outline (mainly from chapter 2)
Data in SQL
CSE303:Ashikur Rahman 3
Relational databases
CSE303:Ashikur Rahman 4
Non-relational databases
CSE303:Ashikur Rahman 5
SQL Introduction
• Standard language for querying and manipulating data
CSE303:Ashikur Rahman 6
Chronology
•1970s IBM - first relational database System R, then DB2.
Others include:
Ingres Database - query language QUEL
Digital - Relational Database Operator
ISBL - relational algebra DML
dBase family of products for PCs
•1979 Oracle
• 1980 First standardisation efforts.
•1984 ISO SQL standard - many flaws but universally adopted.
•1992 Update to standard called SQL92 - The basic standard for
any modern database
•1999 Update to standard called SQL99 - Oracle database
conforms to SQL99
•2003 Current standard SQL2003 - Not many databases fully
support this standard yet.
CSE303:Ashikur Rahman 7
FOUR major operations of any
RDBMS
CSE303:Ashikur Rahman 8
SQL
• Data Definition Language (DDL)
– Create/alter/delete tables and their attributes
– Today’s lecture...
• Data Manipulation Language (DML)
– Insert/delete/modify tuples in tables
– Query one or more tables
– Following lectures
CSE303:Ashikur Rahman 9
Table name Attribute names
Tables in SQL
Product
CSE303:Ashikur Rahman 11
Tables Explained
• A tuple = a record/ a row in the table
– Example:
(SingleTouch, 149.99, Photography, Canon)
• A table = a set of tuples
– Like a list…
– …but it is unordered:
no first(), no next(), no last().
CSE303:Ashikur Rahman 12
Domains
• Data types of attributes
– Restriction: all attributes are of atomic type
• that is elementary type i.e., integer, string
• Each attribute has an associated domain
– i.e. the domain of Pname is string
– The domain of price is real numbers
CSE303:Ashikur Rahman 13
Creating Tables: SQL command
CREATE TABLE Student (
name CHAR(50),
id NUMBER(10),
gender CHAR(1),
address CHAR(100),
gpa FLOAT
);
CSE303:Ashikur Rahman 14
Common data types
• Characters:
Syntax: CHAR[(maximum_size)]
– Examples `kamal’
• CHAR(40)
– name CHAR(40)
• CHAR
– gender CHAR
– Padded with blank at right
CSE303:Ashikur Rahman 15
Common data types
• VARCHAR:
Syntax: VARCHAR(maximum_size)
– Variable length character string
– Example
• VARCHAR(40)
– address VARCHAR(100)
• VARCHAR not permitted
– Memory is allocated based on exact length of
string.
CSE303:Ashikur Rahman 16
Common data types
• CHAR vs. VARCHAR:
– VARCHAR: no wastage of memory
– CHAR: faster processing
– Which one to use when?
• If you want to store password?
• If you want to store last name/first name etc...?
CSE303:Ashikur Rahman 17
Common data types
• For numbers (like C)
– INT or INTEGER
– REAL
– FLOAT
CSE303:Ashikur Rahman 18
Common data types
• Fixed point decimal numbers:
– DECIMAL[(precision , scale)] in SQL
– NUMBER[(precision , scale)] in ORACLE
• precision: how many digits in total
• scale: how many digits after/before decimal
• In SQL Decimal(p,s)
CSE303:Ashikur Rahman 19
Example
When you insert 7,456,123.89
(In this number 9 digits in total, 2 decimal digits)
Data Type Value stored
- Number(9) 7456124
- Number(9,1) 7456123.9
- Number(9,2) 7456123.89
- Number(7,-2) 7456100
- Number(6) Rejected
Standard SQL
YYYY-MM-DD
Example:
Date ‘1994-10-21’
CSE303:Ashikur Rahman 21
Common data types
• Oracle provides a function to_date for creating
an object of Date data type:
General syntax
TO_DATE(<string>, '<format>')
Example:
TO_DATE(‘1994-10-21’,’YYYY –MM-DD’)
TO_DATE(‘1994-OCT-21’,’YYYY –MON-DD’)
TO_DATE(‘1994-OCT-21’,’YYYY –MON-DD’)
CSE303:Ashikur Rahman 22
Common data types
• Oracle’s default date format is DD-MON-YY
– If you use this format you don’t have to use
to_date function.
CSE303:Ashikur Rahman 23
Common data types
• For outputing date you can use to_char
function.
• The general usage of TO_CHAR is:
TO_CHAR(<date>, '<format>')
where the <format> string can be formed from over 40
options. Some of the more popular ones are on the next
slide
CSE303:Ashikur Rahman 24
Common data types
CSE303:Ashikur Rahman 25
Common data types
• Two dates can be compared using = <> > etc.
• Two dates can only be subtracted NO addition, multiplication
or division.
CSE303:Ashikur Rahman 26
NULL data
CSE303:Ashikur Rahman 27
NULL data
CSE303:Ashikur Rahman 29
Creating table
CSE303:Ashikur Rahman 30
Creating table
CSE303:Ashikur Rahman 31
Case doesn’t matter
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’
• Constants:
– ‘abc’ - yes
– “abc” - no
CSE303:Ashikur Rahman 32
CREATE TABLE Product (
Convention to follow maker VARCHAR(10),
model NUMBER(10),
type VARCHAR(10),
);
• SQL Commands: all caps
• Example: CREATE TABLE
• Table name : Start with upper case , any
subsequent word upper case
• Example: Product, MovieStar
• Attributes: Start with lower case , any subsequent
word upper case or separate by underscore.
• Example: maker, producer, certNo (or cert_no)
• Data types
• Example: CHAR, VARCHAR
CSE303:Ashikur Rahman 33
Activity 1: Create student database
CSE303:Ashikur Rahman 34
Keys
Key = minimal set of attributes acting as a unique
tuple identifier.
– Consider the universe of all potential relation data,
not just what the table currently contains.
p_name price manufacturer
CSE303:Ashikur Rahman 35
What is the key here?
A. dept
B. dept, number
Course
C. dept, number, section
dept number section instructor
D. dept, number, section, instructor
MATH 101 A Jones
CSE303:Ashikur Rahman 36
Primary keys
Every table should select one key as primary key.
– Each tuple must have distinct non-NULL values of
primary key attributes.
– Adding a new record with duplicate or NULL primary key will
fail.
p_name price manufacturer
MovieStar relation
CSE303:Ashikur Rahman 38
Movie relation
CSE303:Ashikur Rahman 41
UNIQUE attribute (example)