Introduction to SQL CIS 205, Spring 2005 page 1 of 4 An Introduction to SQL by C. Herbert for CIS 205 – Database Management Systems Community College of Philadelphia March, 2005 What is SQL? SQL stands for Structured Query Language. According to Oracle Corporation, SQL is “The standardized relational database language for querying, manipulating, and updating information in a relational database.“ The first standardized version of SQL was approved by ANSI in 1986, and an international standard was adopted by ISO in 1992. Many commercially available database management systems, such as Oracle 9i, MySQL, and Microsoft SQL Server are based on the SQL-92 standard. Each of these software packages also contain extension to SQL 92 that provide increased capability, but only work within their own systems. Generally, SQL databases are compatible with one another. SQL is not the same as SEQUEL, which is an older database management product from IBM. SQL is a Data Definition Language (DDL), a Data Manipulation Language (DML), and a Data Control Language (DCL) all in one. A DDL allows users to define a database, including creating, altering, and dropping tables and establishing database constraints. A constraint is a rule for the database, such as defining a particular attribute as a primary key, or establishing that a particular attribute cannot be NULL. A DML allows the user to manipulate a database. Most importantly, a DML provides the ability to query a database. A DCL is used to control the database administratively, such as scheduling events, committing data, and managing user rights and privileges within a database. In CIS 205, we will look at SQL as a DDL and a DML, but not as a DCL. I will need to use the DCL features of SQL to set up Oracle accounts for you on our systems, and you may see some DCL commands, but we will focus on implementing and querying a database using SQL’s DDL and DML features. Using a standardized database language, such as SQL: increases productivity among both database managers and users, makes database application more portable and more useful over time, reduces dependence on a single vendor, a single programmer, or programming team, such as a particular consulting firm, reduced training costs for those who work with databases, allows multiple database management systems to communicate with one another. Introduction to SQL CIS 205, Spring 2005 page 2 of 4 In SQL, a schema is the description of a database and the tables it contains. We might say that the schema describes the structure of a database, including its metadata. SQL as a DDL In SQL, CREATE statements are used to define database objects, such as tables. Note: SQL is not case-sensitive, therefore, it is important for people to use uppercase and lowercase language to make SQL statements easier to read. Many people use uppercase lettering for SQL statements and key words, and lowercase lettering for the names of tables, columns, etc., as in this example: SELECT last, first, hours, rate, (hours * rate) as gross FROM payroll WHERE hours <= 40; Also note that SQL statements end with a semicolon (;). The most important SQL DDL statement is “CREATE TABLE”, which is used to define a table and its columns. Two other commonly used SQL DDL statements are: ALTER TABLE, which can be used to change the structure of a table, and DROP TABLE, which is used to remove tables from a database. The INSERT statement, which is used to add data to a database, can be considered either a DDL or a DML statement. So can the UPDATE statement, which can be used to change data in a database. I will not explain each how statement works here, as that information is contained in the tutorials and in the reference information that I will provide next week. Here are some examples of each of these statements: 1. CREATE TABLE student (stnumber VARCHAR(10) NOT NULL, lname VARCHAR(15), fname VARCHAR(15), address VARCHAR(15), city VARCHAR(15), state VARCHAR(15), zip VARCHAR(15), major VARCHAR(25) NOT NULL, GPA NUMBER(5,3)), CONSTRAINT student_pk PRIMARY KEY (stnumber)); 2. ALTER TABLE inventory ADD (stockno VARCHAR(10)); 3. DROP TABLE courses; Introduction to SQL CIS 205, Spring 2005 page 3 of 4 4. INSERT INTO customer VALUES (001, ‘Contemporary Casuals’, 1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601); 5. INSERT INTO product (product_id, product_description,product_finish, standard_price, product_on_hand) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8); 6. INSERT INTO customerB SELECT * FROM CUSTOMERA WHERE state = ‘CA’; 7. DELETE FROM customer WHERE state = ‘HI’; 8. UPDATE product SET unit_price = 775 WHERE product_id = “SK37”; SQL as a DML – The SELECT Statement Perhaps the most important statement in SQL is the SELECT statement. The SELECT Statement can be used for queries on a single table or on multiple tables. The SELECT … FROM … WHERE statement combines both projection and selection from relational algebra in a single statement. Remember that projection determines which attributes we see in our result set (which columns) and selection contains a condition that determines which instances of the entity (which rows) we will see. The SELECT … FROM … WHERE … statement does both, and more. Here is an example of the statement: SELECT last, first, hours FROM employees WHERE hours > 40; This statement tells us which columns we want (last, first, hours) just like a projection operation in relational algebra, and from rows that meet the condition (hours > 40), just like a selection operation in relational algebra. The result will be a table, called a “result set” that contains rows and columns with the data we requested. Of course, it is possible for the result set table to be a single cell, for the result to be a NULL value if no data is returned, or for the result to be an error message if our SQL statement cannot be understood by the database management system processing the SQL statement. In its simplest form, the statement has three parts, or clauses, which are easy to see — the SELECT clause, the FROM clause, and the WHERE clause, as follows: the SELECT clause contains the word SELECT, followed by a list of the columns from which we wish to retrieve data, the FROM clause contains the word FROM, followed by the names of one or more tables from which we wish to retrieve data, and Introduction to SQL CIS 205, Spring 2005 page 4 of 4 the WHERE clause includes the word WHERE followed by the condition that will determine which rows are included in the result set. In this form of the statement, the list of columns in the SELECT clause is just like the list of attributes we need for relational algebra’s projection operation, and the condition in the FROM clause mirrors the condition in a relation algebra selection operation. More sophisticated uses of the SELECT statement The SELECT statement has many additional features beyond the basic SELECT … FROM … WHERE statement. We can also use aliases aggregate functions, and additional modifying clauses in the SELECT statement. An alias is as alternative name for a column that will appear in the result set, such as in the following: SELECT enumber as employee, last, first, hours as hrs FROM employees WHERE hours > 40; In the example above, employee and hrs are aliases that will be used in the result set as the names of two of the columns in place of enumber and hours. An aggregate function returns a result set with summary information about a table, such as: SELECT COUNT(*) FROM employees WHERE hours > 40; This would return a result set with a single cell containing the number of rows n the employees table with hours greater than 40. In addition to the SELECT, FROM, and WHERE, clauses, several extension clauses can be added to the statement to modify how it works, including the following: ORDER BY - Sorts the rows in the result according to specified criteria GROUP BY – allows categorization of the results by a particular attribute HAVING - indicate the conditions under which a category (group) will be included in the result set We will be learning SQL over the next few weeks. Next week I will give you a reference manual for MySQL, which contains extended descriptions of many SQL statements. For now, more information on the statements of SQL that we will start with is contained in the tutorials.