ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: www.cse.yorku.ca/~gordon/itec3220S07 Office: CSEB3020.

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

ITEC 3220M Using and Designing Database Systems
7 7 SQL Data Definition 4Spread throughout chapter 7.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
5 Chapter 5 Structured Query Language (SQL1) Revision.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Oracle Data Definition Language (DDL)
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Concepts of Database Management Seventh Edition
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Concepts of Database Management Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
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.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1 Chapter 1 File Systems and Databases Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel l 工程資料庫專題 –NTUST/ME-2007Q4.
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 5 Introduction to SQL.
Advanced SQL Advanced SQL Week 8 Comparison Operators
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020

Chapter 7 Introduction to Structured Query Language (SQL)

3 Introduction to SQL SQL functions fit into two broad categories: –Data definition language SQL includes commands to create –Database objects such as tables, indexes, and views –Commands to define access rights to those database objects –Data manipulation language Includes commands to insert, update, delete, and retrieve data within the database tables

4 SQL Data Definition Commands

5 Data Manipulation Commands

6 Creating the Database Two tasks must be completed –create the database structure –create the tables that will hold the end- user data First task –RDBMS creates the physical files that will hold the database –Tends to differ substantially from one RDBMS to another

7 Data Types CHAR (n): Character string n characters long DATE: Dates in the form DD-MON-YYYY OR MM/DD/YYYY DECIMAL (p, q): Decimal number p digits long with q if these being decimal places to the right of the decimal points INTEGER: Range from to SMALLINT: Similar to INTEGER but does not occupy as much space. It ranges from to NULL

8 Creating Table Structures Use one line per column (attribute) definition Use spaces to line up the attribute characteristics and constraints Table and attribute names are capitalized NOT NULL specification UNIQUE specification Primary key attributes contain both a NOT NULL and a UNIQUE specification RDBMS will automatically enforce referential integrity for foreign keys Command sequence ends with a semicolon

9 Table Creation Steps Steps in table creation: 1.Identify data types for attributes 2.Identify columns that can and cannot be null 3.Identify columns that must be unique 4.Identify primary key-foreign key mates 5.Determine default values 6.Identify constraints on columns (domain specifications) 7.Create the table CREATE TABLE ( );

10 An Example CREATE TABLE STUDENT( 2 STU_NUM INTEGER NOT NULL UNIQUE, 3 STU_NAME VARCHAR(15) NOT NULL, 4 GPA DECIMAL(3,2) NOT NULL, 5 PRIMARY KEY(STU_NUM)); Table created. Define attributes and their data types Semicolon indicates end of command Message indicates table was created Comma indicates end of description of an attribute Not null specifications Unique specifications Identify primary key

11 SQL Integrity Constraints Adherence to entity integrity and referential integrity rules is crucial –Entity integrity enforced automatically if primary key specified in CREATE TABLE command sequence –Referential integrity can be enforced in specification of FOREIGN KEY –Other specifications to ensure conditions met: ON DELETE RESTRICT ON UPDATE CASCADE

12 Advanced Data Definition Commands All changes in the table structure are made by using the ALTER command –Followed by a keyword that produces specific change –Three options are available ADD MODIFY DROP

13 Changing a Column’s Data Type ALTER can be used to change data type Some RDBMSs (such as Oracle) do not permit changes to data types unless the column to be changed is empty

14 Changing a Column’s Data Characteristics Use ALTER to change data characteristics If the column to be changed already contains data, changes in the column’s characteristics are permitted if those changes do not alter the data type

15 Adding or Dropping a Column Use ALTER to add a column –Do not include the NOT NULL clause for new column Use ALTER to drop a column –Some RDBMSs impose restrictions on the deletion of an attribute

16 Data Manipulation Commands Adding table rows Saving table changes Listing table rows Updating table rows Restoring table contents Deleting table rows Inserting table rows with a select subquery

17 Common SQL Data Manipulation Commands

18 Data Entry Enter data into a table INSERT INTO VALUES (attribute 1 value, attribute 2 value, … etc.);

19 Listing Table Rows SELECT –Used to list contents of table Syntax –SELECT columnlist FROM tablename Columnlist represents one or more attributes, separated by commas Asterisk can be used as wildcard character to list all attributes

20 Updating Table Rows UPDATE –Modify data in a table Syntax –UPDATE tablename SET columnname = expression [, columname = expression] [WHERE conditionlist]; If more than one attribute is to be updated in the row, separate corrections with commas

21 Saving Table Changes Changes made to table contents are not physically saved on disk until –Database is closed –Program is closed –COMMIT command is used Syntax –COMMIT –Will permanently save any changes made to any table in the database

22 Restoring Table Contents ROLLBACK –Used to restore the database to its previous condition –Only applicable if COMMIT command has not been used to permanently store the changes in the database Syntax –ROLLBACK; COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows

23 Deleting Table Rows DELETE –Deletes a table row Syntax –DELETE FROM tablename [WHERE conditionlist ]; WHERE condition is optional If WHERE condition is not specified, all rows from the specified table will be deleted

24 Inserting Table Rows with a Select Subquery INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery Query that is embedded (or nested) inside another query Executed first Syntax –INSERT INTO tablename SELECT columnlist FROM tablename

25 Selecting Rows with Conditional Restrictions Select partial table contents by placing restrictions on rows to be included in output –Add conditional restrictions to the SELECT statement, using WHERE clause Syntax –SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;

