Chapter 2
Chapter 2
2. Relational Databases
[email protected]
Chapter 2. Relational Databases
• Relational model
• Relations
• Attributes
• Data domains
1200
1000
800
600
400
200
0
Oracle MySQL Microsoft PostgreSQL MongoDB DB2 Elasticsearch Redis (Key- Microsoft Cassandra
(Relational) (Relational) SQL Server (Relational) (Document (Relational) (Search Value Store) Access (Wide
(Relational) Store) Engine) (Relational) Column
Store)
Relational model
STUDENTS
First Name Last Name Date of Birth Faculty … Table header
Mihaela Andreescu 1998-06-23 ETTI …
Andrei Barbu 1999-10-14 ETTI … Rows (tuples)
Georgiana Constantin 1998-02-23 ETTI …
Laura Dumitrescu 1997-11-24 ETTI …
Attribute values
Representing relations through tables
• * - The maximum length varies between DBMSs (good practice is to not exceed
255 characters)
• ** - TEXT data type, although not a part of the official SQL standard is found
in many DBMSs such as Oracle, MySQL, MS SQL Server or PostgreSQL
SQL data types
• * - TIMESTAMP and DATETIME types may not both be present on all DBMSs.
TIMESTAMP values are stored as seconds since (or before in some cases) a
"start date" (such as Unix time – January 1st 1970). On some DBMSs, the range
of TIMESTAMP values is limited
• ** - The DATETIME data type may not be present in all DBMSs
Notation conventions
• Creating a database
CREATE DATABASE database_name;
• The database name must consist only in alpha-numeric characters
and underscore (_)
• Changing database characteristics
ALTER DATABASE database_name alter_specification;
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
Using and deleting databases
• Using a database
USE database_name;
• A database can be viewed as a container for its entities: tables,
views, functions, procedures and triggers
• Deleting a database
DROP DATABASE [IF EXISTS] database_name;
• Dropping a database will also delete all the files that the database
system may create during normal operation
Creating tables
• Example:
CREATE TABLE students(
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth DATE,
faculty VARCHAR(255),
enrolment_year INT,
`group` VARCHAR(255)
);
Editing tables
• Changing attributes
ALTER TABLE table CHANGE col_old_name col_new_name
data_domain [column_constraints];
• Example:
ALTER TABLE students CHANGE enrolment_year
enrolment_year INT UNSIGNED;
• Adding column constraints
ALTER TABLE table ADD [CONSTRAINT [name]] {
PRIMARY KEY | UNIQUE }({column | column_list});
Editing tables
• Example:
ALTER TABLE students ADD CONSTRAINT unique_email
UNIQUE(email);
ALTER TABLE students ADD UNIQUE(email);
ALTER TABLE students ADD PRIMARY KEY(id);
• Deleting columns or constraints
ALTER TABLE table DROP {[COLUMN] | INDEX | PRIMARY
KEY | FOREIGN KEY} {column_name | key_name} ;
• Example:
ALTER TABLE students DROP INDEX unique_email;
ALTER TABLE students DROP email;
Deleting tables
• Deleting a table
DROP TABLE [IF EXISTS] table_name;
• Example:
DROP TABLE students;
• Dropping a table will delete all the data from the table, as well as
the table structure
• The operation is irreversible
Integrity constraints
• The integrity constraints are the rules that can be added for the
stored data to best correspond with the modelled reality:
• They can be defined when designing the database
• They must be respected by any state of the relation
• The inherent constraints are those of the data model itself, which
don’t need to be explicitly defined as they are included in the
database management system
• Example: in the relational model the constraint that the value of
each attribute is atomic (indivisible) is an inherent constraint
Integrity constraints
• Domain constraints:
• NOT NULL constraint
• constraint of default value (DEFAULT)
• check constraint (CHECK)
• The NOT NULL constraint means that the attribute can not take the
NULL value in any tuple of the relation
• The NULL value of an attribute in a tuple means that the value of
that attribute is not known for that tuple
• Examples:
• the birth date of a historical person is not known at all
• the value of an attribute is not known when inserting the tuple, but it will be
known and updated later
Domain constraints
• Example:
• Passport number, CNP for Romanian citizens, SSN for US citizens
Primary and artificial keys
• Two domains are compatible if they are of the same data type and
semantic (it makes sense to compare them)
• In SQL, domain checking is limited to verifying the compatibility of data types
and semantic compatibility has to be ensured by the designer
Inter-relation constraints (foreign keys)
• The column list can be omitted if values are entered for all
attributes of the table
• The order of the values must be the same as the order of the
columns of the table
• The order of the columns comes from the order of the attribute
definitions in the CREATE TABLE instruction, as well as all
subsequent table alterations (ALTER TABLE)
• The order of the columns can be queried using the DESCRIBE
statement
• If values are not specified for all columns, the missing columns
will get a DEFAULT value (if specified) or NULL (if accepted)
The DESCRIBE statement
• Examples:
SELECT 3, 10, 5 + 7, 'Test';
SELECT * FROM students;
SELECT first_name, last_name FROM students;
SELECT first_name, last_name FROM students WHERE
`group` = '441F';
SELECT COUNT(*) FROM students;
SELECT NOW();
SELECT *, DATE(NOW()) FROM students;
SELECT DISTINCT `group` FROM students;
The SELECT statement
• The FROM clause specifies the tables (or views) from which the
data is queried. The syntax for the FROM clause is:
FROM table_list
• The WHERE clause allows adding one or more conditions (using
the logical comparison operators AND or OR) which must be met
by the resulting data.
WHERE condition [AND |OR condition] […]
• To control the order of the operations, parentheses () may be used
when necessary. The AND operator takes precedence over the OR
operator.
The SELECT statement
Function Result
COUNT Returns the number of rows in the resulting table
SUM Returns the sum of the values in the column passed as the argument
MIN Returns the minimum value in the column passed as the argument
MAX Returns the maximum value in the column passed as the argument
AVG Returns the average of the values in the column passed as the argument
Aggregate functions
• Example:
SELECT CEIL(COUNT(*)/10) FROM products;
Scalar functions
• String functions:
ASCII() INSTR() OCT() RIGHT()
CHAR() LEFT() QUOTE() RPAD()
CONCAT() LENGTH() REGEXP_LIKE() RTRIM()
CONCAT_WS() LOCATE() REGEXP_REPLACE() SPACE()
FORMAT() LOWER() REGEXP_SUBSTR() SUBSTR()
FROM_BASE64() LPAD() REPEAT() TO_BASE64()
HEX() LTRIM() REPLACE() TRIM()
INSERT() MID() REVERSE() UPPER()
• Example:
SELECT CONCAT_WS(' ', first_name, last_name) FROM students;
SELECT SUBSTR(filename, -3) FROM files;
Scalar functions
• Example:
SELECT DATE_FORMAT(dob, '%d.%m.%Y') FROM students;
SELECT DATE_ADD(NOW(), INTERVAL 3 MONTH);
Aliases