Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
SQL. Components of SQL Data definition language (DDL) –To create database structures –To enforce constraints etc. Data manipulation language (DML) –To.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
Introduction to SQL J.-S. Chou Assistant Professor.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
ITBIS373 Database Development
BACS--485 SQL 11 BACS 485 Structured Query Language.
SQL (DDL & DML Commands)
Wednesday, January 14,  Proposed by E.F.Codd in his 1970 paper  Used in System R, IBM’s research relational database in early 1970s-D.D. Chamberlin.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Query Processing – Implementing Set Operations and Joins Chap. 19.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Copyright © Curt Hill SQL The Data Manipulation Language.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
Active Database Concepts
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
HAVING,INDEX,COMMIT & ROLLBACK
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
Structured Query Language – The Basics
IST 318 Database Administration
Presentation transcript:

Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

ALTER TABLE Command To add a new column ALTER TABLE basetablename ADD COLUMN columnname datatype; Ex. ALTER TABLE Student ADD COLUMN birthdate DATETYPE; –Cannot specify NOT NULL, since existing records have no value for this field To drop a column ALTER TABLE basetablename DROP COLUMN columnname; Ex. ALTER TABLE Student DROP COLUMN major; Change database to allow double majors

ALTER TABLE Command To add a constraint ALTER TABLE basetablename ADD CONSTRAINT constraint_defn; To drop a constraint ALTER TABLE basetablename DROP CONSTRAINT constraint_name; Add a constraint that does not allow two classes to be scheduled in the same room

Other Changes to Tables Renaming a table: RENAME TABLE old-table-name TO new-table-name; Ex: RENAME TABLE FACULTY TO TEACHERS; Dropping a table: –CASCADE; //remove table and all references to it (ex. in a REFERENCES) –RESTRICT; //remove the table only if there are no references to it elsewhere DROP TABLE basetablename; Ex. DROP TABLE CLASS; Dropping an index: DROP INDEX indexname; Ex. DROP INDEX Student_lastName_firstName_ndx;

SQL Database Design Language Lecture 16

SQL DML Non-procedural, declarative language Can be interactive, can be embedded in host language, or can be stand-alone programming language (SQL/PSMs) Basic commands SELECT UPDATE INSERT DELETE

INSERT Operator INSERT INTOtablename [(colname [,colname]...)] VALUES(constant [,constant]...); Used for inserting new records into database, one at a time Not necessary to name columns if values are supplied for all columns, in proper order To insert null value for a column, specify only the other columns or write null as the value Can specify values for some columns, in any order, as long as values match order

UPDATE Operator UPDATEtablename SETcolumnname = expression [columnname = expression]... [WHEREpredicate]; Used for changing values in existing records Can update, zero, one, many, or all records in a table For null value, use SET columnname = NULL can use a sub-query to identify records to be updated

Examples Change number of credits Change credits and major Set major to null

Examples Change the grade of all students enrolled in CSC201A to ‘A’ Increase the number of credits for all students by 3

DELETE Operator DELETE FROMtablename WHEREpredicate; Used for deleting existing records from database Can delete zero, one, many, or all records Operation may not work if referential integrity would be lost Can use a sub-query to target records to be deleted If you delete all records from a table, its structure still remains, and you can insert into it later

