Professional Documents
Culture Documents
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/1
No two rows are identical Order is unimportant The row is called a tuple
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/2
Relational Algebra
Relational algebra defines a set of operators that may work on relations. Recall that relations are simply data sets. As such, relational algebra deals with set theory. The operators in relational algebra are very similar to traditional algebra except that they apply to sets.
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/3
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/4
Union Operator
The union operator adds tuples from one relation to another relation A union operation will result in combined relation This is similar to the logical operator OR
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/5
Union Operator
JUNIOR and HONOR-STUDENT relations and their union:
(a) Example of JUNIOR relation (b) Example HONORSTUDENT relation (c) Union of JUNIOR and HONORSTUDENT relations
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/6
Difference Operator
The difference operator produces a third relation that contains the tuples that appear in the first relation, but not the second This is similar to a subtraction
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/7
Difference Operator
JUNIOR relation HONORSTUDENT relation JUNIOR minus HONORSTUDENT relation
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/8
Intersection Operator
An intersection operation will produce a third relation that contains the tuples that are common to the relations involved. This is similar to the logical operator AND
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/9
Intersection Operator
JUNIOR relation HONORSTUDENT relation Intersection of JUNIOR and HONORSTUDENT relations
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/10
Product Operator
A product operator is a concatenation of every tuple in one relation with every tuple in a second relation The resulting relation will have n x m tuples, where
n = the number of tuples in the first relation and m = the number of tuples in the second relation
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/11
Projection Operator
A projection operation produces a second relation that is a subset of the first. The subset is in terms of columns, not tuples The resulting relation will contain a limited number of columns. However, every tuple will be listed.
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/12
Selection Operator
The selection operator is similar to the projection operator. It produces a second relation that is a subset of the first. However, the selection operator produces a subset of tuples, not columns. The resulting relation contains all columns, but only contains a portion of the tuples.
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/13
Join Operator
The join operator is a combination of the product, selection, and projection operators. There are several variations of the join operator
Equijoin Natural join Outer join
Left outer join Right outer join
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/14
123
158 271 105
Jones
Parks Smith Anderson
History
Math History Management ClassName H350
JR
GR JR SN PositionNumber 1
StudentNumber 123
105
123
BA490
B490
3
7
Chapter 7/15
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Join Examples
Equijoin
Natural Join
Chapter 7/16
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/17
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/18
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/19
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/20
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/21
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/22
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/24
Application Requirements
View Ridge application requirements
Track customers and their artist interests Record gallery's purchases Record customers' art purchases List the artists and works that have appeared in the gallery Report how fast an artist's works have sold and at what margin Show current inventory in a Web page
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/25
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/26
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/27
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/28
Sample Values
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/29
Sample Values
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/30
Sample Values
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/31
Sample Values
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/32
Sample Values
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/33
CHECK CONSTRAINT
CHECK CONSTRAINT defines limits for column values Two common uses
Specifying a range of allowed values Specifying an enumerated list
SQL Views
SQL view is a virtual table that is constructed from other tables or views It has no data of its own, but obtains data from tables or other views SELECT statements are used to define views
A view definition may not include an ORDER BY clause
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/35
SQL Views
Views may be used to
Hide columns or rows Show the results of computed columns Hide complicated SQL statements Provide a level of indirection between application programs and tables Assign different sets of processing permissions to tables Assign different sets of triggers
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/36
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/37
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/38
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/39
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/40
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/41
Updating Views
Views may or may not be updatable Rules for updating views are both complicated and DBMS-specific
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/42
Updating Views
Guidelines:
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/43
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/44
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/45
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/46
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/47
CHECK TABLE
CHECK TABLE view_name: to check the validation of views
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/49
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/50
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/51
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/52
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/53
EXAMPLES
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/55
EXPORTING DATA
SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/56
EXAMPLES
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/57
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/58
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/59
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/60
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/61
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/62
RESTORE DATA
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/63
Chapter 7/64
Triggers
A trigger is a stored program that is executed by the DBMS whenever a specified event occurs on a specified table or view Three trigger types: BEFORE, INSTEAD OF, and AFTER
Each type can be declared for Insert, Update, and Delete Resulting in a total of nine trigger types
Oracle supports all nine trigger types SQL Server supports six trigger types (only for INSTEAD OF and AFTER triggers)
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/65
CREATE TRIGGERS
CREATE TRIGGER trigger_name {BEFORE|AFTER} {INSERT|UPDATE|DELETE} ON table_name FOR EACH ROW trigger_statement
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/66
Firing Triggers
When a trigger is fired, the DBMS supplies
Old and new values for the update New values for inserts Old values for deletions
The way the values are supplied depends on the DBMS product Trigger applications:
Checking validity (Figure 7-14) Providing default values (Figure 7-15) Updating views (Figure 7-16) Enforcing referential integrity actions (Figure 7-17, 7-18)
Chapter 7/67
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
EXAMPLES
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/68
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/69
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/70
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/71
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/72
DROP TRIGGER
DROP TRIGGER trigger_name
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/73
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/74
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/75
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/76
EXAMPLES
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/77
String operators
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/78
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/80
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/81
Syntax
PREPARE stmt_name FROM preparable_stmt EXECUTE stmt_name [USING @var_name [, @var_name] ...] {DEALLOCATE | DROP} PREPARE stmt_name
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/82
Prepare statement
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hethuctamgiacvuong'; mysql> SET @a = 3; mysql> SET @b = 4; mysql> EXECUTE stmt1 USING @a, @b;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/83
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/84
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/85
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/86
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/87
Drop Prepare
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/88
E-PROJECT
Nhom 1: Men (TL), Phu, Trong Nhom 2: Tu, Khai (TL), Quyet, Hien
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/89
Ni dung
La chn v gii thiu ch (c kho st thc t) Thit k c s d liu, quan h gia cc bng (quan h cc bng c biu din bng s ) D liu >=50 records S dng View cho cc truy vn trn nhiu bng
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/90
Export kt qu ra file txt hoc xls S dng triggers kim sot nhp, xut d liu S dng cc stored functions, routines Phn quyn ngi dng (root,admin,users) Cho php kt ni database server t nhiu ip khc nhau
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/91
Bo co
Th 7 (3/12/2011) : trnh by trn lp File export (.txt, .xls), file .word, .ppt, .sql
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/92
Stored Procedures
A stored procedure is a program that is stored within the database and is compiled when used
In Oracle, it can be written in PL/SQL or Java In SQL Server, it can be written in TRANSACT-SQL
Stored procedures can receive input parameters and they can return results Stored procedures can be called from
Programs written in standard languages, e.g., Java, C# Scripting languages, e.g., JavaScript, VBScript SQL command prompt, e.g., SQL Plus, Query Analyzer
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/93
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/95
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/96
Examples
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/97
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/98
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/99
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/100
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/101
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/102
IN and OUT
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/103
Declare statement
Used for declaring varibles, conditions Syntax: DECLARE var_name data_type
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/104
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/105
Chapter 7/106
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/107
EXAMPLE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/108
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/110
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/111
CURSORS
MySQL supports cursors inside stored programs. The syntax is as in embedded SQL. Cursors have these properties: 1. Asensitive: The server may or may not make a copy of its result table 2. Read only: Not updatable 3. Nonscrollable: Can be traversed only in one direction and cannot skip rows Cursors must be declared before declaring handlers and after declaring variables and conditions.
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/112
DECLARE cursor_name CURSOR FOR select_statement OPEN cursor_name FETCH cursor_name INTO var_name [, var_name] ... CLOSE cursor_name
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/113
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/114
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/115
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/116
Flow control
IF and CASE: conditional testing LOOP, REPEAT, WHILE: loops
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/117
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ... [ELSE statement_list] END IF
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/118
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/119
CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list] END CASE
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/120
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/121
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/122
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/123
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/124
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/125
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/126
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/127
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/128
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/129
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/130
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/131
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/132
GRANT syntax
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user_specification [, user_specification] ... [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}] [WITH with_option ...]
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/133
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/134
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/135
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/136
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/137
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/138
The particulars by which SQL code is inserted into applications depend on the language and data-manipulation methodology used
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/139
More features
Cross-database joins Outer joins API: C/C++, Eiffel, Java, PHP, Perl, Python, TCL Runs on Windows, UNIX, and Mac High performance
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/141
SQL syntax
CREATE TABLE people (name CHAR(10)) INSERT INTO people VALUES (Joe) SELECT name FROM people WHERE name like J%
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/142
SQL commands
SHOW DATABASES SHOW TABLES Data types: INT, REAL, CHAR(l), VARCHAR(l), TEXT(l), DATE, TIME ALTER TABLE mytable MODIFY mycolumn TEXT(100) ENUM(cat,dog,rabbit,pig)
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/143
SQL commands
CREATE DATABASE dbname CREATE TABLE tname (id NOT NULL PRIMARY KEY AUTO_INCREMENT) CREATE INDEX part_of_name ON customer (name(10)) INSERT INTO tname (c1, , cn) values (v1, , vn)
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/144
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/145
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/146
Aggregate queries
SELECT position FROM people GROUP by position SELECT position, AVG (salary) FROM people GROUP BY position HAVING AVG (salary) > 50000.00
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/147
INSERT INTO WebCache (url, ptext) VALUES (index.html, Welcome to the University of Michigan); SELECT url from WebCache WHERE MATCH (ptext) against (Michigan);
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/148
Advanced features
Transactions Table locking Functions Unions Outer joins
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/149
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/150
Useful pointers
Small example: http://www.itl.nist.gov/div897/ctg/dm/sql_exa mples.htm MySQL documentation: http://www.mysql.com/doc/en/index.html (official) MySQL tutorial: http://www.mysql.com/doc/en/Tutorial.html Online, interactive tutorials: http://sqlzoo.net/ http://sql.grussell.org/
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke Chapter 7/151
http://www.itl.nist.gov/div897/ctg/dm/sql_examples.htm
use test;
CREATE TABLE STATION (ID INTEGER PRIMARY KEY, CITY CHAR(20), STATE CHAR(2), LAT_N REAL, LONG_W REAL);
DESCRIBE STATION;
INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112); INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105); INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68);
SELECT * FROM STATION WHERE LAT_N > 39.7; Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/152
SELECT ID, CITY, STATE FROM STATION WHERE LAT_N > 39.7;
CREATE TABLE STATS (ID INTEGER REFERENCES STATION(ID), MONTH INTEGER CHECK (MONTH BETWEEN 1 AND 12), TEMP_F REAL CHECK (TEMP_F BETWEEN -80 AND 150), RAIN_I REAL CHECK (RAIN_I BETWEEN 0 AND 100), PRIMARY KEY (ID, MONTH));
INSERT INTO STATS VALUES (13, 1, 57.4, 0.31); INSERT INTO STATS VALUES (13, 7, 91.7, 5.15); INSERT INTO STATS VALUES (44, 1, 27.3, 0.18); INSERT INTO STATS VALUES (44, 7, 74.8, 2.11); INSERT INTO STATS VALUES (66, 1, 6.7, 2.10);
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/153
SELECT MONTH, ID, RAIN_I, TEMP_F FROM STATS ORDER BY MONTH, RAIN_I DESC;
SELECT LAT_N, CITY, TEMP_F FROM STATS, STATION WHERE MONTH = 7 AND STATS.ID = STATION.ID ORDER BY TEMP_F;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/154
SELECT ID,
MONTH, (TEMP_F - 32) * 5 /9, RAIN_I * 0.3937 FROM STATS;
SELECT * FROM METRIC_STATS WHERE TEMP_C < 0 AND MONTH = 1 ORDER BY RAIN_C;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/155
COMMIT WORK;
ROLLBACK WORK;
COMMIT WORK;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/156
WHERE MONTH = 7
OR ID IN (SELECT ID FROM STATION WHERE LONG_W < 90);
COMMIT WORK;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/157
http://www.mysql.com/doc/en/Tutorial.html
INSERT INTO animals (name) VALUES ("dog"),("cat"),("penguin"), ("lax"),("whale"),("ostrich"); SELECT * FROM animals;
CREATE TABLE shop ( article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, dealer CHAR(20) price DOUBLE(16,2) DEFAULT '' NOT NULL,
SELECT * FROM shop; Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/158
INSERT INTO articles VALUES (NULL,'MySQL Tutorial', 'DBMS stands for DataBase ...'), (NULL,'How To Use MySQL Efficiently', 'After you went through a ...'), (NULL,'Optimizing MySQL','In this tutorial we will show ...'), (NULL,'1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
(NULL,'MySQL vs. YourSQL', 'In the following database comparison ...'), (NULL,'MySQL Security', 'When configured properly, MySQL ...');
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/159
SELECT article, dealer, price FROM shop ORDER BY price DESC LIMIT 1;
SELECT article, MAX(price) AS price FROM shop GROUP BY article; Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/160
INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;
SELECT shop.article, dealer, shop.price FROM shop, tmp WHERE shop.article=tmp.article AND shop.price=tmp.price;
UNLOCK TABLES;
0.00+LEFT(
FROM shop
GROUP BY article;
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/161
# foreign keys
CREATE TABLE person ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, name CHAR(60) NOT NULL, PRIMARY KEY (id) );
CREATE TABLE shirt ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
Chapter 7/162
INSERT INTO shirt VALUES (NULL, 'polo', 'blue', LAST_INSERT_ID()), (NULL, 'dress', 'white', LAST_INSERT_ID()), (NULL, 't-shirt', 'blue', LAST_INSERT_ID());
INSERT INTO shirt VALUES (NULL, 'dress', 'orange', LAST_INSERT_ID()), (NULL, 'polo', 'red', LAST_INSERT_ID()), (NULL, 'dress', 'blue', LAST_INSERT_ID()), (NULL, 't-shirt', 'white', LAST_INSERT_ID());
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/163
# unions
select id, style from shirt where color = 'blue' union select id, style from shirt where color = 'orange'
CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL, day INT(2) UNSIGNED ZEROFILL); INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2), (2000,2,23),(2000,2,23);
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/164
References
Bi ging MySQL Ai-ti Aptech SQL applications Dragomir R. Radev Fall 2005 http://dev.mysql.com/doc/refman/5.5/ en/
Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke
Chapter 7/165