Structured English Query Language (SQL) represents the implementation of the relational model, it is used for data definitions as well as queries and updates. SQL can also specify authorisation and security, define integrity constraints, define views, and specify transaction controls.
SQL has a number of data types that may be used for attributes:
Integers: typically we use INT / INTEGER which are implicitly signed, we may used UNSIGNED INTEGER for the unsigned variant. INT(n) specifies the number of digits used.
Approximate real numbers: use FLOAT / REAL / DOUBLE, can specify digit precision as FLOAT(n) where n is the number of bits used to store the mantissa of the floating point number.
Exact real numbers: use DECIMAL(i,j) to specify a fixed-point decimal number with i being the precision (total number of digits to store the number), and j denoting the position of the point / the scale.
Strings: We can use a number of types here:
CHAR(n) / CHARACTER(n): fixed length, right padded with spaces
VARCHAR(n) / CHAR VARYING(n): varying length
CLOB / TEXT: character large object
Binary data: Again we can use similar notation:
BIT(n): fixed length
BIT VARYING(n): varying length
BLOB: binary large object
title: Best practice: hash binary data and store that in the database with the metadata.
Boolean: use BOOLEAN or BIT(1), choice may vary from implementation to implementation, and in some scenarios it may be `NULL`
Date and Time: We have multiple different types to specify time:
DATE: made up of year-month-day (“yyyy-mm-dd”)
TIME: made up of hour:minute:second (“hh:mm:ss”)
TIME(i): TIME plus i additional digits for fractions of a second (“hh:mm:ss:ii…i”)
DATETIME / TIMESTAMP: both DATE and TIME components
Interval: we can use INTERVAL to specify relative time value as opposed to absolute, can be day/time intervals or year/month intervals. Can be positive or negative when added to or subtracted from an absolute value, the result is an absolute value.
There are additional domain specific / complex types which aren’t relevant for the course:
A referential integrity constraint may be violated when tuples in the referenced tuple are updated / deleted. By default REJECT is used for operations that violate constraints.
However, we can specify what should happen on different events such as:
ON DELETE
ON UPDATE
And trigger actions such as:
RESTRICT
CASCADE
SET NULL
SET DEFAULT
Example:
CREATE TABLE EMPLOYEE ( ..., FOREIGN KEY(DNO) REFERENCES DEPARTMENT(DNUMBER) ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE(SSN) ON DELETE SET NULL ON UPDATE CASCADE);
The basic form of the SQL SELECT statement is called a mapping or SELECT-FROM-WHERE block.
SELECT <attr list> FROM <table list> WHERE <condition>
<attr list> is a list of attribute names whose values are to be retrieved by the query
<table list> is a list of the relation names required to process the query
<condition> is a conditional boolean expression that identifies the tuples to be retrieved by the query.
We have access to basic logical operators in the <condition>: =, <, <=, >, >=, <>, !=, AND, OR, NOT, IS NULL, and IS NOT NULL.
In SQL, we can use the same name for multiple attributes as long as the attributes are in different relations. If two or more attributes in different relations have the same name, we need to specify them by the relation name. We can qualify the attribute name with the relation nae by prefixing the relation name to the attribute name.
SELECT EMPLOYEE.FNAME, EMPLOYEE.LNAME, EMPLOYEE.ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.DNAME='Research' AND DEPARTMENT.DNUMBER=EMPLOYEE.DNO
Some queries may need to refer to the same relation twice, we can give aliases to relation names to not have to write as much: (we can do the same to the projection)
SELECT E.FNAME AS EMPLOYEE_FNAME, E.LNAME AS EMPLOYEE_LNAME, S.FNAME, S.LNAMEFROM EMPLOYEE E, EMPLOYEE SWHERE E.SUPERSSN=S.SSN
SQL provides some set operations for us to use: UNION, EXCEPT, INTERSECTION.
The resulting relations of these set operations are sets of tuples - duplicates are eliminated from the result.
Set operations apply only to union compatible relations:
Two relations must have the same number of attributes.
Each corresponding pair of attributes has the same domain.