CHAPTER 6: INTRODUCTION TO SQL

Slides:



Advertisements
Similar presentations
Chapter 4 5 6_ SQL SQL Is: Structured Query Language
Advertisements

Chapter 7: Introduction to SQL
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
1 © Prentice Hall, 2002 Chapter 7: SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 IS 4420 Database Fundamentals Chapter 7: Introduction to SQL Leon Chen.
Introduction to Structured Query Language (SQL)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 6: Introduction to SQL
Database System SQL November 1st, 2009 Software Park, Bangkok Thailand Pree Thiengburanathum College of Arts and Media Chiang Mai University.
1 Chapter 7 Introduction to SQL. 2 Objectives Definition of terms Interpret history and role of SQL Define a database using SQL data definition language.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 6: Introduction to SQL Modern Database Management 10 th Edition Jeffrey A. Hoffer,
Guofeng Cao CyberInfrastructure and Geospatial Information Laboratory Department of Geography National Center for Supercomputing Applications (NCSA) University.
© 2007 by Prentice Hall 1 Chapter 7: Introduction to SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Dr. Chen, Data Base Management Chapter 6: Introduction to SQL Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall Chapter 6: Introduction to SQL Modern Database Management 11 th Edition Jeffrey A.
CHAPTER 6: INTRODUCTION TO SQL Copyright © 2014 Pearson Education, Inc. 1 Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
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.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lec 3- B Database Integrity 1. Overview Define a database using SQL data definition language Work with Views Write single table queries Establish referential.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
© 2005 by Prentice Hall 1 Chapter 7: Introduction to SQL Modern Database Management 7 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Chapter 7: Introduction to SQL
© 2011 Pearson Education 1 Chapter 6: Basic SQL Modern Database Management 10 th Edition, International Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Chapter 6: Introduction to SQL
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
SQL Overview Structured Query Language
1 © Prentice Hall, 2002 Chapter 7: SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Querying Single Tables with SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Lecture # 24 Introduction to SQL Muhammad Emran Database Systems.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Lecture 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer,
Copyright © 2016 Pearson Education, Inc. Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki Topi CHAPTER 6: INTRODUCTION.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Introduction to SQL Karolina muszyńska
SQL: Schema Definition and Constraints Chapter 6 week 6
LIS569 – Database Systems Dr
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
CHAPTER 7: ADVANCED SQL.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 6: Introduction to SQL
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

CHAPTER 6: INTRODUCTION TO SQL

Outline SQL Language statements: Data Definition Language (DDL) DML Create Insert Database Values Table Values which include null values View From an other table Alter Delete Drop Certain rows All rows Update Select

SQL OVERVIEW Structured Query Language: The standard for relational database management systems (RDBMS). RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables

BENEFITS OF A STANDARDIZED RELATIONAL LANGUAGE Reduced training costs. Productivity. Application portability. Application longevity. Reduced dependence on a single vendor. Cross-system communication.

SQL ENVIRONMENT Catalog: A set of schemas that constitute the description of a database. Schema: The structure that contains descriptions of objects created by a user (base tables, views, constraints). SQL Language Statements: Data Definition Language (DDL) Commands that define a database, including creating, altering, and dropping tables and establishing constraints. Data Manipulation Language (DML) Commands that maintain and query a database. Data Control Language (DCL) Commands that control a database, including administering privileges and committing data.

DDL, DML, DCL, and the database development process

Data Definition Language (DDL) SQL create statements. SQL alter statements. SQL drop statements.

1. SQL create statements Major CREATE statements: CREATE DATABASE–defines a database. CREATE TABLE–defines a new table and its fields (columns). CREATE VIEW–defines a logical table from one or more tables or views. Other CREATE statements: CHARACTER SCHEMA, SET, COLLATION, TRANSLATION, ASSERTION, DOMAIN.

a. Create Database SYNTAX: CREATE DATABASE databaseName; Example: CREATE DATABASE PineValley;

