advertisement

Math and SQL Table and math: Table includes rows and columns of information. All information from one column must be of the same type. The set of ALL possible values of one column is called DOMAIN of this column. Row represents one entity and columns define its characteristics. The DOMAIN of one column is called ATTRIBUTE. Let C1, C2, …Cn be the column names of a table T. - n is called the dimension of the table. - The column names can be associated with attributes or DOMAIN Let assume that the table T has k rows (rows can be duplicated). - k is call the cardinality of the table. - Each row is a tuple, a member of the Cartesian product C1 x C2 x…x Cn - In math the Cartesian product C1 x C2 x…x Cn is a vector space of n dimension. Hence Table T is a subset of the Cartesian product. Projection of a n-dimensional vector space to a lesser dimension space is denoted as . - The projection : C1 x C2 x…x Cn C1 will project the n-tuple onto the first dimension C1. - In SQL we express this function as: SELECT C1 from T; - It is possible that we have many duplicates of values in C1 after the projection. If ALL duplicates are collected, we have the following SQL: SELECT ALL C1 from T; If we want to keep only one copy, we use: SELECT DISTINCT C1 FROM T; - In math, we use subscript d in where d can be ALL or DISTINCT. Algebra and Simple SQL Statements SQL: Math: Meaning: Select * From R Group by GA; G[GA] R Group table R on the grouping columns GA= {GA1, GA2, …GAn} R1 x R2: the Cartesian product of table R1 and R2 SQL: Math: Meaning: SELECT * FROM R WHERE C; [C]R Select all rows of table R that satisfy condition C. Duplicate rows are not eliminated. SQL: Math: Meaning: SELECT [ALL/DISTINCT] B FROM R; d[B]R Project table R on columns B, without eliminating duplicates when d = ALL and with duplicate elimination when d = DISTINCT SQL: Math: Meaning: SELECT GA, A, F[AA] FROM R GROUP BY GA; F[AA]R Project table R on columns GA (grouping columns), on columns A (nongrouping columns, and on the aggregation columns AA={A1, A2,…, An} with the aggregation functions F = {f1, f2,…fn}