Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-608 Database Systems

Similar presentations


Presentation on theme: "CPSC-608 Database Systems"— Presentation transcript:

1 CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes 4

2 Database is just another model of information processing
(in disks) Then why study DB? Much more data, regular data techs would be very inefficient: How should data be stored? Operations are simpler & more specific: How do we take advantage of it? New programming languages for the above. (ACID) Reliability, security, consistency, currency π, σ, ρ, ∩, ⋃, \, ╳, ⋈, ⋈C SQL

3 SQL: Structured Query language
a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C or Java. Database management system figures out the “best” way to execute queries * called “query optimization” For both data definition and data manipulation.

4 Creating (Declaring) a Relation
Simplest form is: CREATE TABLE <name> ( <list of elements> );

5 Creating (Declaring) a Relation
Simplest form is: CREATE TABLE <name> ( <list of elements> ); Example: CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) NOT NULL );

6 Changing Relation Schema: Adding Attributes
We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>;

7 Changing Relation Schema: Adding Attributes
We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>; Example: ALTER TABLE Bars ADD phone CHAR(16)DEFAULT ’unlisted’;

8 Changing Relation Schema: Deleting Attributes
Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>;

9 Changing Relation Schema: Deleting Attributes
Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>; Example: we don’t really need the license attribute for bars: ALTER TABLE Bars DROP license;

10 Changing Relation Schema: Removing Relations
To delete a relation: DROP TABLE <name>;

11 SQL: Structured Query language
How does SQL manipulate (search in/read from/write to) tables?

12 SQL: Structured Query language
How does SQL manipulate (search in/read from/write to) tables? This is the job of a data manipulation language (DML). DML does not change database schema.

13 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions

14 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions input

15 Select-From-Where Statements
SELECT attributes FROM tables WHERE conditions output (as a table) input

16 Our Running Example All our SQL queries will be based on the following database schema. Underline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

17 Example Using Beers(name, manf), what beers are made by Anheuser-Busch? SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

18 Example Using Beers(name, manf), what beers are made by Anheuser-Busch? SELECT name FROM Beers WHERE manf=’Anheuser-Busch’; Notice SQL uses single-quotes for strings. SQL is case-insensitive, except inside strings.

19 Result of Query name Bud Bud Lite Michelob ……
The answer is a relation with a single attribute, name, and tuples with the name of each beer by Anheuser-Busch, such as Bud.

20 Meaning of Single-Relation Query

21 Meaning of Single-Relation Query
Begin with the relation in the FROM clause.

22 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause.

23 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projection indicated by the SELECT clause.

24 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projection indicated by the SELECT clause. SELECT attributes FROM tables WHERE conditions

25 Meaning of Single-Relation Query
Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projection indicated by the SELECT clause. SELECT attributes FROM tables WHERE conditions Step 3 Step 1 Step 2

26 Operational Semantics
Beers name manf Bud Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

27 Operational Semantics
Beers name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. tv SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

28 Operational Semantics
Beers name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. Check if the “current” tuple satisfies the WHERE clause. tv Check if manf = Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

29 Operational Semantics
Beers Include tv.name in the result name manf Bud Anheuser-Busch To implement this algorithm think of a tuple variable tv ranging over each tuple of the relation mentioned in the FROM Clause. Check if the “current” tuple satisfies the WHERE clause. If so, compute the attributes or expressions of the SELECT clause using the components of this tuple. tv Check if manf = Anheuser-Busch SELECT name FROM Beers WHERE manf=’Anheuser-Busch’;

30 * in SELECT clauses

31 * in SELECT clauses When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.”

32 * in SELECT clauses SELECT *
When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’;

33 * in SELECT clauses SELECT *
When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’; The output has the same schema as Beers: name manf Bud Anheuser-Busch Bud Lite Michelob ……

34 Renaming Attributes

35 Renaming Attributes you want the result to have different attribute names, use “AS <new name>” to rename an attribute.

36 Renaming Attributes SELECT name AS beer, manf
you want the result to have different attribute names, use “AS <new name>” to rename an attribute. Example using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’

37 Renaming Attributes SELECT name AS beer, manf
you want the result to have different attribute names, use “AS <new name>” to rename an attribute. Example using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’ The result is: beer manf Bud Anheuser-Busch Bud Lite Michelob ……

38 Expressions in SELECT Clauses

39 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause.

40 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause. Example: from Sells(bar, beer, price): SELECT bar, beer, price*114 AS priceInYen FROM Sells;

41 Expressions in SELECT Clauses
Any expression that makes sense can appear as an element of a SELECT clause. Example: from Sells(bar, beer, price): SELECT bar, beer, price*114 AS priceInYen FROM Sells; The result is bar beer priceInYen Joe’s Bud 285 Sue’s Miller 342 ……

42 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause.

43 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause. Example: from Likes(drinker, beer): SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes; WHERE beer = ‘Bud’;

44 Expressions in SELECT Clauses
We can also have a “constant expression” in the SELECT clause. Example: from Likes(drinker, beer): SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes; WHERE beer = ‘Bud’; The result is drinker whoLikesBud Sally likes Bud Fred ……

45 Complex Conditions in WHERE Clause
From Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’;

46 Complex Conditions in WHERE Clause
From Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’; Notice how we get a single-quote in strings.

47 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match.

48 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>

49 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern> Pattern is a quoted string with % = “any string”; _ = “any character.”

50 Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to check match. General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern> Pattern is a quoted string with % = “any string”; _ = “any character.” Example. From Drinkers(name, addr, phone), find the drinkers with exchange 555 SELECT name FROM Drinkers WHERE phone LIKE ’%555-_ _ _ _’;


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google