IE 171 Lecture 7 - SQL (Part 1)
IE 171 Lecture 7 - SQL (Part 1)
Query
Language (SQL)
IE 171: INFORMATION SYSTEMS 1
P RE P ARE D B Y C A S O N DAY
04/18/2023 1
Outline
► Introduction to SQL
◦ DML
◦ DDL
► Why SQL?
► Writing SQL Commands
DBMS Software
Definition: Software that controls the organization,
storage, retrieval, security and integrity of data in a
database.
It accepts requests from the application and instructs the
operating system to transfer the appropriate data.
DBMS Software
Commercial software
◦ Sybase Adaptive Server Enterprise
◦ IBM DB2
◦ Oracle
◦ Microsoft SQL Server
◦ Teradata
Free/GPL/Opensource:
◦ MySQL
◦ PostgreSQL
SQL
The definition of a relational system, requires that
a single language – sometimes called a
comprehensive data sublanguage – be able to
handle all communications with the database.
This language is SQL: Structure Query Language
SQL: pronounced si-kwel or S-Q-L?
A HUGE debate among practitioners
Bill Gates says si-kwel though
6 PREPARED BY CASONDAY
DML and DDL
DML (data manipulation language) – Adds rows, chooses columns to
show
◦ INSERT, UPDATE, SELECT
English-like syntax
Based on Relational Algebra & Relational Calculus
Data Definition
Language (DDL)
BUILDING A SCHEMA ON POSTGRESQL VIA
SQL
10 PREPARED BY CASONDAY
Let’s Start
Open the Query Tool to have a canvas on where to write your code.
11 PREPARED BY CASONDAY
Let’s Start
Open the Query Tool to have a canvas on where to write your code.
12 PREPARED BY CASONDAY
Running a code in pgAdmin
Type SELECT version(); into your Query Editor.
Run the script by pressing F5 or clicking on the play button above the
editor.
13 PREPARED BY CASONDAY
Writing SQL Commands
SQL statement consists of reserved words and user-
defined words.
Reserved words are a fixed part of SQL and must be spelt
exactly as required and cannot be split across lines.
User-defined words are made up by user and represent
names of various database objects such as relations,
columns, views.
CREATE TABLE Staff(staffNo VARCHAR(5),
lName VARCHAR(15),
salary DECIMAL(7,2));
Understanding Notations
Use extended form of BNF (Backus Naur Form) notation:
Example: {a | b} (, c…)
16 PREPARED BY CASONDAY
DDL Schema
Schema Definition
◦ is a structure of a particular database described using standard SQL Code
Best Interpretation:
◦ Schema: House Design Plan
◦ Database: House
◦ Table: Kitchen
Building a Schema
For the next slides, feel free to try the following blocks of code into
your pgAdmin on the database ddl_practice
You may Generate ERD for the database from time to time to check
on the changes on the schema
18 PREPARED BY CASONDAY
Let us build this simple schema
19 PREPARED BY CASONDAY
Building your Schema
Create new tables first.
CREATE TABLE TableName [ (columnName datatype [constraints]) ]
The serial datatype is used for id
numbers that automatically
increment with new data
instances
In practice, concatenated
keys can be implemented
with single primary key.
23 PREPARED BY CASONDAY
PG data types
Boolean (bool) – true or false
Character → char, varchar, text
Numeric → integer, float
Date, time, timestamp, interval
Array
JSON
Hstore
More details:
https://www.postgresql.org/docs/current/datatype.html#DATATYPE-
TABLE
24 PREPARED BY CASONDAY
Other DDL Commands
INSERT – adds a data object to the table specified
UPDATE – updates values in a table given the specified filters
DELETE
ALTER
25 PREPARED BY CASONDAY
Create the table
30 PREPARED BY CASONDAY
Updating Rows
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
Authors
41 PREPARED BY CASONDAY
Thank you for coming to
class! Have a nice day!
PREPARED BY CASONDAY 42