Difference between T-SQL and the relational model

Similar documents
Querying Data with Transact SQL

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

II. Structured Query Language (SQL)

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Chapter 3. Introducation to Relational Database 9/2/2014 CSC4341 1

T-SQL Training: T-SQL for SQL Server for Developers

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

II. Structured Query Language (SQL)

Slicing and Dicing Data in CF and SQL: Part 2

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Querying Data with Transact-SQL

Querying Data with Transact-SQL

20761B: QUERYING DATA WITH TRANSACT-SQL

Database Management Systems,

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

SQL functions fit into two broad categories: Data definition language Data manipulation language

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Querying Data with Transact-SQL

20461: Querying Microsoft SQL Server

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Querying Microsoft SQL Server (MOC 20461C)

Querying Data with Transact-SQL

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Unit Assessment Guide

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Chapter 3: The Relational Database Model

Querying Data with Transact-SQL

Inside Microsoft* SQL Server 2008: T-SQL Querying

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Chapter 4: SQL Basics

Data Manipulation Language (DML)

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

Querying Microsoft SQL Server

20761C: Querying Data with Transact-SQL

20461: Querying Microsoft SQL Server 2014 Databases

Microsoft Querying Microsoft SQL Server 2014

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

Assignment 6: SQL III Solution

CSC Web Programming. Introduction to SQL

Course 20461C: Querying Microsoft SQL Server

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Querying Data with Transact-SQL

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

After completing this course, participants will be able to:

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

AVANTUS TRAINING PTE LTD

Fundamentals of Database Systems

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Duration Level Technology Delivery Method Training Credits. Classroom ILT 5 Days Intermediate SQL Server

Midterm Review. Winter Lecture 13

How to design a database

Sql Server Syllabus. Overview

Graph Databases. Guilherme Fetter Damasio. University of Ontario Institute of Technology and IBM Centre for Advanced Studies IBM Corporation

ORACLE DATABASE 12C INTRODUCTION

Querying Microsoft SQL Server

Querying Data with Transact-SQL

COURSE OUTLINE: Querying Microsoft SQL Server

Querying Microsoft SQL Server 2008/2012

Querying Microsoft SQL Server

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Querying Data with Transact-SQL (20761)

MIS NETWORK ADMINISTRATOR PROGRAM

Querying Microsoft SQL Server

Querying Microsoft SQL Server 2014

MTA Database Administrator Fundamentals Course

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

Full file at

Querying Microsoft SQL Server

Enhanced Data Models for Advanced Applications. Active Database Concepts

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

Microsoft Querying Data with Transact-SQL - Performance Course

Introduction to Data Management CSE 414

Querying Microsoft SQL Server 2014

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Querying Microsoft SQL Server 2012/2014

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say

8) A top-to-bottom relationship among the items in a database is established by a

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Topic of most interest

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Database Systems SQL SL03

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Lecture 03. Spring 2018 Borough of Manhattan Community College

$99.95 per user. Writing Queries for SQL Server (2005/2008 Edition) CourseId: 160 Skill level: Run Time: 42+ hours (209 videos)

20461D: Querying Microsoft SQL Server

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Basant Group of Institution

CS W Introduction to Databases Spring Computer Science Department Columbia University

DBMS. Relational Model. Module Title?

Lecture 2: Chapter Objectives. Relational Data Structure. Relational Data Model. Introduction to Relational Model & Structured Query Language (SQL)

Transcription:

Difference between T-SQL and the relational model Davor Lozić 1, dr.sc. Alen Šimec 2 1 Tehničko veleučilište u Zagrebu, Vrbik 8, Zagreb, Croatia 2 Tehničko veleučilište u Zagrebu, Vrbik 8, Zagreb, Croatia Abstract - T-SQL, dialect of SQL, is a language used for a relational database management system (primarily Microsoft s SQL Server), which in turn is based on the relational model. Understanding some of the key foundation principals can help in a better understanding of the language. This paper introduces basics of the relational model and the difference between the relational model and the actual T-SQL implementation. Several tightly related topics like predicates, relations, and common misconceptions about databases are also discussed. Keywords relational model, T-SQL, predicate, relation 1 Introduction The relational model is a mathematical model for data management and manipulation which was initially proposed by Edgar Codd in 1969 [1]. It s based on set theory and predicate logic (Picture 1). 1.1 Set theory Picture 1 - T-SQL George Cantor, the founder of set theory, defined a set as follows: By an "aggregate" we are to understand any collection into a whole M of definite and separate objects m of our intuition or our thought. These objects are called the "elements" of M. The German word for a set is Menge, which is the reason Cantor denotes a set by M and its elements by m. Menge is translated as an

