Sie sind auf Seite 1von 21

1

Structured Query Language


Many database management systems support some version of structured query language (SQL). In some DBMSs (i.e., ORACLE) SQL is the primary data manipulation interface. Consequently, SQL is a very important topic. The purpose of this document is to introduce you to the major SQL statements and to show you how they work. This document will concentrate primarily on ORACLE SQL; however, some attention also will be given to other versions of SQL. This is not a complete reference of SQL. If you are interested in a more detailed coverage I can suggest several good textbooks for outside reading. SQL commands can be broken into the 3 following functional groups: . - Data Definition Language (DDL) - used to define the schema (structure) of the database - CREATE TABLE - ALTER TABLE - CREATE INDEX - DROP TABLE - DROP INDEX

- Data Manipulation Language (DML) - used to retrieve and update from the database - SELECT - INSERT - UPDATE - DELETE - Data Control Language (DCL) - used to manipulate the processing of data and erform other misc. functions - COMMIT - ROLLBACK

I. SQL DATA DEFINITION (DDL) TABLES CREATE TABLE - Define the structure of a new table

Format: CREATE TABLE tablename ({col-name type [(size)][constraint],...}); The 'constraint' clause in the CREATE TABLE statement is used to enforce referential integrity. Specifically, PRIMARY KEY, FOREIGN KEY, and CHECK integrity can be set when you define the table. The syntax for key and check constraints is shown below. {PRIMARY KEY | FOREIGN KEY} (local-field) [REFERENCES foreign-field] } for attribute constraints... CHECK (condition) Example: Define the student table CREATE TABLE STUDENT ( STUID CHAR(5), LNAME CHAR(10) NOT NULL, FNAME CHAR(8), MAJOR CHAR(7) CHECK (MAJOR IN (HIST,ART,CIS)), CREDITS INTEGER CHECK (CREDITS > 0), PRIMARY KEY (STUID));

