Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL."— Presentation transcript:

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

2 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.

3 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

4 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

5 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

6 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

7

8 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

9 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).

10 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.

11 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

12 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);

13 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

14 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);

15 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

16 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;

17 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

18 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

19 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

20 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);

21 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

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

23 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

24 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

25

26 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

27 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)

28 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.

29 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

30 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

31 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 )

32 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 > 10000000) ; 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);

33 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.

34 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Substring Pattern Matching

35 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

36 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

37 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.

38 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

39 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.

40 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.

41 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

42 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

43 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

44 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


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

Similar presentations


Ads by Google