In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina



Similar documents
SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

Databases What the Specification Says

In This Lecture. Physical Design. RAID Arrays. RAID Level 0. RAID Level 1. Physical DB Issues, Indexes, Query Optimisation. Physical DB Issues

Relational Databases. Christopher Simpkins


Fundamentals of Database Design

IT2305 Database Systems I (Compulsory)

1. INTRODUCTION TO RDBMS

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

IT2304: Database Systems 1 (DBS 1)

Database Query 1: SQL Basics

Lecture 6. SQL, Logical DB Design

Oracle Database: SQL and PL/SQL Fundamentals NEW

How To Create A Table In Sql (Ahem)

Oracle SQL. Course Summary. Duration. Objectives

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Programming with SQL

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

EECS 647: Introduction to Database Systems

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Oracle 10g PL/SQL Training

The Relational Model. Why Study the Relational Model?

Normalisation to 3NF. Database Systems Lecture 11 Natasha Alechina

Basic Concepts of Database Systems

Oracle Database: SQL and PL/SQL Fundamentals

2. Basic Relational Data Model

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

4 Logical Design : RDM Schema Definition with SQL / DDL

Relational Database Concepts

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database 12c: Introduction to SQL Ed 1.1

Introduction to Microsoft Jet SQL

3. Relational Model and Relational Algebra

Introduction to Database. Systems HANS- PETTER HALVORSEN,

A basic create statement for a simple student table would look like the following.

Information Systems Modelling Information Systems I: Databases

INTRODUCTION TO DATABASE SYSTEMS

Programming Database lectures for mathema

14 Triggers / Embedded SQL

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Oracle Database 10g Express

CSC 443 Data Base Management Systems. Basic SQL

Relational Database Basics Review

Information Systems SQL. Nikolaj Popov

Introduction to Databases

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Advance DBMS. Structured Query Language (SQL)

BCA. Database Management System

SQL. Short introduction

Database Design and Programming

Ch.5 Database Security. Ch.5 Database Security Review

Instant SQL Programming

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Databases and BigData

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Data Modeling. Database Systems: The Complete Book Ch ,

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Lab 2: PostgreSQL Tutorial II: Command Line

Information Management

SQL, PL/SQL FALL Semester 2013

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Oracle Database: SQL and PL/SQL Fundamentals NEW

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

ECS 165A: Introduction to Database Systems

Databases in Engineering / Lab-1 (MS-Access/SQL)

ATTACHMENT 6 SQL Server 2012 Programming Standards

Database Systems. S. Adams. Dilbert. Available: Hans-Petter Halvorsen, M.Sc.

SQL Server Database Coding Standards and Guidelines

2. Oracle SQL*PLUS Winter Some SQL Commands. To connect to a CS server, do:

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

DATABASE INTRODUCTION

Procedural Extension to SQL using Triggers. SS Chung

Database 2 Lecture I. Alessandro Artale

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Creating Database Tables in Microsoft SQL Server

5. CHANGING STRUCTURE AND DATA

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

5.1 Database Schema Schema Generation in SQL

DATABASE MANAGEMENT SYSTEMS. Question Bank:

php tek 2006 in Orlando Florida Lukas Kahwe Smith

There are five fields or columns, with names and types as shown above.

Normal Form vs. Non-First Normal Form

Transcription:

This Lecture Database Systems Lecture 5 Natasha Alechina The language, 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. Originally Sequel - Structured English query Language, part of an IBM project in the 70 s Sequel was already taken, so it became - Structured Query Language ANSI Standards -89-92 (2) -99 (3) Most modern DBMS use a variety of Most based on 2, increasingly 3 Few (if any) are true to the standard provides A data definition language (DDL) A data manipulation language (DML) A data control language (DCL) addition Can be used from other languages Is often extended to provide common programming constructs (such as ifthen tests, loops, variables, etc.) Notes Non-Procedural Programming is (usually) not case-sensitive, but we ll write keywords in upper case for emphasis statements will be written in BOLD COURIER FONT Strings in are surrounded by single quotes: 'I AM A STRING' Single quotes within a string are doubled: 'I''M A STRING' The empty string:'' 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) ple: Given a database with tables with attributes,, with attributes, with attributes, Get a list of students who take the module Database Systems 1

Procedural Programming Non-Procedural () Set M to be the first Record /* Find module code for */ = /* Database Systems */ While (M is not null) and ( = ) If (M. = Database Systems ) Then = M. Set M to be the next Record Set NAMES to be empty /* A list of student names */ Set S to be the first Record While S is not null /* For each student... */ Set E to be the first Record While E is not null /* For each enrolment... */ If (E. = S.) And /* If this student is */ (E. = ) Then /* enrolled in DB Systems */ NAMES = NAMES + S.NAME /* add them to the list */ Set E to be the next Record Set S to be the next Record Return NAMES SELECT FROM, WHERE (. =.) AND (. = (SELECT FROM WHERE = Database Systems )), the Relational Model, and E/R Design Relations, Entities, Tables is based on the relational model It has many of the same ideas Databases that support are often described as relational databases It is not always true to the model E/R designs can be implemented in Entities, attributes, and relationships can all be expressed in terms of Many-to-many relationships are a problem, so should be removed Relational model E/R Diagram Relation Tuple Attribute Foreign Key Primary Key Entity Table stance Row Attribute Column or Field M:1 Relationship Foreign Key Primary Key Implementing E/R Designs Entities and Attributes Given an E/R design The entities become tables Attributes of an entity become columns in the corresponding table Relationships may be represented by foreign keys 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 With columns for,,, and 2

CREATE TABLE Column Definitions 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) <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 Column Definitions ple 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 CREATE TABLE ( stu INT NOT NULL, stu VARCHAR(50) NOT NULL, stu VARCHAR(50), stu INT DEFAULT 1) Constraints Primary Keys 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 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 PRIMARY KEY (col1, col2, ) 3

Unique Constraints ple 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 UNIQUE (col1, col2, ) CREATE TABLE ( stu INT NOT NULL, stu VARCHAR(50) NOT NULL, stu VARCHAR(50), stu INT DEFAULT 1, CONSTRAINT pk PRIMARY KEY (stu)) Relationships Representing 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 The table Will have columns for the and attributes Will have a foreign key to for the has relationship Will have a foreign key to for the in relationship Foreign Keys ple 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 FOREIGN KEY (col1,col2, ) REFERENCES <table> [(ref1,ref2, )] If the FK references the PK of <table> you don t need to list the columns CREATE TABLE ( stu INT NOT NULL, mod CHAR(6) NOT NULL, enr INT, enr INT, CONSTRAINT enrpk PRIMARY KEY (stu, mod), CONSTRAINT enrstu FOREIGN KEY (stu) REFERENCES (stu), CONSTRAINT enrmod FOREIGN KEY (mod) REFERENCES (mod)) 4

Next Lecture More 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 Coursework Cw1: Entity-relationship diagram and table definitions deadline 29 February at 3, submit to the School Office. Cw2: creating tables in Oracle marked in the labs on 20/02, latest submission by email to me 22/02. Labs start on the 13 th 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 exercise (cw2) 5