* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download SQL: Single Table Queries
Survey
Document related concepts
Extensible Storage Engine wikipedia , lookup
Microsoft Access wikipedia , lookup
Tandem Computers wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Functional Database Model wikipedia , lookup
Oracle Database wikipedia , lookup
Ingres (database) wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
ContactPoint wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Clusterpoint wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Transcript
SQL - Structured Query Language ABOUT SQL DDL: CREATE DML: INSERT, DELETE, MODIFY D. Christozov / G.Tuparov INF 280 Database Systems: SQL 1 Wikipedia: About SQL (1) • SQL (Structured Query Language) is the most popular computer language used to Create, Retrieve, Update and Delete (CRUD) data from relational database management systems. The language has evolved beyond its original purpose, and now supports object-relational database management systems. • SQL has been standardized by both ANSI and ISO. – – – – Appeared: Designed by: Developer: First commercial: D. Christozov / G.Tuparov 1974 D. Chamberlin & R. Boyce IBM Oracle INF 280 Database Systems: SQL 2 Wikipedia: About SQL (2) • Pronunciation: SQL is commonly spoken either as the names of the letters ess-cue-el, or like the word sequel. Each of the major database products (or projects) containing the letters SQL has its own convention: – MySQL is officially and commonly pronounced "My Ess Cue El"; – PostgreSQL is expediently pronounced "postgres" (being the name of the predecessor to PostgreSQL); – Microsoft SQL Server is commonly spoken as "Microsoft-sequel-server". D. Christozov / G.Tuparov INF 280 Database Systems: SQL 3 SQL: History (1) • An influential paper, "A Relational Model of Data for Large Shared Data Banks", by Dr. Edgar F. Codd, was published in June, 1970 in the Association for Computing Machinery (ACM) journal, Communications of the ACM, although drafts of it were circulated internally within IBM in 1969. • Codd's model became widely accepted as the definitive model for relational database management systems (RDBMS or RDMS). • During the 1970s, a group at IBM's San Jose research centre developed a database system "System R" based upon, but not strictly faithful to, Codd's model. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 4 SQL: History (2) • Structured English Query Language (SEQL "SEQUEL") was designed to manipulate and retrieve data stored in System R. The acronym SEQUEL was later condensed to SQL. Although SQL was influenced by Codd's work, Donald D. Chamberlin and Raymond F. Boyce at IBM were the authors of the SEQUEL language design. • The first non-commercial, relational, non-SQL database, Ingres, was developed in 1974 at U.C. Berkeley. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 5 SQL: History (3) • Ingres spawned a number of commercial database applications, including Sybase, Microsoft SQL Server, HP NonStop SQL and a number of others. • Postgres (Post Ingres), a project which started in the mid-1980s, later evolved into PostgreSQL. G.Tuparov INF 280 Database Systems: SQL 6 SQL: History (4) • In 1978, IBM began to develop commercial products based on their System R prototype that implemented SQL, including the System/38 (announced in 1978 and commercially available in August 1979), SQL/DS (introduced in 1981), and DB2 (in 1983). • At the same time Relational Software, Inc. (now Oracle Corporation) developed their own version of a RDBMS for the Navy, CIA and others. In the summer of 1979 was introduced Oracle V2 (Version2) for VAX computers as the first commercially available implementation of SQL. In fact they only beat IBM's release of the System/38 by a few weeks. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 7 MySQL • MySQL is an open source database, a SQL client/server relational database management system originating from Scandinavia. (Now is owned by Oracle Corp.) • Multiplatform (Unix, Linux, Windows, Mac …) • Pluggable storage engines (MyISAM, InnoDB, etc) • MySQL is used mainly because it's free, reliable, easy to setup and maintain. Useful links: – http://www.mysql.com – http://www.datadisk.co.uk/html_docs/mysql/mysql.htm – http://thinkingmonster.wordpress.com/mysql/ G.Tuparov INF 280 Database Systems: SQL 8 MySQL Architecture G.Tuparov INF 280 Database Systems: SQL 9 Drop-in replacements for MySQL • MariaDB http://www.mariadb.org • Percona Server http://www.percona.com/software/percona-server G.Tuparov INF 280 Database Systems: SQL 10 SQL Standards Year Name 1986 SQL-86 Alias SQL-87 ANSI – 1986; Refined by ISO in 1987 Minor revisions 1989 SQL-89 1992 SQL-92 Comments SQL2 Major Revisions 1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, nonscalar types and some object-oriented features. (The last two are somewhat controversial and not yet widely supported.) 2003 SQL:2003 Introduced XML-related features, window functions, standardized sequences and columns with auto-generated values (including identity-columns). 2006 SQL:2006 Defines ways SQL can be used in conjunction with XML: importing and storing XML data in an SQL database, manipulating it within the database and publishing both XML and conventional SQL-data in XML form; provides facilities that permit applications to integrate into their SQL code the use of XQuery, the XML Query Language published by the World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data and XML documents D. Christozov / G.Tuparov INF 280 Database Systems: SQL 11 Relational Algebra – as a fundament of SQL • Relational algebra, an offshoot of first-order logic, is a set of relations closed under operators. Operators operate on one or more relations to yield a relation. Relation algebra in pure mathematics is an algebraic structure, relevant to mathematical logic and set theory. • E.F. Codd proposed such an algebra as a basis for database query languages. SQL is loosely based on a relational algebra, though the operands in SQL (tables) are not exactly relations and several useful theorems about the relational algebra do not hold in the SQL counterpart. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 12 Relational Algebra – Primitive operators (1) • The six primitive operators of Codd's algebra are – Selection; – Projection; – Cartesian product (also called the cross product or cross join); – Set union; – Set difference; – Rename. • These six operators are fundamental in the sense that none of them can be omitted without losing expressive power. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 13 Relational Algebra – Primitive operators • Many other operators have been defined in terms of these six. Among the most important are – Set intersection; – Division; – Natural join. • Altogether, the operators of relational algebra have identical expressive power to that of domain relational calculus or tuple relational calculus. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 14 Relational Algebra – Set operators • Although three of the six basic operators are taken from set theory, there are additional constraints that are present in their relational algebra counterparts: – For set union and set difference, the two relations involved must be union-compatible — that is, the two relations must have the same set of attributes. – As set intersection can be defined in terms of set difference, the two relations involved in set intersection must also be union-compatible. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 15 Relational Algebra – Set Union • Union: R U S Result: Relation with tuples from R and S with duplicates removed. R FName LName Age FName LName S Age John Smith 35 John Smith 35 Mary Shelley 29 Mary Shelley 29 John Doe 45 John Doe 45 Nicky Little 25 Nicky Little 25 FName LName Age Peter White 30 John Doe 45 Nicky Black 27 Peter White 30 Nicky Black 27 G.Tuparov RUS= Union is a commutative operation RUS=SUR INF 280 Database Systems: SQL 16 Relational Algebra – Set Difference • Difference: R - S Result: Relation with tuples from R but not from S. R FName LName Age S John Smith 35 Mary Shelley 29 John Doe 45 Nicky Little 25 FName LName Age John Doe 45 Peter White 30 Nicky Black 27 G.Tuparov R–S= FName LName Age John Smith 35 Mary Shelley 29 Nicky Little 25 Difference is not a commutative operation R - S ≠ S - R INF 280 Database Systems: SQL 17 Relational Algebra – Set Intersection • Intersection: R ∩ S Result: Relation with tuples that appear in both R and S. R FName LName Age S John Smith 35 Mary Shelley 29 John Doe 45 Nicky Little 25 FName LName Age John Doe 45 Peter White 30 Nicky Black 27 G.Tuparov R∩S= FName LName Age John Doe 45 Intersection is a commutative operation, i.e. R ∩ S = S ∩ R INF 280 Database Systems: SQL 18 Relational Algebra – Cartesian product • Cartesian product: R x S Result: All combinations of tuples from two relations. R FName LName Age FName LName Age Car Color S John Smith 35 John Smith 35 BMW Black Mary Shelley 29 John Smith 35 Audi John Doe 45 Mary Shelley 29 BMW Black Nicky Little 25 Mary Shelley 29 Audi John Doe 45 BMW Black RxS= White White Car Color John Doe 45 Audi BMW Black Nicky Little 25 BMW Black Audi White Nicky Little 25 Audi D. Christozov / G.Tuparov INF 280 Database Systems: SQL White White 19 Relational Algebra - Projection • A projection is a unary operation, where a1,...,an(R) is a subset of attribute names of the set of all attribute names of the relation R. • Project only the names and cars of the employees in R Fname, Lname, Car (R) Result R FName LName Age Car Color FName LName Car John Smith Mary BMW Red John Smith Shelley 29 Mini Black Mary Shelley Mini John Doe 45 Audi White John Doe Audi Nicky Little 25 BMW White Nicky Little BMW Peter White 30 Reno Red Peter White Reno Nicky Black 27 Seat Green Nicky Black Seat D. Christozov / G.Tuparov 35 INF 280 Database Systems: SQL BMW 20 Relational Algebra - Selection (1) • A selection is a unary operation, where condition(s) (R) selects all those tuples in R for which condition(s) holds. The selection operation acts like a filter on a relation. • The resulting relation will have the same degree as the original relation. • The condition can be made up of any combination of comparison or logical operators that operate on the attributes of R. – Comparison operators: = ≠ > < ≥ ≤ – Logical operators: ᴧ (AND) v (OR) ┐ (NOT) D. Christozov / G.Tuparov INF 280 Database Systems: SQL 21 Relational Algebra - Selection (2) Example 1: Select only those tuples from R that have BMW car: Car=‘BMW’ (R) Result FName LName Age Car John Smith FName LName Age Car BMW Red John Smith 35 BMW Red Mary Shelley 29 Mini Black Nicky Little 25 BMW White John Doe 45 Audi White Nicky Little 25 BMW White Peter White 30 Reno Red Nicky Black 27 Seat Green G.Tuparov 35 Color INF 280 Database Systems: SQL Color 22 Relational Algebra - Selection (3) Example 2: Select only those tuples from R that have white BMW car : Car=‘BMW’ ᴧ Color=‘White’ (R) Result FName LName Age Car John Smith BMW Red Mary Shelley 29 Mini Black John Doe 45 Audi White Nicky Little 25 BMW White Peter White 30 Reno Red Nicky Black 27 Seat Green G.Tuparov 35 Color FName LName Age Car Nicky BMW White INF 280 Database Systems: SQL Little 25 Color 23 Relational Algebra - Selection (4) Example 3: Select only those tuples from R that have BMW or green car : Car=‘BMW’ V Color=‘Green’ (R) Result FName LName Age Car John Smith FName LName Age Car BMW Red John Smith 35 BMW Red Mary Shelley 29 Mini Black Nicky Little 25 BMW White John Doe 45 Audi Blue Nicky Black 27 Seat Nicky Little 25 BMW White Peter White 30 Reno Red Nicky Black 27 Seat Green G.Tuparov 35 Color INF 280 Database Systems: SQL Color Green 24 Relational Algebra - Selection (5) Example 4: Select only those tuples from R that have not BMW or not black car : ┐(Car=‘BMW’ ᴧ Color=‘Black’) (R) Result FName LName Age Car John Smith FName LName Age Car Color BMW Red John Doe 45 Audi White Mary Shelley 29 Mini Black Peter White 30 Reno Red John Doe 45 Audi White Nicky Black 27 Seat Green Nicky Little 25 BMW White Peter White 30 Reno Red Nicky Black 27 Seat Green G.Tuparov 35 Color INF 280 Database Systems: SQL 25 Relational Algebra – Combining Selection and Projection Example: Show only the names of the employees in R that drive red cars. Fname, Lname ( Color=‘Red’ (R)) Result FName LName Age Car John Smith BMW Red John Smith Mary Shelley 29 Mini Black Peter White John Doe 45 Audi White Nicky Little 25 BMW White Peter White 30 Reno Red Nicky Black 27 Seat Green G.Tuparov 35 Color INF 280 Database Systems: SQL FName LName 26 Relational Algebra - The aggregation operation • There are five aggregate functions that are included with most databases. These operations are – Sum – Count – Average – Maximum – Minimum • Aggregate functions could be written using – the Projection operator or – the Script F (F) character (in Elmasri/Navathe book) D. Christozov / G.Tuparov INF 280 Database Systems: SQL 27 Relational Algebra - Sum Example: Show the sum of all salaries from R F SUM (Salary) (R) Result: FName LName Salary SUM (Salary) John Smith 12000 Mary Shelley 2500 John Doe 2000 Nicky Little 2000 Peter White 3000 Nicky Black 1000 D. Christozov / G.Tuparov 1500 INF 280 Database Systems: SQL 28 Relational Algebra - Count Example: Count the number of employees from R F COUNT (FName) (R) Result: FName LName Salary COUNT (FName) John Smith 6 Mary Shelley 2500 John Doe 2000 Nicky Little 2000 Peter White 3000 Nicky Black 1000 D. Christozov / G.Tuparov 1500 INF 280 Database Systems: SQL 29 Relational Algebra - Average Example: Show the average of all salaries from R F AVG (Salary) (R) Result: FName LName Salary AVG (Salary) John Smith 2000 Mary Shelley 2500 John Doe 2000 Nicky Little 2000 Peter White 3000 Nicky Black 1000 D. Christozov / G.Tuparov 1500 INF 280 Database Systems: SQL 30 Relational Algebra - Min Example: Show the minimum Salary from R: F MIN (Salary) (R) Result: FName LName Salary MIN (Salary) John Smith 1000 Mary Shelley 2500 John Doe 2000 Nicky Little 2000 Peter White 3000 Nicky Black 1000 D. Christozov / G.Tuparov 1500 INF 280 Database Systems: SQL 31 Relational Algebra - Max Example: Show the maximum Salary from R: F MAX (Salary) (EMP) Result: FName LName Salary MAX(Salary) John Smith 3000 Mary Shelley 2500 John Doe 2000 Nicky Little 2000 Peter White 3000 Nicky Black 2500 D. Christozov / G.Tuparov 1500 INF 280 Database Systems: SQL 32 Relational Algebra – Natural Join • Join operations bring together two relations and combine their attributes and tuples in a specific fashion (according to specific condition). • The generic join operator (Theta Join) is: ⋈ • It takes as arguments the attributes from the two relations that are to be joined. • The join condition can be = ≠ > < ≥ ≤ • When the join condition operator is = then we call this an Equijoin D. Christozov / G.Tuparov INF 280 Database Systems: SQL 33 Relational Algebra – Natural Join Example: R ⋈ R.Dept = S.Dept S R S FName LName Dept John Smith Mary Shelley HR John Doe IT FName LName Dept Office Peter White IT John Smith IT 203 Nicky Black PR Mary Shelley HR 201 John Doe IT 203 Peter White IT 203 Nicky Black PR 202 Dept Office HR 201 PR 202 IT 203 G.Tuparov IT Result INF 280 Database Systems: SQL 34 SQL DDL: Creating Environment, Schema, DBSpace Environment Database Schema DBspace Storage pool D. Christozov / G.Tuparov Holds a single database. Consists of the set of all schemes defined in that environment. Consists of zero or more tables, views, and privileges, belonging to a user. Logical allocation of space in the database portion of total database. A user must have access to at least one DBspace. Different DBspaces may have different locking levels. Consists of dbextents, provided by the DB administrator. INF 280 Database Systems: SQL 35 SQL DDL: Creating Environment, Schema, DBSpace CREATE DATABASE database_name DROP DATABASE database_name ACQUIRE [ PUBLIC | PRIVATE ] DBSPACE NAMED dbspace_name [ (option [, option] ...)] Option:LOCK = { DBSPACE | PAGE | ROW } specifies the locking level. DBMS locked the specified resource, for simultaneous access by a second user. PRIVATE DB are locked on entire dbspace level. D. Christozov / G.Tuparov INF 280 Database Systems: SQL 36 SQL DDL: Creating Environment, Schema, DBSpace ALTER DBSPACE dbspace_name DROP DBSPACE dbspace_name Activating a Database: USE database_name or START database_name D. Christozov / G.Tuparov INF 280 Database Systems: SQL 37 SQL DDL: Create Table • Before creating a table, the following must be completed: – a database (environment) to hold the schema (tables) must exists; – a dbspace, to hold the tables must exists; – the database chosen to hold the table must be active; – the user must have the authority to create schemes (table); D. Christozov / G.Tuparov INF 280 Database Systems: SQL 38 SQL DDL: Create Schema, Table CREATE SCHEMA database_schema_name AUTHORIZATION owner_name; MySQL Dialect CREATE DATABASE database_name; USE database_name; GRANT list_of_privilegies ON database_name TO user_name@host_name IDENTIFIED BY some_password; D. Christozov / G.Tuparov INF 280 Database Systems: SQL 39 SQL DDL: Create Table CREATE TABLE table_name ( column_name data_type [null_specifier], ... PRIMARY KEY (column_name [,...]), FOREIGN KEY (column_name) REFERENCES table_name (column_name) ); DROP TABLE table_name CASCADE; ALTER TABLE table_name ADD column_name ...; DROP column_name; ALTER column_name SET ... … …. ; D. Christozov / G.Tuparov INF 280 Database Systems: SQL 40 SQL DDL: Data Types, Domain Standard SQL Data type CHARACTER (length) left_justified, truncated or rejected VARCHAR (max_length) probably a given limit exists LONG VARCHAR - without specifying the max_length MySQL Data Type CHAR(length) 1-255 characters DATE TIME DATE, DATETIME TIMESTAMP, TIME YEAR D. Christozov / G.Tuparov VARCHAR(length) 1-65535 chars TINYTEXT(length) up to 255 chars TEXT(length) up to 65535 chars MEDIUMTEXT(length) LONGTEXT(length) INF 280 Database Systems: SQL 41 SQL DDL: Data Types, Domain Standard SQL Whole numbers: INTEGER SMALLINT Floating point numbers: FLOAT D. Christozov / G.Tuparov MySQL General format: INT_DATA_TYPE [size] [unsigned] TINYINT [-128 to 127] or 255 unsign. SMALLINT [-32768 to 32767] MEDIUMINTINT … INT or INTEGER … BIGINT … FLOAT(length, dec) length – number of digits dec – number of decimal places DOUBLE(length, dec) DECIMAL(length, dec) INF 280 Database Systems: SQL 42 SQL DML: Insert EMPLOYEE Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO INSERT INTO EMPLOYEE VALUES (‘Richard’,‘K’, ‘Marini’,‘653298653’,‘1962-12-30’, ’98 Oak Forest,Katy,TX’,‘M’,37000,‘987654321’,4); INSERT INTO EMPLOYEE (Fname, Lname, DNO, SSN) VALUES (‘Richard’, ‘Marini’, 4, ‘653298653’); INSERT INTO EMPLOYEE (Fname, Lname, SSN, DNO) VALUES (‘Robert’, ‘Hatcher’, ‘980760540’, 2); INSERT INTO EMPLOYEE (Fname, Lname, DNO) VALUES (‘Robert’, ‘Hatcher’, 5); D. Christozov / G.Tuparov INF 280 Database Systems: SQL 43 SQL DML: Delete (1) EMPLOYEE Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO John B Smith 123456789 1965-01-09 …TX… M 30000 3334455555 5 Franklin T Wong 333445555 1956-12-08 …TX… M 40000 888665555 5 Alicia J Zelaya 999887777 1968-01-19 …TX… F 25000 987654321 4 Jennifer S Wallace 987654321 1941-06-02 …TX… F 43000 888665555 4 Ramesh K Narayan 666884444 1962-09-15 …TX… M 38000 333445555 5 Joyce A English 453453453 1972-07-31 …TX… F 25000 333445555 5 Ahmad V Jabbar 987987987 1969-03-29 …TX… M 25000 987654321 4 James E Borg 888665555 1937-11-10 …TX… M 55000 Null 1 DEPARTMENT DELETE FROM EMPLOYEE WHERE Lname=‘Zelaya’; DELETE FROM EMPLOYEE WHERE SSN=‘123456789’; D. Christozov / G.Tuparov Dname Dnumber MGRSSN MGRSDate Research 5 333445555 1988-05-22 Administration 4 987654321 1995-01-01 Headquarter 1 888665555 1981-06-19 INF 280 Database Systems: SQL 44 SQL DML: Delete (2) EMPLOYEE Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO John B Smith 123456789 1965-01-09 …TX… M 30000 3334455555 5 Franklin T Wong 333445555 1956-12-08 …TX… M 40000 888665555 5 Alicia J Zelaya 999887777 1968-01-19 …TX… F 25000 987654321 4 Jennifer S Wallace 987654321 1941-06-02 …TX… F 43000 888665555 4 Ramesh K Narayan 666884444 1962-09-15 …TX… M 38000 333445555 5 Joyce A English 453453453 1972-07-31 …TX… F 25000 333445555 5 Ahmad V Jabbar 987987987 1969-03-29 …TX… M 25000 987654321 4 James E Borg 888665555 1937-11-10 …TX… M 55000 Null 1 DELETE FROM EMPLOYEE WHERE DNO IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname=‘Headquarter’); DELETE FROM EMPLOYEE; D. Christozov / G.Tuparov DEPARTMENT Dname Dnumber MGRSSN MGRSDate Research 5 333445555 1988-05-22 Administration 4 987654321 1995-01-01 Headquarter 1 888665555 1981-06-19 INF 280 Database Systems: SQL 45 SQL DML: Modify (1) EMPLOYEE Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO John B Smith 123456789 1965-01-09 …TX… M 30000 3334455555 5 Franklin T Wong 333445555 1956-12-08 …TX… M 40000 888665555 5 Alicia J Zelaya 999887777 1968-01-19 …TX… F 25000 987654321 4 Jennifer S Wallace 987654321 1941-06-02 …TX… F 43000 888665555 4 Ramesh K Narayan 666884444 1962-09-15 …TX… M 38000 333445555 5 Joyce A English 453453453 1972-07-31 …TX… F 25000 333445555 5 Ahmad V Jabbar 987987987 1969-03-29 …TX… M 25000 987654321 4 James E Borg 888665555 1937-11-10 …TX… M 55000 Null 1 UPDATE EMPLOYEE SET Salary = Salary *1.1 WHERE DNO IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname=‘Research’); D. Christozov / G.Tuparov DEPARTMENT Dname Dnumber MGRSSN MGRSDate Research 5 333445555 1988-05-22 Administration 4 987654321 1995-01-01 Headquarter 1 888665555 1981-06-19 INF 280 Database Systems: SQL 46 SQL DML: Modify PROJECT Pname Pnumber Plocation DNum UPDATE PROJECT SET Plocation = ‘Bellaire’, DNum = 5 WHERE PNumber = 10; UPDATE EMPLOYEE DEPARTMENT SET Salary = Salary *1.1 Dname Dnumber WHERE DNO IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname=‘Research’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL MGRSSN MGRSDate 47 Questions? D. Christozov / G.Tuparov INF 280 Database Systems: SQL 48