26 Comparison Operators

27 Arithmetic Operators: The Rule of Precedence Perform operations within parentheses Perform power operations Perform multiplications and divisions Perform additions and subtractions

28 Special Operators BETWEEN –Used to check whether attribute value is within a range IS NULL –Used to check whether attribute value is null LIKE –Used to check whether attribute value matches a given string pattern IN –Used to check whether attribute value matches any value within a value list EXISTS –Used to check if a subquery returns any rows

29 More Complex Queries and SQL Functions Listing unique values –DISTINCT clause produces list of different values Aggregate functions –Mathematical summaries SELECT DISTINCT V_CODE FROM PRODUCT;

30 Example Aggregate Function Operations COUNT MAX and MIN SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT; SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT WHERE P_PRICE <= 10.00; SELECT MIN(P_PRICE) FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT WHERE P_PRICE = MAX(P_PRICE);

31 Example Aggregate Function Operations (Cont’d) SUM AVG SELECT SUM(P_ONHAND * P_PRICE) FROM PRODUCT; SELECT P_DESCRIPT, P_ONHAND, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE > (SELECT AVG(P_PRICE) FROM PRODUCT) ORDER BY P_PRICE DESC;

32 More Complex Queries and SQL Functions (Cont’d) Ordering a listing Results ascending by default –Descending order uses DESC Cascading order sequence ORDER BY ORDER BY DESC ORDER BY

33 More Complex Queries and SQL Functions (cont’d) Grouping data –Creates frequency distributions –Only valid when used with SQL arithmetic functions –HAVING clause operates like WHERE for grouping output SELECT P_SALECODE, MIN(P_PRICE) FROM PRODUCT_2 GROUP BY P_SALECODE; SELECT V_CODE,COUNT(DISTINCT(P_CODE)),AVG(P_PRICE) FROM PRODUCT_2 GROUP BY V_CODE HAVING AVG(P_PRICE) < 10;

34 SQL Exercise Write SQL code that will create the relations shown. Assume the following attribute data types: –Student_ID: integer –Student_Name: 25 characters –Faculty_ID: integer –Faculty_Name: 25 characters –Course_ID: 25 characters –Course_Name: 15 characters –Date_Qualified: date –Section_ID: integer –Semester: 7 characters

35 SQL Exercise (Cont’d) STUDENT (Primary key: Student_ID) Student_ ID Student_ Name 38214Letersky 54907Altvater 66324Aiken 70542Marra IS_QUALIFIED (Primary key: Faculty_ID, Course_ID) Faculty_ ID Course_I D Date_ Qualified 2143ISM31129/ ISM42129/ ISM49309/ ISM31139/ ISM31129/1991

36 SQL Exercise (Cont’d) FACULTY (Primary key: Faculty_ID) Faculty_I D Faculty_Name 2143Birkin 3467Berndt 4756Collins SECTION (Primary key: Section_ID) Section_IDCourse_ID 2712ISM ISM ISM ISM4930

37 SQL Exercise (Cont’d) COURSE ((Primary key: Course_ID) Course_IDCourse_ Name ISM3113Syst Analysis ISM3112Syst Design ISM4212Database ISM4930Networking IS_REGISTERED (Primary key: Student_ID, Section_ID) Student_I D Section_I D Semester I I I I

38 SQL Exercise (Cont’d) Write SQL queries to answer the following questions: –Display the course ID and course name for all courses with an ISM prefix. –Is any instructor qualified to teach ISM 3113 and not qualified to teach ISM 4930? –How many students are enrolled in section 2714 during semester I – 2001? – Which students were not enrolled in any courses during semester I – 2001?

39 Summary SQL commands can be divided into two overall categories: –Data definition language commands –Data manipulation language commands Basic data definition commands allow you to create tables, indexes, and views Many SQL constraints can be used with columns Aggregate functions –Special functions that perform arithmetic computations over a set of rows

40 Summary ( Cont’d ) ORDER BY clause –Used to sort output of a SELECT statement –Can sort by one or more columns and use either an ascending or descending order Join output of multiple tables with SELECT statement Natural join uses join condition to match only rows with equal values in specified columns

41 Lab Instruction Before you go to the lab sessions, please use your Passport York to create Acadlab account (NOVELL account) and AML account if you don't have them yet. These two accounts will allow you to get access to the ORACLE database. How to get access to Oracle in ITEC labs –Log into the workstations using your ACADLAB account. –Choose Oracle from programs under the start menu and then choose sqlplus –When prompted for the username/password enter (where your_username is your AML username) at the username prompt and your AML password at the password prompt.

42 Lab Instruction How to get access to Oracle at home –Login to unix.aml.yorku.ca –At the prompt type the following commands: source /javainit –Start Oracle SQL*PLUS environment by typing the following command: sqlplus –When prompted for the username/password enter (where your_username is your AML username) at the username prompt and your AML password at the password prompt.

43 Lab Content Practice Question 2 of Assignment Two. –Create table structure for each table –Fill the tables with the data –Run the SQL queries –Print your queries and the answers

44 Lab Tips To list all tables you have in your Oracle account use the following SQL command: select table_name from user_tables; To describe a given Oracle table use the following Oracle environment command (note that this is not an SQL command): desc tablename (where tablename is the name of the table that you have in your account)