ALTER TABLE - Add a column to the "right" of an existing table, modify an existing attribute, or drop an existing column or constraint. Format: ALTER TABLE tablename {ADD {col-name type [(size)] | constraint}} | MODIFY {col_name type [(size)] | DROP {col-name [drop-clause]; where the drop clause is ... DROP {PRIMARY KEY | UNIQUE {col-name} | CONSTRAINT constraint| Example1: Add column called MINOR to STUDENT table ALTER TABLE STUDENT ADD MINOR CHAR(8); Example2: Drop the MGR-SSN column ALTER TABLE EMPLOYEE DROP MGR-SSN; Example3: Modify existing attribute

ALTER TABLE DEPT MODIFY MINOR CHAR(10); LIMITATIONS/ENHANCEMENTS: When you add a column, all existing tuples get the extra column filled with NULL values. You have to go in and update the column to enter valid data later to get rid of the NULLs. You can only add or drop a single column at a time in the ALTER statement. CREATE INDEX - Create an index on attribute(s) within a table Indexes are used to improve system performance by providing a more efficient means of accessing selected attributes. Format: CREATE [UNIQUE] INDEX index-name ON table-name {(col-name [ASC | DESC])}; Example1: Create an index on the STUID attribute of the STUDENT table CREATE INDEX stuindex ON STUDENT (STUID); Example2: Create a unique index on SSN of EMPLOYEES and make it sort in reverse order CREATE UNIQUE INDEX empindex ON EMPLOYEES (SSN) DESC; Example 3: Create a composite index on COURSENUM and SDUID from ENROLL table CREATE INDEX enroll-idx ON ENROLL (COURSENUM, STUID); DROP TABLE - Remove a table (and all data) or an index on a table from database. If the table has any foreign key constraints then the CASCADE CONSTRAINTS clause is required to drop the table. Format: DROP TABLE table-name [CASCADE CONSTRAINTS]; DROP INDEX index-name; Example1: Delete the table STUDENT (no foreign key constraints) DROP TABLE STUDENT; Example2: Delete the table ENROLL (with foreign key constraints) DROP TABLE ENROLL CASCADE CONSTRAINTS; Example3: Remove the emp-name index on employee table DROP INDEX emp-name; When you drop a table you also delete all data currently in that table. Be careful!

II. SQL DATA MANIPULATION LANGUAGE (DML) The DML component of SQL is the part that is used to query and update the tables (once they are built via DDL commands or other means). By far, the most commonly used DML statement is the SELECT. It combines a range of functionality into one complex command. SELECT Used primarily to retrieve data from the database. Also used to create copies of tables, create views, and to specify rows for updating. General Format: Generic overview applicable to most commercial SQL implementations - lots of potential combinations. There are several variations available in Oracle. SELECT {field-list | * | ALL | DISTINCT | expression} FROM table-list WHERE expression GROUP BY group-fields HAVING group-expression ORDER BY field-list; Only the SELECT and the FROM clauses are required. The others are optional. FROM - A required clause that lists the tables that the select works on. You can define "alias" names with this clause to speed up query input and to allow recursive "self-joins". WHERE - An optional clause that selects rows that meet the stated condition. A "subselect" can appear as the expression of a where clause. This is called a "nested select". GROUP BY - An optional clause that groups rows according to the values in one or more columns and sorts the results in ascending order (unless otherwise specified). The duplicate rows are not eliminated, rather they are consolidated into one row. This is similar to a control break in traditional programming. HAVING - An optional clause that is used with GROUP BY. It selects from the rows that result from applying the GROUP BY clause. This works the same as the WHERE clause, except that it only applies to the output of GROUP BY. ORDER BY - An optional clause that sorts the final result of the SELECT into either ascending or descending order on one or more named columns. There can be complex interaction between the WHERE, GROUP BY, and HAVING clauses. When all three are present the WHERE is done first, the GROUP BY is done second, and the HAVING is done last.

Example 1: Select all employees from the 'ACCT department. SELECT * FROM EMPLOYEES WHERE EMP-DEPT = 'ACCT'; Example 2: Show what salary would be if each employee recieved a 10% raise. SELECT LNAME, SALARY AS CURRENT, SALARY * 1.1 AS PROPOSED FROM EMPLOYEES; SIMPLE SINGLE TABLE RETRIEVAL Example 1: Retrieve all information about students ('*' means all attributes) SELECT * FROM STUDENT; STUID S1001 S1010 S1015 S1002 S1020 S1013 LNAME Smith Burns Jones Chin Rivera McCarchy FNAME Tom Edward Mary Ann Jane Owen MAJOR History Art Math Math CIS Math CREDITS 90 63 42 36 15 9

Example 2: Find the last name, ID, and credits of all students SELECT LNAME, STUID, CREDITS FROM STUDENT; LNAME Smith Burns Jones Chin Rivera McCarthy STUID S1001 S1010 S1015 S1002 S1020 S1013 CREDITS 90 63 42 36 15 9

Example 3: Find all information about students who are math majors SELECT * FROM STUDENT WHERE MAJOR = 'Math'; STUID S1015 S1002 S1013 LNAME Jones Chin McCarthy FNAME Mary Ann Owen MAJOR Math Math Math CREDITS 42 36 9

Example 4: Find the student ID of all History SELECT STUID FROM STUDENT WHERE MAJOR = 'History'; STUID S1001 Example 5: Retrieve a list of all majors that currently have students SELECT DISTINCT MAJOR FROM STUDENT; MAJOR Art CIS History Math MORE COMPLEX SINGLE TABLE RETRIEVAL The WHERE clause can be enhanced to be more selective. Operators that can appear in WHERE conditions include: =, <> ,< ,> ,>= ,<= IN BETWEEN...AND... LIKE IS NULL AND, OR, NOT

Example 1: Find the student ID of all math majors with more than 30 credit hours. SELECT STUID FROM STUDENT WHERE MAJOR = 'Math' AND CREDITS > 30; STUID S1015 S1002

Example 2: Find the student ID and last name of students with between 30 and 60 hours (inclusive). SELECT STUID, LNAME FROM STUDENT WHERE CREDITS BETWEEN 30 AND 60; this is the same as... SELECT STUID, LNAME FROM STUDENT WHERE (CREDITS >= 30) AND (CREDITS <= 60); STUID S1015 S1002 LNAME Jones Chin

Example 3: Retrieve the ID of all students who are either a math or an art major. SELECT STUID FROM STUDENT WHERE MAJOR IN ('Math','Art'); this is the same as... SELECT STUID FROM STUDENT WHERE (MAJOR = 'Math') OR (MAJOR = 'Art'); STUID S1010 S1015 S1002 S1013

Example 4: Retrieve the ID and course number of all students without a grade in a class. SELECT STUID, COURSENUM FROM ENROLL WHERE GRADE IS NULL; STUID S1010 S1010 COURSENUM ART103A MTH103C

NOTE: IS NULL may only appear in the WHERE clause. Also note that you say "IS NULL", not "= NULL". NULL means "unknown" and does not really have a value in the normal sense. Example 5: List the ID and course number for all students that successfully completed classes (the inverse of #4 above). SELECT STUID, COURSENUM FROM ENROLL WHERE GRADE IS NOT NULL; STUID S1001 S1020 S1002 S1002 S1020 S1001 S1002 COURSENUM ART103A CIS201A CIS201A ART103A MTH101B HST205A MTH103C

Example 6: List the course number and faculty ID for all math courses. SELECT COURSENUM, FACID FROM CLASS WHERE COURSENUM LIKE 'MTH%'; COURSENUM MTH101B MTH103C FACID F110 F110

NOTE: % is a wildcard for any number of characters. _ is a wildcard that replaces a single character. They can be used together along with normal characters.

COLUMN FUNCTIONS (AGGREGATE FUNCTIONS)

Aggregate functions allow you to calculate values based upon all data in an attribute of a table. The SQL aggregate functions are: Max, Min, Avg, Sum, Count, StdDev, Variance. Note that AVG and SUM work only with numeric values and both exclude NULL values from the calculations. Example 1: How many students are there? SELECT COUNT(*) FROM STUDENT; COUNT(*) 6

NOTE: COUNT can be used in two ways. COUNT(*) is used to count the number of tuples that satisfy a query. COUNT with DISTINCT is used to count the number of unique values in a named column. Example 2: Find the number of departments that have faculty in them. SELECT COUNT(DISTINCT DEPT) FROM FACULTY; COUNT(DISTINCT) 4 Example 3: Find the average number of credits for students who major in math. SELECT AVG(CREDITS) FROM STUDENT WHERE MAJOR = 'Math'; AVG(CREDITS) 29

ORDERING OF THE QUERY RESULT The ORDER BY clause is used to force the query result to be sorted based on one or more column values. You can select either ascending or descending sort for each named column. Example 1: List the names and IDs of all faculty members arranged in alphabetical order. SELECT FACID, FACNAME

10

FROM FACULTY ORDER BY FACNAME; FACID F101 F110 F221 F202 F105 FACNAME Adams Byrne Smith Smith Tanaka

Example 2: List names and IDs of faculty members. The primary sort is the name and the secondary sort is by descending ID (for seniority purposes). SELECT FACID, FACNAME FROM FACULTY ORDER BY FACNAME, FACID DESC; GROUPING QUERY RESULTS The GROUP BY clause is used to specify one or more fields that are to be used for organizing tuples into groups. Rows that have the same value(s) are grouped together. The only fields that can be displayed are the ones used for grouping and ones derived using column functions. The column function is applied to a group of tuples instead to the entire table. If the HAVING clause is used, it takes the intermediate table produced by the GROUP BY and applies further selection criteria. Note that the data are aggregated when the HAVING is applied, so the HAVING expression should be written appropriately. Example 1: Find the number of students enrolled in each course. Display the course number and the count. SELECT COURSENUM, COUNT(*) FROM ENROLL GROUP BY COURSENUM;

11

COURSENUM ART103A CIS201A HST205A MTH101B MTH103C

COUNT(*) 3 2 1 1 2

Example 2: Find the average number of hours taken for all majors. Display the name of the major, the number of students, and the average. SELECT MAJOR, COUNT(*), AVG(CREDITS) FROM STUDENT GROUP BY MAJOR; MAJOR Art CIS History Math COUNT(*) 1 1 1 3 AVG(CREDIT) 63 15 90 29

Example 3: Find all courses in which fewer than three students are enrolled. SELECT COURSENUM FROM ENROLL GROUP BY COURSENUM HAVING COUNT(*) < 3; COURSENUM CIS201A HST205A MTH101B MTH103C

MULTIPLE TABLE QUERIES A JOIN operation is performed when more than one table is specified in the FROM clause. You would join two tables if you need information from both. You must specify the JOIN condition explicitly in SQL. This includes naming the columns in common and the comparison operator.

12

Example 1: Find the name and courses that each faculty member teaches. SELECT FACULTY.FACNAME, COURSENUM FROM FACULTY, CLASS WHERE FACULTY.FACID = CLASS.FACID; FACULTY.FACNAME Adams Tanaka Byrne Smith Byrne Tanaka COURSENUM ART103A CIS201A MTH101B HST205A MTH103C CIS203A

When both tables have an attribute name in common, you must specify which version of the attribute that you are referring to by preceding the attribute name with the table name and a period. (e.g., table-name.col-name). This is called "qualification". It is sometimes more convenient to use an "alias" (an alternative name) for each table. SQL specifies alias names in the FROM clause immediately following the actual table. Once defined, you can use the alias anywhere in the SELECT where you would normally use the table name. Example 2: Find the course number and the major of all students taught by the faculty member with ID number 'F110'. (3 table JOIN) SELECT ENROLL.COURSENUM, LNAME, MAJOR FROM CLASS , ENROLL, STUDENT WHERE FACID = 'F110' AND CLASS.COURSENUM = ENROLL.COURSENUM AND ENROLL.STUID = STUDENT.STUID;

ENROLL.COURSENUM MTH101B MTH103C MTH103C

LNAME Rivera Burns Chin

MAJOR CIS ART Math

Using aliases, this would be:

13

SELECT E.COURSENUM, LNAME, MAJOR FROM CLASS C, ENROLL E, STUDENT S WHERE FACID = 'F110' AND C.COURSENUM = E.COURSENUM AND E.STUID = S.STUID;

NESTED QUERIES SQL allows the nesting of one query inside another, but only in the WHERE and the HAVING clauses. In addition, SQL permits a subquery only on the right hand side of an operator. Example 1: Find the names and IDs of all faculty members who teach a class in room 'H221'. You could do this with 2 queries as follows: SELECT FACID FROM CLASS WHERE ROOM = 'H221'; ---> RESULT: F101, F102 SELECT FACNAME, FACID FROM FACULTY WHERE FACID IN (F101, F102); or you could combine the 2 into a nested query: SELECT FACNAME, FACID FROM FACULTY WHERE FACID IN (SELECT FACID FROM CLASS WHERE ROOM = 'H221'); Note that the nested SELECT is executed first and its results are used as the argument to the outer SELECTs IN clause. FACNAME Adams Smith FACID F101 F202

Example 2: Retrieve an alphabetical list of last names and IDs of all students in any class taught by faculty number 'F110'.

SELECT LNAME, STUID FROM STUDENT

14

WHERE STUID IN (SELECT STUID FROM ENROLL WHERE COURSENUM IN (SELECT COURSENUM FROM CLASS WHERE FACID = 'F110')) ORDER BY LNAME; LNAME Burns Chin Rivera STUID S1010 S1002 S1020

The most deeply nested SELECT is done first. Thus, after the first select you have: SELECT LNAME, STUID FROM STUDENT WHERE STUID IN (SELECT STUID FROM ENROLL WHERE COURSENUM IN ('MTH101B','MTH103C')) ORDER BY LNAME; Next, the next most deeply is done. SELECT LNAME, STUID FROM STUDENT WHERE STUID IN ('S1020','S1010','S1002') ORDER BY LNAME; Finally, the outer Select is executed giving the result printed above.

15

Example 3: Find the name and IDs of students who have less than the average number of credits. SELECT LNAME, STUID FROM STUDENT WHERE CREDITS < (SELECT AVG(CREDITS) FROM STUDENT); LNAME Chin Rivera McCarthy STUID S1002 S1020 S1013

UNION QUERIES A union query performs the 'union' set operation on two or more tables. The union operation returns all tuples from all tables (like appending a second table to the bottom of the first). The union operation also allows you to sort the resulting data, perform where restriction, etc. The syntax for the UNION operator is shown below. Format: SELECT fields FROM tables WHERE criteria GROUP BY field HAVING criteria UNION SELECT fields FROM tables WHERE criteria GROUP BY field HAVING criteria ORDER BY sortcriteria; Each select is a standard select with two exceptions. First, the fields shown in the SELECT clause must be 'union compatible' (i.e., equivalent number, type, and order). Second, there can only be one order by for the entire query. Example 1: Join a compatible customer table and a supplier table for all customers and suppliers located in 'Brazil'. Sort the final result by zip code.

16

SELECT CompanyName, City, Zip, Region, SupplierID AS ID FROM Suppliers WHERE Country = 'Brazil' UNION SELECT CompanyName, City, Zip, Region, CustomerID AS ID FROM Customer WHERE Country = 'Brazil' ORDER BY Zip; Oracle SQL also supports an INTERSECTION and MINUS clause that is similar to the UNION clause. These two new connectors have the same syntax as the UNION clause and perform the set operations indicated by their name. UPDATE Update gives you a way to modify individual attributes of a single tuple, a group of tuples, or a whole table (or view). Format: UPDATE table/view SET col-name = {value | expression} [col-name = value | subquery,...] [WHERE update_criteria]; You can only update tuples already present in the table (i.e., you cannot use UPDATE to add new tuples). You can either UPDATE one table at a time. You don't have to know the present value of a field to set it (although you can refer to it in the "expression" clause). The expression cannot be a sub-query or involve aggregate operations. Example 1: Change the major of student 'S1020' to music. (Update a single field of one tuple) UPDATE STUDENT SET MAJOR = 'Music' WHERE STUID = 'S1020'; Example 2: Change Tanaka's department to MIS and rank to Assistant. (Update several fields in one tuple) UPDATE FACULTY SET DEPT = 'MIS' RANK = 'Assistant' WHERE FACNAME = 'Tanaka'; Example 3: Change the major of student 'S1013' from math to NULL. (Updating using NULL)

17

UPDATE STUDENT SET MAJOR = NULL WHERE STUID = 'S1013'; Example 4: Change grades of all students in 'CIS201A' to A. (Updating several tuples) UPDATE ENROLL SET GRADE = 'A' WHERE COURSENUM = 'CIS201A'; Example 5: Give all students three extra credits. (Update all tuples) UPDATE STUDENT SET CREDITS = CREDITS + 3; Example 6: Change the room to 'B220' for all courses taught by Tanaka. (Updating with a subquery) UPDATE CLASS SET ROOM = 'B220' WHERE FACID = (SELECT FACID FROM FACULTY WHERE FACNAME = 'Tanaka'); INSERT The INSERT operator is used to put new records into a table. Normally it is not used to load an entire database (since other utilities can do that more efficiently). Aside from this, older implementations of SQL use it to remove columns from existing tables (before the ALTER TABLE had this capability). Format1: INSERT INTO table (fieldlist) SELECT fieldlist FROM table WHERE append_criteria; OR Format2 INSERT INTO table (col1, col2...) VALUES (val1, val2...); On the general format2 above, you can specify the columns in any order you wish and the system will match them to the appropriate table attributes. Example 1: Insert a new faculty record with ID of 'F330', name of Jones, department of CIS, and rank of Instructor. (Inserting a single record).

18

INSERT INTO FACULTY (FACID, FACNAME, DEPT, RANK) VALUES ('F330','Jones','CIS',Instructor'); Since you are inserting for all fields, you can leave off the column names after FACULTY and get the same effect. For instance, the following two examples are equivalent: INSERT INTO FACULTY VALUES ('F330','Jones','CIS',Instructor'); INSERT INTO FACULTY SELECT * FROM DATATABLE; 'Datatable' is a table that holds the data to be inserted. Since the data is already in a table, this format of the INSERT is not as useful as the first version. Example 2: Insert a new student record with Id of 'S1031', name of Maria Bono, 0 credits, and no major. (Insert a record with NULL value in a field) INSERT INTO STUDENT (FNAME, LNAME, STUID, CREDITS) VALUES ('Maria', 'Bono', 'S1031', 0); Notice that the field names are rearranged from the table order. This does not matter. Also notice that major is missing and will therefore be inserted into the tuple as NULL. Example 3: Create and fill a new table that shows each course and the number of students enrolled in it. (Inserting multiple tuples into a new table) CREATE TABLE ENROLLMENT (COURSENUM CHAR(7) PRIMARY KEY, STUDENTS INTEGER); INSERT INTO ENROLLMENT (COURSENUM, STUDENTS) SELECT COURSENUM, COUNT(*) FROM ENROLL GROUP BY COURSENUM;

DELETE The DELETE operator is used to erase records (not table structure). The number of records deleted may be 0, 1, or many, depending on how many satisfy the predicate.

19

Format: DELETE FROM table/view WHERE delete_criteria; Example 1: Erase the record of student 'S1020' (Delete a single tuple) DELETE FROM STUDENT WHERE STUID = 'S1020'; Example 2: Erase all enrollment records for student 'S1020'. (Delete several tuples). DELETE FROM ENROLL WHERE STUID = 'S1020'; Example 3: Erase all the class records. (Deleting all the tuples from a table) DELETE FROM CLASS; Note that the table CLASS still exists, but is empty. To remove the data and the table you use the DROP TABLE operator. Example 4: Erase all enrollment records for Owen McCarthy. (Delete with a subquery) DELETE FROM ENROLL WHERE STUID = (SELECT STUID FROM STUDENT WHERE FNAME = 'Owen' AND LNAME = 'McCarthy'); In our database there is no such student, so no records are deleted from ENROLL.

20

EXAMPLE DATABASE
The following E/R diagram and tables are used in the examples throughout this handout.

STUDENT

ENROLL

CLASS

FAC-CLASS

FACULTY

21

STUDENT STUID S1001 S1010 S1015 S1002 S1020 S1015

LNAME Smith Burns Jones Chin Rivera McCarthy

FNAME Tom Edward Mary Ann Jane Owen

MAJOR History Art Math Math CIS Math

CREDITS 90 63 42 36 15 9

CLASS COURSENUM ART103A CIS201A MTH101B HST205A MTH103C CIS203A FACULTY FACID F101 F202 F105 F110 F221

FACID F101 F105 F110 F202 F110 F105

SCHED MWF9 TUTHF10 MTUTH9 MWF11 MWF11 MTHF12

ROOM H221 M110 H225 H221 H225 M110

FACNAME Adams Smith Tanaka Byrne Blume

DEPT Art History CIS Math CIS

RANK Professor Associate Instructor Asistant Professor

ENROLL COURSENUM ART103A CIS201A CIS201A ART103A ART103A MTH101B HST205A MTH103C MTH103C

STUID S1001 S1020 S1002 S1010 S1002 S1020 S1001 S1010 S1002

GRADE A B F -0D A C -0B