Lecture 8 DDL
Lecture 8 DDL
4
Data Definition Language (DDL)
• Allows the specification of not only a set of
relations but also information about each
relation, including:
– The schema for each relation.
– The domain of values associated with each attribute.
– Integrity constraints
– The set of indices to be maintained for each relations.
– Security and authorization information for each
relation.
– The physical storage structure of each relation on disk.
5
SQL Data Definition and Data
Types
• Terminology:
– Table, row, and column used for relational model
terms relation, tuple, and attribute
– Schema with database
• CREATE statement
– Main SQL command for data definition
6
Schema and Catalog Concepts
in SQL
• SQL schema
– Identified by a schema name
– Includes an authorization identifier and descriptors
for each element
• Schema elements include
– Tables, constraints, views, domains, and other
constructs
• Each statement in SQL ends with a semicolon
7
Schema and Catalog Concepts
in SQL (cont’d.)
• CREATE SCHEMA statement
– CREATE SCHEMA COMPANY AUTHORIZATION
‘Jsmith’;
• Catalog
– Named collection of schemas in an SQL environment
– Special schema: INFORMATION_SCHEMA
• SQL environment
– Installation of an SQL-compliant RDBMS on a
computer system
8
The CREATE TABLE Command
in SQL
• Specify a new relation
– Provide name
– Specify attributes and initial constraints
• Can optionally specify schema:
– CREATE TABLE COMPANY.EMPLOYEE ...
or
– CREATE TABLE EMPLOYEE ...
9
The CREATE TABLE Command
in SQL (cont’d.)
• Base tables (base relations)
– Relation and its tuples are actually created and stored
as a file by the DBMS
• Virtual relations
– Created through the CREATE VIEW statement
10
Create Table Construct
• An SQL relation is defined using the create table
command:
create table R (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
– r is the name of the relation
– each Ai is an attribute name in the schema of relation r
– Di is the data type of values in the domain of attribute Ai
• Example:
create table BRANCH
(branch-name char(15) not null,
branch-city char(30),
11
12
13
The CREATE TABLE Command
in SQL (cont’d.)
• Some foreign keys may cause errors
– Specified either via:
o Circular references
o Or because they refer to a table that has not yet been created
14
Attribute Data Types and
Domains in SQL
• Basic data types
– Numeric data types
o Integer numbers: INTEGER, INT, and SMALLINT
o Floating-point (real) numbers: FLOAT or REAL, and
DOUBLE PRECISION
– Character-string data types
o Fixed length: CHAR(n), CHARACTER(n)
o Varying length: VARCHAR(n), CHAR VARYING(n),
CHARACTER VARYING(n)
o CHARACTER LARGE OBJECT (CLOB)
– Kilobytes or Megabytes
15
Attribute Data Types and
Domains in SQL (cont’d.)
– Bit-string data types
o Fixed length: BIT(n)
o Varying length: BIT VARYING(n)
o BINARY LARGE OBJECT (BLOB)
– Boolean data type
o Values of TRUE or FALSE or NULL
– DATE data type
o Ten positions
o Components are YEAR, MONTH, and DAY in the form
YYYY-MM-DD
16
Attribute Data Types and
Domains in SQL (cont’d.)
• Additional data types
– Timestamp data type (TIMESTAMP)
o E.g. timestamp ‘2001-7-27 09:00:30.75’
o Includes the DATE and TIME fields
o Plus a minimum of six positions for decimal fractions of
seconds
o Optional WITH TIME ZONE qualifier
17
Date/Time Types in SQL
• date. Dates, containing a (4 digit) year, month and date
– E.g. date ‘2001-7-27’
• time. Time of day, in hours, minutes and seconds.
– E.g. time ’09:00:30’ time ’09:00:30.75’
• timestamp: date plus time of day
– E.g. timestamp ‘2001-7-27 09:00:30.75’
• Interval: period of time
– E.g. Interval ‘1’ day
– Subtracting a date/time/timestamp value from another gives an interval
value
– Interval values can be added to date/time/timestamp values
• Can extract values of individual fields from date/time/timestamp
– E.g. extract (year from r.starttime)
• Can cast string types to date/time/timestamp
– E.g. cast <string-valued-expression> as date
18
Specifying Constraints in SQL
• Basic constraints:
– Key Constraints
– Referential integrity constraints
– Restrictions on attribute domains and NULLs
– Constraints on individual tuples within a relation
19
Specifying Attribute Constraints
and Attribute Defaults
• NOT NULL
– NULL is not permitted for a particular attribute
• Default value
– DEFAULT <value>
• CHECK clause
– Dnumber INT NOT NULL CHECK (Dnumber >
0 AND Dnumber < 21);
20
21
Specifying Key and Referential
Integrity Constraints
• PRIMARY KEY clause
– Specifies one or more attributes that make up the
primary key of a relation
– Dnumber INT PRIMARY KEY;
• UNIQUE clause
– Specifies alternate (secondary) keys
– Dname VARCHAR(15) UNIQUE;
22
Specifying Key and Referential
Integrity Constraints (cont’d.)
• FOREIGN KEY clause
– Default operation: reject update on violation
– Attach referential triggered action clause
o Options include SET NULL, CASCADE, and SET
DEFAULT
o Action taken by the DBMS for SET NULL or SET
DEFAULT is the same for both ON DELETE and ON
UPDATE
o CASCADE option suitable for “relationship” relations
A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server.
23
Giving Names to Constraints
• Keyword CONSTRAINT
– Name a constraint
– Useful for later altering
24
Specifying Constraints on
Tuples Using CHECK
• CHECK clauses at the end of a CREATE TABLE
statement
– Apply to each tuple individually
– CHECK (Dept_create_date <=
Mgr_start_date);
25
Drop and Alter Table Constructs
• The drop table command deletes all information about the dropped
relation from the database.
• The alter table command is used to add attributes to an existing
relation. All tuples in the relation are assigned null as the value for
the new attribute. The form of the alter table command is
alter table r add A D
where A is the name of the attribute to be added to relation r and D is
the domain of A.
• The alter table command can also be used to drop attributes of a
relation
27
The INSERT Command
• Specify the relation name and a list of values for
the tuple
28
The DELETE Command
• Removes tuples from a relation
– Includes a WHERE clause to select the tuples to be
deleted
29
The UPDATE Command
• Modify attribute values of one or more selected
tuples
• Additional SET clause in the UPDATE command
– Specifies attributes to be modified and new values
30
Additional Features of SQL
• Techniques for specifying complex retrieval
queries
• Writing programs in various programming
languages that include SQL statements
• Set of commands for specifying physical database
design parameters, file structures for relations, and
access paths
• Transaction control commands
31
Additional Features of SQL
(cont’d.)
• Specifying the granting and revoking of privileges
to users
• Constructs for creating triggers
• Enhanced relational systems known as object-
relational
• New technologies such as XML and OLAP
32