SQL, More Relational Model




CS157a

Chris Pollett

Sep 9, 2019

Outline

Introduction

SQL - Overview

SQL - Relations

SQL - Types

Before we give the syntax of CREATE TABLE in any detail, let's consider some of the supported attribute data types:

  1. Character strings of fixed or varying length. CHAR(n) is used to denote a fixed-length string of up to `n` characters padded using trailing spaces to make `n` chars. So a CHAR(5) column asked to store 'foo' stores it as 'foo '. VARCHAR(n) is used to store strings of up to `n` characters. These are usually represented as null terminated strings or as structs with a length component.
  2. Bit strings of fixed or varying length. BIT(n) and BIT VARYING(n) denote strings of length `n` or up to length `n` respectively.
  3. BOOLEAN. This can have as values TRUE, FALSE, or UNKNOWN.
  4. INT or INTEGER. These denote integer values (32 bits). SHORTINT and BIGINT might be used to store an integer number of half as many or twice as many bits.
  5. Floating-point numbers. Typical floating point numbers (32 bits) are indicated using REAL or FLOAT (synonyms). Higher precision floats can be represented using DOUBLE PRECISION. There is also a fixed decimal point type DECIMAL(n,d) where `n` is the total number of digits and d is after the decimal point. For example, DECIMAL(6,2) might store a value like: 0123.45
  6. Dates and times. DATE for dates and TIME for times. Example values might be: DATE '1948-05-14' and TIME '15:00:02.5'.

Quiz

Which of the following is true?

  1. To fully describe a data model one must specify how it structures data, what operations it allows, and what kinds of constraints it permits.
  2. The DBMS log manager is responsible for transaction concurrency control.
  3. To speed up DBMS access, log records are only stored in RAM.

SQL - Table Definitions

The simplest form of declaration of a relation in SQL consists of the keywords CREATE TABLE followed by the name of the relation and a parenthesized, comma separated list of the attribute names and their types. Below are some examples:

CREATE TABLE Movies (
    title CHAR(100), 
    year INT, 
    length INT, 
    genre CHAR(10), 
    studioName CHAR(30), 
    producerC# INT
);

CREATE TABLE MovieStar (
    name CHAR(30),
    address VARCHAR(255), 
    gender CHAR(1), 
    birthdate DATE
);

SQL - Table Modification

SQL - Default Values

Declaring Keys

Algebraic Query Languages

Overview of the Relational Algebra

Set Operations on Relations

The three most common operations on sets are union, intersection, and difference. For relations, we will use the following notations and definitions for their meaning:

Projection

Selection