created by Don Chamberlin and Ray Boyce from IBM in the 1970’s as part of the System R project which meant to provide a practical implementation to Codd’s relational model.
SQL was adopted as a standard language in 1986 by the American National Standards Institute (ANSI) and by the International Standards Organization (ISO) in 1987.
view: is a virtual table derived from one or more tables or other views. It is virtual because it does not contain any data, but a definition of a table based on the result of a SELECT statement.
Views allow you to hide data or limit access to a select number of columns; therefore, they can also be used for security purposes.
A SELECT statement returns its result set in no particular order
executing the same SELECT statement multiple times will generate same rows but in different order, adding ORDER BY clause will return the same order every times
SELECTcol1FROMmyTableORDERBYcol1DESC
cursor: is a result set holding the result of a SELECT statement. The syntax to declare, open, fetch, and close
Rather than returning all the rows of an SQL statement to an application at once, a cursor allows the application to process rows one at a time. Using FETCH statements within a loop in the application, developers can navigate through each row pointed by the cursor and apply some logic to the row or based on the row contents.
For example, the following code snippet sums all the salaries of employees using a cursor
outer joins: Left outer join, Right outer join, Full outer join
type
definitions
Equal join (equi)
two tables are joined based on the equality of specified columns
natural join
improved version of an equi-join where the joining column does not require specification. The system automatically selects the column with same name in the tables and applies the equality operation on it and remove all duplicate attributes. ambiguous, not liked by most DBs.
cross join
Cartesian product of the tables to be joined
left outer join
the result set is a union of the results of an equi-join, including any non-matching rows from the LEFT table
right outer join
the union of results of an equi-join, including any non-matching rows from the RIGHT table.
full outer join
the result set is the union of results of an equi- join, including any non-matching rows of the LEFT and the RIGHT table.
examples:
-- Example 1 (equi join):SELECT*FROMstudent,enrollmentWHEREstudent.enrollment_no=enrollment.enrollment_no-- Example 2 (equi join):SELECT*FROMstudentINNERJOINenrollmentONstudent.enrollment_no=enrollment.enrollment_no-- example 3 (natural join):SELECT*FROMSTUDENTNATURALJOINENROLLMENT-- Example 4 (cross join):SELECT*FROMSTUDENT,ENROLLMENT-- Example 5 (left outer join):SELECT*FROMSTUDENTLEFTOUTERJOINENROLLMENTONSTUDENT.ENROLLMENT_NO=ENROLLMENT_NO-- Example 6 (right outer join):SELECT*FROMSTUDENTRIGHTOUTERJOINENROLLMENTONSTUDENT.ENROLLMENT_NO=ENROLLMENT_NO-- Example 7 (full outer join ):SELECT*FROMSTUDENTFULLOUTERJOINENROLLMENTONSTUDENT.ENROLLMENT_NO=ENROLLMENT_NO
join two data sets having the same column definitions and in the same order. removes any duplicate rows.
intersection
returns a result set common to both data sets
difference (EXCEPT)
returns the result set that exists only in the RIGHT or First table as A EXCEPT B = A MINUS [A INTERSECT B]
examples
-- Example 1 (union):SELECT*FROMstudent_table_aUNIONSELECT*fromstudent_table_b-- Example 2 (union with duplicates):SELECT*FROMstudent_table_aUNIONALL-- duplicate rows will stay in the resultSELECT*fromstudent_table_b-- Example 3 (intersection):select*fromstudent_table_aINTERSECTselect*fromstudent_table_b-- Example 4 (intersection with duplicates):select*fromstudent_table_aINTERSECTALL-- duplicate rows will stay in the resultselect*fromstudent_table_b
When a query is applied within a query, the outer query is referred to as the main query or parent query and the internal query is referred as the sub-query or inner query
sub query may return a scalar value, single or multiple tuples, or a NULL data set
Sub-queries are executed first, and then the parent query is executed utilizing data returned by the sub-queries.
When a sub-query returns a data set that represents multiple values for a column (like a list of names) or array of values for multiple columns (like Name, age and date of birth for all students), then the sub-query is said to be returning vector values.
the example will get a list of students who are enrolled in courses offered by the computer science department:
sub-query returns a list of all courses that are offered in the “Computer Science” department and the outer query lists all students enrolled in the courses of the sub-query result set.
When a sub-query is executed for each row of the parent table, instead of once then the sub-query is referred to as a correlated sub-query.
the example below searches for a list of students with who have been awarded maximum marks in each department.
For each row on the LEFT table, the sub-query finds max(marks) for the department of the current row and if the values of marks in the current row is equal to the sub-query result set, then it is added to the outer query result set.
The above query uses a sub-query in the FROM clause. The sub-query returns maximum, minimum and average marks for each department. The outer query uses this data and filters the data further by adding filter conditions in the WHERE clause of the outer query
Object-relational mapping (ORM) libraries such as Hibernate, pureQuery are popular to provide a framework for this mapping between the object-oriented world and the relational world.
The table below shows the correspondence between conceptual, logical and physical model concepts
data stored in the DB in a way that can not be read by humans that’s why we need DBMS.
in early days of DBs the network and hierarchy data models were the most popular.
SQL is a relational database language, but it is not a DBMS, instead it is a way to communicate with the DBMS.
SQL is non procedural database language, which means that when executing a a SQL command you don’t need to run,compile a program for each query or define the location of data, DBMS will take care of that.
interactive SQL can be run in the command line and output results to the console.
programmatic SQL where SQL commands are embedded in a host language (COBOL, C.SQL).
SQL needs a host language cause it is not a complete programming language, it has no branching or looping functionalities, so it relays on the host language to allow this.
VIEWS are virtual tables, creating a view will create a new table in the database based on select statement provided when creating the view.
the view then gets updated when any updates happen on the original table(s).
when a new DB is created it is owned by the user who created it and have all privileges over it.
Numeric types TINYINT, SMALLINT, INTEGER and BIGINT with fixed binary precision, NUMERIC and DECIMAL are types with user-defined decimal precision. DOUBLE type is a 64-bit.
BOOLEAN type is for logical values and can hold TRUE, FALSE or UNKNOWN.
Character string types are CHAR(L), VARCHAR(L) and CLOB,
type
comments
TINYINT, SMALLINT, INTEGER and BIGINT
fixed binary precision
NUMERIC and DECIMAL
user-defined decimal precision
DOUBLE
64-bit
CHAR(L)
fixed length strings, if you provide shorter strings, spaces will be added. if no L provided its a single char
VARCHAR(L)
general strings, performance issues appear for long strings with this type, avoid in large strings (> 10* KB).
CLOB
for large strings, avoid for short strings
LONGCHAR
a synonym for a long VARCHAR and can be used without specifying the size
BINARY(L)
fixed length strings such as UUID, e pads short binary strings with zero bytes, if no L it is a single byte
VARBINARY(L)
general binary strings
BLOB(L)
for large binary objects
LONGVARBINARY
used as VARBINARY without specifying the size, can be mapped to BLOB instead.
BIT(L) and BITVARYING(L)
avoid use without specifying L, use BOOLEAN for single bit.
UUID
stored as BINARY. UUID and BINARY strings, can be used to insert or to compare.
DATE, TIME, and TIMESTAMP, TIME ZONE
INTERVAL
used with datetime, not widely supported
OTHER
storage of json objects, if object is large, serialize and save as BLOB.
[1] Sharma, N., Perniu, L., Chong, R. F., Iyer, A., Nandan, C., Mitea, A. C., Nonvinkere, M., & Danubianu, M. (2010). Databases fundamentals. chapter 5.
[2] Din, A. I. (2006). Structured Query Language (SQL): A practical introduction. NCC Blackwell. chapters 1 & 2. Retrieved from http://www.managedtime.com/freesqlbook.php.