Presentation is loading. Please wait.

Presentation is loading. Please wait.

603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.

Similar presentations


Presentation on theme: "603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems."— Presentation transcript:

1 603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems

2 SQL Defining a Database Schema: CREATE TABLE name (list of elements). *Principal elements are attributes and their types, but key declarations and constraints also appear. *Similar CREATE X commands for other schema elements X: views, indexes, assertions, triggers.

3 SQL *“DROP X name” deletes the created element of kind X with that name Example: CREATE TABLE Sells ( bar CHAR (20), beer VARCHAR (20) price REAL ) ; DROP TABLE Sells;

4 SQL SQL includes some features from relational algebra but is based largely on TRC. Request: Get the first and last names of employees with Salaries greater than $50,000. TRC: {t.FNAME, t.LNAME | EMPLOYEE (t) and t.SALARY > $50,000} SQL: SELECTT.FNAME, T.LNAME FROMEMPLOYEE AS T WHERET.SALARY > 50,000 ;

5 SQL As a language, SQL has two major components: 1) DDL (Data Definition Language) to define the database structure 2) DML (Data Manipulation Language) for retrieving and updating data

6 SQL SQL: SELECTT.FNAME, T.LNAME FROMEMPLOYEE AS T WHERET.SALARY > 50,000 ; SELECT ==> extremely powerful command capable of performing the equivalent of the following three relational algebra statements in a single statement! 1. Selection 2. Projection 3. Join

7 SQL SQL Syntax: CREATE TABLE ( [NOT NULL] {, [NOT NULL]}) DROP TABLE. DROP VIEW

8 SQL SQL Queries: *Principal form: SELECTdesired attributes FROMtuple variables - range over relations WHEREcondition about tuple variables

9 SQL Example Database Schema: Beers (name, manf) Bars (name, addr, license) Drinkers (name, addr, phone) Likes (drinker, beer) Sells (bar, beer, price) Frequents (drinker, bar)

10 SQL Query: What beers are made by Anheuser-Busch? Beers ( name, manf) SELECTname FROMBeers WHEREmanf = ‘Anheuser-Busch’ ; Bud Bud Lite Michelob name Resulting Relation Note single quotes for strings

11 SQL SELECT* FROMBeers WHEREmanf = ‘Anheuser-Busch’ ; namemanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch * yields all of the attributes of Beers

12 The Database Language SQL Renaming Columns: Beers ( name, manf) SELECTname AS beer FROMBeers WHEREmanf = ‘Anheuser-Busch’ ; beer Bud Bud Lite Michelob Column label ‘name’ changed to ‘beer’

13 SQL Language Simple Queries in SQL: Perhaps the simplest form of query in SQL asks for those tuples of some one relation that satisfy a condition. Database Schema below about movies: Movie(title, year, length, inColor, studioName, producerC#) StarsIn(movieTitle, movieYear, starName) MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#)

14 SQL Language Query (SQL): Ask about the relation Movie for all movies produced by Disney Studios in 1990. SELECT* FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; FROM clause ==> relation or relations to which the query refers. In our example, the query is about the relation Movie.

15 SQL SELECT* FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; WHERE clause ==> is a condition, much like the selection- condition in relational algebra. Tuples must satisfy the condition in order to match the query. studioName attribute has value ‘Disney’ year attribute has value 1990 Both must be TRUE Tuples with all attributes

16 SQL SELECT* FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; The result of the query is the Movie tuples for those movies produced by Disney in 1990, for example, Pretty Woman. title yearlength inColor studioName producerC# Pretty Women 1990 119 true Disney 999

17 SQL Projection (  ) in SQL: SELECTtitle, length FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; The results of this query is the table with two columns: titlelength Pretty Woman119......

18 SQL SELECTtitle, length FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; We can modify this query to produce a relation with attributes name and duration in place of title and length as follows: SELECTtitle AS name, length AS duration FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ;

19 SQL SELECTtitle AS name, length AS duration FROMMovie WHEREstudioName = ‘Disney’ AND year = 1990 ; Results: nameduration Pretty Woman119

20 SQL SELECT title AS name, length*0.016667 AS lengthInHours FROM Movie WHERE studioName = ‘Disney’ AND year = 1990 ; Results: name lengthInHours Pretty Woman 1

21 SQL SELECT title AS name, length*0.016667 AS lengthInHours FROM Movie WHERE studioName = ‘Disney’ AND year = 1990 ; We allow constants as expressions in SELECT: SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHours FROM Movie WHERE studioName = ‘Disney’ AND year = 1990 ;

22 SQL SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHours FROM Movie WHERE studioName = ‘Disney’ AND year = 1990 ; Results: name length inHours Pretty Woman1.98334 hrs.

23 SQL Next Lecture MORE SQL


Download ppt "603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems."

Similar presentations


Ads by Google