Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Database Design -- Basic SQL
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Copyright © Curt Hill SQL The Data Definition Language.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
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.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
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.
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 6 Basic SQL Slide 6- 1.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 10 SQL DDL.
More SQL: Complex Queries,
CHAPTER 6 Basic SQL. CHAPTER 6 Basic SQL Chapter 6 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries.
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
Database Systems Basic SQL
Introduction to Structured Query Language (SQL)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL

Copyright © 2011 Ramez Elmasri and Shamkant Navathe SQL - Structured Query Language  Structured Query Language, is a computer language designed for  retrieval and management of data in relational database management systems  database schema creation and modification  database object access control management.  History:  The first version of SQL was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s.  Standardized in 1986 by ANSI.  Subsequent versions of the SQL standard have been released as ISO standards.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Schema and Catalog Concepts in SQL  Terminology:  Table, row, and column used for relational model terms relation, tuple, and attribute  SQL schema  Identified by a schema name  Includes an authorization identifier and descriptors for each element  Schema elements include  Tables, constraints, views, domains, and other constructs  Each statement in SQL ends with a semicolon

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Schema and Catalog Concepts in SQL (cont’d.)  CREATE statement  Main SQL command for data definition  CREATE SCHEMA statement  CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;  Catalog: Named collection of schemas in an SQL environment  A catalog always contains a special schema called INFORMATION_SCH EMA, which provides information on all the schemas in the catalog and all the element descriptors in these schemas.  Integrity constraints such as referential integrity can be defined between relations only if they exist in schemas within the same catalog.  Schemas within the same catalog can also share certain elements, such as domain definitions.  SQL environment  Installation of an SQL-compliant RDBMS on a computer system

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CREATE TABLE Command in SQL  Specify a new relation  Provide name  Specify attributes and initial constraints  Can optionally specify schema:  CREATE TABLE COMPANY.EMPLOYEE... or  CREATE TABLE EMPLOYEE...  Base tables (base relations)  Relation and its tuples are actually created and stored as a file by the DBMS  Virtual relations  Created through the CREATE VIEW statement

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The CREATE TABLE Command in SQL (cont’d.)  Some foreign keys may cause errors  Specified either via: Circular references Or because they refer to a table that has not yet been created To deal with this type of problem, these constraints can be left out of the initial CREATETABLE statement, and then added later using the ALTER TAB LE statement

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL  Basic data types  Numeric data types Integer numbers: INTEGER, INT, and SMALLINT Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION Formatted numbers can be declared by using DECIMAL(i,j)—or DEC(i,j) or NUMERIC(i,j)—where i, the precision, is the total number of decimal digits and j, the scale, is the number of digits after the decimal point. The default for scale is zero, and the default for precision is implementation-defined.  Character-string data types Fixed length: CHAR(n), CHARACTER(n) a shorter string is padded with blank characters to the right. Varying length : VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n) where n is the maximum number of characters. There is also a concatenation operator denoted by || (double vertical bar) that can concatenate two strings in SQL. Another variable-length string data type called CHARACTER LARGE OBJECT or CLOB is also available to specify columns that have large text values, such as documents. The CLOB maximum length can be specified in kilobytes (K), megabytes (M), or gigabytes (G).

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)  Bit-string data types Fixed length: BIT(n) Varying length: BIT VARYING(n) where n is the maximum number of bits. The default for n, the length of a character string or bit string, is 1. Another variable-length bit string data type called BINARY LARGE OBJECT or B LOB is also available to specify columns that have large binary values, such as images.  Boolean data type Values of TRUE or FALSE or NULL  DATE data type has Ten positions, with the Components are YEAR, MONTH, and DAY in the form YYYY-MM-DD TIME data type has at least eight positions, with the components HOUR, MINUTE, and SECOND in the form HH:MM:SS. TIME(i) data type, where i is called time fractional seconds precision, specifies i + 1 additional positions for TIME—one position for an additional period (.) separator character, and i positions for specifying decimal fractions of a second. TIME WITH TIMEZONE data type includes an additional six positions for specifying the displacement from the standard universal time zone, which is in the range +13:00 to – 12:59 in units of HOURS:MINUTES.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)  Additional data types  Timestamp data type ( TIMESTAMP ) Includes the DATE and TIME fields, plusing a minimum of six positions for decimal fractions of seconds, and an Optional WITH TIME ZONE qualifier. Literal values are represented by single-quoted strings preceded by the keyword TIMESTAMP, with a blank space between data and time  INTERVAL data type specifies a relative value that can be used to increment or decrement an absolute value of a date, time, or timestamp

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)  Domain  Name used with the attribute specification  Makes it easier to change the data type for a domain that is used by numerous attributes  Improves schema readability  Example: CREATE DOMAIN SSN_TYPE AS CHAR(9);

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Constraints in SQL  Basic constraints:  Key and referential integrity constraints  Restrictions on attribute domains and NULLs  Constraints on individual tuples within a relation

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Attribute Constraints and Attribute Defaults  NOT NULL  NULL is not permitted for a particular attribute  Default value  DEFAULT  If no default clause is specified, the default value is NULL for attributes that do not have the NOT NULL constraint.  CHECK clause  following an attribute or domain definition. Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Key and Referential Integrity Constraints  PRIMARY KEY clause  Specifies one or more attributes that make up the primary key of a relation Dnumber INT PRIMARY KEY;  UNIQUE clause  Specifies alternate (secondary) keys Dname VARCHAR(15) UNIQUE;

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Key and Referential Integrity Constraints (cont’d.)  FOREIGN KEY clause  Default operation: reject update on violation  Attach referential triggered action clause Options include SET NULL, CASCADE, and SET DEFAULT Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE CASCADE option suitable for “relationship” relations

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Giving Names to Constraints  Keyword CONSTRAINT  Name a constraint  The names of all constraints within a particular schema must be unique  Useful for later altering

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Constraints on Tuples Using CHECK  CHECK clauses at the end of a CREATE TABLE statement  tuple-based constraints Apply to each tuple individually checked whenever a tuple is inserted or modified CHECK (Dept_create_date <= Mgr_start_date);

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Basic Retrieval Queries in SQL  SELECT statement  One basic statement for retrieving information from a database  SQL allows a table to have two or more tuples that are identical in all their attribute values  Unlike relational model  Multiset or bag behavior

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The SELECT-FROM-WHERE Structure of Basic SQL Queries  Basic form of the SELECT statement:

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The SELECT-FROM-WHERE Structure of Basic SQL Queries (cont’d.)  Logical comparison operators  =,, >=, and <>  Projection attributes  Attributes whose values are to be retrieved  Selection condition  Boolean condition that must be true for any retrieved tuple

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Ambiguous Attribute Names  Same name can be used for two (or more) attributes  As long as the attributes are in different relations  Must qualify the attribute name with the relation name to prevent ambiguity

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Aliasing, Renaming, and Tuple Variables  The ambiguity of attribute names also arises in the case of queries that refer to the same relation twice.  Aliases or tuple variables  An alias can follow the keyword AS  or it can directly follow the relation name  It is also possible to rename the relation attributes within the query in SQL by giving them aliases. EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex, Sal, Sssn, Dno)

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unspecified WHERE Clause and Use of the Asterisk  Missing WHERE clause  Indicates no condition on tuple selection  If more than one relation is specified in the FROM clause and there is no WHERE clause, then the CROSS PRODUCT —all possible tuple combinations—of these relations is selected.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unspecified WHERE Clause and Use of the Asterisk (cont’d.)  Specify an asterisk (*)  Retrieve all the attribute values of the selected tuples

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Tables as Sets in SQL  SQL does not automatically eliminate duplicate tuples in query results  Duplicate elimination is an expensive operation. One way to implement it is to sort the tuples first and then eliminate duplicates.  The user may want to see duplicate tuples in the result of a query.  When an aggregate function is applied to tuples, in most cases we do not want to eliminate duplicates.  Use the keyword DISTINCT in the SELECT clause  Only distinct tuples should remain in the result

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Tables as Sets in SQL (cont’d.)  Set operations  UNION, EXCEPT (difference), INTERSECT  Corresponding multiset operations: UNION ALL, EXCEPT ALL, INTERSECT ALL )

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Tables as Sets in SQL (cont’d.)  Consider MovieStar (name, address, gender, birthdate) and MovieExec (name, address, cert#, networth), MovieExec (name, address, cert#, networth) 1.Get the names and addresses of all female movie stars who are also movie executives with a net worth over $10,000,000. (SELECT name, address FROM MovieStar WHERE gender = 'F' ) INTERSECT (SELECT name, address FROM MovieExec WHERE networth > ) ; 2.Get the names and addresses of movie stars who are not also movie executives, regardless of gender or net worth. (SELECT name, address FROM Moviestar) EXCEPT (SELECT name, address FROM MovieExec) ; 3.Get all the titles and years of movies that appeared in either the Movies or StarsIn relation. (SELECT title, year FROM Movie) UNION (SELECT movieTitle AS title, movieyear AS year FROM StarsIn);

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Substring Pattern Matching  LIKE comparison operator  Used for string pattern matching  % replaces an arbitrary number of zero or more characters  underscore (_) replaces a single character  If an underscore or % is needed as a literal character in the string, the character should be preceded by an escape character, which is specified after the string using the keyword ESCAPE. For example, ‘AB\_CD\%EF’ ESCAPE ‘\’ represents the literal string ‘AB_CD%EF’ because \ is specified as the escape character.  If an apostrophe (’) is needed, it is represented as two consecutive apostrophes (”) so that it will not be interpreted as ending the string.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Substring Pattern Matching

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Arithmetic Operators  Standard arithmetic operators:  Addition (+), subtraction (–), multiplication (*), and division (/)  For string data types, the concatenate operator || can be used in a query to append two string values.  For date, time, timestamp, and interval data types, operators include incrementing (+) or decrementing (–) a date, time, or timestamp by an interval.  BETWEEN comparison operator

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Ordering of Query Results  Use ORDER BY clause  Keyword DESC to see result in a descending order of values  Keyword ASC to specify ascending order explicitly

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Discussion and Summary of Basic SQL Retrieval Queries  A simple retrieval query in SQL can consist of up to four clauses, but only the first two—SELECT and FROM—are mandatory.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe INSERT, DELETE, and UPDATE Statements in SQL  Three commands used to modify the database:  INSERT, DELETE, and UPDATE

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The INSERT Command  Add tuples to a relation.  Specify the relation name and a list of values for the tuple.  A second form of the INSERT statement allows the user to specify explicit attribute names that correspond to the values provided in the INSERT command.  It is also possible to insert into a relation multiple tuples separated by commas in a single INSERT command. The attribute values forming each tuple are enclosed in parentheses.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The INSERT Command  The tuples inserted should follow all the integrity constraints that can be specified in the DDL.  A variation of the INSERT command inserts multiple tuples into a relation in conjunction with creating the relation and loading it with the result of a query.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The DELETE Command  Removes tuples from a relation  Includes a WHERE clause to select the tuples to be deleted

Copyright © 2011 Ramez Elmasri and Shamkant Navathe The UPDATE Command  Modify attribute values of one or more selected tuples  Additional SET clause in the UPDATE command  Specifies attributes to be modified and new values

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Features of SQL  Techniques for specifying complex retrieval queries  Writing programs in various programming languages that include SQL statements  Set of commands for specifying physical database design parameters, file structures for relations, and access paths  Transaction control commands

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Features of SQL (cont’d.)  Specifying the granting and revoking of privileges to users  Constructs for creating triggers  Enhanced relational systems known as object-relational  New technologies such as XML and OLAP