Introduction to Basic SQL

 

Introduction to Basic SQL

SQL (Structured Query Language) is the standard language for relational database management systems (RDBMSs) and is one of the main reasons for the widespread commercial success of relational databases. Because SQL is standardized, users can migrate database applications between different relational DBMS products with relatively low cost and effort, provided they adhere to standard SQL features.

SQL enables portability, interoperability, and vendor independence, allowing applications to access data stored in multiple relational DBMSs using the same language.


SQL vs Formal Relational Languages

The formal relational model is based on:

  • Relational Algebra (procedural)

  • Relational Calculus (declarative)

Relational algebra requires users to specify how to retrieve data (step-by-step operations), which is too low-level for most users. SQL, on the other hand, is a high-level declarative language, where users specify what data they want, and the DBMS decides how to execute and optimize the query.

Although SQL borrows ideas from relational algebra, it is more closely based on tuple relational calculus and provides a more user-friendly syntax.


History and Standardization of SQL

  • Originally developed at IBM Research as SEQUEL for the System R project

  • Renamed SQL (Structured Query Language)

  • Standardized jointly by:

    • ANSI (American National Standards Institute)

    • ISO (International Standards Organization)

Major SQL Standards:

  • SQL-86 (SQL1)

  • SQL-92 (SQL2) – major expansion

  • SQL:1999 (SQL3) – object-oriented features

  • SQL:2003 & SQL:2006 – XML support

  • SQL:2008, SQL:2011 – additional object and advanced features

New features are added gradually, and commercial DBMSs may take time to fully support them.


Scope and Capabilities of SQL

SQL is a comprehensive database language, supporting:

  • DDL (Data Definition Language) – schema and table creation

  • DML (Data Manipulation Language) – queries and updates

  • View definition

  • Integrity constraints

  • Authorization and security

  • Transaction control

  • Embedded SQL in programming languages (Java, C/C++)

Later standards divide SQL into:

  • Core SQL – must be implemented by all compliant DBMSs

  • Optional extensions – for applications like OLAP, data mining, spatial data, multimedia, etc.


Organization of SQL 

  • Basic SQL

    • Schema and table creation

    • Constraints

    • Simple queries

    • Insert, delete, update

  • Advanced SQL

    • Complex queries

    • Views

    • Assertions

    • Triggers

  • Other SQL features  (XML, transactions, security, OLAP, etc.)


SQL Terminology vs Relational Model

Relational Model    SQL Term
Relation            Table
Tuple        Row
Attribute        Column

These terms are used interchangeably.


SQL Schemas and Catalogs

SQL Schema

  • Introduced in SQL-92

  • Groups related database objects

  • Identified by:

    • Schema name

    • Authorization identifier (owner)

Schema elements include:

  • Tables

  • Views

  • Domains

  • Types

  • Constraints

  • Authorization rules

Example:

CREATE SCHEMA COMPANY AUTHORIZATION 'Jsmith';

Only authorized users can create schemas and schema elements.

Catalog

  • A collection of schemas

  • Contains a special schema called INFORMATION_SCHEMA, which stores metadata

  • Referential integrity constraints can be defined only within the same catalog


CREATE TABLE Command

The CREATE TABLE statement defines a base relation by:

  • Specifying table name

  • Declaring attributes and data types

  • Defining constraints:

    • PRIMARY KEY

    • UNIQUE

    • FOREIGN KEY

    • NOT NULL

Tables created using CREATE TABLE are called base tables and are physically stored by the DBMS.

Attributes are ordered, but rows are unordered.

Foreign key constraints involving:

  • Circular references

  • Forward references
    may need to be added later using ALTER TABLE.


SQL Data Types

Numeric Data Types

  • INTEGER, SMALLINT

  • FLOAT, REAL, DOUBLE PRECISION

  • DECIMAL(i, j) / NUMERIC(i, j) for fixed precision


Character String Types

  • Fixed-length: CHAR(n)

  • Variable-length: VARCHAR(n)

  • Large text: CLOB

String literals:

  • Enclosed in single quotes

  • Case-sensitive

  • Can be concatenated using ||


Bit String Types

  • BIT(n), BIT VARYING(n)

  • Large binary data: BLOB

  • Bit literals prefixed with B or X (hexadecimal)


Boolean Type

  • TRUE, FALSE

  • UNKNOWN (due to NULL values)

  • Uses three-valued logic


Date and Time Types

  • DATE: YYYY-MM-DD

  • TIME: HH:MM:SS

  • TIMESTAMP: DATE + TIME + fractional seconds

  • TIME WITH TIME ZONE

Comparisons are allowed based on chronological order.


INTERVAL Type

  • Represents a relative time period

  • Used to add or subtract from dates/timestamps

  • Types:

    • YEAR–MONTH

    • DAY–TIME


Domains and User-Defined Types

Domains

  • Named reusable data types

  • Improve readability and maintainability

Example:

CREATE DOMAIN SSN_TYPE AS CHAR(9);

Domains can include default values and constraints.

User-Defined Types (UDTs)

  • Created using CREATE TYPE

  • Used for object-oriented features

  • Discussed in later chapters


Key Takeaway 

SQL is a standardized, declarative, and comprehensive language for relational databases that supports data definition, manipulation, constraints, views, transactions, and advanced database features, making it the foundation of modern relational DBMSs.

 

Comments

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS