CS 480: Database Systems Lecture 13 February 13,2013.

Slides:



Advertisements
Similar presentations
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Advertisements

INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
SQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Oracle Data Definition Language (DDL)
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
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.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
INTRODUCTION TO SQL Database System Concepts. Agenda Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations.
Chapter 3: Introduction to SQL
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 5-1: Introduction to SQL. 2 Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations.
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.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
CPSC-310 Database Systems
Chapter 3 Introduction to SQL
CS 480: Database Systems Lecture 17 February 22, 2013.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Introduction to SQL.
Chapter 3: Introduction to SQL
Chapter 3: Introduction to SQL
Chapter 3: Introduction to SQL
Instructor: Mohamed Eltabakh
Chapter 3: Introduction to SQL
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
Chapter 3: Introduction to SQL
Defining a Database Schema
CMPT 354: Database System I
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Instructor: Mohamed Eltabakh
IST 318 Database Administration
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CMSC-461 Database Management Systems
Chapter 3: Introduction to SQL
SQL (Structured Query Language)
Presentation transcript:

CS 480: Database Systems Lecture 13 February 13,2013

Aggregation with GROUP BY INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT dept,SUM(salary) AS budget FROM instructor GROUP BY dept The attributes in the GROUP BY clause have to also appear in the SELECT clause to appear in the result.

Database Modification Operations So far all we’ve done is: accessing the data (querying). Now we can also manipulate the data: Inserting new tuples Deleting tuples Updating tuples

Deletion DELETE FROM r WHERE P Format: r is the relation from which to choose the tuples to delete. P is a predicate that selects the tuples to remove. If the WHERE clause is omitted, all tuples in r are deleted. Operates on only one relation. DELETE FROM r WHERE P It first finds all tuples t such that P(t) is true, and then deletes the tuple.

Deletion INSTRUCTOR(name,dept,salary) Example #1: Delete all instructors that are part of the CS department. Example #2: Delete all instructors that have a salary between $13,000 and $15000.

Deletion INSTRUCTOR(name,dept,salary) Example #1: Delete all instructors that are part of the CS department. Example #2: Delete all instructors that have a salary between $13,000 and $15000.

Deletion INSTRUCTOR(name,dept,salary) Example #1: Delete all instructors that are part of the CS department. Example #2: Delete all instructors that have a salary between $13,000 and $15000. DELETE FROM instructor WHERE dept=‘CS’

Deletion INSTRUCTOR(name,dept,salary) Example #1: Delete all instructors that are part of the CS department. Example #2: Delete all instructors that have a salary between $13,000 and $15000. DELETE FROM instructor WHERE dept=‘CS’

Deletion INSTRUCTOR(name,dept,salary) Example #1: Delete all instructors that are part of the CS department. Example #2: Delete all instructors that have a salary between $13,000 and $15000. DELETE FROM instructor WHERE dept=‘CS’ DELETE FROM instructor WHERE salary >= 13000 AND salary <= 15000

Deletion Deletion is from only one relation. However, we can reference other relations in SELECT-FROM-WHERE format in nested subqueries inside in the WHERE clause.

Deletion STUDENT(id,name,address,GPA) ENROL(id,course,grade) Example #3: Delete the records of every student that got an F in CS480.

Deletion STUDENT(id,name,address,GPA) ENROL(id,course,grade) Example #3: Delete the records of every student that got an F in CS480.

Deletion STUDENT(id,name,address,GPA) ENROL(id,course,grade) Example #3: Delete the records of every student that got an F in CS480. DELETE FROM student WHERE id IN (SELECT id FROM enrol WHERE grade=‘F’ AND course=‘CS480’)

Deletion INSTRUCTOR(name,dept,salary) Consider the following query: DELETE FROM instructor WHERE salary < (SELECT AVG(salary) FROM instructor)

Deletion INSTRUCTOR(name,dept,salary) Consider the following query: DELETE FROM instructor WHERE salary < (SELECT AVG(salary) FROM instructor) Deletion of tuples, changes the value of the AVG(salary) Deletion processing note: Selection of all tuples to be deleted should be done prior to the actual deletion of the tuples. Why?

Insertion Two possible ways: Specify a tuple to be inserted Write a query whose result is a set of tuples to be inserted. Attribute values for inserted tuples must be members of the corresponding attribute’s domain. It first finds all tuples t such that P(t) is true, and then deletes the tuple.

Insertion Insertion by specifying a tuple to be inserted COURSE(name,title,dept,credits)

Insertion Insertion by specifying a tuple to be inserted COURSE(name,title,dept,credits) INSERT INTO course VALUES(‘CS480’,’Database Systems’,‘CS’,4)

Insertion Insertion by specifying a tuple to be inserted COURSE(name,title,dept,credits) INSERT INTO course VALUES(‘CS480’,’Database Systems’,‘CS’,4) Order of values should be based in the order that the schema was defined. If order is unknown then one can specify the order explicitly:

Insertion Insertion by specifying a tuple to be inserted COURSE(name,title,dept,credits) INSERT INTO course VALUES(‘CS480’,’Database Systems’,‘CS’,4) Order of values should be based in the order that the schema was defined. If order is unknown then one can specify the order explicitly: INSERT INTO course(name,title,dept,credits) VALUES(‘CS480’,’Database Systems’,‘CS’,4)

Insertion Insertion by specifying a tuple to be inserted COURSE(name,title,dept,credits) INSERT INTO course VALUES(‘CS480’,’Database Systems’,‘CS’,4) Order of values should be based in the order that the schema was defined. If order is unknown then one can specify the order explicitly: INSERT INTO course(name,title,dept,credits) VALUES(‘CS480’,’Database Systems’,‘CS’,4) INSERT INTO course(name,dept,credits,title) VALUES(‘CS401’,’CS’,4,’Algorithms’)

Insertion Insertion by writing a query whose result is a set of tuples to be inserted. INSTRUCTOR(name,dept,salary) STUDENT(id,name,address,dept,tot_credit) Suppose we want to make every CS student an instructor with a salary of $18,000 if they have earned more than 150 credits.

Insertion Insertion by writing a query whose result is a set of tuples to be inserted. INSTRUCTOR(name,dept,salary) STUDENT(id,name,address,dept,tot_credit) Suppose we want to make every CS student an instructor with a salary of $18,000 if they have earned more than 150 credits.

Insertion Insertion by writing a query whose result is a set of tuples to be inserted. INSTRUCTOR(name,dept,salary) STUDENT(id,name,address,dept,tot_credit) Suppose we want to make every CS student an instructor with a salary of $18,000 if they have earned more than 150 credits.

Insertion Insertion by writing a query whose result is a set of tuples to be inserted. INSTRUCTOR(name,dept,salary) STUDENT(id,name,address,dept,tot_credit) Suppose we want to make every CS student an instructor with a salary of $18,000 if they have earned more than 150 credits. INSERT INTO instructor SELECT name,dept,18000 FROM student WHERE dept=‘CS’ AND tot_credit > 150

Updates Used for changing a value in a tuple without changing all values in the tuple. We can choose which tuples to update. Format: Ai’s are attributes, ai’s are arithmetic expressions, P is a predicate. UPDATE r SET A1=a1,A2=a2,…,An=an WHERE P

Updates INSTRUCTOR(name,dept,salary) Annual salary increases need to be made. All instructor salaries will be increased by 5%.

Updates INSTRUCTOR(name,dept,salary) Annual salary increases need to be made. All instructor salaries will be increased by 5%. UPDATE instructor SET salary = salary*1.05

Updates INSTRUCTOR(name,dept,salary) Annual salary increases need to be made. All instructors with salary less than $70,000 will have their salaries increased by 5%.

Updates INSTRUCTOR(name,dept,salary) Annual salary increases need to be made. All instructors with salary less than $70,000 will have their salaries increased by 5%. UPDATE instructor SET salary = salary*1.05 WHERE salary < 70000

Updates INSTRUCTOR(name,dept,salary) All instructors that earn over $100,000 will get a 3% raise. All other instructors will get a 5% raise.

Updates INSTRUCTOR(name,dept,salary) All instructors that earn over $100,000 will get a 3% raise. All other instructors will get a 5% raise. UPDATE instructor SET salary = salary*1.03 WHERE salary > 100000 SET salary = salary*1.05 WHERE salary <= 100000

Updates INSTRUCTOR(name,dept,salary) All instructors that earn over $100,000 will get a 3% raise. All other instructors will get a 5% raise. UPDATE instructor SET salary = CASE WHEN salary <= 100000 then salary*1.05 ELSE salary*1.03 END SQL provides a CASE construct

Updates with CASE construct In general: UPDATE r SET A = CASE WHEN P1 then a1 WHEN P2 then a2 … WHEN Pn then an ELSE a0 END SQL provides a CASE construct

Updates One could also update a value with the result of a scalar query (one that returns just one value). STUDENT(id,name,address,tot_credit) COURSE(name,title,credits) ENROL(id,course_name,grade) UPDATE student AS s SET tot_credit = ( SELECT SUM(credits) FROM enrol NATURAL JOIN course WHERE s.id=enrol.id AND enrol.grade <> ‘F’)

Data Definition Language SQL DDL provides commands for: Defining relation schemas Deleting relations Modifying relation schemas When defining a relation in SQL we can specify: The schema for each relation Types of values associated with each attribute Other constraints (integrity, access, storage)

Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. Many others not covered here

Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. Many others not covered here

Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with p digits and with d of the digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. Many others not covered here

Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with p digits and with d of the digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. Many others not covered here Date, blob (anything, files?,pictures?)…

