SlideShare a Scribd company logo
1 of 26
SQL Data Definition
Instructor:
Muhammad Javaid Iqbal
Lecturer
javaid.iqbal@superior.edu.pk
SQL Data Definition
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’ -
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
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
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
Non-Procedural Programming
• SQL is a declarative
(non-procedural)
language
• Procedural - say
exactly what the
computer has to do
• Non-procedural –
describe the required
result (not the way to
compute it)
• Example: Given a
database with tables
• Student with
attributes ID, Name,
Address
• Module with attributes
Code, Title
• Enrolment with
attributes ID, Code
• 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
relational model
• It has many of the
same ideas
• Databases that
support SQL are often
described as relational
databases
• 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
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
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
Assignment
Exam
Assignment
Exam
Credits
SQL Data Definition
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
CREATE TABLE
CREATE TABLE
<name> (
<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
Column Definitions
<col-name> <type>
[NULL|NOT NULL]
[DEFAULT <val>]
[constraint-1 [,
constraint-2[,
...]]]
• Each column has a
name and a type
• Common types
• INT
• REAL
• CHAR(n)
• VARCHAR(n)
• DATE
SQL Data Definition
Column Definitions
• Columns can be
specified as NULL or
NOT NULL
• NOT NULL columns
cannot have missing
values
• If neither is given
then columns are
assumed NULL
• Columns can be
given a default value
• You just use the
keyword DEFAULT
followed by the
value, eg:
num INT DEFAULT 0
SQL Data Definition
Example
CREATE TABLE Student (
stuID INT NOT NULL,
stuName VARCHAR(50) NOT NULL,
stuAddress VARCHAR(50),
stuYear INT DEFAULT 1)
Student
ID
Name
Address
Year
SQL Data Definition
Constraints
CONSTRAINT
<name>
<type>
<details>
• Common <type>s
• PRIMARY KEY
• UNIQUE
• FOREIGN KEY
• INDEX
• Each constraint is
given a name -
Access requires a
name, but some
others don’t
• Constraints which
refer to single
columns can be
included in their
definition
SQL Data Definition
Primary Keys
• Primary Keys are
defined through
constraints
• A PRIMARY KEY
constraint also
includes a UNIQUE
constraint and
makes the columns
involved NOT NULL
• The <details> for a
primary key is a list
of columns which
make up the key
CONSTRAINT <name>
PRIMARY KEY
(col1, col2, …)
SQL Data Definition
Unique Constraints
• As well as a single
primary key, any set
of columns can be
specified as UNIQUE
• This has the effect of
making candidate
keys in the table
• The <details> for a
unique constraint are
a list of columns
which make up the
candidate key
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
• Depends on the type
• 1:1 are usually not
used, or can be
treated as a special
case of M:1
• 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
Assignment
Exam
Assignment
Exam
Credits
SQL Data Definition
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
Assignment
Exam
Assignment
Exam
Credits
SQL Data Definition
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 <name>
FOREIGN KEY
(col1,col2,…)
REFERENCES
<table>
[(ref1,ref2,…)]
• If the FK references
the PK of <table> you
don’t need to list the
columns
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)

More Related Content

Similar to lecture5.ppt

lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxcAnhTrn53
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuyVo27
 
Lesson-02 (1).pptx
Lesson-02 (1).pptxLesson-02 (1).pptx
Lesson-02 (1).pptxssuserc24e05
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL MuhammadWaheed44
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptxFayChan8
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleFarhan Aslam
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxAnusha sivakumar
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
Database Akjljljlkjlkjkljlkjldiministration.pptxDatabase Akjljljlkjlkjkljlkjldiministration.pptx
Database Akjljljlkjlkjkljlkjldiministration.pptxEliasPetros
 
Database queries
Database queriesDatabase queries
Database querieslaiba29012
 
Structured Query Language (SQL).pptx
Structured Query Language (SQL).pptxStructured Query Language (SQL).pptx
Structured Query Language (SQL).pptxEllenGracePorras
 

Similar to lecture5.ppt (20)

lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptx
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
 
Lesson-02 (1).pptx
Lesson-02 (1).pptxLesson-02 (1).pptx
Lesson-02 (1).pptx
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
 
My sql
My sqlMy sql
My sql
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
Database Akjljljlkjlkjkljlkjldiministration.pptxDatabase Akjljljlkjlkjkljlkjldiministration.pptx
Database Akjljljlkjlkjkljlkjldiministration.pptx
 
Database queries
Database queriesDatabase queries
Database queries
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Structured Query Language (SQL).pptx
Structured Query Language (SQL).pptxStructured Query Language (SQL).pptx
Structured Query Language (SQL).pptx
 
ddl (1).ppt
ddl (1).pptddl (1).ppt
ddl (1).ppt
 
ddl (1).ppt
ddl (1).pptddl (1).ppt
ddl (1).ppt
 

More from Javaid Iqbal

Introduction to Tools and Techniques in the Data Sceicne
Introduction to Tools and Techniques in the Data SceicneIntroduction to Tools and Techniques in the Data Sceicne
Introduction to Tools and Techniques in the Data SceicneJavaid Iqbal
 
W2_Lec03_Lec04_Agents.pptx
W2_Lec03_Lec04_Agents.pptxW2_Lec03_Lec04_Agents.pptx
W2_Lec03_Lec04_Agents.pptxJavaid Iqbal
 
W1_Lec01_Lec02_Introduction.pptx
W1_Lec01_Lec02_Introduction.pptxW1_Lec01_Lec02_Introduction.pptx
W1_Lec01_Lec02_Introduction.pptxJavaid Iqbal
 
