SQL Data Definition: Database Systems Lecture 5 Natasha Alechina

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

SQL Data Definition

Database Systems Lecture 5


Natasha Alechina
In This Lecture
• SQL
• The SQL language
• SQL, the relational model, and E/R diagrams
• CREATE TABLE
• Columns
• Primary Keys
• Foreign Keys
• For more information
• Connolly and Begg chapter 6
• Ullman and Widom 3.2, 6.6.

SQL Data Definition


SQL
• Originally ‘Sequel’ - • ANSI Standards
Structured English • SQL-89
query Language, • SQL-92 (SQL2)
part of an IBM • SQL-99 (SQL3)
project in the 70’s • Most modern DBMS
• Sequel was already use a variety of SQL
taken, so it became • Most based on SQL2,
SQL - Structured increasingly SQL3
Query Language • Few (if any) are true
to the standard

SQL Data Definition


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

SQL Data Definition


Notes
• SQL is (usually) not • Strings in SQL are
case-sensitive, but surrounded by single
we’ll write SQL quotes:
keywords in upper 'I AM A STRING'
case for emphasis • Single quotes within
• SQL statements will a string are doubled:
be written in BOLD 'I''M A STRING'
COURIER FONT • The empty string:''

SQL Data Definition


Non-Procedural Programming
• SQL is a declarative • Example: Given a
(non-procedural) database with tables
language • Student with
• Procedural - say attributes ID, Name,
exactly what the Address
computer has to do • Module with attributes
• Non-procedural – Code, Title
describe the required • Enrolment with
result (not the way to attributes ID, Code
compute it)
• Get a list of students
who take the module
‘Database Systems’
SQL Data Definition
Procedural Programming
Set M to be the first Module Record /* Find module code for */
Code = ‘’ /* ‘Database Systems’ */
While (M is not null) and (Code = ‘’)
If (M.Title = ‘Database Systems’) Then
Code = M.Code
Set M to be the next Module 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


Non-Procedural (SQL)
SELECT Name FROM Student, Enrolment
WHERE
(Student.ID = Enrolment.ID)
AND
(Enrolment.Code =
(SELECT Code FROM Module WHERE
Title = ‘Database Systems’))

SQL Data Definition


SQL, the Relational Model,
and E/R Design
• SQL is based on the • E/R designs can be
relational model implemented in SQL
• It has many of the • Entities, attributes,
same ideas and relationships can
• Databases that all be expressed in
support SQL are often terms of SQL
described as relational • Many-to-many
databases relationships are a
• It is not always true problem, so should be
to the model removed

SQL Data Definition


Relations, Entities, Tables

Relational model E/R Diagram SQL


Relation Entity Table
Tuple Instance Row
Attribute Attribute Column or Field
Foreign Key M:1 Relationship Foreign Key
Primary Key Primary Key

SQL Data Definition


Implementing E/R Designs
ID Address
• Given an E/R design Student
• The entities become Name Year
SQL tables Has
• Attributes of an entity
become columns in
the corresponding Exam Enrolment Assignment
table
• Relationships may be In
represented by Exam
foreign keys Code
Module Assignment
Title
Credits
SQL Data Definition
Entities and Attributes
• Each entity becomes
a table in the ID Address
database Student
Name Year
• The name of the table
is often the name of
the entity • A table called Student
• The attributes become • With columns for ID,
columns of the table Name, Address, and
with the same name Year

SQL Data Definition


CREATE TABLE
CREATE TABLE • You supply
<name> ( • A name for the table
<col-def-1>, • A list of column
<col-def-2>, definitions
: • A list of constraints
<col-def-n>, (such as keys)
<constraint-1>,
:
<constraint-k>)

SQL Data Definition


Column Definitions
• Each column has a
<col-name> <type> name and a type
[NULL|NOT NULL] • Common types
• INT
[DEFAULT <val>]
• REAL
[constraint-1 [, • CHAR(n)
constraint-2[, • VARCHAR(n)
...]]] • DATE

SQL Data Definition


Column Definitions
• Columns can be • Columns can be
specified as NULL or given a default value
NOT NULL • You just use the
• NOT NULL columns keyword DEFAULT
cannot have missing followed by the
values value, eg:
• If neither is given
then columns are num INT DEFAULT 0
assumed NULL

SQL Data Definition


Example
CREATE TABLE Student (
stuID INT NOT NULL,
stuName VARCHAR(50) NOT NULL,
stuAddress VARCHAR(50),
stuYear INT DEFAULT 1)

ID Address
Student
Name Year

SQL Data Definition


Constraints
CONSTRAINT • Each constraint is
<name> given a name -
<type> Access requires a
name, but some
<details> others don’t
• Common <type>s • Constraints which
• PRIMARY KEY refer to single
• UNIQUE columns can be
• FOREIGN KEY included in their
• INDEX definition

SQL Data Definition


Primary Keys
• Primary Keys are • The <details> for a
defined through primary key is a list
constraints of columns which
• A PRIMARY KEY make up the key
constraint also
includes a UNIQUE CONSTRAINT <name>
constraint and
PRIMARY KEY
makes the columns
involved NOT NULL (col1, col2, …)

SQL Data Definition


Unique Constraints
• As well as a single • The <details> for a
primary key, any set unique constraint are
of columns can be a list of columns
specified as UNIQUE which make up the
• This has the effect of candidate key
making candidate
keys in the table CONSTRAINT <name>
UNIQUE
(col1, col2, …)

SQL Data Definition


Example
CREATE TABLE Student (
stuID INT NOT NULL,
stuName VARCHAR(50) NOT NULL,
stuAddress VARCHAR(50),
stuYear INT DEFAULT 1,
CONSTRAINT pkStudent
PRIMARY KEY (stuID))

SQL Data Definition


Relationships
ID Address
• Depends on the type Student
• 1:1 are usually not Name Year
used, or can be Has
treated as a special
case of M:1
• M:1 are represented Exam Enrolment Assignment
as a foreign key from
the M-side to the 1 In
• M:M are split into two Exam
M:1 relationships Code
Module Assignment
Title
Credits
SQL Data Definition
Representing Relationships
ID Address
• The Enrolment table Student
• Will have columns for Name Year
the Exam and
Has
Assignment attributes
• Will have a foreign key
to Student for the Exam Enrolment Assignment
‘has’ relationship
• Will have a foreign key In
to Module for the ‘in’ Exam
relationship Code
Module Assignment
Title
Credits
SQL Data Definition
Foreign Keys
• Foreign Keys are CONSTRAINT <name>
also defined as FOREIGN KEY
constraints (col1,col2,…)
• You need to give REFERENCES
<table>
• The columns which
make up the FK [(ref1,ref2,…)]
• The referenced table • If the FK references
• The columns which the PK of <table> you
are referenced by the don’t need to list the
columns
FK

SQL Data Definition


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
Next Lecture
• More SQL
• DROP TABLE
• ALTER TABLE
• INSERT, UPDATE, and DELETE
• Data dictionary
• Sequences
• For more information
• Connolly and Begg chapters 5 and 6
• Ullman and Widom 6.5
SQL Data Definition
Coursework
• Cw1: Entity-relationship diagram and table definitions –
deadline 29 February at 3, submit to the School Office.

• Cw2: SQL creating tables in Oracle – marked in the labs


on 20/02, latest submission by email to me 22/02.

• Labs start on the 13th of February (next week).


• This week there are still no labs, but you can start on
your own:
• Set up an Oracle account (instructions on
http://www.cs.nott.ac.uk/~nza/G51DBS)
• Start creating tables required in the first SQL exercise
(cw2)
SQL Data Definition

You might also like