B. Create Table Identify data types for attributes. Identify attributes that can and cannot be null (Primary keys can never have null values). Determine default values. Identify constraints on columns (domain specifications). Identify columns that must be unique (candidate keys). Identify primary keys (simple OR composite). Identify primary key–foreign key mates. Create the table and associated indexes.

B. Create Table SYNTAX: CREATE TABLE tableName ( fieldName fieldDataType NOT NULL DEFAULT defaultValue CHECK constraint, ……. CONSTRAINT pkName PRIMARY KEY(pkFieldName), CONSTRAINT fkName FOREIGN KEY(fkFieldName) REFERENCES parentTableName(parentFieldName), ON UPDATE constrain ); In Red  optional

Example - EER Model

Example – Relational Model Customer(customerID, customerName, customerCity, customerState, customerPostalCode) Product(ProductID, productDescription, productFinish, productStandardPrice, productLineID, customerID) Order(orderID, orderDate) OrderLine(orderID, productID, quantity)

Example – Create Table CREATE TABLE Customer_T( customerID NUMBER(11,0) NOT NULL, customerName VARCHAR2(25) NOT NULL, customerCity VARCHAR2(20), customerState CHAR(2), customerPostalCode VARCHAR2(9), CONSTRAINT customer_PK PRIMARY KEY(customerID ) );