Web Engineering Lec01-02 - Introduction to Web.pptx
Web Engineering Lec01-02 - Introduction to Web.pptxWeb Engineering Lec01-02 - Introduction to Web.pptx
Web Engineering Lec01-02 - Introduction to Web.pptxJavaid Iqbal
 
W16_Lec_31_Lec_32_Widgets.pptx
W16_Lec_31_Lec_32_Widgets.pptxW16_Lec_31_Lec_32_Widgets.pptx
W16_Lec_31_Lec_32_Widgets.pptxJavaid Iqbal
 
W14_Lec_27_Lec_28_Services.pptx
W14_Lec_27_Lec_28_Services.pptxW14_Lec_27_Lec_28_Services.pptx
W14_Lec_27_Lec_28_Services.pptxJavaid Iqbal
 

More from Javaid Iqbal (7)

Introduction to Tools and Techniques in the Data Sceicne
Introduction to Tools and Techniques in the Data SceicneIntroduction to Tools and Techniques in the Data Sceicne
Introduction to Tools and Techniques in the Data Sceicne
 
W2_Lec03_Lec04_Agents.pptx
W2_Lec03_Lec04_Agents.pptxW2_Lec03_Lec04_Agents.pptx
W2_Lec03_Lec04_Agents.pptx
 
W1_Lec01_Lec02_Introduction.pptx
W1_Lec01_Lec02_Introduction.pptxW1_Lec01_Lec02_Introduction.pptx
W1_Lec01_Lec02_Introduction.pptx
 
Lecture 10.pptx
Lecture 10.pptxLecture 10.pptx
Lecture 10.pptx
 
Web Engineering Lec01-02 - Introduction to Web.pptx
Web Engineering Lec01-02 - Introduction to Web.pptxWeb Engineering Lec01-02 - Introduction to Web.pptx
Web Engineering Lec01-02 - Introduction to Web.pptx
 
W16_Lec_31_Lec_32_Widgets.pptx
W16_Lec_31_Lec_32_Widgets.pptxW16_Lec_31_Lec_32_Widgets.pptx
W16_Lec_31_Lec_32_Widgets.pptx
 
W14_Lec_27_Lec_28_Services.pptx
W14_Lec_27_Lec_28_Services.pptxW14_Lec_27_Lec_28_Services.pptx
W14_Lec_27_Lec_28_Services.pptx
 

Recently uploaded

Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 

Recently uploaded (20)

Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 

lecture5.ppt

  • 1. SQL Data Definition Instructor: Muhammad Javaid Iqbal Lecturer javaid.iqbal@superior.edu.pk
  • 2. SQL Data Definition 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.
  • 3. SQL Data Definition 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
  • 4. SQL Data Definition 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.)
  • 5. SQL Data Definition 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:''
  • 6. SQL Data Definition Non-Procedural Programming • SQL is a declarative (non-procedural) language • Procedural - say exactly what the computer has to do • Non-procedural – describe the required result (not the way to compute it) • Example: Given a database with tables • Student with attributes ID, Name, Address • Module with attributes Code, Title • Enrolment with attributes ID, Code • Get a list of students who take the module ‘Database Systems’
  • 7. 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
  • 8. 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’))
  • 9. SQL Data Definition 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 databases • 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
  • 10. SQL Data Definition 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
  • 11. SQL Data Definition 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 Assignment Exam Assignment Exam Credits
  • 12. SQL Data Definition 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
  • 13. SQL Data Definition CREATE TABLE CREATE TABLE <name> ( <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)
  • 14. SQL Data Definition Column Definitions <col-name> <type> [NULL|NOT NULL] [DEFAULT <val>] [constraint-1 [, constraint-2[, ...]]] • Each column has a name and a type • Common types • INT • REAL • CHAR(n) • VARCHAR(n) • DATE
  • 15. SQL Data Definition Column Definitions • Columns can be specified as NULL or NOT NULL • NOT NULL columns cannot have missing values • If neither is given then columns are assumed NULL • Columns can be given a default value • You just use the keyword DEFAULT followed by the value, eg: num INT DEFAULT 0
  • 16. SQL Data Definition Example CREATE TABLE Student ( stuID INT NOT NULL, stuName VARCHAR(50) NOT NULL, stuAddress VARCHAR(50), stuYear INT DEFAULT 1) Student ID Name Address Year
  • 17. SQL Data Definition Constraints CONSTRAINT <name> <type> <details> • Common <type>s • PRIMARY KEY • UNIQUE • FOREIGN KEY • INDEX • Each constraint is given a name - Access requires a name, but some others don’t • Constraints which refer to single columns can be included in their definition
  • 18. SQL Data Definition Primary Keys • Primary Keys are defined through constraints • A PRIMARY KEY constraint also includes a UNIQUE constraint and makes the columns involved NOT NULL • The <details> for a primary key is a list of columns which make up the key CONSTRAINT <name> PRIMARY KEY (col1, col2, …)
  • 19. SQL Data Definition Unique Constraints • As well as a single primary key, any set of columns can be specified as UNIQUE • This has the effect of making candidate keys in the table • The <details> for a unique constraint are a list of columns which make up the candidate key CONSTRAINT <name> UNIQUE (col1, col2, …)
  • 20. 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))
  • 21. SQL Data Definition Relationships • Depends on the type • 1:1 are usually not used, or can be treated as a special case of M:1 • 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 Assignment Exam Assignment Exam Credits
  • 22. SQL Data Definition 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 Assignment Exam Assignment Exam Credits
  • 23. SQL Data Definition 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 <name> FOREIGN KEY (col1,col2,…) REFERENCES <table> [(ref1,ref2,…)] • If the FK references the PK of <table> you don’t need to list the columns
  • 24. 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))
  • 25. 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
  • 26. 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)