0% found this document useful (0 votes)
41 views42 pages

CSE 303 Lec 2 SQLIntro

The document discusses the structure query language (SQL) and relational databases. It provides an introduction to SQL and outlines its history. It describes how data is organized in relational databases using tables with rows and columns, and how this differs from non-relational databases. The document also discusses keys concepts in SQL including data types, domains, creating tables, and NULL values.

Uploaded by

niloy2105044
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)
41 views42 pages

CSE 303 Lec 2 SQLIntro

The document discusses the structure query language (SQL) and relational databases. It provides an introduction to SQL and outlines its history. It describes how data is organized in relational databases using tables with rows and columns, and how this differs from non-relational databases. The document also discusses keys concepts in SQL including data types, domains, creating tables, and NULL values.

Uploaded by

niloy2105044
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/ 42

Database Systems

CSE 215

Lecture 02
2022
Structure Query Language (SQL)

CSE303:Ashikur Rahman 1
Today’s Outline (mainly from chapter 2)

Relational Model vs Non-relational model

SQL introduction & Brief History

Data in SQL

Basic Schema definition

Keys and Constraints


CSE303:Ashikur Rahman 2
Two types of databases

Relational Databases Non-relational databases


• Organize data into • Organize data into
one or more tables anything but traditional
tables
– Each table has
columns and rows – Key-value pairs
– A Unique key – Documents (JSON,
identifies each row XML)
– Graphs

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

Structured Query Language

• Original name was SEQUEL:


Structured English QUEry Language

• Correct modern pronunciation is


S-Q-L.

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

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Tuples or rows CSE303:Ashikur Rahman 10


Tables Explained
• A relation is a table.
– (Envisioned by E.F. Codd 1970, IBM)
• The schema of a table is the table name and
its attributes:
Product(PName, Price, Category, Manfacturer)
• Database = collection of relations/tables.
• Database schema = set of all relation
schemas in the database.

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

C/C++  Data type comes first and then variable name


SQL  Variable name comes first and then Data type

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

• Only NUMBER  any fraction (up to 38 decimal digits)


20
CSE303:Ashikur Rahman
Common data types
• Date data type:

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

MM Numeric month (e.g., 07)


MON Abbreviated month name (e.g., JUL)
MONTH Full month name (e.g., JULY)
DD Day of month (e.g., 24)
DY Abbreviated name of day (e.g., FRI)
YYYY 4-digit year (e.g., 1998)
YY Last 2 digits of the year (e.g., 98)
RR Like YY, but the two digits are ``rounded'' to a year
in the range 1950 to 2049. Thus, 06 is considered
2006 instead of 1906

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

Any value of any type can be NULL.


– Signals missing value.
Students(sid: string, name: string, email: string, gpa: float
dob: date)
– Value doesn’t exist.
– Value exists but unknown.
– Value not applicable to this record.

CSE303:Ashikur Rahman 27
NULL data

Any value of any type can be NULL.


– Signals missing value.
Students(sid: string, name: string, email: string, gpa: float
dob: date)
– Value doesn’t exist. (for example email of a student)
– Value exists but unknown. (for example dob needs
verification)
– Value not applicable to this record. (for example, L1 T1
student)
CSE303:Ashikur Rahman 28
Creating table

CREATE TABLE <tableName> (


<list of attributes and types>
);

CSE303:Ashikur Rahman 29
Creating table

CREATE TABLE <tableName> (


attribute1 type1,
attribute2 type2,
….
….
);

CSE303:Ashikur Rahman 30
Creating table

CREATE TABLE Product (


maker VARCHAR(10),
model NUMBER(10),
type VARCHAR(10),
);

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

Students(sid: string, name: string, email: string, gpa: float


dob: date)

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

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks Product


Widget 29.99 WidgetsRUs

HyperWidget 203.99 Hyper

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

MATH 101 B Smith

MATH 102 A Williams

PHYS 101 A Baker

PHYS 102 B Baker

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

Gizmo 19.99 GizmoWorks


Product
Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper


Product (p_name: string, price: float, manufacturer: string)
CSE303:Ashikur Rahman 37
Class Exercise
Movie relation

MovieStar relation

CSE303:Ashikur Rahman 38
Movie relation

Movie(title: string, year:int, length: int, genre: string)

CREATE TABLE Movie(


title VARCHAR(50),
year INTEGER,
length INTEGER,
genre CHAR(10),
PRIMARY KEY (title, year)
)
CSE303:Ashikur Rahman 39
MovieStar relation

MovieStar(name: string, address:string, gender: char, birthdate: date)

CREATE TABLE MovieStar(


name VARCHAR(50),
address VARCHAR(50),
gender CHAR(1),
birthdate DATE,
PRIMARY KEY (name)
);
CSE303:Ashikur Rahman 40
UNIQUE attribute

– Each tuple must have distinct values of UNIQUE


attributes
– More than one tuple may have null values in unique
attribute(s)
– Adding a new record with duplicate in unique attribute will fail.

Students(sid: string, name: string, email: string, gpa: float


dob: date, username: string)

CSE303:Ashikur Rahman 41
UNIQUE attribute (example)

Students(sid: string, name: string, email: string, gpa: float


dob: date, username: string)
CREATE TABLE Students(
sid CHAR(10),
name VARCHAR(50),
email VARCHAR(50),
gpa FLOAT,
dob DATE,
username CHAR(5) UNIQUE,
PRIMARY KEY (sid)
);
CSE303:Ashikur Rahman 42

You might also like