SQL Sangeeta Devadiga CS157A, Fall 2006. Outline Background Data Definition Basic Structure Set Operation.

Slides:



Advertisements
Similar presentations
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Advertisements

INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
E-R Diagram for a Banking Enterprise
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
©Silberschatz, Korth and Sudarshan1.1Database System Concepts - 5 th Edition, May 23, 2005 Introduction to Database System Concepts.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
SQL Lecture 3 SQL Lecture 3. 2 SQL SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex.
MySQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Holliday - COEN 1781 Introduction to SQL. Holliday - COEN 1782 DB Tables and SQL The data is stored in the database in relations or tables Data types.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
CS 370 Database Systems Lecture 12 Introduction to SQL.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
CS 370 Database Systems Lecture 11 Relational Algebra.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Relational Model.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Chapter 5: SQL. Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Chapter 3 Introduction to SQL
Chapter 3: SQL.
Module 4: SQL.
Introduction to SQL.
Chapter 3: SQL.
Structured Query Language (SQL)
Chapter 3: SQL.
Chapter 3: SQL.
Introduction to SQL Holliday - COEN 178.
Presentation transcript:

SQL Sangeeta Devadiga CS157A, Fall 2006

Outline Background Data Definition Basic Structure Set Operation

Background 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 Authorization

Basic 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 command create table r( A 1 D 1, A 2 D 2, ….., A n D n, (integrity constraint 1 ), ………., (integrity constraint k )) r  is the name of relation A 1 ….A n  are the names of attributes D 1 …D n  are the types of values in the domain

Examples create table Example 1: create table customer (customer_name char(20), customer_street char(30), customer_city char(30), primary key (customer_name))

Example 2 create 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 A 1,A 2, …,A n from r 1, r 2, ….,r m where P A1 is a attribute r1 represents a relation P is a predicate Equivalent Query:  A 1, A 2, …, A n (  P (r 1 X r 2 X …… X r m )) The result of a SQL query is a relation

Example 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 10000

From 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_number

Set 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 all

Examples 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 borrower