SQL-DDL
SQL-DDL
® We now introduce SQL, the standard query language for relational DBS.
® As RA, an SQL query takes one or two input tables and returns one
output table.
2
Database Application; Engineering building process
Real World
Database Implementation (Case Study)
Data
Design
App-1
S Q L (DDL)
App-2
SQL Relational Database
Database
... (DML) Management System
App-n
(RDBMS) ...
Back-end; Data server side
3
History
® IBM Sequel language developed as part of System R project at the IBM San Jose
Research Laboratory
® Renamed Structured Query Language (SQL)
® ANSI and ISO standard SQL:
§ SQL-86
§ SQL-89
§ SQL-92
§ SQL:1999 (language name became Y2K compliant!)
§ SQL:2003
® Commercial systems offer most, if not all, SQL-92 features, plus varying feature
sets from later standards and special proprietary features.
§ Not all examples here may work on your particular system.
4
Database Schema and Instance
® Database schema
§ The description of a database
− Database structure
− Constraints that should hold on the database
§ Also called Intension
® Database Instance
§ The actual data stored in the database at a particular moment
§ Also called database state (or snapshot)
§ Also called extension (of the schema)
5
Database Languages
§ Integrity constraints.
7
Data Definition Language(DDL)
® Domain Types in SQL
ü char(n) Fixed length character string, with user-specified length n.
ü varchar(n) Variable length character string, with user-specified maximum length n.
ü int integer (a finite subset of the integers that is machine-dependent).
ü smallint Small integer (a machine-dependent subset of the integer domain type).
ü Numeric(p,d) Fixed point number, with user-specified precision of p digits, with d digits to the right of
decimal point.
ü real, double precision Floating point and double-precision floating point numbers, with machine-dependent
precision.
ü float(n) Floating point number, with user-specified precision of at least n digits.
ü date Dates, containing a (4 digits) year, month and date.
ü time Time of day, in hours, minutes and seconds.
® Remarks
• Null values are allowed in all the domain types. Declaring an attribute
to be not null prohibits null values for that attribute.
9
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>
)
10
Integrity Constraints
® not null
® primary key(A1,…, An)
§ ensures uniqueness of key values by definition;
§ automatically ensures not null in SQL-92
® check(P), ensures predicate P is always true
® Example: declare branch-name as primary key for branch and ensure that the values of
assets are non-negative.
create table branch
( branch-name char(15) not null,
branch-citychar(30),
assets integer,
primary key (branch-name),
check (assets >=0) )
primary key declaration on an attribute automatically ensures not null in
SQL-92 onwards.
11
Drop and Alter Table Constructs
® 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.
alter table r add A D
where A is the name of the attribute to be added to relation r and D is domain
of A.
® Example: Alter table employee Add Job varchar(12)
® The alter table command can also be used to drop attributes of a relation
alter table r drop A
where A is the name of an attribute of relation r.
12
Student data; courses, exams, …
13
Database creation UnivRegDB
Database
CREATE DATABASE UnivReg2022 implementation
ON ( NAME = UnivReg2022_dat,
FILENAME = 'D:\DBLabs\data\UnivReg2022.mdf')
LOG ON ( NAME = 'UnivReg2022_log',
FILENAME = 'D:\DBLabs\data\UnivReg2022.ldf') UnivRegDB.mdf
GO
UnivRegDB.ldf
14
DDL scripts
15
DDL scripts
create table Student (
sid char(5) not null,
tutor char(5) not null,
supervisor char(5) not null,
sname varchar(30) null,
bdate datetime null,
phone varchar(24) null,
primary key (sid),
foreign key (supervisor) references Teacher (tid),
foreign key (tutor) references Teacher (tid)
)
16
UnivRegDB
DDL scripts
The managers of the library enforce that a student will not be allowed to borrow more
than three books simultaneously (during the same period). The library indicates that a
book can be written by several authors.
Note that, there are several copies by book.
Questions:
1. Give the corresponding ER-diagram.
2. Transform the above ER-diagram into relational model; draw-
up the corresponding physical diagram.
3. Give the DDL scripts leading to create the tables.
19
Database Diagram - PDM
20
Database creation
USE master
GO
CREATE DATABASE ULibDB
ON ( NAME = ULibDB_dat, FILENAME = 'H:\... \DataBase\lab\ULibrary.Lab\data\ULibDB.mdf')
LOG ON
( NAME = 'ULibDB_log',
Database
FILENAME = 'H:\... \DataBase\lab\ULibrary.Lab\data\ULibDB.ldf') Schema
go
Database
implementation
ULibDB
• ULibDB.mdf
• ULibDB.ldf
21
Database creation (1/2)
create table Books ( create table Authors (
bookId char(10) not null, authId char(10) not null,
publisher char(10) not null, authName varchar(30) null,
title varchar(50) null, Nationality varchar(30) null,
nbOfPages int null, domains varchar(50) null,
keyWords varchar(200) null, -- key constraints
bookLang char(5) null, primary key (authId)
nbOfCopies int null, )
nbOfCopiesInLib int null, go
publishingDate datetime null,
create table Publishers (
-- key constraints pubId char(10) not null,
primary key (bookId), pubName varchar(30) null,
foreign key (publisher) pudAddress varchar(30) null,
references Publishers (pubId) -- key constraints
) primary key (pubId)
go )
go
create table Students (
studId char(5) not null, create table Teachers (
name varchar(30) null, teachId char(5) not null,
address varchar(30) null, name varchar(30) null,
phone varchar(20) null, address varchar(30) null,
email varchar(30) null, phone varchar(20) null,
-- key constraints email varchar(30) null,
primary key (studId) -- key constraints
) primary key (teachId)
go )
go
22
Database creation (2/2)
create table Borrowings (
borrowId char(10) not null,
student char(5) null,
teacher char(5) null,
book char(10) not null,
borrowingFrom datetime null,
borrowingTo datetime null,
-- key constraints
primary key (borrowId),
foreign key (student) references Students (studId),
foreign key (book) references Books (bookId),
foreign key (teacher) references Teachers (teachId)
)
-- key constraints
primary key (book, author),
foreign key (book) references Books (bookId),
foreign key (author) references Authors(authId)
)
23