SELECT Statement SELECT [DISTINCT] col-name [AS newname], [,col-name..]… FROMtable-name [alias] [,table-name]… [WHEREpredicate] [GROUP BY col-name [,col-name]…[HAVING predicate] or [ORDER BY col-name [,col-name]…]; Powerful command – equivalent to relational algebra’s SELECT, PROJECT, JOIN and more… Can be applied to one or more tables or views Can display one or more columns (renaming if desired) Predicate is optional, and may include usual operators and connectives Can put results in order by one or more columns Can also group together records with the same value for column(s) Can also use predefined functions

Example – Simple Retrieval with Condition Get names, IDs, and number of credits of all Math majors

Example – Use of Asterisk Notation for “all columns” Get all information about CSC Faculty

Example – Retrieval without Condition Get the course number of courses in which students are enrolled Eliminate duplicates

Example – Use of “ORDERED BY” and “AS” Get names and IDs of all Faculty members, arranged in alphabetical order by name. Call the resulting columns FacultyName and FacultyNumber

Example – Use of Multiple Conditions Get names of all math majors who have more than 30 credits standard comparison operators: =, <>,, >= standard logical operators: AND, OR, and NOT

Example – Natural Join Find IDS and names of all students taking ART103A

Example – Natural Join with Ordering Find stuId and grade of all students taking any course taught by the Faculty member whose facId is F110. Arrange in order by stuId.

Example – Natural Join of Three Tables Find course numbers and the names and majors of all students enrolled in the courses taught by Faculty member F110

Example – Use of Aliases Get a list of all courses that meet in the same room, with their schedules and room numbers

Example – Join without Equality Condition Find all combinations of students and Faculty where the student’s major is different from the Faculty member’s department

Examples – Using a subquery with Equality Find the numbers of all the courses taught by Byrne of the math department

Example – Subquery Using ‘IN’ Find the names and IDS of all Faculty members who teach a class in Room H221

Example – Nest Subqueries Get an alphabetical list of names and IDs of all students in any class taught by F110

Example – Using EXISTS Find the names of all students enrolled in CSC201A

Example – Query Using NOT EXIST Find the names of all students who are not enrolled in CSC201A

Example – Query Using UNION Get IDs of all Faculty who are assigned to the history department or who teach in Room H221

Example – Using Functions Find the total number of students enrolled in ART103A COUNTreturns the number of values in the column SUMreturns the sum of the values in the column AVGreturns the mean of the values in the column MAXreturns the largest value in the column MINreturns the smallest value in the column

Example – Using Functions Find the number of departments that have Faculty in them. Find the average number of credits student have.

Examples – Using Functions Find the student with the largest number of credits.

Examples – Using Functions Find the ID of the student(s) with the highest grade in any course Find names and IDs of students who have less than the average number of credits

Example – Using an Expression and a String Constant Assuming each course is three credits list, for each student, the number of courses he or she has completed

Example – Use of GROUP BY For each course, show the number of students enrolled GROUP BY allows us to put together all records with a single value in the specified field

Example – Use of HAVING Find all courses in which fewer than three students are enrolled HAVING is used to determine which groups have a quality, just as WHERE is used with tuples to determine which records have some quality.

Example – Use of LIKE Get details of all MTH courses % The percent character stands for any sequence of characters of any length >= 0 _ The underscore character stands for any single character.

Example – Use of NULL Find the stuId and classNumber of all students whose grades in that course are missing

Example – Inserting multiple records Create and fill a new table that shows each course and the number of students enrolled in it

Example – Updating with a Query Change the room to B220 for all courses taught by Tanaka

Example – Delete with a subquery Erase all enrollment records for Owen McCarthy

Active Databases-Constraints DBMS monitors database to prevent illegal states, using constraints and triggers Constraints –can be specified when table is created, or later –IMMEDIATE MODE: constraint checked when each INSERT, DELETE, UPDATE is performed –DEFERRED MODE: postpones constraint checking to end of transaction – write SET CONSTRAINT name DEFERRED –Can use DISABLE CONSTRAINT name, and later ENABLE CONSTRAINT name

Triggers More flexible than constraints Must have three parts: –event, some change made to the database –condition, a logical predicate (can be empty) –action, a procedure done when the event occurs and the condition is true, also called firing the trigger Can be fired before or after insert, update, delete Trigger can access values it needs as :OLD. and :NEW. –prefix :OLD refers to values in a tuple deleted or to the values replaced in an update –prefix :NEW refers to the values in a tuple just inserted or to the new values in an update. Can specify whether trigger fires just once for each triggering statement, or for each row that is changed by the statement

Trigger Syntax CREATE OR REPLACE TRIGGER trigger_name [BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON table_name [FOR EACH ROW] [WHEN condition] BEGIN trigger body END; Can disable triggers using ALTER TRIGGER name DISABLE; Later write ALTER TRIGGER name ENABLE; Can drop triggers using DROP TRIGGER name;

Trigger for Student Enrolling in a Class CREATE TRIGGER ADDENROLL AFTER INSERT ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll + 1 WHERE RevClass.classNumber = :NEW.classNumber; END;

Trigger for Student Dropping a Class CREATE TRIGGER DROPENROLL AFTER DELETE ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll – 1 WHERE RevClass.classNumber = :OLD.classNumber; END;

Trigger for Student Changing Classes CREATE TRIGGER SWITCHENROLL AFTER UPDATE OF classNumber ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll + 1 WHERE RevClass.classNumber = :NEW.classNumber; UPDATE RevClass SET currentEnroll = currentEnroll – 1 WHERE RevClass.classNumber = :OLD.classNumber; END;

Trigger for Checking for Over- enrollment Before Enrolling Student CREATE TRIGGER ENROLL_REQUEST BEFORE INSERT OR UPDATE OF classNumber ON RevEnroll FOR EACH ROW DECLARE numStu number; maxStu number; BEGIN setmaxEnroll into maxStu fromRevClass whereRevClass.classNumber = :NEW.classNumber; setcurrentEnroll + 1 into numStu fromRevClass whereRevClass.classNumber = :NEW.classNumber; if numStu > maxStu RequestClosedCoursePermission(:NEW.stuId, :NEW.classNumber, RevClass.currentEnroll, RevClass.maxEnroll); end if; END;

Example Trigger Prevent students from enrolling in two classes that meet at the same time

Ending Transactions COMMIT makes permanent changes in the current transaction ROLLBACK undoes changes made by the current transaction