aggregate, but it has since become common to use the word set instead [2]. Fundamental concept to understand about sets is that they are determined uniquely by its members [3]. That means that sets {5, 5, 7, 2} and {5, 7, 2} are identical. Order of elements inside the set is not important so the set {2, 5, 7} is the same as the previous two. 1.2 Predicate logic A predicate is a generalization of a propositional variable [4]. In other words, a predicate is an expression that, when attributed to some object, makes a proposition either true or false [5]. For example, when has a car predicate is evaluated against employee, it s a proposition. Predicates make filtering and data integrity possible. They can be even used for defining the data model itself. Example: invoice with ID 1128 has been created on 2015-01-01 by the user with ID 201. In this example, defining the data model is simple: invoiceid INT, invoicecreated DATE, userid INT. 2 Common misconceptions A common mistake is to think that relational in relational model has something to do with foreign keys and relationships between tables. Relation is what SQL calls a table but the two are not synonymous. Table is an engineering approximation to the relational model. A relation has a heading with a set of attributes and a body with a set of tuples [6]. SQL attempts to represent attributes with columns and a set of tuples with rows. Attribute is identified with name and type name and each tuple is defined with heading (which corresponds to the heading of the relation) and values with a respective types (Picture 2). 3 Differences Picture 2 - Relational model Main differences between T-SQL and relational model will be shown in this chapter. Picture 3 shows the data used for queries [7]. Picture 3 - Example data (Users) Set has no duplicates but the T-SQL doesn t enforce this rule. Example: SELECT City FROM Users; Picture 4 shows the query result. As shown, SELECT statement doesn t enforce unique rows in result. Picture 4 - Result does not contain unique rows T-SQL implements keyword DISTINCT which guarantees unique rows and returns relational result: SELECT DISTINCT City FROM Users;

Picture 6 shows a result which guarantees the order of rows. Picture 5 - Unique results returned by DISTINCT Another non-relational concept which T-SQL allows is having a column without defining a name. Often, there is a need for defining the result based on the expression: SELECT Name + + City FROM Users; This query creates attribute without the name but the relational model doesn t allow creation of attributes without giving them a unique name inside the relation. Attribute name uniqueness is another problem. Consider having a simple join between two tables where both tables have the attribute name. To solve both problems, T-SQL implemented AS clause which can assign the alias to the given target. [8] Next query perfectly respects all relational model rules: SELECT Name + + City AS FullDescription FROM Users; T-SQL s implementation has another deviation from the relational model. Its predicate logic only implements TRUE, FALSE, and NULL but according to Edgar Codd, there should be another value. Example is when the user doesn t want to provide or when the user doesn t have an email. Both cases should have different value inside the relation. Picture 6 - Example data (Users) After executing a query, result also returns a list attributes in a specified order. If the query contains SELECT *, T-SQL guarantees the same order in the result based on their order inside the table definition. The true relation doesn t care about the order of attributes and that is another deviation from the relational model. In T-SQL, order of columns is defined and significant but the relational model requires there to be no significance to any ordering. It s not even easy to change the column order inside most of the SQL implementations. In SQL Server, when a user changes the column order, system recreates the table from scratch. SQL Management Studio will not allow such operation if the option Prevent saving changes that require table re-creation is checked (Picture 7). For a query to be called relational, it needs to return a relation after executed. Query with the ORDER BY clause is not relational because the set theory clearly states that the order in which the elements of a given set are listed does not matter [2]. The next query guarantees ordering by name and that is the reason why the result of the query is not a relation: SELECT Name FROM Users ORDER BY Name DESC;

one place which could also be used as a reference for creating relational queries. # this query doesn t guarantee # uniqueness, use DISTINCT clause SELECT City FROM Users; # with DISTINCT clause SELECT DISTINCT City FROM Users; Picture 7 Prevent saving changes that require table re-creation To prevent T-SQL from recreating a whole table, Views can be used. A view is a virtual table and it doesn t exist physically. It s data source is based on the result of an SQL statement. Consider the data in Picture 3. Requested column order is Id, City, Name, instead of Id, Name, City. CREATE VIEW UsersView AS SELECT Id, City, Name FROM Users; Getting the data is same as previous examples: SELECT * FROM UsersView; Result is shown in Picture 8. # no attribute name in this query # AS must be used, also this query # doesn t guarantee uniqueness, # use DISTINCT clause SELECT Name + + City FROM Users; # with DISTINCT and AS clause SELECT Name + + City AS UniqueName FROM Users; # this query contains ORDER BY # in Set Theory, order is not # important, also this query # doesn t guarantee uniqueness, # use DISTINCT clause SELECT Name FROM Users ORDER BY Name DESC; # with DISCTINCT and without # ORDER BY clause SELECT DISTINCT Name FROM Users; 5 Conclusion Picture 8 Result after getting data from a View 4 Non-relational queries This chapter will provide some of the nonrelational queries seen in this paper, grouped in Virtually all relational database management systems are based on SQL which is derived from a relational model. T-SQL and relational model is based on a strong mathematical foundations and understanding them can help in better understanding of the language. T-SQL deviates in a lot of ways from a relational model but almost all deviations could be avoided with appropriate language constructs.

6 References [1] E. F. Codd, Relational Completeness of Data Base Sublanguages, IBM Research, 1972. [2] G. Bezhanishvili and E. Landreth, An Introduction to Elementary Set Theory. [3] F. Stephan, Set Theory, 2009-2010. [4] A. Aho and J. Ullman, Foundations of Computer Science, 1992. [5] I. Ben-Gan, D. Sarka and R. Talmage, Querying Microsoft SQL Server 2012, 2012. [6] T. L. Saito, Silk: A Scalable Data Format In- Between Relations and Trees, Department of Computational Biology University of Tokyo, Japan. [7] S. Misner and M. Ross, Introducing Microsoft SQL Server 2014, Washington: Microsoft Press, 2014. [8] G. Fritchey, SQL Server 2012 Query Performance Tuning, New York: Apress, 2012.