Database Systems Lecture 5 Natasha Alechina

Slides:



Advertisements
Similar presentations
More SQL Data Definition
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Database Management System LICT 3011 Eyad H. Elshami.
Entity/Relationship Modelling
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
The Relational Model These slides are based on the slides of your text book.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
The Relational Model Database Systems Lecture 3 Natasha Alechina.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Visual Programing SQL Overview Section 1.
SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Basics BCIS 3680 Enterprise Programming.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 480: Database Systems Lecture 13 February 13,2013.
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL: Structured Query Language
DATABASE Purpose of database
SQL (Structured Query Language)
Presentation transcript:

Database Systems Lecture 5 Natasha Alechina SQL Data Definition Database Systems Lecture 5 Natasha Alechina

In This Lecture SQL For more information 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 In addition SQL 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 M:1 Relationship SQL Table Row Column or Field SQL Data Definition

Implementing E/R Designs Enrolment Student Module In Has ID Code Title Name Address Year Assignment Exam Credits 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 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 You supply CREATE TABLE <name> ( <col-def-1>, : <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 Each column has a name and a type <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 Enrolment Student Module Address In Has ID Code Title Name Address Year Assignment Exam Credits 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 SQL Data Definition

Representing Relationships Enrolment Student Module In Has ID Code Title Name Address Year Assignment Exam Credits 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 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 For more information 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