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-86, 89, 92, 99, query Language, 2003 part of an IBM • Current SQL:2008 project in the 70’s • Most modern DBMS • Sequel was already use a variety of SQL taken, so it became • Few (if any) are true SQL - Structured to the standard Query Language • Oracle 10g SQL which we will be using is mostly compliant to SQL:2003
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 // Find module code for Database Systems
Set M to be the first Module Record
Code = ‘’ while (M is not null) and (Code = ‘’) if (M.Title = ‘Database Systems’) Code = M.Code Set M to be the next Module Record
SQL Data Definition
Procedural Programming // Find a list of student names
Set NAMES to be empty
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 (E.Code = Code) // if a student is enrolled in DBS NAMES = NAMES + S.NAME // add 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 problem, so should be completely true to the removed model
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 • M:1 relationships 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, e.g.: • 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 SQL 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 ‘has’ Exam Enrolment Assignment 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,…) REFERENCES • You need to give <table> • The columns which make up the FK [(ref1,ref2,…)] • The referenced table • If the FK references the PK of <table> you • The columns which don’t need to list the are referenced by 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 Why M:M a problem ID Address • Student table includes Student modules? Name Year • (ID,Name,Address,Year, Code): Takes Exam • (111,Smith,Newark,1, G51DBS), Code (111,Smith,Newark,1, Module Assignment G51FUN),… Title • ID no longer a candidate Credits key, need (ID,Code) • Redundancy (address repeated for every module) • Symmetrical relationship translated asymmetrically
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