DOC PREVIEW
SJSU CS 157A - SQL

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 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 15 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 15 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 15 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 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SQLOutlineBackgroundParts of SQLBasic Domain TypesBasic Schema Definition in SQLExamples create tableExample 2Basic SQL Query StructureExample select ClauseWhere ClauseFrom ClauseRenameSet OperationExamples Set OperationsSQLSangeeta DevadigaCS157A, Fall 2006OutlineBackgroundData DefinitionBasic StructureSet OperationBackgroundIBM developed the original version named sequel in early 1970’sSequel language has evolved into SQLSQL (Structured Query Language) Versions of SQL SQL 92 SQL 99 SQL 2003 (latest version)Parts of SQLData Definition Language (DDL)Data manipulation Language (DML)IntegrityView DefinitionTransaction controlEmbedded SQL and Dynamic SQLAuthorizationBasic Domain Typeschar(n): A fixed length character string with user specifed length n. character can be used instead.varchar(n): A variable length character string with user specified max length n. character varying is equivalent.int: An Integer, the full form integer is equivalent.smallint: A small integer, a subset of integer domain typenumeric(p,d): A fixed point number with user specified precision. E.g: numeric(3,1) allows 31.5 to be defined preciselyreal, double precision: Floating-point and double precision floating-point numbers with machine-dependent precision.float(n): A floating point number, with precision of at least n digits.Basic Schema Definition in SQL We create SQL relation using the create table commandcreate table r( A1D1 , A2D2 , ….. , AnDn , (integrity constraint1), ………. , (integrity constraintk))r  is the name of relationA1….An  are the names of attributesD1…Dn  are the types of values in the domainExamples create tableExample 1:create table customer (customer_name char(20), customer_street char(30), customer_city char(30), primary key (customer_name))Example 2create table account (account_number char(10), branch_name char(15), balance numeric (12, 2), primary key (account_number))Basic SQL Query StructureSQL is based on set and relational operations with some modification and enhancement.SQL query has the form select A1,A2, … ,An from r1, r2, …. ,rm where PA1 is a attributer1 represents a relation P is a predicate Equivalent Query: A1, A2 , … , An(P (r1 X r2 X …… X rm))The result of a SQL query is a relationExample select Clauseselect branch_name from loanIn relational Algebra, the query would be branch_name(loan)SQL allows duplicates in query resultuse distinct if no duplicates in resultuse all if duplicates required in resultselect distinct branch_name from loan (result has distinct branch names)select all branch_name from loan (result may have duplicates)Where ClauseCorresponds to the selection predicate of relational algebraTo find loan numbers for loans made at San Jose branch with loan amounts greater than $500 select loan_number from loan where branch_name = “San Jose” and amount > $500Comparison result can be combined with logical connectives and, or, and notSQL includes between comparison operatorTo find loan numbers between amt. 900 and 10,000 select loan_number from loan where amount between 900 and 10000From ClauseCorresponds to Cartesian product operation of relational algebraExample: To find name, loan number, amount of all customers having loan at San Jose branch. select customer_name, loan_number, amount from borrower , loan where borrower.loan_number = loan.loan_number and branch_name = “San Jose”RenameSQL allows renaming relations and attributes using as clauseExample: To find name, loan_number, amount of all customers and rename column loan_number as loan_id. select customer_name, loan_number as loan_id, amount from borrower , loan where borrower.loan_number = loan. loan_numberSet OperationThe set operations union, intersect and except corresponds to U, , - respectively of relational algebra.Each of the above operation automatically eliminates duplicatesTo retain all duplicates use union all, intersect all, except allExamples Set OperationsFind all customers who have a loan, a account or both: select customer_name from depositor union select customer_name from borrowerFind all customers who have both loan and an account: select customer_name from depositor intersect select customer_name from borrowerFind all customers who have an account, but no loan: select customer_name from depositor except select customer_name from


View Full Document

SJSU CS 157A - SQL

Documents in this Course
SQL

SQL

18 pages

Lecture

Lecture

44 pages

Chapter 1

Chapter 1

56 pages

E-R Model

E-R Model

16 pages

Lecture

Lecture

48 pages

SQL

SQL

26 pages

Lossless

Lossless

26 pages

SQL

SQL

16 pages

Final 3

Final 3

90 pages

Lecture 3

Lecture 3

22 pages

SQL

SQL

25 pages

Load more
Download 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 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 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?