0% found this document useful (0 votes)
3 views

SQL-DDL

The document introduces SQL as the standard query language for relational databases, detailing its structure and implementation through a case study. It covers various aspects of SQL including Data Definition Language (DDL), Data Manipulation Language (DML), and integrity constraints, along with examples of creating tables and managing database schemas. Additionally, it discusses the history of SQL and provides a case study on a university library database design and implementation.

Uploaded by

Marie Sarkis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL-DDL

The document introduces SQL as the standard query language for relational databases, detailing its structure and implementation through a case study. It covers various aspects of SQL including Data Definition Language (DDL), Data Manipulation Language (DML), and integrity constraints, along with examples of creating tables and managing database schemas. Additionally, it discusses the history of SQL and provides a case study on a university library database design and implementation.

Uploaded by

Marie Sarkis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

SQL Language

§ Structure of Relational Databases

§ Database implementation - Case study


Introduction

® 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.

® Any RA query can also be formulated in SQL, but in a more user-friendly


manner.

® In addition, SQL contains certain features of great practical importance


that go beyond the expressiveness of RA, e.g. sorting and aggregation
functions.

2
Database Application; Engineering building process
Real World
Database Implementation (Case Study)

Data
Design

Database Set of inter-related tables;


Students, Courses, Teachers, …
Schema
We are here Implementation
? DBMS to use
MSSQLServer, MySql, Oracle, etc.

Front-end; Physical Database Schema


end-user applications

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)

® The database schema changes very infrequently


® The database state changes every time the database is updated

5
Database Languages

® Data Definition Language DDL


§ Create and removes schemas and tables
− Create schema, Create table, Drop table, ….

® Data Manipulation Language DML


§ Insert, Update, Delete rows in table
− Insert, Delete, Update
§ Retrieve rows from tables
− Select

® Data Control Language


§ Access Control
− Grant, Revoke
6
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 relation.

§ Security and authorization information for each relation.

§ The physical storage structure of each relation on disk.

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.

• create domain in SQL-92 creates user-defined domain types


create domain person-name char(20) not null
8
Date/Time Types in SQL (Cont.)
® 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

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>
)

§ r is the name of the relation A domain consisting of


§ each Aj is an attribute name and Dj is the domain of Aj all 15-char strings,
excluding null

create table branch (


branch-name char(15) not null,
branch-city char(30),
assets integer )

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

® drop table deletes the schema and the tuples of a relation

® 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

USE master – the master DB


GO Database Schema

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

create table Course (


cid char(5) not null,
coordinator char(5) not null,
teacher char(5) not null,
ccode varchar(10) null,
cname varchar(50) null,
hours int null,
credits int null,
primary key (cid),
foreign key (teacher) references Teacher (tid),
foreign key (coordinator) references Teacher (tid)
)

create table Register (


student char(5) not null,
course char(5) not null, create table Teacher (
exam char(5) not null, tid char(5) not null,
note decimal(6,2) null, tname varchar(30) null,
primary key (student, course, exam), phone varchar(24) null,
foreign key (course) references Course (cid), address varchar(30) null,
foreign key (exam) references Exam (xid), speciality varchar(30) null,
foreign key (student) references Student (sid) primary key (tid)
) )

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)
)

create table Exam (


xid char(5) not null,
xname varchar(30) null,
xdate datetime null,
create table Teacher (
tid char(5) not null,
location varchar(50) null,
tname varchar(30) null,
constraint PK_EXAM primary key nonclustered (xid)
phone varchar(24) null,
)
address varchar(30) null,
speciality varchar(30) null,
primary key (tid)
)

16
UnivRegDB
DDL scripts

create table Register (


student char(5) not null,
course char(5) not null,
exam char(5) not null,
note decimal(6,2) null,

primary key (student, course, exam), create table Author (


teacher char(5) not null,
foreign key (course) references Course (cid), course char(5) not null,
foreign key (exam) references Exam (xid), primary key (teacher, course),
foreign key (student) references Student (sid)
foreign key (course) references Course (cid),
)
foreign key (teacher) references Teacher (tid)
)
17
Case study - University Library
§ Database design
− ER modeling
− Mapping ERDiagram to Database schema (Physical
Data Model)
§ DDL to create the database
University Library
Consider a typical university library to borrow books; the database-application of the
library considers information about books, students, teachers, authors, publishers
(publishing houses), and about borrowing operations.

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)
)

create table BooKAuthor (


book char(10) not null,
author char(10) not null,

-- key constraints
primary key (book, author),
foreign key (book) references Books (bookId),
foreign key (author) references Authors(authId)
)
23

You might also like