SQL Tables, Keys, Views, Indexes



Similar documents
SQL NULL s, Constraints, Triggers

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

CS143 Notes: Views & Authorization

Relational Database Design: FD s & BCNF

EECS 647: Introduction to Database Systems

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

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

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Lecture 6. SQL, Logical DB Design

The Relational Model. Why Study the Relational Model?

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

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

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

SQL Query Evaluation. Winter Lecture 23

Chapter 5. SQL: Queries, Constraints, Triggers

How To Create A Table In Sql (Ahem)

Databases 2011 The Relational Model and SQL

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

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

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

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

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Introduction to SQL for Data Scientists

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Advance DBMS. Structured Query Language (SQL)

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

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

Databases What the Specification Says

CSC 443 Data Base Management Systems. Basic SQL

ECS 165A: Introduction to Database Systems

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Lecture 9. Semantic Analysis Scoping and Symbol Table

SQL: Queries, Programming, Triggers

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

Relational Database: Additional Operations on Relations; SQL

Financial Data Access with SQL, Excel & VBA

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

Database Management Systems. Chapter 1

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Physical DB design and tuning: outline

Performance Tuning for the Teradata Database

Information Systems SQL. Nikolaj Popov

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

Inside the PostgreSQL Query Optimizer

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

Database Design and Programming

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

COMP5138 Relational Database Management Systems. Databases are Everywhere!

Quiz! Database Indexes. Index. Quiz! Disc and main memory. Quiz! How costly is this operation (naive solution)?

SQL is capable in manipulating relational data SQL is not good for many other tasks

The Import & Export of Data from a Database

A Brief Introduction to MySQL

Introduction to Triggers using SQL

SQL. Short introduction

Unit Storage Structures 1. Storage Structures. Unit 4.3

Normal Form vs. Non-First Normal Form


Systems Infrastructure for Data Science. Web Science Group Uni Freiburg WS 2012/13

ICOM 6005 Database Management Systems Design. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 August 23, 2001

Database Design--from E-R Diagram to SQL Statement

Review: Participation Constraints

Introduction to Database Systems. Chapter 1 Introduction. Chapter 1 Introduction

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

Database Systems. Lecture 1: Introduction

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

Database Design Patterns. Winter Lecture 24

Query Optimization in Teradata Warehouse

Part VI. Object-relational Data Models

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

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

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Exercise 1: Relational Model

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

SQL Server An Overview

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Overview. Introduction to Database Systems. Motivation... Motivation: how do we store lots of data?

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Chapter 6 The database Language SQL as a tutorial

CS 525 Advanced Database Organization - Spring 2013 Mon + Wed 3:15-4:30 PM, Room: Wishnick Hall 113

Query-by-Example (QBE)

Complex Data and Object-Oriented. Databases

DBMS / Business Intelligence, SQL Server

Using Temporary Tables to Improve Performance for SQL Data Services

Using SQL Server Management Studio

Chapter 1: Introduction

Programming Database lectures for mathema

How To Improve Performance In A Database

Database Query 1: SQL Basics

CS 564: DATABASE MANAGEMENT SYSTEMS

Winter Winter

Database security. André Zúquete Security 1. Advantages of using databases. Shared access Many users use one common, centralized data set

Transcription:

CS145 Lecture Notes #8 SQL Tables, Keys, Views, Indexes Creating & Dropping Tables Basic syntax: CREATE TABLE ( DROP TABLE ;,,..., ); Types available: INT or INTEGER REAL or FLOAT CHAR( ), VARCHAR( ) DATE, TIME CREATE TABLE Student (SID INTEGER, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10)); CREATE TABLE Course (CID CHAR(10), title VARCHAR(100)); Keys Recall: a set of attributes is a key for a relation if (1) In no instance of will two different tuples agree on all attributes of ; i.e., is a tuple identifier (2) No proper subset of satisfies (1); i.e., is minimal Declaring Keys SQL allows multiple keys to be declared for one table: At most one PRIMARY KEY per table Any number of UNIQUE keys per table Jun Yang 1 CS145 Spring 1999

Two places to declare keys in CREATE TABLE: After an attribute s type, if the attribute is a key by itself As a separate element (essential if key has more than one attribute) CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10), PRIMARY KEY(SID, CID)); CREATE TABLE Course (CID CHAR(10) PRIMARY KEY, title VARCHAR(100) UNIQUE); Why declare keys? They are integrity constraints enforced by the DBMS They tell the DBMS to expect frequent lookups using key values Keys vs. FD s in SQL Recall in the pure relational model (where every relation is duplicate-free): is a tuple identifier for is a tuple identifier for In SQL (where a table may contain duplicate tuples): If is a tuple identifier for, then must be duplicate-free and may still hold when contains duplicates is a tuple identifier or Views A view is like a virtual table: It is defined by a view definition query which describes how to compute the view contents DBMS stores the view definition instead of the view contents It can be used in queries just like a regular table Jun Yang 2 CS145 Spring 1999

Creating & Dropping Views Syntax: CREATE VIEW AS ; DROP VIEW ; StudentRoster view CS145Roster view Using Views in Queries Semantics: SELECT...FROM...,,...WHERE...; SELECT...FROM...,( ),...WHERE...; find the SID of a CS145 student named Bart No more joins! DBMS typically rewrites the query to make it more efficient to evaluate Why use views? To hide some data from the users To make certain queries easier or more natural to express Real database applications use tons and tons of views Modifying Views Does not seem to make sense since views are virtual But does make sense if that is how user views the database Modify the base tables such that the modification would appear to have been accomplished on the view DELETE FROM StudentRoster WHERE SID = 123; Jun Yang 3 CS145 Spring 1999

Sometimes it is not possible CREATE VIEW HighGPAStudent AS SELECT * FROM Student WHERE GPA > 3.7; INSERT INTO HighGPAStudent VALUES(888, Nelson, 10, 2.50); Sometimes there are too many possibilities CREATE VIEW AvgGPA AS SELECT AVG(GPA) AS GPA FROM Student; UPDATE AvgGPA SET GPA = 2.5; Precise conditions for modifiable views are very complicated SQL2 uses conservative conditions: views must be defined as singletable SELECT with simple WHERE, no aggregates, no subqueries, etc. Indexes An index on attribute Creates auxiliary persistent data structure Can dramatically speed up accesses of the form: (sometimes; depending the type of index) An index can be built on a combination of multiple attributes as well Data structures for indexes: sorted lookup tables, hash tables, search trees, etc. (CS245 and CS346) SELECT * FROM Student WHERE name = Bart ; Without index on Student.name: because we store tables as flat collections of unordered tuples, we must scan all Student tuples With index: go directly to tuples with name = Bart SELECT * FROM Student, Take WHERE Student.SID = Take.SID; Use index on either Student.SID or Take.SID to speed up join Jun Yang 4 CS145 Spring 1999

Creating & Dropping Indexes Syntax: CREATE INDEX ON (,..., ); DROP INDEX ; If CREATE is followed by UNIQUE, DBMS will also enforce that is a key of Choosing which indexes to create is a difficult design issue: Depends on the expected query/update load and size of tables Jun Yang 5 CS145 Spring 1999