Dbms Lecture 5
Dbms Lecture 5
Introduction to SQL
Department of Computer Science
School of Natural Sciences
University of Zambia
Overview
• SQL Language
• SQL is a comprehensive database language that includes both DDL (Data
Definition Language) and DML (Data Manipulation Language) features
• It allows defining schemas, creating and managing views, specifying constraints
(e.g., integrity, referential), security/authorization, and transaction controls
• SQL is also used for embedding in general-purpose programming languages like
Java or C/C++
• We will look at
• SQL DDL commands for schema and table creation, along with basic data
types in SQL
• How to define basic constraints (e.g., key, referential integrity)
• Basic SQL constructs for retrieval queries
• SQL data manipulation commands for inserting, deleting, and updating data
• SQL uses the terms table, row, and column to refer to the relational model's
relation, tuple, and attribute, respectively
• These terms are used interchangeably in discussions
• The main SQL command for data definition is the CREATE statement,
which is versatile and can be used to create:
• Schemas
• Tables (relations)
• Types
• Domains
• Other constructs such as views, assertions, and triggers
SQL Data Definition and Data Types - Schema and Catalog
Concepts in SQL
• SQL has a schema to group tables and other database constructs
• A schema has a name and an authorization identifier for the owner
• Schema elements include tables, types, constraints, views, and more
• Authorization: Only users with proper privileges can create schemas and
schema elements, which are granted by the system administrator or DBA
SQL Data Definition and Data Types - Schema and Catalog
Concepts in SQL
• SQL Catalog: A catalog is a collection of schemas, with a default schema
typically available for users upon login
• Each attribute is defined with a name, a data type (domain), and optional
constraints (like NOT NULL)
• Tables created with the CREATE TABLE command are base tables—they
are physically stored in the DBMS
• Virtual tables can be created using the CREATE VIEW command, but
these may not be physically stored
SQL Data Definition and Data Types - The CREATE TABLE
Command in SQL
• The schema where a table resides can be specified explicitly
• CREATE TABLE COMPANY.EMPLOYEE
• Or implicitly
• CREATE TABLE EMPLOYEE
• Circular references (e.g., a foreign key that references the same table) and
references to tables that have not yet been created may cause errors
• In these cases, foreign key constraints can be added later using ALTER
TABLE to avoid issues during the initial table creation
SQL Data Definition and Data Types - The CREATE TABLE
Command in SQL
SQL Data Definition and Data Types - The CREATE TABLE
Command in SQL
SQL Data Definition and Data Types - Attribute Data Types
and Domains in SQL
• SQL offers a variety of data types for defining attributes in tables, such
as numeric, character, bit string, Boolean, date, and time types
• Additional Types
• INTERVAL: Specifies intervals to increment/decrement date or time values
SQL Data Definition and Data Types - Attribute Data Types
and Domains in SQL
• Domains: A named data type used to simplify schema definitions and enforce
consistency. For example:
• CREATE DOMAIN SSN_TYPE AS CHAR(9);
• This domain can be reused in table definitions instead of specifying CHAR(9)
repeatedly
• For composite primary keys, the PRIMARY KEY clause is placed at the end
of the attribute list
• Example: PRIMARY KEY (Dnumber, Dlocation)
Specifying Constraints in SQL - Specifying Key and
Referential Integrity Constraints
• Unique Key Constraint
• The UNIQUE clause ensures that all values in a specified column (or set of
columns) are unique across the relation
• Example: Dname VARCHAR(15) UNIQUE,
• SELECT Statement
• The primary SQL command for retrieving data from a database
• In the formal relational model, tables are sets with no duplicate tuples
• SQL tables can be constrained to sets using key constraints or the DISTINCT option in the
SELECT statement
• Example Queries
• Example queries will be based on the schema in the following figures
Figures used for Example Queries
Figures used for Example Queries
Figures used for Example Queries
Basic Retrieval Queries in SQL - The SELECT-FROM-
WHERE Structure of Basic SQL Queries
• Basic Structure of SQL Queries
• SELECT: Specifies the attributes to retrieve (projection)
• FROM: Lists the tables involved in the query
• WHERE: Provides the conditions that must be met for tuples to be included
in the result (selection)
• Example Query 1A: Retrieve the name and address of employees in the
'Research' department
SELECT Fname, EMPLOYEE.Name, Address
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.Name = ‘Research’ AND DEPARTMENT.Dnumber =
EMPLOYEE.Dnumber;
Basic Retrieval Queries in SQL - Ambiguous Attribute
Names, Aliasing, Renaming, and Tuple Variables
• Example Query 1’: Retrieve the name and address of employees in the
'Research' department
SELECT EMPLOYEE.Fname, EMPLOYEE.LName, EMPLOYEE.Address
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.DName = ‘Research’ AND DEPARTMENT.Dnumber =
EMPLOYEE.Dno;
Basic Retrieval Queries in SQL - Ambiguous Attribute
Names, Aliasing, Renaming, and Tuple Variables
• Example Query 1B: Retrieve the name and address of employees in the
'Research' department
SELECT E.Fname, E.LName, E.Address
FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE D.DName = ‘Research’ AND D.Dnumber = E.Dno;
Basic Retrieval Queries in SQL - Unspecified WHERE
Clause and Use of the Asterisk
• Example Query 9:
SELECT Ssn
FROM EMPLOYEE;
• Example Q10A:
SELECT *
FROM EMPLOYEE, DEPARTMENT;
Basic Retrieval Queries in SQL - Tables as Sets in SQL
• SQL also has union (UNION), set difference (EXCEPT), and set intersection
(INTERSECT) operations
• Example Query 4: Make a list of all project numbers for projects that involve
an employee whose last name is ‘Smith’, either as a worker or as a manager of
the department that controls the project
( SELECT DISTINCT Pnumber
UNION
FROM EMPLOYEE
• Example Query 14. Retrieve all employees in department 5 whose salary is between
$30,000 and $40,000.
SELECT *
FROM EMPLOYEE
((Salary >= 30000) AND (Salary <= 40000)) is another way of writing it
Basic Retrieval Queries in SQL - Ordering of Query Results
• Example Query 15: Retrieve a list of employees and the projects they are working on,
ordered by department and, within each department, ordered alphabetically by last
name, then first name
• SELECT D.Dname, E.Lname, E.Fname, P.Pname
• FROM DEPARTMENT AS D, EMPLOYEE AS E, WORKS_ON AS W, PROJECT AS P
• WHERE D.Dnumber = E.Dno AND E.Ssn = W.Essn AND W.Pno = P.Pnumber
• ORDER BY D.Dname, E.Lname, E.Fname;
• The default order is in ascending order of values - the keyword ASC can be used to
specify ascending order explicitly
• We can specify the keyword DESC if we want to see the result in a descending order
of values
• Example, if we want descending alphabetical order on Dname and ascending order on Lname,
Fname, can be written as ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC
Basic Retrieval Queries in SQL - Summary of Basic SQL
Retrieval Queries
Query Skeleton
SELECT <attribute list>
FROM <table list>
[ WHERE <condition> ]
[ ORDER BY <attribute list> ];
INSERT, DELETE, and UPDATE Statements in SQL - The
INSERT Command
• Example U1
INSERT INTO EMPLOYEE
VALUES ( ‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘1962-12-30’, ’98 Oak Forest, Katy, TX’,
‘M’, 37000, ‘653298653’, 4 );
• Example U1A
INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn)
• Example U2A:
INSERT INTO EMPLOYEE (Fname, Lname, Dno)
VALUES (‘Robert’, ‘Hatcher’, 5);
• Example U4B: Delete one tuple • Example U4D : Delete all tuples
DELETE FROM EMPLOYEE DELETE FROM EMPLOYEE;
WHERE Ssn = ‘123456789’;
INSERT, DELETE, and UPDATE Statements in SQL - The
UPDATE Command
• Example U5
UPDATE PROJECT
• Example U6
UPDATE EMPLOYEE
WHERE Dno = 5;
Practical Understanding of SQL
• Below are some useful practical tutorials useful for practising concepts
learnt during the lectures and also for your lab projects
• Mandatory reading and you should all complete at least one of them