0% found this document useful (0 votes)
2 views

Relational modeling and SQL basics

The document provides an overview of relational modeling and SQL basics, focusing on the relational model's principles, SQL syntax, and query structure. It covers key concepts such as the SELECT statement, filtering and sorting data using WHERE and ORDER BY clauses, and debugging SQL queries. Additionally, it includes examples and assignments to reinforce learning outcomes related to data retrieval and manipulation in relational databases.

Uploaded by

temmybryan74
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Relational modeling and SQL basics

The document provides an overview of relational modeling and SQL basics, focusing on the relational model's principles, SQL syntax, and query structure. It covers key concepts such as the SELECT statement, filtering and sorting data using WHERE and ORDER BY clauses, and debugging SQL queries. Additionally, it includes examples and assignments to reinforce learning outcomes related to data retrieval and manipulation in relational databases.

Uploaded by

temmybryan74
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Relational

modeling and SQL


basics
GBETNKOM NJIFON Jeff, Lecturer.
Lesson objectives
● Describe the relational model and its advantages for data representation
and manipulation.
● Write SQL statements using the correct syntax and basic query structure,
such as keywords, clauses, expressions, operators, and delimiters.
● Use the SELECT statement to retrieve data from one or more tables in a
relational database.
● Apply filtering and sorting criteria to the data using the WHERE and
ORDER BY clauses, and use logical operators and comparison operators
to specify conditions.
● Test and debug SQL queries using a database management system or an
online tool.
Content
● Relational modeling principles
● SQL syntax and basic query structure
● SELECT statement for data retrieval
● Filtering and sorting data using WHERE and ORDER BY clauses
Relational modeling principles
Presentation
● The Relational model underpins most of the major database systems in
commercial use today. As such, an understanding of the ideas described
in this chapter is fundamental to these systems.
● A relation or schema is a table with columns and tuples. A database can
contain as many tables as the designer wants. Each table is an
implementation of a real-world entity.
● For example, the university keeps information about students. A student
is represented as an entity during database design stage. When the
design is implemented, a student is represented as a table.
Terminologies
● An attribute is a named column in the table.
● The domain is the allowable values for a column in the table. For
example, a name of a student can be made of a maximum of 30 lower
and upper case characters.
● A tuple is the row of the table.
● The degree of a relation/table is the number of columns it contains.
● The cardinality of a relation/table is the number of rows it contains.
● We say that a column for which the value is not currently known, or for
which a value is not applicable, is null.
Example
● The table shown below represents a relation named Student.
Some rules to pass from E/R to Relational
● All entities are transformed into schema.
● One-to-one relationships are transformed into the relational database
schema.
● A one-to-many relationship is transformed into two tables where there is
a foreign key (PK of one-side entity) on the n-side entity.
● A many-to-many relationship creates three tables: one per entities, and
one for the relationship. The relationship table’s primary key is composed
of two foreign keys (PK of the two entities).
SQL syntax and basic query structure
Data definition
● It deals with databases and tables creation, update and deletion.
● Syntaxes:
○ CREATE DATABASE name;
○ CREATE TABLE name (attribute1_name attribute1_type options, …, attn_name attn_type
options);
○ DROP DATABASE name;
○ DROP/DELETE table;
● An index is a structure that provides accelerated access to the rows of a
table based on the values of one or more columns.
○ CREATE INDEX IndexName ON TableName;
Data types
Data manipulation
● INSERT INTO tableName(attributes…) VALUES tuples;
● SELECT attributes FROM tableName;
● UPDATE tableName SET attribute = value WHERE attribute = value;
● DELETE FROm tableName WHERE condition;
SELECT statement
WHERE clause
● It consists of the keyword WHERE followed by a search condition that
specifies the rows to be retrieved. The five basic search conditions are as
follows:
○ Comparison: compare the value of one expression to the value of another expression.
○ Range: test whether the value of an expression falls within a specified range of values.
○ Set membership: test whether the value of an expression equals one of a set of values.
○ Pattern match: test whether a string matches a specified pattern.
○ Null: test whether a column has a null (unknown) value.
Comparison
● = equals
● < is less than
● > is greater than
● != (<>) is not equal to
● <= is less than or equal to
● >= is greater than or equal to
● Example: SELECT staffNo, fName, IName, position, salary FROM Staff
WHERE salary > 10000;
Logical operators
● The rules for evaluating a conditional expression are:
○ an expression is evaluated left to right;
○ subexpressions in brackets are evaluated first;
○ NOTs are evaluated before ANDs and ORs;
○ ANDs are evaluated before ORs.
● Example: SELECT * FROM Branch WHERE city = ‘Douala’ OR city =
‘Bamenda’;
Range search
● Range search is performed using BETWEEN and AND.
● Example: SELECT staffNo, fName, IName, position, salary FROM Staff
WHERE salary BETWEEN 20000 AND 30000;
IN (set membership)
● The set membership test (IN) tests whether a data value matches one of a
list of values.
● Example: SELECT staffNo, fName, IName, position FROM Staff WHERE
position IN (‘Manager’, ‘Supervisor’);
Sorting
ORDER BY clause
● The ORDER BY clause consists of a list of column identifiers that the result is to
be sorted on, separated by commas.
● A column identifier may be either a column name or a column number† that
identifies an element of the SELECT list by its position within the list, 1 being the
first (leftmost) element in the list, 2 the second element in the list, and so on.
● Column numbers could be used if the column to be sorted on is an expression
and no AS clause is specified to assign the column a name that can
subsequently be referenced.
● The ORDER BY clause allows the retrieved rows to be ordered in ascending (ASC)
or descending (DESC) order on any column or combination of columns,
regardless of whether that column appears in the result.
Examples
● SELECT staffNo, fName, IName, salary FROM Staff ORDER BY salary
DESC;
● SELECT staffNo, fName, IName, salary FROM Staff: This part of the
query retrieves the staff number, first name, last name, and salary of all
the staff members from the table named Staff in the database.
● ORDER BY salary DESC: This part of the query sorts the retrieved data in
descending order by the salary column, meaning that the staff member
with the highest salary will be shown first and the one with the lowest
salary will be shown last.
Assignment

● List all book titles.


● List all borrower details.
● List all books titles published between 2010 and 2014.
● Remove all books published before 1950 from the database.
● List all book titles that have never been borrowed by any borrower.
● List all book titles that contain the word ‘database’ and are available for loan.
Cheers mates !

You might also like