Docsity

SQL Data Definition: Understanding the Role of SQL in Database Systems, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
SQL
• Originally ‘Sequel’ -
Structured English
QUEry Language,
part of an IBM
project in the 70’s
• Sequel was already
taken, so it became
SQL - Structured
Query Language
•ANSI Standards
•SQL-89
• SQL-92 (SQL2)
• SQL-99 (SQL3)
•Most modern DBMS
use a variety of SQL
• Most based on SQL2,
increasingly SQL3
• Few (if any) are true
to the standard
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
SQL
•SQL provides
• A data definition
language (DDL)
• A data manipulation
language (DML)
• A data control
language (DCL)
•In addition SQL
• Can be used from
other languages
• Is often extended to
provide common
programming
constructs (such as if-
then tests, loops,
variables, etc.)
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Notes
• SQL is (usually) not
case-sensitive, but
we’ll write SQL
keywords in upper
case for emphasis
• SQL statements will
be written in BOLD
COURIER FONT
•Strings in SQL are
surrounded by single
quotes:
'I AM A STRING'
• Single quotes within
a string are doubled:
'I''M A STRING'
• The empty string:''
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Non-Procedural Programming
• SQL is described as a
non-procedural
language
• Procedural - say
exactly what the
computer has to do
• Non-procedural - say
what you want the
computer to acheive
• Example: Given a
database with tables
• Student with
attributes ID, Name,
Address
•Courses with
attributes Code, Title
• Enrolment with
attributes ID, Code
• Get a list of students
who take the paper
‘Database Systems’
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Procedural Programming
Set C to be the first Course Record /* Find course code for */
Code = ‘’ /* ‘Database Systems’ */
While (C is not null) and (Code = ‘’)
If (C.Title = ‘Database Systems’) Then
Code = C.Code
Set C to be the next Course Record
Set NAMES to be empty /* A list of student names */
Set S to be the first Student Record
While S is not null /* For each student... */
Set E to be the first Enrolment Record
While E is not null /* For each enrolment... */
If (E.ID = S.ID) And /* If this student is */
(E.Code = Code) Then /* enrolled in DB Systems */
NAMES = NAMES + S.NAME /* add them to the list */
Set E to be the next Enrolment Record
Set S to be the next Student Record
Return NAMES
SQL Data Definition G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Non-Procedural (SQL)
SELECT Name FROM Student, Enrolment
WHERE
(Student.ID = Enrolment.ID)
AND
(Enrolment.Code =
(SELECT Code FROM Course WHERE
Title = ‘Database Systems’))
pf3
pf4

Partial preview of the text

Download SQL Data Definition: Understanding the Role of SQL in Database Systems and more Slides Introduction to Database Management Systems in PDF only on Docsity!

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

SQL

  • Originally ‘Sequel’ -

Structured English

QUEry Language,

part of an IBM

project in the 70’s

  • Sequel was already

taken, so it became

SQL - Structured

Query Language

  • ANSI Standards
    • SQL-
    • SQL-92 (SQL2)
    • SQL-99 (SQL3)
  • Most modern DBMS

use a variety of SQL

  • Most based on SQL2, increasingly SQL
  • Few (if any) are true to the standard

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

SQL

  • SQL provides
    • A data definition language (DDL)
    • A data manipulation language (DML)
    • A data control language (DCL) - In addition SQL - Can be used from other languages - Is often extended to provide common programming constructs (such as if- then tests, loops, variables, etc.)

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Notes

  • SQL is (usually) not

case-sensitive, but

we’ll write SQL

keywords in upper

case for emphasis

  • SQL statements will

be written in BOLD

COURIER FONT

  • Strings in SQL are

surrounded by single

quotes:

'I AM A STRING'

  • Single quotes within

a string are doubled:

'I''M A STRING'

  • The empty string:''

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Non-Procedural Programming

  • SQL is described as a

non-procedural

language

  • Procedural - say exactly what the computer has to do
  • Non-procedural - say what you want the computer to acheive - Example: Given a

database with tables

  • Student with attributes ID, Name, Address
  • Courses with attributes Code, Title
  • Enrolment with attributes ID, Code
  • Get a list of students

who take the paper

‘Database Systems’

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Procedural Programming