CREATE TABLE construct SQL relations are defined using the CREATE TABLE command: CREATE TABLE r (A1 D1, A2 D2, … An Dn, <integrity_constraint1>, <integrity_constraintk>) A’s are attributes D’s are domains for those attributes We’ll see next what are some possible integrity constraints…

Some Integrity Constraints PRIMARY KEY(B1,B2,…,Bm) B’s must be a subset of the already specified attributes (A’s). Requires the key attributes to be unique. Optional, but it’s generally good practice to specify one. FOREIGN KEY(C1,C2,…,Cd) REFERENCES s The values of these C’s should correspond to values of these attributes of some tuple in relation s. SQL will prevent any update to the database that violates an integrity constraint.

SQL Beer-drinker Database Relational Schema: DRINKER(d-name,d-city), BAR(ba-name,ba-city), BEER(be-name,type), FREQUENTS(d-name,ba-name), SERVES(ba-name,be-name), LIKES(d-name,be-name) SQL schema definition CREATE TABLE drinker (d_name VARCHAR(30), d_city VARCHAR(20), PRIMARY KEY (d_name)); CREATE TABLE bar (ba_name VARCHAR(30), ba_city VARCHAR(20), PRIMARY KEY (ba_name)); CREATE TABLE beer (be_name VARCHAR(30), type VARCHAR(20), PRIMARY KEY (be_name));

SQL Beer-drinker Database Relational Schema: DRINKER(d-name,d-city), BAR(ba-name,ba-city), BEER(be-name,type), FREQUENTS(d-name,ba-name), SERVES(ba-name,be-name), LIKES(d-name,be-name) SQL schema definition CREATE TABLE frequents (d_name VARCHAR(30), ba_name VARCHAR(30), FOREIGN KEY(d_name) REFERENCES drinker, FOREIGN KEY(ba_name) REFERENCES bar); CREATE TABLE serves (ba_name VARCHAR(30), be_name VARCHAR(30), FOREIGN KEY(be_name) REFERENCES beer, CREATE TABLE likes FOREIGN KEY(d_name) REFERENCES drinker);

CHECK clause An additional integrity constraint. When applied to an attribute declaration it specifies a predicate that must be satisfied by every tuple in the relation. Common use is to ensure that attribute values satisfy specified conditions (creates a powerful type system) Syntax: CHECK(P)

CHECK clause CREATE TABLE beer (be_name VARCHAR(30), type VARCHAR(20), PRIMARY KEY (be_name), CHECK (type IN (‘ale’,‘lager’, ‘stout’,‘pilsner’, ‘porter’,‘bitter’, ‘other’)));

CHECK clause CREATE TABLE employee (id CHAR(10) name VARCHAR(30), salary NUMERIC(10,2), PRIMARY KEY (id), CHECK (salary > 0));

Deleting Tables To remove a relation from an SQL database we use the DROP TABLE command. Syntax: DROP TABLE r More drastic than “DELETE FROM r” DROP TABLE removes the schema. Therefore, one no longer can insert tuples or query r after the DROP TABLE. If you do DELETE FROM r, the relation is still there, it’s just empty.

Altering Tables We can use the ALTER TABLE command to change the scheme of a relation by: Adding attributes to the relation Syntax: ALTER TABLE r ADD A D; Where r is an existing relation, A is the name of the new attribute and D is the domain of A. All values for the new attribute are initially assigned the special value NULL. Removing attributes from the relation Syntax: ALTER TABLE r DROP A; Where r is an existing relation and A is an attribute of r.

Relational Calculus

Tuple Relational Calculus Relational algebra expressions provide a sequence of procedure that generates the answer to our query. Tuple Relational Calculus in contrast is a formal query language that is nonprocedural. It describes the desired information without giving a specific procedure for obtaining the information.

Tuple Relational Calculus General format of a relational calculus expression: { x | <condition>} Variables are tuples Returns a set.

Example #1 student1(STUDENT) student2(STUDENT) STUDENT = {id,name,major,GPA} RA Query: student1  student2 Calculus: { y | y  student1  y  student2 }

Example #2 student(STUDENT) STUDENT = {id,name,major,GPA} RA Query: Πid(student) Calculus: { y |  x  student (x[id] = y[id]) } y has scheme {id} because because the conditions set by the expression on y are only for the id attribute.

Example #3 student(STUDENT), enrol(ENROL) STUDENT = {id,name,major,GPA} ENROL = {id,course,grade} RA Query: Πname,grade(student enrol) Calculus: y has scheme {id} because because the conditions set by the expression on y are only for the id attribute. { x |  y  student  z  enrol (x[name] = y[name]  x[grade] = z[grade]  y[id] = z[id]) }