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

Similar documents
SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

In This Lecture. The Relational Model. The Relational Model. Relational Data Structure. Unnamed and named tuples. New thing:scheme (and attributes)

In this Lecture. More SQL Data Definition. Deleting Tables. Creating Tables. ALTERing Columns. Changing Tables. More SQL

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

The Structured Query Language Get Started

Chapter 1 SQL and Data

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Solved MCQ on fundamental of DBMS. Set-1

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Chapter 4: SQL Basics

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

Lab # 4. Data Definition Language (DDL)

Physical DB Issues, Indexes, Query Optimisation. Database Systems Lecture 13 Natasha Alechina

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

The Relational Model Constraints and SQL DDL

SQL DATA DEFINITION LANGUAGE

SQL Data Definition and Data Manipulation Languages (DDL and DML)

G64DBS Database Systems. Lecture 6 More SQL Data. Creating Relations. Column Constraints. Last Lecture s Question

QQ Group

Database Applications (15-415)

SQL: DDL. John Ortiz Cs.utsa.edu

CS317 File and Database Systems

Basant Group of Institution

Mahathma Gandhi University

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

G64DBS Database Systems. G64DBS Module. Recommended Textbook. Assessment. Recommended Textbook. Recommended Textbook.

CS317 File and Database Systems

user specifies what is wanted, not how to find it

Introduction to Databases

SQL: Data Definition Language

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Chapter 3: Introduction to SQL

The Relational Model of Data (ii)

Chapter 3: Introduction to SQL

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

CS143: Relational Model

Chapter 9: Working with MySQL

CSC 355 Database Systems

1. Data Definition Language.

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Databases 1. Daniel POP

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

DATABASES SQL INFOTEK SOLUTIONS TEAM

CS2300: File Structures and Introduction to Database Systems

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Informatics 1: Data & Analysis

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

CGS 3066: Spring 2017 SQL Reference

The SQL data-definition language (DDL) allows defining :

Relational Database Systems Part 01. Karine Reis Ferreira

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Data about data is database Select correct option: True False Partially True None of the Above

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CMPT 354: Database System I. Lecture 3. SQL Basics

Who, where, when. Database Management Systems (LIX022B05) Literature. Evaluation. Lab Sessions. About this course. After this course...

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Database Management System 9

MTAT Introduction to Databases

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Oracle Create Table Foreign Key On Delete No

Running Example Tables name location

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

The Relational Model

Assignment Session : July-March

INTRODUCTION TO DATABASE

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk


Database Management Systems,

Databases: Introduction

The Relational Model

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Part I: Structured Data

SQL Interview Questions

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

SQL. SQL Data Manipulation: Queries

The Relational Model. Database Management Systems

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CIS 45, The Introduction. What is a database? What is data? What is information?

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

CIS430 /CIS530 Lab Assignment 6

Data Base Concepts. Course Guide 2

Informatics 1: Data & Analysis

COMP 430 Intro. to Database Systems

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

INFORMATION TECHNOLOGY NOTES

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 -86, 89, 92, 99, 2003 Current :2008 Most modern DBMS use a variety of Few (if any) are true to the standard Oracle 10g which we will be using is mostly compliant to :2003 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 // Find module code for Database Systems Set M to be the first Record = while (M is not null) and ( = ) if (M. = Database Systems ) = M. Set M to be the next Record Procedural Programming // Find a list of student names Set NAMES to be empty 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 (E. = ) // if a student is enrolled in DBS NAMES = NAMES + S.NAME // add to the list Set E to be the next Record Set S to be the next Record return NAMES Non-Procedural () SELECT FROM, WHERE (. =.) AND (. = (SELECT FROM WHERE = Database Systems )), the Relational Model, and E/R Design 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 completely 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 Relations, Entities, Tables Implementing E/R Designs Relational model E/R Diagram Relation Tuple Attribute Foreign Key Primary Key Entity stance Attribute M:1 Relationship Table Row Column or Field Foreign Key Primary Key Given an E/R design The entities become tables Attributes of an entity become columns in the corresponding table M:1 relationships represented by foreign keys 2

Entities and Attributes CREATE TABLE 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 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) Column Definitions 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 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, e.g.: num INT DEFAULT 0 ple Constraints CREATE TABLE ( stu INT NOT NULL, stu VARCHAR(50) NOT NULL, stu VARCHAR(50), stu INT DEFAULT 1) 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 3

Primary Keys Unique Constraints 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, ) 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, ) ple Relationships CREATE TABLE ( stu INT NOT NULL, stu VARCHAR(50) NOT NULL, stu VARCHAR(50), stu INT DEFAULT 1, CONSTRAINT pk PRIMARY KEY (stu)) 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 Representing Relationships Foreign Keys 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 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 4

ple Why M:M a problem 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)) table includes modules? (,,,, ): (111,Smith,Newark,1, G51DBS), (111,Smith,Newark,1, G51FUN), no longer a candidate key, need (,) Redundancy (address repeated for every module) Symmetrical relationship translated asymmetrically Takes Next Lecture More DROP TABLE ALTER TABLE INSERT, UPDATE, and DELETE Data dictionary Sequences For more information Connolly and Begg chapters 5 and 6 Ullmanand Widom6.5 5