Set C to be the first Course Record /* Find course code for / Code = ‘’ / ‘Database Systems’ / While (C is not null) and (Code = ‘’) If (C.Title = ‘Database Systems’) Then Code = C.Code Set C to be the next Course Record Set NAMES to be empty / A list of student names / Set S to be the first Student Record While S is not null / For each student... / Set E to be the first Enrolment Record While E is not null / For each enrolment... / If (E.ID = S.ID) And / If this student is / (E.Code = Code) Then / enrolled in DB Systems / NAMES = NAMES + S.NAME / add them to the list */ Set E to be the next Enrolment Record Set S to be the next Student Record Return NAMES

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Non-Procedural (SQL)

SELECT Name FROM Student, Enrolment

WHERE

(Student.ID = Enrolment.ID)

AND

(Enrolment.Code =

(SELECT Code FROM Course WHERE

Title = ‘Database Systems’))

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

SQL, the Relational Model,

and E/R Design

  • SQL is based on the

relational model

  • It has many of the same ideas
  • Databases that support SQL are often described as relational datbases
  • It is not always true to the model - E/R designs can be

implemented in SQL

  • Entities, attributes, and relationships can all be expressed in terms of SQL
  • Many-to-many relationships are a problem, so should be removed

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relations, Entities, Tables

Relational model

Relation

Tuple

Attribute

Foreign Key

Primary Key

E/R Diagram

Entity

Instance

Attribute

M:1 Relationship

SQL

Table

Row

Column or Field

Foreign Key

Primary Key

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Implementing E/R Designs

  • Given an E/R design
    • The entities become SQL tables
    • Attributes of an entity become columns in the corresponding table
    • Relationships may be represented by foreign keys

Enrolment

Student

Module

In

Has

ID

Code

Title

Name

Address

Year

Exam Assignment

Assignment

Exam

Credits SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Entities and Attributes

  • Each entity becomes

a table in the

database

  • The name of the table is often the name of the entity
  • The attributes become columns of the table with the same name - A table called Student - With columns for ID, Name, Address, and Year

Student

ID

Name

Address

Year

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

CREATE TABLE

CREATE TABLE

(

<col-def-1>, <col-def-2>, : <col-def-n>, <constraint-1>, : <constraint-k>)

  • You supply
    • A name for the table
    • A list of column definitions
    • A list of constraints (such as keys)

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Column Definitions

<col-name>

[NULL|NOT NULL]

[DEFAULT ]

[constraint-1 [,

constraint-2[,

...]]]

  • Each column has a

name and a type

  • Common types
    • INT
    • REAL
    • CHAR(n)
    • VARCHAR(n)
    • DATE

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relationships

  • Depends on the type
    • 1:1 are usually not used, or can be treated as a special case of M:
    • M:1 are represented as a foreign key from the M-side to the 1
    • M:M are split into two M:1 relationships

Enrolment

Student

Module

In

Has

ID

Code

Title

Name

Address

Year

Exam Assignment

Assignment

Exam

Credits SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Representing Relationships

  • The Enrolment table
    • Will have columns for the Exam and Assignment attributes
    • Will have a foreign key to Student for the ‘has’ relationship
    • Will have a foreign key to Module for the ‘in’ relationship

Enrolment

Student

Module

In

Has

ID

Code

Title

Name

Address

Year

Exam Assignment

Assignment

Exam

Credits

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Foreign Keys

  • Foreign Keys are

also defined as

constraints

  • You need to give
    • The columns which make up the FK
    • The referenced table
    • The columns which are referenced by the FK

CONSTRAINT FOREIGN KEY (col1,col2,…) REFERENCES

[(ref1,ref2,…)]
  • If the FK references the PK of
you don’t need to list the columns

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example

CREATE TABLE Enrolment ( stuID INT NOT NULL, modCode CHAR(6) NOT NULL, enrAssignment INT, enrExam INT, CONSTRAINT enrPK PRIMARY KEY (stuID, modCode), CONSTRAINT enrStu FOREIGN KEY (stuID) REFERENCES Student (stuID), CONSTRAINT enrMod FOREIGN KEY (modCode) REFERENCES Module (modCode))

SQL Data Definition G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

This Lecture in Exams

Create a new table called “Reviews” which should have the following columns:

rID, an integer that will be the primary key

cID, an integer that will be a foreign key to the CD table

Title, a string of up to 100 characters

Text, a string of up to 5000 characters

(5 marks)