Unformatted text preview:

Lecture 6 SQL Chapter 3 Database System Concepts 5th Ed Silberschatz Korth and Sudarshan See www db book com for conditions on re use Administrative issues SQL assignment SQL plus documentation http www oracle com technology docs tech sql plus index html SQL documentation See SQL reference in http www oracle com pls db102 homepage Note for drawing ER diagrams DiaCze Windows http www cze cz downloads php Kivio Linux Mac http www koffice org download Dia Linux Windows http live gnome org Dia Download Database System Concepts 5th Ed Silberschatz Korth and Sudarshan See www db book com for conditions on re use 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 3 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 4 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 5 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 6 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 A A A 1 2 n P r 1 r 2 r m The result of an SQL query is a relation Database System Concepts 5th Edition Oct 5 2006 3 7 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 8 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 9 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 10 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 11 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 between 90000 and 100000 Database System Concepts 5th Edition Oct 5 2006 3 12 Silberschatz Korth and Sudarshan The from Clause The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra Find the Cartesian product borrower X loan select from borrower loan Find the name loan number and loan amount of all customers having a loan at the Perryridge branch select customer name borrower loan number amount from borrower loan where borrower loan number loan loan number and branch name Perryridge Database System Concepts 5th Edition Oct 5 2006 3 13 Silberschatz Korth and Sudarshan The Rename Operation The SQL allows renaming relations and attributes using the as clause old name as new name Find the name loan number


View Full Document

UMD CMSC 424 - Lecture 6: SQL

Documents in this Course
Lecture 2

Lecture 2

36 pages

Databases

Databases

44 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 6: 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 Lecture 6: SQL 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?