Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Learning Objectives:
Understand database systems Learn how to use SQL to query and update relational databases, and how to use SQL together with a programming language
Table of Contents:
Chapter
1 2 Database Relational Database Management System
Topic
Page No
5 21
3
4 5 6 7 8 9 10 11 12 13 14 15
DBMS Assignment
History of SQL Data Definition Language Data Modification Language Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 References & Books Reference web sites
34
35 46 77 82 85 88 93 96 102 107 108 109
Introduction
Every organization has data that needs to be collected, managed, and analyzed. Most people are familiar with some kind of spreadsheet, such as Microsoft Excel. Spreadsheets are easy and convenient to use, and they may be employed by an individual. Spreadsheets are commonly used to store information in a tabular format. A spreadsheet can store data in rows and columns, it can link cells on one sheet to those on another sheet, and it can force data to be entered in a specific cell in a specific format. Its easy to calculate formulas from groups of cells on the spreadsheet, create charts, and work with data in other ways.
A database fulfills these needs. Along with the powerful features of a relational database come requirements for developing and maintaining the database. Data analysts, database designers, and database administrators (DBAs) need to be able to translate the data in a database into useful information for both day-to-day operations and longterm planning.
Database
Lname, FName, Age, Salary|Smith, John, 35, $280|Doe, Jane, 28, $325|Brown, Scott, 41, $265|Howard, Shemp, 48, $359|Taylor, Tom, 22, $250
Types of Databases
By Function
Analytical Database Also referred as On-Line Analytical processing (OLAP), are those used to keep track of statistics
Read only access to analyze the data
Operational Database Also referred as On-Line Transactional Processing (OLTP), are those, let you actually change and manipulate the data in database
Types of Databases
By Data Model
Flat File database model
Data is stored in numerous files No linkage between files so repetition of information in different files
Database Systems
What is DBMS?
Need for information management A very large, integrated collection of data. Models real-world enterprise.
A Database Management System (DBMS) is a software package designed to store and manage databases.
Data independence and efficient access. Data integrity and security. Uniform data administration. Concurrent access, recovery from crashes. Replication control Reduced application development time.
10
at the low end: access to physical world at the high end: scientific applications Digital libraries, interactive video, Human Genome project, ecommerce, sensor networks ... need for DBMS/data services exploding OS, languages, theory, AI, multimedia, logic
11
Data Models
A data model is a collection of concepts for describing data. A schema is a description of a particular collection of data, using the a given data model. The relational model of data is the most widely used model today.
Main concept: relation, basically a table with rows and columns. Every relation has a schema, which describes the columns, or fields.
12
Data Models
A data model is a collection of concepts for describing data. A schema is a description of a particular collection of data, using the a given data model. The relational model of data is the most widely used model today.
Main concept: relation, basically a table with rows and columns. Every relation has a schema, which describes the columns, or fields.
13
Key
Primary Key a field or fields making every record unique and cannot be null. Ex SSO id in Emp_Table in Genpact Foreign Key a field in one table matches the primary key of another table. Ex CoE in COE_Table is primary key while CoE field in Emp_Table is foreign key
14
Key
Primary Key Foreign Key
Emp_Table SSO_Id Emp_Name CoE 123456 David D'SouzaIndustrial 234567 Ram Kumar Analytics 345678 Naveen P External
15
Suppose we are building a system to store the information about: Employee CoE Supervisor who goes where, who reports whom
16
Sure we can! Start by storing the data in files: Employee.txt coe.txt supervisor.txt
17
Read Employee.txt Read CoE.txt Find&update the record XYZ Find&update the record Analytics Write Employee.txt Write coe.txt
18
System crashes:
Read Employee.txt Read CoE.txt Find&update the record XYZ Find&update the record Analytics Write Employee.txt Write coe.txt
CRASH !
19
Enters a DBMS
Data files
Applications
20
It is a general purpose software system that facilitates the process of defining , constructing and manipulating a database. It helps in defining data types , structures and constraints It provides functions such as: querying the database to retrieve specific data Updating the database to reflect the changes in the miniworld Generating reports from the data
21
22
Data represents known facts that can be recorded and have an implicit meaning
Each database consists of various database objects. Some of the database objects are:
-Tables, Views , Indexes , Partitions , -Constraints -Meta Data
23
Each logical data item is stored in one place allowing a consistent way to store data
Access to information can be controlled depending upon the role of the user
Popular Products : Oracle , IBM DB2 , MySQL, Sybase etc Lets learn about different database objects in the next slide.
24
Database Objects
What is a Table?
Data is stored in Rows & Columns
Rows represents different tuple Columns represents attributes of entities Ex: Account_Dim in CDCI
-Columns : Account Number , First Name , Last Name
Col 1
Col 2
Col 3
Account Number
Row 1
Row 2
ABC
DBC
Manmohan
Singh
25
Database Objects
What is a View?
A view is a single table derived from other table(s) in the database Are dynamically updated whenever the tables are refreshed Also called as Virtual Tables
Ex: Account_Dim_Level1, Account_Dim_Level2 in CDCI Has been used to support field masking in CDCI
26
Database Objects
What is an Index?
Pointers to physical location of the information in DB Defined on a field(s) in the DB Used to retrieve or update information faster in DB
Ex: Account_Key in Account_Dim
27
Database Objects
What are partitions ?
Partitions are a way of dividing tables & indexes into Manageable Pieces Paritions can be spread out in different locations /disks Partitions support Parallelism Types : List , Range , Hash
Ex: Posting_date in Transaction_fact Billing_cycle_date in Statement_Fact
Table
Billing Cycle Date
Statement Fact
Client 1 Client 2
Database Objects
What are Constraints?
Constraints are restrictions imposed on data Constraints help in enforcing consistency in representation of data Constraints enforces integrity into data Types
Domain Constraints
Account Number can only be of 16 digits No 2 account numbers can have the same account key Account Key cannot be null on Account_dim Every transaction made by cardholder needs to be associated with the cardholders information in Account_dim Account Key in transaction fact is a primary key in Account_dim Account number cannot be NULL on Account_dim
Referential Integrity
Foreign Key Constraint
-
29
Meta Data
What is Meta Data
Meta Data is data about data Information about data stored in the DB Ex: All_tables stores information about every table in the DB All_Indexes stores information of every index available in the DB
30
Database
Usually, only one data type can be stored in a database table column. Columns in a database table have a fixed value. Single rows of a database table are uniquely identified by a unique value (typically a primary key, as described later in this chapter). Multiple users can access a database table at the same time, with various combinations of read and write capabilities in different parts of the database. A database usually has transactioncontrol capabilities, making it possible to roll back a change if something happened to prevent it from completing successfully (such as a power failure). There are many tools for repairing and recovering databases.
31
Things to Remember
32
Additional Information
33
Database Assignments
What is the difference between DBMS & RDBMS? What is Primary Key in any database table? Can Primary key be null? What is Metadata? How do you differentiate between a View and a table? Row of a table represents Tuple. Is it true? What is the advantage of creating an index on a table? If Account_Schdl_No is Primary Key in table Account. It can store null value as well. Is this statement true?
34
History of SQL
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).
35
History
During the 1970s, a group at IBM's San Jose research center developed a database system "System R" based upon, but not strictly faithful to, Codd's model. Structured English Query Language ("SEQUEL") was designed to manipulate and retrieve data stored in System R. The acronym SEQUEL was later condensed to SQL because the word 'SEQUEL' was held as a trademark by the Hawker-Siddeley aircraft company of the UK. 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.[1] Their concepts were published to increase interest in SQL.
36
History
The first non-commercial, relational, non-SQL database, Ingres, was developed in 1974 at U.C. Berkeley. In 1978, methodical testing commenced at customer test sites. Demonstrating both the usefulness and practicality of the system, this testing proved to be a success for IBM. As a result, 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).[1]
37
History
At the same time Relational Software, Inc. (now Oracle Corporation) saw the potential of the concepts described by Chamberlin and Boyce and developed their own version of a RDBMS for the Navy, CIA and others. In the summer of 1979 Relational Software, Inc. introduced Oracle V2 (Version2) for VAX computers as the first commercially available implementation of SQL. Oracle is often incorrectly cited as beating IBM to market by two years, when in fact they only beat IBM's release of the System/38 by a few weeks. Considerable public interest then developed; soon many other vendors developed versions, and Oracle's future was ensured.
38
History
Standardization SQL was adopted as a standard by ANSI (American National Standards Institute) in 1986 and ISO (International Organization for Standardization) in 1987. ANSI has declared that the official pronunciation for SQL is /s kju l/, although many Englishspeaking database professionals still pronounce it as sequel.
39
History
Year 1986
Name SQL-86
Alias SQL-87
Comments
1989
SQL-89
1992
SQL-92
SQL2
Major revision. Added regular expression matching, recursive queries, triggers, non-scalar types and some object-oriented features. (The last two are somewhat controversial and not yet widely supported.) Introduced XML-related features, window functions, standardized sequences and columns with autogenerated values (including identitycolumns).
40
1999
SQL:1999
SQL3
2003
SQL:2003
What is SQL?
SQL SQL is a syntax for querying and manipulating relational databases. It was originally known as SEQUEL (Structured English Query Language), but this was shortened to SQL due to a trademark dispute. You can use SQL to read data from a database. Such queries can be quite sophisticated - you can choose which columns of the table to extract, you can use conditional expressions to decide which rows to extract, you can sort the result, and limit the number of rows returned. It is also possible to "join" tables, that is to retrieve data from multiple related tables in a single query. SQL also allows you to insert and modify records in a table. In that sense, the term "query" is something of a misnomer. You can also create, modify or delete entire tables within the database using SQL queries.
41
Topic: cont..
SQL stands for Structured Query Language. It is the most commonly used relational database language today. SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic.
42
Topic: cont..
Data Manipulation Data Definition Data Administration All are expressed as an SQL statement or command.
43
Topic: cont..
Represent all info in database as tables Keep logical representation of data independent from its physical storage characteristics Use one high-level language for structuring, querying, and changing info in the database Support the main relational operations Support alternate ways of looking at data in tables Provide a method for differentiating between unknown values and nulls (zero or blank) Support Mechanisms for integrity, authorization, transactions, and recovery
44
Topic: cont..
SQL commands can be divided into two main sublanguages. The Data Definition Language (DDL) contains the commands used to create and destroy databases and database objects. After the database structure is defined with DDL, database administrators and users can utilize the Data Manipulation Language to insert, retrieve and modify the data contained within it.
45
46
The Data Definition Language (DDL) is used to create and destroy databases and database objects. These commands will primarily be used by database administrators during the setup and removal phases of a database project. Let's take a look at the structure and usage of four basic DDL commands: The Data Definition Language (DDL) is used to create and destroy databases and database objects. These commands will primarily be used by database administrators during the setup and removal phases of a database project. Let's take a look at the structure and usage of four basic DDL commands:
47
Topic: cont ..
SQL Data Definition Language (DDL) The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are: CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
48
CREATE CREATE command can be used for this purpose. The command: CREATE TABLE personal_info (first_name char(20) not null, last_name char(20) not null, employee_id int not null) establishes a table titled "personal_info" in the current database. In our example, the table contains three attributes: first_name, last_name and employee_id.
49
Once you've created a table within a database, you may wish to modify the definition of it. The ALTER command allows you to make changes to the structure of a table without deleting and recreating it. Take a look at the following command: ALTER TABLE personal_info ADD salary money null
This example adds a new attribute to the personal_info table -- an employee's salary. The "money" argument specifies that an employee's salary will be stored using a dollars and cents format. Finally, the "null" keyword tells the database that it's OK for this field to contain no value for any given employee.
50
The final command of the Data Definition Language, DROP, allows us to remove entire database objects from our DBMS. For example, if we want to permanently remove the personal_info table that we created, we'd use the following command: DROP TABLE personal_info Similarly, the command below would be used to remove the entire employees database:
51
Tables are the basic structure where data is stored in the database. Given that in most cases, there is no way for the database vendor to know ahead of time what your data storage needs are, chances are that you will need to create tables in the database yourself. Many database tools allow you to create tables without writing SQL, but given that tables are the container of all the data, it is important to include the CREATE TABLE syntax in this tutorial.
52
Before we dive into the SQL syntax for CREATE TABLE, it is a good idea to understand what goes into a table. Tables are divided into rows and columns. Each row represents one piece of data, and each column can be thought of as representing a component of that piece of data. So, for example, if we have a table for recording customer information, then the columns may include information such as First Name, Last Name, Address, City, Country, Birth Date, and so on. As a result, when we specify a table, we include the column headers and the data types for that particular column.
53
So what are data types? Typically, data comes in a variety of forms. It could be an integer (such as 1), a real number (such as 0.55), a string (such as 'sql'), a date/time expression (such as '2000-JAN-25 03:22:22'), or even in binary format. When we specify a table, we need to specify the data type associated with each column (i.e., we will specify that 'First Name' is of type char(50) - meaning it is a string with 50 characters). One thing to note is that different relational databases allow for different data types, so it is wise to consult with a database-specific reference first.
54
The SQL syntax for CREATE TABLE is CREATE TABLE "table_name" ("column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2", ... ) So, if we are to create the customer table specified as above, we would type in CREATE TABLE customer (First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date date)
55
Views can be considered as virtual tables. Generally speaking, a table has a set of definition, and it physically stores the data. A view also has a set of definitions, which is build on top of table(s) or other view(s), and it does not physically store the data. The syntax for creating a view is as follows: CREATE VIEW "VIEW_NAME" AS "SQL Statement" "SQL Statement" can be any of the SQL statements we have discussed in this tutorial.
56
Let's use a simple example to illustrate. Say we have the following table: TABLE Customer (First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date date)
and we want to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table, we would type in, CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer
57
Now we have a view called V_Customer with the following structure: View V_Customer (First_Name char(50), Last_Name char(50), Country char(25)) We can also use a view to apply joins to two tables. In this case, users only see one view rather than two tables, and the SQL statement users need to issue becomes much simpler. Let's say we have the following two tables:
58
Table Store_Information
Table Geography
Date Jan-05-1999 Jan-07-1999 Jan-08-1999 Jan-08-1999 West West region_na me East East store_na me Boston New York Los Angeles San Diego
Topic: View
and we want to build a view that has sales by region information. We would issue the following SQL statement: CREATE VIEW V_REGION_SALES AS SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name GROUP BY A1.region_name This gives us a view, V_REGION_SALES, that has been defined to store sales by region records. If we want to find out the content of this view, we type in, SELECT * FROM V_REGION_SALES
59
60
61
62
There is no strict rule on how to name an index. The generally accepted method is to place a prefix, such as "IDX_", before an index name to avoid confusion with other database objects. It is also a good idea to provide information on which table and column(s) the index is used on.
63
64
65
Table customer
Column Name First_Name Last_Name Address City Country Birth_Date Gender Data Type char(50) char(50) char(50) char(50) char(25) date char(1)
First, we want to add a column called "Gender" to this table. To do this, we key in: ALTER table customer add Gender char(1)
66
67
Table customer
Column Name First_Name
Last_Name Addr City Country Birth_Date Gender
68
Table customer
Column Name First_Name Last_Name Addr City Data Type char(50) char(50) char(30) char(50)
Country
Birth_Date
char(25)
date
69
70
Below are examples for specifying a primary key by altering a table: ALTER TABLE Customer ADD PRIMARY KEY (SID);
71
72
Table CUSTOMER
column name SID Last_Name charac teristic
Table ORDERS
column name Order_ID Order_Date Customer_SID Amount Foreig n Key charac teristic
Primar y Key
Primar y Key
First_Name
In the above example, the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table.
73
Below are examples for specifying a foreign key by altering a table. This assumes that the ORDERS table has been created, and the foreign key has not yet been put in: ALTER TABLE ORDERS ADD (CONSTRAINT fk_orders1) FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);
74
75
76
77
SELECT Statement
The SELECT statement is used to query the database and retrieve selected data that match the criteria that you specify. The SELECT statement has five main clauses to choose from, although, FROM is the only required clause. Each of the clauses have a vast selection of options, parameters, etc. The clauses will be listed below, but each of them will be covered in more detail later in the tutorial.
78
Example 1.1:
SELECT name, age, salary FROM employee WHERE age > 50; The above statement will select all of the values in the name, age, and salary columns from the employee table whose age is greater than 50. Note: Remember to put a semicolon at the end of your SQL statements. The ; indicates that your SQL statment is complete and is ready to be interpreted
Comparison Operators = > < Equal Greater than Less than
>=
<= <> or != LIKE
80
Example:
SELECT DISTINCT age FROM employee_info; This statement will return all of the unique ages in the employee_info table. ALL will display "all" of the specified columns including all of the duplicates. The ALL keyword is the default if nothing is specified.
Note: The following two tables will be used throughout this course. It is recommended to have them open in another window or print them out
81
Assignment 1:
1. From the items_ordered table, select a list of all items purchased for customerid 10449. Display the customerid, item, and price for this customer. Select all columns from the items_ordered table for whoever purchased a Tent. Select the customerid, order_date, and item values from the items_ordered table for any items in the item column that start with the letter "S". Select the distinct items in the items_ordered table. In other words, display a listing of each of the unique items from the items_ordered table. Make up your own select statements and submit them
2.
3. 4. 5.
82
Aggregate Functions :
MIN MAX SUM AVG COUNT COUNT(*) returns the smallest value in a given column returns the largest value in a given column returns the sum of the numeric values in a given column returns the average value of a given column returns the total number of values in a given column returns the number of rows in a table
Aggregate functions are used to compute against a "returned column of numeric data" from your SELECT statement. They basically summarize the results of a particular column of selected data. We are covering these here since they are required by the next topic, "GROUP BY". Although they are required for the "GROUP BY" clause, these functions can be used without the "GROUP BY" clause.
83
Examples:
SELECT AVG(salary) FROM employee; This statement will return a single result which contains the average value of everything returned in the salary column from the employee table. SELECT AVG(salary) FROM employee; WHERE title = 'Programmer';
This statement will return the average salary for all employees whose title is equal to 'Programmer'
SELECT Count(*) FROM employees;
This particular statement is slightly different from the other aggregate functions since there isn't a column supplied to the count function. This statement will return the number of rows in the employees table. .
84
Assignment 2:
1. Select the maximum price of any item ordered in the items_ordered table. Hint: Select the maximum price only.>
2.
3. 4.
Select the average price of all of the items ordered that were purchased in the month of Dec.
What are the total number of rows in the items_ordered table? For all of the tents that were ordered in the items_ordered table, what is the price of the lowest tent? Hint: Your query should return the price only
85
GROUP BY clause:
The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns. GROUP BY clause syntax: SELECT column1, SUM(column2)column(n) FROM Table 1. GROUP BY column1Column(n)
86
Examples:
Let's say you would like to retrieve a list of the highest paid salaries in each dept: SELECT max(salary), dept FROM employee GROUP BY dept;
This statement will select the maximum salary for the people in each unique department. Basically, the salary for the person who makes the most in each department will be displayed. Their, salary and their department will be returned.
Let's say you want to group everything of quantity 1 together, everything of quantity 2 together, everything of quantity 3 together, etc. If you would like to determine what the largest cost item is for each grouped quantity (all quantity 1's, all quantity 2's, all quantity 3's, etc.), you would enter: SELECT quantity, max(price) FROM items_ordered GROUP BY quantity; Enter the statement in above, and take a look at the results to see if it returned what you were expecting. Verify that the maximum price in each Quantity Group is really the maximum price.
87
Assignment 3:
1. How many people are in each unique state in the customers table? Select the state and display the number of people in each. Hint: count is used to count rows in a column, sum works on numeric data only. From the items_ordered table, select the item, maximum price, and minimum price for each specific item in the table. Hint: The items will need to be broken up into separate groups. How many orders did each customer make? Use the items_ordered table. Select the customerid, number of orders they made, and the sum of their orders. Click the Group By answers link below if you have any problems.
2.
3.
88
HAVING clause:
The HAVING clause allows you to specify conditions on the rows for each group - in other words, which rows should be selected will be based on the conditions you specify. The HAVING clause should follow the GROUP BY clause if you are going to use it. HAVING clause syntax: SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list" HAVING "condition";
89
Examples:
Let's say you have an employee table containing the employee's name, department, salary, and age. If you would like to select the average salary for each employee in each department, you could enter: SELECT dept, avg(salary) FROM employee GROUP BY dept; But, let's say that you want to ONLY calculate & display the average if their salary is over 20000: SELECT dept, avg(salary) FROM employee GROUP BY dept HAVING avg(salary) > 20000;
90
ORDER BY clause:
ORDER BY is an optional clause which will allow you to display the results of your query in a sorted order (either ascending order or descending order) based on the columns that you specify to order by. ORDER BY clause syntax: SELECT column1, SUM(column2) FROM "list-of-tables" ORDER BY "column-list" [ASC | DESC]; [ ] = optional ASC = Ascending Order - default DESC = Descending Order
91
Examples:
This statement will select the employee_id, dept, name, age, and salary from the employee_info table where the dept equals 'Sales' and will list the results in Ascending (default) order based on their Salary.: SELECT employee_id, dept, name, age, salary FROM employee_info WHERE dept = 'Sales' ORDER BY salary; If you would like to order based on multiple columns, you must seperate the columns with commas. For example: SELECT employee_id, dept, name, age, salary FROM employee_info WHERE dept = 'Sales' ORDER BY salary, age DESC;
92
Assignment 4:
1. Select the lastname, firstname, and city for all customers in the customers table. Display the results in Ascending Order based on the lastname. Same thing as exercise #1, but display the results in Descending order. Select the item and price for all of the items in the items_ordered table that the price is greater than 10.00. Display the results in Ascending order based on the price.
2. 3.
93
94
Examples:
SELECT employeeid, firstname, lastname, title, salary FROM employee_info WHERE salary >= 50000.00 AND title = 'Programmer';
This statement will select the employeeid, firstname, lastname, title, and salary from the employee_info table where the salary is greater than or equal to 50000.00 AND the title is equal to 'Programmer'. Both of these conditions must be true in order for the rows to be returned in the query. If either is false, then it will not be displayed. Although they are not required, you can use paranthesis around your conditional expressions to make it easier to read: SELECT employeeid, firstname, lastname, title, salary FROM employee_info WHERE (salary >= 50000.00) AND (title = 'Programmer'); SELECT firstname, lastname, title, salary FROM employee_info WHERE (title = 'Sales') OR (title = 'Programmer'); This statement will select the firstname, lastname, title, and salary from the employee_info table where the title is either equal to 'Sales' OR the title is equal to 'Programmer'.
95
Assignment 5:
1. Select the customerid, order_date, and item from the items_ordered table for all items unless they are 'Snow Shoes' or if they are 'Ear Muffs'. Display the rows as long as they are not either of these two items. Select the item and price of all items that start with the letters 'S', 'P', or 'F'.
2.
96
97
Examples:
SELECT employeeid, lastname, salary FROM employee_info WHERE lastname IN ('Hernandez', 'Jones', 'Roberts', 'Ruiz'); This statement will select the employeeid, lastname, salary from the employee_info table where the lastname is equal to either: Hernandez, Jones, Roberts, or Ruiz. It will return the rows if it is ANY of these values. The IN conditional operator can be rewritten by using compound conditions using the equals operator and combining it with OR - with exact same output results: SELECT employeeid, lastname, salary FROM employee_info WHERE lastname = 'Hernandez' OR lastname = 'Jones' OR lastname = 'Roberts' OR lastname = 'Ruiz';
As you can see, the IN operator is much shorter and easier to read when you are testing for more than two or three values.
You can also use NOT IN to exclude the rows in your list.
98
Examples:
The BETWEEN conditional operator is used to test to see whether or not a value (stated before the keyword BETWEEN) is "between" the two values stated after the keyword BETWEEN. SELECT employeeid, age, lastname, salary FROM employee_info WHERE age BETWEEN 30 AND 40; This statement will select the employeeid, age, lastname, and salary from the employee_info table where the age is between 30 and 40 (including 30 and 40). This statement can also be rewritten without the BETWEEN operator: SELECT employeeid, age, lastname, salary FROM employee_info WHERE age >= 30 AND age <= 40; You can also use NOT BETWEEN to exclude the values between your range.
99
ABS(x) SIGN(x) MOD(x,y) FLOOR(x) CEILING(x) or CEIL(x) POWER(x,y) ROUND(x) ROUND(x,d) SQRT(x)
returns the absolute value of x returns the sign of input x as -1, 0, or 1 (negative, zero, or positive respectively) modulo - returns the integer remainder of x divided by y (same as x%y) returns the largest integer value that is less than or equal to x returns the smallest integer value that is greater than or equal to x returns the value of x raised to the power of y returns the value of x rounded to the nearest whole integer returns the value of x rounded to the number of decimal places specified by the value d returns the square-root value of x
100
Examples:
SELECT round(salary), firstname FROM employee_info
This statement will select the salary rounded to the nearest whole value and the firstname from the employee_info table.
101
Assignment 6:
1. Select the item and per unit price for each item in the items_ordered table. Hint: Divide the price by the quantity.
2.
Click the exercise answers link below if you have any problems.
102
Table Joins:
All of the queries up until this point have been useful with the exception of one major limitation - that is, you've been selecting from only one table at a time with your SELECT statement. It is time to introduce you to one of the most beneficial features of SQL & relational database systems - the "Join". To put it simply, the "Join" makes relational database systems "relational". Joins allow you to link data from two or more tables together into a single query result--from one single SELECT statement. A "Join" can be recognized in a SQL SELECT statement if it has more than one table after the FROM keyword SELECT "list-of-columns" FROM table1,table2 WHERE "search-condition(s)"
103
Joins can be explained easier by demonstrating what would happen if you worked with one table only, and didn't have the ability to use "joins". This single table database is also sometimes referred to as a "flat table". Let's say you have a one-table database that is used to keep track of all of your customers and what they purchase from your store:
Everytime a new row is inserted into the table, all columns will be be updated, thus resulting in unnecessary "redundant data". For example, every time Wolfgang Schultz purchases something, the following rows will be inserted into the table:
id first last address city state zip date item price
10982
10982 10982 10982 10982
Wolfgang
Wolfgang Wolfgang Wolfgang Wolfgang
Schultz
Schultz Schultz Schultz Schultz
Yuma
Yuma Yuma Yuma Yuma
AZ
AZ AZ AZ AZ
85002
85002 85002 85002 85002
032299
082899 091199 100999 022900
snowboard
snow shovel gloves lantern tent
45.00
35.00 15.00 35.00 85.00
An ideal database would have two tables: 1. One for keeping track of your customers 2. And the other to keep track of what they purchase:
104
An ideal database would have two tables: 1. One for keeping track of your customers 2. And the other to keep track of what they purchase: "Customer_info" table:
customer_number firstname lastname address city state zip
"Purchases" table:
customer_number date item price
Now, whenever a purchase is made from a repeating customer, the 2nd table, "Purchases" only needs to be updated! We've just eliminated useless redundant data, that is, we've just normalized this database! Notice how each of the tables have a common "cusomer_number" column. This column, which contains the unique customer number will be used to JOIN the two tables. Using the two new tables, let's say you would like to select the customer's name, and items they've purchased.
105
Examples:
SELECT customer_info.firstname, customer_info.lastname, purchases.item FROM customer_info, purchases WHERE customer_info.customer_number = purchases.customer_number; This particular "Join" is known as an "Inner Join" or "Equijoin". This is the most common type of "Join" that you will see or use. Notice that each of the colums are always preceeded with the table name and a period. This isn't always required, however, it IS good practice so that you wont confuse which colums go with what tables. It is required if the name column names are the same between the two tables. I recommend preceeding all of your columns with the table names when using joins. SELECT employee_info.employeeid, employee_info.lastname, employee_sales.comission FROM employee_info, employee_sales WHERE employee_info.employeeid = employee_sales.employeeid; This statement will select the employeeid, lastname (from the employee_info table), and the comission value (from the employee_sales table) for all of the rows where the employeeid in the employee_info table matches the employeeid in the employee_sales table.
106
Assignment 7:
1. Write a query using a join to determine which items were ordered by each of the customers in the customers table. Select the customerid, firstname, lastname, order_date, item, and price for everything each customer purchased in the items_ordered table. Repeat exercise #1, however display the results sorted by state in descending order.
2.
107
Additional Reference:
Judith Bowman, Sandra Emerson, and Marcy Darnovsky, The Practical SQL Handbook: Using Structured Query Language, Third Edition, Addison-Wesley, ISBN 0-201-44787-8, 1996. C. J. Date and Hugh Darwen, A Guide to the SQL Standard: A User's Guide to the Standard Database Language SQL, Fourth Edition, Addison-Wesley, ISBN 0-201-96426-0, 1997. C. J. Date, An Introduction to Database Systems, Volume 1, Sixth Edition, AddisonWesley, 1994. Ramez Elmasri and Shamkant Navathe, Fundamentals of Database Systems, 3rd Edition, Addison-Wesley, ISBN 0-805-31755-4, August 1999. Jim Melton and Alan R. Simon, Understanding the New SQL: A Complete Guide, Morgan Kaufmann, ISBN 1-55860-245-3, 1993. Jeffrey D. Ullman, Principles of Database and Knowledge: Base Systems, Volume 1, Computer Science Press, 1988.
108
Reference websites:
Wikipedia: SQL - http://en.wikipedia.org/wiki/SQL History and overview of the language. SQLCourse - http://www.sqlcourse.com/ Interactive/On-line SQL Tutorial with SQL Interpreter & live practice database. A Gentle Introduction to SQL - http://sqlzoo.net/ An Introduction to Database Normalization - http://dev.mysql.com/tech-resources/articles/intro-tonormalization.html Online SQL tutorial featuring a live interpreter to test SQL commands. SQL Tutorial - http://www.firstsql.com/tutor.htm Complete SQL Tutorial using SQL92. SQL Tutorial - http://www.1keydata.com/sql/sql.html This site aims to teach beginners the building blocks of SQL. Database and SQL eLearning - http://db.grussell.org/ Database theory and an online tutorial interface to an Oracle database system, allowing a user to learn SQL interactively. The site automatically checks and marks SQL and gives instant feedback. SQL exercises - http://www.sql-ex.ru Introduction to Structured Query Language - http://riki-lb1.vet.ohiostate.edu/mqlin/computec/tutorials/SQLTutorial.htm SQL for Web Nerds - http://eveander.com/arsdigita/books/sql/ A nicely structured manuscript on SQL by Philip Greenspun, based on the Oracle database. Queries, transactions, triggers, and RDBMS concepts are covered. SQL School - http://www.w3schools.com/sql/ http://directory.google.com/Top/Computers/Programming/Languages/SQL/FAQs,_Help,_and_Tutorial s/
-The End109