DOC PREVIEW
UMBC CMSC 461 - Chapter 3: SQL

This preview shows page 1-2-3-4-31-32-33-34-35-63-64-65-66 out of 66 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 66 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 3 SQL Database System Concepts 5th Ed Silberschatz Korth and Sudarshan See www db book com for conditions on re use Chapter 3 SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification of the Database Joined Relations Database System Concepts 5th Edition Oct 5 2006 3 2 Silberschatz Korth and Sudarshan History IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language SQL ANSI and ISO standard SQL SQL 86 SQL 89 SQL 92 SQL 1999 language name became Y2K compliant SQL 2003 Commercial systems offer most if not all SQL 92 features plus varying feature sets from later standards and special proprietary features Not all examples here may work on your particular system Database System Concepts 5th Edition Oct 5 2006 3 3 Silberschatz Korth and Sudarshan Data Definition Language Allows the specification of not only a set of relations but also information about each relation including The schema for each relation The domain of values associated with each attribute Integrity constraints The set of indices to be maintained for each relations Security and authorization information for each relation The physical storage structure of each relation on disk Database System Concepts 5th Edition Oct 5 2006 3 4 Silberschatz Korth and Sudarshan Domain Types in SQL char n Fixed length character string with user specified length n varchar n Variable length character strings with user specified maximum length n int Integer a finite subset of the integers that is machine dependent smallint Small integer a machine dependent subset of the integer domain type numeric p d Fixed point number with user specified precision of p digits with n digits to the right of decimal point real double precision Floating point and double precision floating point numbers with machine dependent precision float n Floating point number with user specified precision of at least n digits More are covered in Chapter 4 Database System Concepts 5th Edition Oct 5 2006 3 5 Silberschatz Korth and Sudarshan Create Table Construct An SQL relation is defined using the create table command create table r A1 D1 A2 D2 An Dn integrity constraint1 integrity constraintk r is the name of the relation each Ai is an attribute name in the schema of relation r Di is the data type of values in the domain of attribute Ai Example create table branch branch name char 15 not null branch city char 30 assets integer Database System Concepts 5th Edition Oct 5 2006 3 6 Silberschatz Korth and Sudarshan Integrity Constraints in Create Table not null primary key A1 An Example Declare branch name as the primary key for branch create table branch branch name char 15 branch city char 30 assets integer primary key branch name primary key declaration on an attribute automatically ensures not null in SQL 92 onwards needs to be explicitly stated in SQL 89 Database System Concepts 5th Edition Oct 5 2006 3 7 Silberschatz Korth and Sudarshan Drop and Alter Table Constructs The drop table command deletes all information about the dropped relation from the database The alter table command is used to add attributes to an existing relation alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A All tuples in the relation are assigned null as the value for the new attribute The alter table command can also be used to drop attributes of a relation alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases Database System Concepts 5th Edition Oct 5 2006 3 8 Silberschatz Korth and Sudarshan Basic Query Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form select A1 A2 An from r1 r2 rm where P Ai represents an attribute Ri represents a relation P is a predicate This query is equivalent to the relational algebra expression The result of an SQL query is a relation A1 A2 An P r1 r2 rm Database System Concepts 5th Edition Oct 5 2006 3 9 Silberschatz Korth and Sudarshan The select Clause The select clause list the attributes desired in the result of a query corresponds to the projection operation of the relational algebra Example find the names of all branches in the loan relation select branch name from loan In the relational algebra the query would be branch name loan NOTE SQL names are case insensitive i e you may use upper or lower case letters E g Branch Name BRANCH NAME branch name Some people use upper case wherever we use bold font Database System Concepts 5th Edition Oct 5 2006 3 10 Silberschatz Korth and Sudarshan The select Clause Cont SQL allows duplicates in relations as well as in query results To force the elimination of duplicates insert the keyword distinct after select Find the names of all branches in the loan relations and remove duplicates select distinct branch name from loan The keyword all specifies that duplicates not be removed select all branch name from loan Database System Concepts 5th Edition Oct 5 2006 3 11 Silberschatz Korth and Sudarshan The select Clause Cont An asterisk in the select clause denotes all attributes select from loan The select clause can contain arithmetic expressions involving the operation and and operating on constants or attributes of tuples The query select loan number branch name amount 100 from loan would return a relation that is the same as the loan relation except that the value of the attribute amount is multiplied by 100 Database System Concepts 5th Edition Oct 5 2006 3 12 Silberschatz Korth and Sudarshan The where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational algebra To find all loan number for loans made at the Perryridge branch with loan amounts greater than 1200 select loan number from loan where branch name Perryridge and amount 1200 Comparison results can be combined using the logical connectives and or and not Comparisons can be applied to results of arithmetic expressions Database System Concepts 5th Edition Oct 5 2006 3 13 Silberschatz Korth and Sudarshan The where Clause Cont SQL includes a between comparison operator Example Find the loan number of those loans with loan amounts between 90 000 and 100 000 that is 90 000 and 100 000 select loan number from loan where amount


View Full Document

UMBC CMSC 461 - Chapter 3: SQL

Download Chapter 3: SQL
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 3: SQL and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 3: SQL 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?