❖ Relational Data Definition |
In order to give a relational data model, we need to:
❖ SQL Data Definition Language |
In the example schema above, we provided only
SQL has a rich data definition language (DDL) that can describe
❖ Defining a Database Schema |
Tables (relations) are described using:
CREATE TABLE TableName ( attribute1 domain1 constraints1, attribute2 domain2 constraints2, ... table-level constraints, ... )
This SQL statement ..
DROP TABLE TableName;
❖ Defining a Database Schema (cont) |
Example: defining the Students
CREATE TABLE Students ( zid serial, family varchar(40), given varchar(40) NOT NULL, d_o_b date NOT NULL, gender char(1) CHECK (gender in ('M','F')), degree integer, PRIMARY KEY (zid), FOREIGN KEY (degree) REFERENCES Degrees(did) );
Note that there is much more info here than in the relational schema diagram.
A primary key attribute is implicitly defined to be UNIQUE
NOT NULL
❖ Defining a Database Schema (cont) |
Example: alternative definition of the Students
CREATE DOMAIN GenderType AS char(1) CHECK (value in ('M','F')); CREATE TABLE Students ( zid serial PRIMARY KEY, -- only works if primary key is one attr family text, -- no need to worry about max length given text NOT NULL, d_o_b date NOT NULL, gender GenderType, degree integer REFERENCES Degrees(did) );
At this stage, prefer to use the long-form declaration of primary and foreign keys
❖ Defining a Database Schema (cont) |
Example: defining the Courses
CREATE TABLE Courses ( cid serial, code char(8) NOT NULL uhs CHECK (code ~ '[A-Z]{4}[0-9]{4}'), term char(4) NOT NULL CHECK (term ~ '[0-9]{2}T[0-3]'), title text UNIQUE NOT NULL, PRIMARY KEY (cid) );
Uses non-standard regular expression checking on code
term
No two Courses
❖ Defining a Database Schema (cont) |
Example: defining the Enrolments
CREATE TABLE Enrolments ( student integer, course integer, mark integer CHECK (mark BETWEEN 0 AND 100), grade GradeType, PRIMARY KEY (student,course), FOREIGN KEY (student) REFERENCES Students(zid) FOREIGN KEY (course) REFERENCES Courses(cid) );
Could not enforce total partcipation constraint if e.g. all courses must have > 0 students
Possible alternative names for foreign keys student_id
course_id
❖ Data Integrity |
Defining tables as above affects behaviour of DBMS when changing data
Constraints and types ensure that integrity of data is preserved
❖ Default Values |
Can specify a DEFAULT
CREATE TABLE Accounts (
acctNo char(5) PRIMARY KEY,
branch varchar(30) REFERENCES Branches(name)
DEFAULT 'Central',
owner integer REFERENCES Customers(custID),
balance float DEFAULT 0.0
);
INSERT INTO Accounts(acctNo,owner) VALUES ('A-456',645342)
-- produces the tuple
Accounts('A-456', 'Central', 645342, 0.0)
❖ Defining Keys |
Primary keys:
NOT NULL
UNIQUE
FOREIGN KEY
customer integer FOREIGN KEY REFERENCES Customers(customerNo)
❖ Defining Keys (cont) |
Defining primary keys assures entity integrity
INSERT INTO Enrolments(student,course,mark,grade) VALUES (5123456, NULL, NULL, NULL);
because no course
mark
grade
NULL
Defining primary keys assures uniqueness
❖ Defining Keys (cont) |
Defining foreign keys assures referential integrity.
On insertion, cannot add a tuple where FK value does not exist as a PK
For example, this insert would fail ...
INSERT INTO Accounts(acctNo, owner, branch, balance) VALUES ('A-123', 765432, 'Nowhere', 5000);
if there is no customer with id 765432
Nowhere
❖ Defining Keys (cont) |
On deletion, interesting issues arise, e.g.
Accounts.branch
Branches.name
If we want to delete a tuple from Branches
Accounts
Account
Account
❖ Attribute Value Constraints |
NOT NULL
UNIQUE
SQL has a general mechanism for specifying attribute constraints
attrName type CHECK ( Condition )
Condition is a boolean expression and can
involve other attributes, relations and SELECT
CREATE TABLE Example ( gender char(1) CHECK (gender IN ('M','F')), Xvalue integer NOT NULL, Yvalue integer CHECK (Yvalue > Xvalue), Zvalue float CHECK (Zvalue > (SELECT MAX(price) FROM Sells) ) );
(but many RDBMSs (e.g. Oracle and PostgreSQL) don't allow SELECT
CHECK
❖ Named Constraints |
A constraint in an SQL table definition can (optionally) be named via
CONSTRAINT constraintName constraint
Example:
CREATE TABLE Example ( gender char(1) CONSTRAINT GenderCheck CHECK (gender IN ('M','F')), Xvalue integer NOT NULL, Yvalue integer CONSTRAINT XYOrder CHECK (Yvalue > Xvalue), );
Produced: 21 Sep 2020