Example – Create Table CREATE TABLE Product_T( productID NUMBER(11,0) NOT NULL, productDescription VARCHAR2(50, productFinish VARCHAR2(20) CHECK productFinish IN(‘cherry’, ‘Natural Ash’, ‘White Ash’, ‘Red Oak’, ‘Natural Oak’, ‘Walnut’), productStandardPrice DECIMAL(6,2), productLineID INTEGER, CONSTRAINT product_PK PRIMARY KEY(productID), CONSTRAINT order_FK FOREIGN KEY(customerID ) REFERENCES Customer_T(customerID ) );

Example – Create Table CREATE TABLE Order_T( orderID NUMBER(11,0) NOT NULL, orderDate DATE DEFAULT SYSDATE, customerID NUMBER(11,0), CONSTRAINT order_PK PRIMARY KEY(orderID) );

Example – Create Table CREATE TABLE OrderLine_T( productID NUMBER(11,0) NOT NULL, orderID NUMBER(11,0) NOT NULL, Quantity NUMBER(11,0), CONSTRAINT orderLine_PK PRIMARY KEY(productID, orderID), CONSTRAINT orderLine_FK1 FOREIGN KEY(productID) REFERENCES Product_T(productID), CONSTRAINT orderLine_FK2 FOREIGN KEY(orderID) REFERENCES Order_T(orderID) );

CREATING TABLES WITH IDENTITY COLUMNS

DATA INTEGRITY CONTROLS Relational integrity is enforced via the primary-key to foreign-key match. Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1:M relationships. Restricting: Deletes of primary records. Updates of primary records. Inserts of dependent records.

Ensuring data integrity through updates

C. CREATE VIEW View has a name. View is based on a SELECT statement. CHECK_OPTION works only for updateable views and prevents updates that would create rows not included in the view. LATER

USING AND DEFINING VIEWS Views provide users controlled access to tables. Base Table is the table containing the raw data. Dynamic View A “virtual table” created dynamically upon request by a user. No data actually stored; instead data from base table made available to user. Based on SQL SELECT statement on base tables or other views. Materialized View Copy or replication of data. Data actually stored. Must be refreshed periodically to match corresponding base tables.

ADVANTAGES OF VIEWS Simplify query commands. Assist with data security (but don't rely on views for security, there are more important security measures). Enhance programming productivity. Contain most current base table data. Use little storage space. Provide customized view for user. Establish physical data independence.

DISADVANTAGES OF VIEWS Use processing time each time view is referenced. May or may not be directly updateable.

2. SQL ALTER statements ALTER TABLE statement allows you to change column specifications: Table Actions: Example (adding a new column with a default value):

3. SQL drop statement DROP statement allows you to remove tables from your schema. SYNTAX: DROP TABLE table_name; Example: DROP TABLE CUSTOMER_T;

Data Manipulation Language (DML) SQL INSERT statements. SQL UPDATE statements. SQL DELETE statements. SQL SELECT statements.

1. INSERT STATEMENT INSERT statement adds one or more rows to a table. Inserting values into a table SYNTAX: INSERT INTO table_name VALUES(…….); Example: INSERT INTO Customer_T VALUES(001, ‘Contemporary Casuals’, ‘Gainsville’, ‘FL’, 32601); Inserting a record that has some null attributes requires identifying the fields that actually get data SYNTAX: INSERT INTO table_name(……) VALUES(…….); Example: INSERT INTO Customer_T (customerID, customerName, customerPostalCode) VALUES (001, ‘Contemporary Casuals’, 32601); Inserting from another table SYNTAX: INSERT INTO table_name SELECT statement; Example: INSERT INTO Customer_T SELECT * FROM Customer_T WHERE CustomerState=‘CA’; LATER

Inserting into TABLES WITH IDENTITY COLUMNS Inserting into a table does not require explicit customer ID entry or field list Example: INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);

Operators Used in Conditions (IN Where Clause) –used in delete, update, and select statements Comparison operators. The IN operator SYNTAX: fieldName IN (fieldValues) The LIKE operator Allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed SYNTAX: fieldName LIKE ‘%string’ The Boolean operators AND, OR, and NOT Processing order is NOT, then AND, then OR Parentheses override the normal precedence of Boolean operators

2. DELETE STATEMENT Removes rows from a table Delete certain rows SYNTAX: DELETE FROM Table_Name WHERE condition; Example: DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’; Delete all rows SYNTAX: DELETE FROM Table_Name; Example: DELETE FROM CUSTOMER_T;

3. UPDATE STATEMENT Modifies data in existing rows. SYNTAX: UPDATE tableName SET fieldname = newValue WHERE condition; Example: UPDATE Product_T SET productStandardPrice = 775 WHERE productID=7;

4. SELECT STATEMENT Used for queries on single or multiple tables Clauses of the SELECT statement: SELECT List the columns (and expressions) to be returned from the query FROM Indicate the table(s) or view(s) from which data will be obtained WHERE Indicate the conditions under which a row will be included in the result GROUP BY Indicate categorization of results HAVING Indicate the conditions under which a category (group) will be included ORDER BY Sorts the result according to specified criteria

4. SELECT STATEMENT In Red  optional SYNTAX: SELECT colunmNames FROM tableName WHERE condition GROUP BY colunmName HAVING condition SORT BY colunmName; Examples: Find products with standard price less than $275 SELECT ProductDescription, ProductStandardPrice FROM Product_T WHERE ProductStandardPrice < 275; SELECT ProductDescription, ProductFinish, ProductStandardPrice FROM Product_T WHERE ProductDescription LIKE ‘%Desk’ OR ProductDescription LIKE ‘%Table’ AND ProductStandardPrice > 300; SELECT CustomerName, CustomerCity, CustomerState FROM Customer_T WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’) ORDER BY CustomerState, CustomerName;

SELECT EXAMPLE USING A FUNCTION Using the COUNT aggregate function to find totals SELECT COUNT(*) FROM OrderLine_T WHERE OrderID= 1004; Note: With aggregate functions you can’t have single-valued columns included in the SELECT clause, unless they are included in the GROUP BY clause.

Select Example with and without parentheses

CATEGORIZING RESULTS USING GROUP BY CLAUSE For use with aggregate functions Scalar aggregate: single value returned from SQL query with aggregate function. Vector aggregate: multiple values returned from SQL query with aggregate function (via GROUP BY). You can use single-value fields with aggregate functions if they are included in the GROUP BY clause

QUALIFYING RESULTS BY CATEGORIES USING THE HAVING CLAUSE For use with GROUP BY Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result.

MERGE STATEMENT Makes it easier to update a table…allows combination of Insert and Update in one statement Useful for updating master tables with new data