3 Basic
Integrity Constraints
that can be defined in a Relation
primary key( Aj1, Aj2, … , Ajm)
Specifies given attributes as the primary key (all together)
Primary key attributes must be non-null and unique
foreign key( Ak1, Ak2, … , Akn) references Relation_S
Attributes must be the Primary Key of some tuple in the other relation
not null
Added to a specific attribute
example: name varchar(20) not null,
Important Parts/Features
of the SQL Language
(7)
SQL Features:
Data Definition Language( DDL )
Commands for
SQL Features:
Data Manipulation Language( DML )
Commands for:
SQL Features:
Integrity
Part of the DDL part of SQL
SQL provides commands for specifying Integrity Constraints
Also prevents updates from violating Integrity Constraints
SQL Features:
View Definition
SQL includes commands for
Defining Views
Part of being DDL
SQL Features;
Transaction Control
Commands for specifying the
beginning and end
of transactions
SQL Features:
Embedded SQL/Dynamic SQL
Defines how SQL statements can be
embedded within
general purpose languages
SQL Features:
Authorization
Includes commands for specifying
Access Rights
to relations and views
Two Aspects of
SQL as a Language
The SQL Language provides functionality for:
Three kinds of Relations
in SQL
SQL Statement
to Define a Relation Schema
CREATE TABLE
SQL Primitive Data Types
CHAR(n), VARCHAR(n)
BIT(n), BITVARYING(n)
BOOLEAN
INT, or INTEGER
FLOAT, or REAL, DOUBLE PRECISION
DECIMAL( n, d)
DATE
TIME
SQL Example:
Declare this schema in SQL:
Movies( title: string, year:integer, genre: string)
CREATE TABLE Movies (
title CHAR(100),
year INT,
genre CHAR(10)
);
end with semicolon, each attribute has the form:
name TYPE
The SQL DDL
allows Specification of
What Relation Properties?
Basic SQL Table Commands (5)
CREATE TABLE r( A1 D1, A2 D2, … );
creates a table with the specified attributes and types
DROP TABLE r;
remove a table from the database
ALTER TABLE r ADD a d;
add the attribute ‘a’ with type ‘d’ to the table
ALTER TABLE r DROP a;
remove attribute ‘a’ from the table
DELETE FROM r;
INSERT INTO r values( v1, v2, … , vn);
add a tuple into the table r